Kaynağa Gözat

models fixes thisUser

Ivan Asmer 5 yıl önce
ebeveyn
işleme
063f610b61
2 değiştirilmiş dosya ile 12 ekleme ve 23 silme
  1. 6 5
      index.js
  2. 6 18
      models.js

+ 6 - 5
index.js

@@ -142,7 +142,7 @@ const anonResolvers = ['login', 'createUser'];
         },
 
 
-        async createEventMoney({eventId, amount}, {jwt: {id}, models: {SlicedSavable, EventMoney}}){
+        async createEventMoney({eventId, amount}, {jwt: {id}, models: {SlicedSavable, EventMoney, thisUser}}){
             let eventMoney = await SlicedSavable.m.EventMoney.findOne({"user._id": ObjectID(id), "event._id": ObjectID(eventId)})
             if (eventMoney){
                 eventMoney.amount = amount;
@@ -153,11 +153,12 @@ const anonResolvers = ['login', 'createUser'];
             if (!event)
                 return null;
 
-            const me = await SlicedSavable.m.User.findOne({_id: ObjectID(id)})
-            if (!me)
+            if (!thisUser)
                 return null;
 
-            return await (new EventMoney({user: me, event, amount})).save()
+            console.log(thisUser, event, amount)
+
+            return await (new EventMoney({user: thisUser, event, amount})).save()
         },
 
 
@@ -218,7 +219,7 @@ const anonResolvers = ['login', 'createUser'];
             if (decoded){
                 console.log('token verified', decoded)
 
-                let slicedModels  = getModels(decoded.sub.id)
+                let slicedModels  = await getModels(decoded.sub.id)
 
                 return {
                     schema: schema,

+ 6 - 18
models.js

@@ -12,20 +12,13 @@ module.exports = async (dbName, dsn="mongodb://localhost:27017/") => {
     const Savable     = mm(db).Savable
     const slice       = mm(db).sliceSavable 
 
-    function getModels(id){
+    async function getModels(id){
         const SlicedSavable = slice([id, 'user'])
 
         class User extends SlicedSavable {
             constructor(...params){
                 super(...params)
-                
-                if (this.moneyEvents instanceof Array)
-                    return
-
-                if (this.moneyEvents instanceof SlicedSavable)
-                    this.moneyEvents = [this.moneyEvents];
-
-                this.moneyEvents = [];
+                this.moneyEvents = this.moneyEvents instanceof Array ? this.moneyEvents : (this.moneyEvents ? [this.moneyEvents] : []) 
             }
 
             static get relations(){
@@ -39,14 +32,7 @@ module.exports = async (dbName, dsn="mongodb://localhost:27017/") => {
         class Event extends SlicedSavable {
             constructor(...params){
                 super(...params)
-
-                if (this.moneyEvents instanceof Array)
-                    return
-
-                if (this.moneyEvents instanceof SlicedSavable)
-                    this.moneyEvents = [this.moneyEvents];
-
-                this.moneyEvents = [];
+                this.moneyEvents = this.moneyEvents instanceof Array ? this.moneyEvents : (this.moneyEvents ? [this.moneyEvents] : []) 
             }
 
             get owner(){
@@ -102,8 +88,10 @@ module.exports = async (dbName, dsn="mongodb://localhost:27017/") => {
         }
         SlicedSavable.addClass(EventMoney)
 
+        const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
+
         return {
-            SlicedSavable, User, Event, EventMoney
+            SlicedSavable, User, Event, EventMoney, thisUser
         }
     }