|
|
@@ -1,5 +1,6 @@
|
|
|
-const Recipe = require("../models/recipe");
|
|
|
-const Merchant = require("../models/merchant");
|
|
|
+const Recipe = require("../models/recipe.js");
|
|
|
+const Merchant = require("../models/merchant.js");
|
|
|
+const RecipeChange = require("../models/recipeChange.js");
|
|
|
const Validator = require("./validator.js");
|
|
|
|
|
|
module.exports = {
|
|
|
@@ -78,8 +79,52 @@ module.exports = {
|
|
|
return res.json(validation);
|
|
|
}
|
|
|
|
|
|
+ let changes = [];
|
|
|
+
|
|
|
Recipe.findOne({_id: req.body.id})
|
|
|
.then((recipe)=>{
|
|
|
+ for(let i = 0; i < req.body.ingredients.length; i++){
|
|
|
+ let isMatch = false;
|
|
|
+ for(let j = 0; j < recipe.ingredients.length; j++){
|
|
|
+ if(req.body.ingredients[i].ingredient === recipe.ingredients[j].ingredient.toString()){
|
|
|
+ let difference = parseFloat((req.body.ingredients[i].quantity - recipe.ingredients[j].quantity).toFixed(2));
|
|
|
+ if(difference !== 0){
|
|
|
+ changes.push({
|
|
|
+ ingredient: recipe.ingredients[j].ingredient,
|
|
|
+ change: difference
|
|
|
+ });
|
|
|
+ }
|
|
|
+ isMatch = true;
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!isMatch){
|
|
|
+ changes.push({
|
|
|
+ ingredient: req.body.ingredients[i].ingredient,
|
|
|
+ change: req.body.ingredients[i].quantity
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for(let i = 0; i < recipe.ingredients.length; i++){
|
|
|
+ let isMatch = false;
|
|
|
+ for(let j = 0; j < req.body.ingredients.length; j++){
|
|
|
+ if(recipe.ingredients[i].ingredient.toString() === req.body.ingredients[i].ingredient){
|
|
|
+ isMatch = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!isMatch){
|
|
|
+ changes.push({
|
|
|
+ ingredient: recipe.ingredients[i].ingredient,
|
|
|
+ change: -recipe.ingredients[i].quantity
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.log(changes);
|
|
|
+
|
|
|
recipe.name = req.body.name;
|
|
|
recipe.price = req.body.price;
|
|
|
recipe.ingredients = req.body.ingredients;
|
|
|
@@ -87,7 +132,15 @@ module.exports = {
|
|
|
return recipe.save()
|
|
|
})
|
|
|
.then((response)=>{
|
|
|
- return res.json({});
|
|
|
+ res.json({});
|
|
|
+
|
|
|
+ let recipeChange = new RecipeChange({
|
|
|
+ recipe: response._id,
|
|
|
+ date: new Date(),
|
|
|
+ changes: changes
|
|
|
+ });
|
|
|
+
|
|
|
+ return recipeChange.save()
|
|
|
})
|
|
|
.catch((err)=>{
|
|
|
return res.json("ERROR: UNABLE TO UPDATE RECIPE");
|