Browse Source

fixes, caused deletion of goods

Ivan Greenking 1 year ago
parent
commit
cef623b22d
2 changed files with 14 additions and 1 deletions
  1. 1 0
      index.js
  2. 13 1
      models.js

+ 1 - 0
index.js

@@ -102,6 +102,7 @@ const upload  = require('multer')({ dest: uploadPath })
             createdAt: String
             price: Float,
             count: Float,
+	    goodName: String,
             good: Good,
             order: Order
             owner: User

+ 13 - 1
models.js

@@ -171,7 +171,15 @@ module.exports = async (dbName='shop-roles') => {
             }
 
             get total(){
-                return (async() => (await Promise.all(this.orderGoods)).reduce((a,b) => (a.total || a) + b.total, 0))()
+                return (async () =>{ 
+			let total = 0
+			try {
+				total = (await Promise.all(this.orderGoods)).reduce((a,b) => (a?.total || a) + (b?.total || 0), 0)
+			}
+			catch(e){
+			}
+			return total
+		})()
             }
 
             static get relations(){
@@ -208,8 +216,12 @@ module.exports = async (dbName='shop-roles') => {
             }
 
             async save(...params){
+		if (!this.good){
+		    throw new Error('Good Required for OrderGood')
+		}
                 if (!this.price && this.good && this.good.price){
                     this.price = this.good.price
+		    this.goodName = this.good.name
                 }
                 return await super.save(...params)
             }