Jelajahi Sumber

Creating a transaction now updates ingredients on the backend

Lee Morgan 5 tahun lalu
induk
melakukan
aad0a1a4ca

+ 28 - 0
controllers/helper.js

@@ -1,6 +1,7 @@
 const axios = require("axios");
 
 const Transaction = require("../models/transaction.js");
+const Merchant = require("../models/merchant.js");
 
 module.exports = {
     getCloverData: async function(merchant){
@@ -181,5 +182,32 @@ module.exports = {
             .catch((err)=>{
                 return "ERROR: UNABLE TO UPDATE TRANSACTION DATA";
             });
+    },
+
+    /*
+    Updates the quanties of ingredients from a list of transactions
+    ingredients = Object. keys = ingredient ids, values = quantity to change (g)
+    user = id of logged in user
+    */
+    updateIngredientQuantities: function(ingredients, user){
+        Merchant.findOne({_id: user})
+            .then((merchant)=>{
+                let keys = Object.keys(ingredients);
+
+                for(let i = 0; i < keys.length; i++){
+                    for(let j = 0; j < merchant.inventory.length; j++){
+                        if(merchant.inventory[j].ingredient._id.toString() === keys[i]){
+                            merchant.inventory[j].quantity -= ingredients[keys[i]];
+
+                            break;
+                        }
+                    }
+                }
+
+                return merchant.save();
+            })
+            .catch((err)=>{
+                return false;
+            });
     }
 }

+ 10 - 1
controllers/transactionData.js

@@ -1,6 +1,8 @@
 const Transaction = require("../models/transaction");
 const Merchant = require("../models/merchant");
 
+const helper = require("./helper.js");
+
 const ObjectId = require("mongoose").Types.ObjectId;
 
 module.exports = {
@@ -58,8 +60,12 @@ module.exports = {
         date: date of the transaction,
         recipes: [{
             recipe: id of the recipe to add,
-            quantity: quantity of the recipe sold
+            quantity: quantity of the recipe sold (in main unit),
         }]
+        ingredientUpdates: an object that contains all of the ingredients that
+            need to be updated as well as the amount to change. 
+            keys = id
+            values = quantity to change in grams
     }
     */
     createTransaction: function(req, res){
@@ -75,11 +81,14 @@ module.exports = {
             recipes: req.body.recipes
         });
 
+        helper.updateIngredientQuantities(req.body.ingredientUpdates, req.session.user);
+
         newTransaction.save()
             .then((response)=>{
                 return res.json(response);
             })
             .catch((err)=>{
+                console.log(err);
                 return res.json("ERROR: UNABLE TO CREATE NEW TRANSACTION");
             });
     },

+ 19 - 6
views/dashboardPage/bundle.js

@@ -1792,25 +1792,37 @@ let newTransaction = {
             return;
         }
         
-        let newTransaction = {
+        let data = {
             date: date,
-            recipes: []
+            recipes: [],
+            ingredientUpdates: {}
         };
 
         for(let i = 0; i < recipeDivs.children.length;  i++){
             let quantity = recipeDivs.children[i].children[1].value;
+            const recipe = recipeDivs.children[i].recipe;
             if(quantity !== "" && quantity > 0){
-                newTransaction.recipes.push({
-                    recipe: recipeDivs.children[i].recipe.id,
+                data.recipes.push({
+                    recipe: recipe.id,
                     quantity: quantity
                 });
+
+                for(let j = 0; j < recipe.ingredients.length; j++){
+                    const ingredient = recipe.ingredients[j];
+
+                    if(data.ingredientUpdates[ingredient.ingredient.id]){
+                        data.ingredientUpdates[ingredient.ingredient.id] += ingredient.quantity * quantity;
+                    }else{
+                        data.ingredientUpdates[ingredient.ingredient.id] = ingredient.quantity * quantity;
+                    }
+                }
             }else if(quantity < 0){
                 banner.createError("CANNOT HAVE NEGATIVE VALUES");
                 return;
             }
         }
 
-        if(newTransaction.recipes.length > 0){
+        if(data.recipes.length > 0){
             let loader = document.getElementById("loaderContainer");
             loader.style.display = "flex";
 
@@ -1819,7 +1831,7 @@ let newTransaction = {
                 headers: {
                     "Content-Type": "application/json;charset=utf-8"
                 },
-                body: JSON.stringify(newTransaction)
+                body: JSON.stringify(data)
             })
                 .then(response => response.json())
                 .then((response)=>{
@@ -1837,6 +1849,7 @@ let newTransaction = {
                     }
                 })
                 .catch((err)=>{
+                    console.log(err);
                     banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
                 })
                 .finally(()=>{

+ 19 - 6
views/dashboardPage/js/newTransaction.js

@@ -27,25 +27,37 @@ let newTransaction = {
             return;
         }
         
-        let newTransaction = {
+        let data = {
             date: date,
-            recipes: []
+            recipes: [],
+            ingredientUpdates: {}
         };
 
         for(let i = 0; i < recipeDivs.children.length;  i++){
             let quantity = recipeDivs.children[i].children[1].value;
+            const recipe = recipeDivs.children[i].recipe;
             if(quantity !== "" && quantity > 0){
-                newTransaction.recipes.push({
-                    recipe: recipeDivs.children[i].recipe.id,
+                data.recipes.push({
+                    recipe: recipe.id,
                     quantity: quantity
                 });
+
+                for(let j = 0; j < recipe.ingredients.length; j++){
+                    const ingredient = recipe.ingredients[j];
+
+                    if(data.ingredientUpdates[ingredient.ingredient.id]){
+                        data.ingredientUpdates[ingredient.ingredient.id] += ingredient.quantity * quantity;
+                    }else{
+                        data.ingredientUpdates[ingredient.ingredient.id] = ingredient.quantity * quantity;
+                    }
+                }
             }else if(quantity < 0){
                 banner.createError("CANNOT HAVE NEGATIVE VALUES");
                 return;
             }
         }
 
-        if(newTransaction.recipes.length > 0){
+        if(data.recipes.length > 0){
             let loader = document.getElementById("loaderContainer");
             loader.style.display = "flex";
 
@@ -54,7 +66,7 @@ let newTransaction = {
                 headers: {
                     "Content-Type": "application/json;charset=utf-8"
                 },
-                body: JSON.stringify(newTransaction)
+                body: JSON.stringify(data)
             })
                 .then(response => response.json())
                 .then((response)=>{
@@ -72,6 +84,7 @@ let newTransaction = {
                     }
                 })
                 .catch((err)=>{
+                    console.log(err);
                     banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
                 })
                 .finally(()=>{