|
@@ -189,24 +189,25 @@ const anonResolvers = ['login', 'createUser'];
|
|
|
|
|
|
|
|
|
async setGood({good}, {jwt: {id}, models: {SlicedSavable, Good}}){
|
|
|
+ let entity;
|
|
|
if ('_id' in good){
|
|
|
- let entity = await SlicedSavable.m.Good.findOne({_id: ObjectID(good._id)})
|
|
|
+ entity = await SlicedSavable.m.Good.findOne({_id: ObjectID(good._id)})
|
|
|
if (entity){
|
|
|
entity.name = good.name
|
|
|
entity.description = good.description
|
|
|
entity.price = good.price
|
|
|
- if (good.categories){
|
|
|
- console.log(good.categories)
|
|
|
- entity.categories = []
|
|
|
- for (catId of good.categories){
|
|
|
- let cat = await SlicedSavable.m.Category.findOne({_id: ObjectID(catId)});
|
|
|
- cat && entity.categories.push(cat)
|
|
|
- }
|
|
|
- }
|
|
|
- return await entity.save()
|
|
|
}
|
|
|
}
|
|
|
- return await (new Good(good)).save()
|
|
|
+ entity = entity || new Good(good)
|
|
|
+ if (good.categories){
|
|
|
+ console.log(good.categories)
|
|
|
+ entity.categories = []
|
|
|
+ for (catId of good.categories){
|
|
|
+ let cat = await SlicedSavable.m.Category.findOne({_id: ObjectID(catId)});
|
|
|
+ cat && entity.categories.push(cat)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return await entity.save()
|
|
|
},
|
|
|
|
|
|
async goods({}, {jwt: {id}, models: {SlicedSavable, Good}}){
|
|
@@ -225,24 +226,20 @@ const anonResolvers = ['login', 'createUser'];
|
|
|
|
|
|
|
|
|
async setOrder({order}, {jwt: {id}, models: {SlicedSavable, Order, thisUser}}){
|
|
|
+ let entity;
|
|
|
if ('_id' in order){
|
|
|
- let entity = await SlicedSavable.m.Order.findOne({_id: ObjectID(good._id)})
|
|
|
- if (entity){
|
|
|
- //entity.name = good.name
|
|
|
- //entity.description = good.description
|
|
|
- if (order.orderGoods){
|
|
|
- entity.orderGoods = []
|
|
|
- for (orderGoodId of entity.orderGoods){
|
|
|
- let orderGood = await SlicedSavable.m.OrderGood.findOne({_id: ObjectID(orderGoodId)});
|
|
|
- orderGood && entity.orderGoods.push(orderGood)
|
|
|
- }
|
|
|
- }
|
|
|
- entity.user = thisUser
|
|
|
- return await entity.save()
|
|
|
+ entity = await SlicedSavable.m.Order.findOne({_id: ObjectID(good._id)})
|
|
|
+ }
|
|
|
+ entity = entity || new Order(order)
|
|
|
+ if (order.orderGoods){
|
|
|
+ entity.orderGoods = []
|
|
|
+ for (orderGoodId of entity.orderGoods){
|
|
|
+ let orderGood = await SlicedSavable.m.OrderGood.findOne({_id: ObjectID(orderGoodId)});
|
|
|
+ orderGood && entity.orderGoods.push(orderGood)
|
|
|
}
|
|
|
}
|
|
|
- order.user = thisUser
|
|
|
- return await (new Order(order)).save()
|
|
|
+ entity.user = thisUser
|
|
|
+ return await entity.save()
|
|
|
},
|
|
|
|
|
|
async orders({}, {jwt: {id}, models: {SlicedSavable}}){
|