Sfoglia il codice sorgente

Comment out routes that are not currently being used. Refactor ingredient adjustment.

Lee Morgan 6 anni fa
parent
commit
2f4644c6b4
3 ha cambiato i file con 42 aggiunte e 31 eliminazioni
  1. 9 6
      controllers/merchantData.js
  2. 21 20
      routes.js
  3. 12 5
      views/dashboardPage/home.js

+ 9 - 6
controllers/merchantData.js

@@ -309,7 +309,7 @@ module.exports = {
     //Inputs:
     //  req.body.ingredients: array of ingredient data
     //      id: id of ingredient to update
-    //      quantityChange: Amount to change ingredient (not the new value)
+    //      quantity: New value for the ingredient
     //Returns: Nothing
     updateMerchantIngredient: function(req, res){
         if(!req.session.user){
@@ -323,18 +323,23 @@ module.exports = {
             .then((merchant)=>{
                 for(let ingredient of req.body){
                     let updateIngredient = merchant.inventory.find(i => i.ingredient.toString() === ingredient.id);
-                    updateIngredient.quantity = (updateIngredient.quantity + ingredient.quantityChange);
 
                     adjustments.push(new InventoryAdjustment({
                         date: Date.now(),
                         merchant: req.session.user,
                         ingredient: ingredient.id,
-                        quantity: ingredient.quantityChange
+                        quantity: ingredient.quantity - updateIngredient.quantity
                     }));
+
+                    updateIngredient.quantity = ingredient.quantity;
                 }
+
                 merchant.save()
                     .then((newMerchant)=>{
                         res.json({});
+
+                        InventoryAdjustment.create(adjustments).catch(()=>{});
+                        return;
                     })
                     .catch((err)=>{
                         return res.json("Error: your data could not be saved");
@@ -342,9 +347,7 @@ module.exports = {
             })
             .catch((err)=>{
                 return res.json("Error: your data could not be retrieved");
-            });
-
-        InventoryAdjustment.create(adjustments).catch(()=>{});
+            });        
     },
 
     //POST - Adds an ingredient to a recipe

+ 21 - 20
routes.js

@@ -10,43 +10,44 @@ module.exports = function(app){
     app.get("/", renderer.landingPage);
     app.get("/dashboard", renderer.displayDashboard);
     app.get("/information", renderer.displayLegal);
-    app.get("/data", renderer.displayData);
     app.get("/resetpassword/*", renderer.displayPassReset);
 
     //Merchant
     app.post("/merchant/create/none", merchantData.createMerchantNone);
-    app.get("/merchant/create/clover", merchantData.createMerchantClover);
-    app.get("/merchant/recipes/update", merchantData.updateRecipes);
-    app.post("/merchant/recipes/remove", merchantData.removeRecipe);
-    app.post("/merchant/ingredients/create", merchantData.addMerchantIngredient);
-    app.post("/merchant/ingredients/remove", merchantData.removeMerchantIngredient);
-    app.post("/merchant/ingredients/update", merchantData.updateMerchantIngredient);
-    app.post("/merchant/recipes/ingredients/create", merchantData.addRecipeIngredient);
-    app.post("/merchant/recipes/ingredients/update", merchantData.updateRecipeIngredient);
-    app.post("/merchant/recipes/ingredients/remove", merchantData.removeRecipeIngredient);
-    app.post("/merchant/update", merchantData.updateMerchant);
-    app.post("/merchant/password", merchantData.updatePassword);
+    app.post("/merchant/create/clover", merchantData.createMerchantClover);
+    // app.get("/merchant/recipes/update", merchantData.updateRecipes);
+    // app.post("/merchant/recipes/remove", merchantData.removeRecipe);
+    // app.post("/merchant/ingredients/add/:id", merchantData.addMerchantIngredient);
+    // app.post("/merchant/ingredients/remove", merchantData.removeMerchantIngredient);
+    app.put("/merchant/ingredients/update", merchantData.updateMerchantIngredient);
+    // app.post("/merchant/recipes/ingredients/create", merchantData.addRecipeIngredient);
+    // app.post("/merchant/recipes/ingredients/update", merchantData.updateRecipeIngredient);
+    // app.post("/merchant/recipes/ingredients/remove", merchantData.removeRecipeIngredient);
+    // app.post("/merchant/update", merchantData.updateMerchant);
+    // app.post("/merchant/password", merchantData.updatePassword);
 
     //Ingredients
-    app.get("/ingredients", ingredientData.getIngredients);
-    app.post("/ingredients/createone", ingredientData.createIngredient);  //also adds to merchant
+    // app.get("/ingredients", ingredientData.getIngredients);
+    // app.post("/ingredients/create", ingredientData.createIngredient);  //also adds to merchant
 
     //Recipes
-    app.post("/recipe/create", recipeData.createRecipe);
+    // app.post("/recipe/create", recipeData.createRecipe);
+
+    //Orders
 
     //Other
-    app.post("/purchases/create", otherData.createOrder);
+    // app.post("/purchases/create", otherData.createOrder);
     app.post("/login", otherData.login);
     app.get("/logout", otherData.logout);
     app.get("/cloverlogin", otherData.cloverRedirect);
     app.get("/cloverauth*", otherData.cloverAuth);
     app.post("/resetpassword", otherData.resetPassword);
 
-    app.post("/getdata", otherData.getData);
+    // app.post("/getdata", otherData.getData);
 
     //Transactions
-    app.post("/transactions", transactionData.getTransactions);
-    app.get("/purchases", transactionData.getOrders);
-    app.post("/transactions/create", transactionData.createTransaction);  //Creates transaction for non-pos merchant
+    // app.post("/transactions", transactionData.getTransactions);
+    app.get("/orders", transactionData.getOrders);
+    // app.post("/transactions/create", transactionData.createTransaction);  //Creates transaction for non-pos merchant
     app.get("/populatesometransactions", transactionData.populate);
 }

+ 12 - 5
views/dashboardPage/home.js

@@ -65,6 +65,9 @@ window.homeStrandObj = {
             }
 
             let ul = document.querySelector("#inventoryCheckCard ul");
+            while(ul.children.length > 0){
+                ul.removeChild(ul.firstChild);
+            }
             for(let rand of rands){
                 let li = document.createElement("li");
                 li.classList = "flexRow";
@@ -180,12 +183,12 @@ window.homeStrandObj = {
             if(li.children[1].value >= 0){
                 let merchIngredient = merchant.inventory[li.ingredientIndex];
 
-                let change = li.children[1].value - merchIngredient.quantity;
+                let value = parseInt(li.children[1].value);
 
-                if(change !== 0){
+                if(value !== merchIngredient.quantity){
                     changes.push({
                         id: merchIngredient.ingredient._id,
-                        quantityChange: change
+                        quantity: value
                     });
                 }
             }else{
@@ -193,10 +196,12 @@ window.homeStrandObj = {
                 return;
             }
         }
+
+        console.log(changes);
         
         if(changes.length > 0){
             fetch("/merchant/ingredients/update", {
-                method: "POST",
+                method: "PUT",
                 headers: {
                     "Content-Type": "application/json;charset=utf-8"
                 },
@@ -209,7 +214,9 @@ window.homeStrandObj = {
                         console.log(err);
                     }else{
                         for(let change of changes){
-                            merchant.inventory.find((item)=> item.ingredient._id === change.id).quantity += change.quantityChange;
+                            merchant.inventory.find((item)=> item.ingredient._id === change.id).quantity = change.quantity;
+                            this.isPopulated = false;
+                            this.display();
                         }
                     }
                 })