Quellcode durchsuchen

Remove nonpostransaction and change over to standard transaction

Lee Morgan vor 6 Jahren
Ursprung
Commit
fb7aa8a024
4 geänderte Dateien mit 80 neuen und 76 gelöschten Zeilen
  1. 0 55
      controllers/otherData.js
  2. 57 1
      controllers/transactionData.js
  3. 2 1
      routes.js
  4. 21 19
      views/inventoryPage/enterTransactions.js

+ 0 - 55
controllers/otherData.js

@@ -6,61 +6,6 @@ const Merchant = require("../models/merchant");
 const Purchase = require("../models/purchase");
 
 module.exports = {
-    //POST - Update non-pos merchant inventory and create a transaction
-    //Inputs:
-    //  recipesSold: list of recipes sold and how much (recipe._id and quantity)
-    //Returns:
-    //  merchant.inventory: entire merchant inventory after being updated
-    createTransaction: function(req, res){
-        if(!req.session.user){
-            res.session.error = "Must be logged in to do that";
-            return res.redirect("/");
-        }
-
-        let transaction = new NonPosTransaction({
-            date: Date.now(),
-            merchant: req.session.user,
-            recipes: []
-        });
-
-        for(let recipe of req.body){
-            transaction.recipes.push({
-                recipe: recipe.id,
-                quantity: recipe.quantity
-            });
-        }
-
-        //Calculate all ingredients used, store to list
-        Merchant.findOne({_id: req.session.user})
-            .populate("recipes")
-            .then((merchant)=>{
-                for(let reqRecipe of req.body){
-                    let merchRecipe = merchant.recipes.find(r => r._id.toString() === reqRecipe.id);
-                    for(let recipeIngredient of merchRecipe.ingredients){
-                        let merchInvIngredient = merchant.inventory.find(i => i.ingredient.toString() === recipeIngredient.ingredient.toString());
-                        merchInvIngredient.quantity -= recipeIngredient.quantity * reqRecipe.quantity;
-                    }
-                }
-
-                merchant.save()
-                    .then((merchant)=>{
-                        res.json({});
-                    })
-                    .catch((err)=>{
-                        return res.json("Error: unable to save user data");
-                    });
-            })
-            .catch((err)=>{
-                return res.json("Error: unable to retrieve user data");
-            });
-
-        transaction.save()
-            .then((transaction)=>{
-                return;
-            })
-            .catch((err)=>{});
-    },
-
     //POST - Creates a new purchase for a merchant
     //Inputs:
     //  req.body: list of purchases (ingredient id and quantity)

+ 57 - 1
controllers/transactionData.js

@@ -1,5 +1,6 @@
 const Transaction = require("../models/transaction");
 const Purchase = require("../models/purchase");
+const Merchant = require("../models/merchant");
 
 module.exports = {
     getTransactions: function(req, res){
@@ -34,5 +35,60 @@ module.exports = {
             .catch((err)=>{
                 return res.json("Error: could not retrieve purchases data");
             })
-    }
+    },
+
+    //POST - Update non-pos merchant inventory and create a transaction
+    //Inputs:
+    //  recipesSold: list of recipes sold and how much (recipe._id and quantity)
+    //Returns:
+    //  merchant.inventory: entire merchant inventory after being updated
+    createTransaction: function(req, res){
+        if(!req.session.user){
+            res.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        let transaction = new Transaction({
+            date: Date.now(),
+            merchant: req.session.user,
+            recipes: []
+        });
+
+        for(let recipe of req.body){
+            transaction.recipes.push({
+                recipe: recipe.id,
+                quantity: recipe.quantity
+            });
+        }
+
+        //Calculate all ingredients used, store to list
+        Merchant.findOne({_id: req.session.user})
+            .populate("recipes")
+            .then((merchant)=>{
+                for(let reqRecipe of req.body){
+                    let merchRecipe = merchant.recipes.find(r => r._id.toString() === reqRecipe.id);
+                    for(let recipeIngredient of merchRecipe.ingredients){
+                        let merchInvIngredient = merchant.inventory.find(i => i.ingredient.toString() === recipeIngredient.ingredient.toString());
+                        merchInvIngredient.quantity -= recipeIngredient.quantity * reqRecipe.quantity;
+                    }
+                }
+
+                merchant.save()
+                    .then((merchant)=>{
+                        res.json({});
+                    })
+                    .catch((err)=>{
+                        return res.json("Error: unable to save user data");
+                    });
+            })
+            .catch((err)=>{
+                return res.json("Error: unable to retrieve user data");
+            });
+
+        transaction.save()
+            .then((transaction)=>{
+                return;
+            })
+            .catch((err)=>{});
+    },
 }

+ 2 - 1
routes.js

@@ -34,7 +34,7 @@ module.exports = function(app){
     app.post("/recipe/create", recipeData.createRecipe);
 
     //Other
-    app.post("/transactions/create", otherData.createTransaction);  //Creates transaction for non-pos merchant
+    
     app.post("/purchases/create", otherData.createPurchase);
     app.post("/login", otherData.login);
     app.get("/logout", otherData.logout);
@@ -45,4 +45,5 @@ module.exports = function(app){
     //Transactions
     app.get("/transactions", transactionData.getTransactions);
     app.get("/purchases", transactionData.getPurchases);
+    app.post("/transactions/create", transactionData.createTransaction);  //Creates transaction for non-pos merchant
 }

+ 21 - 19
views/inventoryPage/enterTransactions.js

@@ -56,26 +56,28 @@ window.enterTransactionsObj = {
             }
         }
 
-        axios.post("/transactions/create", recipes)
-            .then((response)=>{
-                if(typeof(response.data) === "string"){
-                    banner.createError(response.data);
-                }else{
-                    for(let recipe of recipes){
-                        let merchRecipe = merchant.recipes.find(r => r._id === recipe.id);
-                        for(let recipeIngredient of merchRecipe.ingredients){
-                            let merchInvIngredient = merchant.inventory.find(i => i.ingredient._id === recipeIngredient.ingredient._id);
-                            merchInvIngredient.quantity -= recipeIngredient.quantity * recipe.quantity;
+        if(recipes.length > 0){
+            axios.post("/transactions/create", recipes)
+                .then((response)=>{
+                    if(typeof(response.data) === "string"){
+                        banner.createError(response.data);
+                    }else{
+                        for(let recipe of recipes){
+                            let merchRecipe = merchant.recipes.find(r => r._id === recipe.id);
+                            for(let recipeIngredient of merchRecipe.ingredients){
+                                let merchInvIngredient = merchant.inventory.find(i => i.ingredient._id === recipeIngredient.ingredient._id);
+                                merchInvIngredient.quantity -= recipeIngredient.quantity * recipe.quantity;
+                            }
                         }
-                    }
 
-                    inventoryObj.isPopulated = false;
-                    inventoryObj.display();
-                    banner.createNotification("Your sales have been logged");
-                }
-            })
-            .catch((err)=>{
-                banner.createError("Something went wrong and your sales could not be logged");
-            });
+                        inventoryObj.isPopulated = false;
+                        inventoryObj.display();
+                        banner.createNotification("Your sales have been logged");
+                    }
+                })
+                .catch((err)=>{
+                    banner.createError("Something went wrong and your sales could not be logged");
+                });
+        }
     }
 }