ソースを参照

Editing recipe ingredients now logs the change

Lee Morgan 6 年 前
コミット
36ce2b63f7

+ 25 - 2
controllers/merchantData.js

@@ -511,7 +511,7 @@ module.exports = {
                             let rc = new RecipeChange({
                                 recipe: recipe,
                                 ingredient: req.body.item.ingredient,
-                                amount: req.body.item.quantity
+                                change: req.body.item.quantity
                             });
                             rc.save()
                                 .catch((err)=>{
@@ -522,6 +522,8 @@ module.exports = {
                                     });
                                     error.save();
                                 });
+
+                            return;
                         })
                     })
                     .catch((err)=>{
@@ -564,12 +566,32 @@ module.exports = {
             .then((recipe)=>{
                 for(let ingredient of recipe.ingredients){
                     if(ingredient._id.toString() === req.body.ingredient._id){
+                        let change = Number(req.body.ingredient.quantity) - ingredient.quantity;
                         ingredient.quantity = req.body.ingredient.quantity;
+
                         recipe.save()
                             .then((recipe)=>{
-                                return res.json({});
+                                res.json({});
+                                
+                                let rc = new RecipeChange({
+                                    recipe: recipe,
+                                    ingredient: ingredient.ingredient,
+                                    change: change
+                                });
+                                rc.save()
+                                    .catch((err)=>{
+                                        let error = new Error({
+                                            code: 120,
+                                            errorMessage: "none",
+                                            error: err
+                                        });
+                                        error.save();
+                                    });
+
+                                return;
                             })
                             .catch((err)=>{
+                                console.log(err);
                                 let errorMessage = "There was an error and the recipe could not be updated";
                                 let error = new Error({
                                     code: 547,
@@ -584,6 +606,7 @@ module.exports = {
                 }
             })
             .catch((err)=>{
+                console.log(err);
                 let errorMessage = "There was an error and the recipe could not be updated";
                 let error = new Error({
                     code: 626,

+ 5 - 0
controllers/renderer.js

@@ -33,6 +33,7 @@ module.exports = {
             req.session.error = "You must logged in to view that page";
             return res.redirect("/");
         }
+        console.log("Session good, start of function");
 
         Merchant.findOne({_id: req.session.user})
             .populate("inventory.ingredient")
@@ -45,9 +46,11 @@ module.exports = {
                 }
             })
             .then((merchant)=>{
+                console.log("Got the merchant");
                 if(merchant.pos === "clover"){
                     axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
                         .then((result)=>{
+                            console.log("axios to clover returned");
                             let transactions = [];
                             for(let order of result.data.elements){
                                 let newTransaction = new Transaction({
@@ -75,12 +78,14 @@ module.exports = {
                                 transactions.push(newTransaction);
                             }
                             merchant.lastUpdatedTime = Date.now();
+                            console.log("Did all calculations on Clover data");
 
                             merchant.save()
                                 .then((updatedMerchant)=>{
                                     merchant.password = undefined;
                                     res.render("inventoryPage/inventory", {merchant: updatedMerchant, error: undefined});
                                     Transaction.create(transactions);
+                                    console.log("Saved merchant, render page");
                                     return;
                                 })
                                 .catch((err)=>{

+ 5 - 1
models/recipeChange.js

@@ -11,9 +11,13 @@ const RecipeChangeSchema = new mongoose.Schema({
         ref: "Ingredient",
         required: true
     },
-    amount: {
+    change: {
         type: Number,
         required: true
+    },
+    date: {
+        type: Date,
+        default: Date.now
     }
 });
 

+ 1 - 0
views/recipesPage/singleRecipe.js

@@ -252,6 +252,7 @@ let singleRecipeObj = {
                     }
                 })
                 .catch((err)=>{
+                    console.log(err);
                     td.innerText = `${originalQuantity} ${ingredient.ingredient.unit}`;
                     banner.createError("There was an error and the ingredient could not be updated");
                 });