Переглянути джерело

Change transaction model. Update transaction saving for cloverers.

Lee Morgan 6 роки тому
батько
коміт
dbb8c35866
2 змінених файлів з 27 додано та 3 видалено
  1. 19 1
      controllers/renderer.js
  2. 8 2
      models/transaction.js

+ 19 - 1
controllers/renderer.js

@@ -59,7 +59,25 @@ module.exports = {
                                 for(let item of order.lineItems.elements){
                                     let recipe = merchant.recipes.find(r => r.posId === item.item.id);
                                     if(recipe){
-                                        newTransaction.recipes.push(recipe._id);
+                                        //Search and increment/add instead of just push
+                                        // newTransaction.recipes.push(recipe._id);
+                                        let isNewRecipe = true;
+                                        for(let newRecipe of newTransaction.recipes){
+                                            if(newRecipe.recipe === recipe._id){
+                                                newRecipe.quantity++;
+                                                isNewRecipe = false;
+                                                break;
+                                            }
+                                        }
+
+                                        if(isNewRecipe){
+                                            newTransaction.recipes.push({
+                                                recipe: recipe._id,
+                                                quantity: 1
+                                            });
+                                        }
+
+                                        //End modifications
                                         for(let ingredient of recipe.ingredients){
                                             let inventoryIngredient = {};
                                             for(let invItem of merchant.inventory){

+ 8 - 2
models/transaction.js

@@ -12,8 +12,14 @@ const TransactionSchema = new mongoose.Schema({
     },
     device: String,
     recipes: [{
-        type: mongoose.Schema.Types.ObjectId,
-        ref: "Recipe"
+        recipe: {
+            type: mongoose.Schema.Types.ObjectId,
+            ref: "Recipe"
+        },
+        quantity: {
+            type: Number,
+            min: [0, "Must be a positive number"]
+        }
     }]
 });