|
@@ -76,10 +76,64 @@ module.exports = {
|
|
|
updateIngredient: function(req, res){
|
|
updateIngredient: function(req, res){
|
|
|
let popMerchant = res.locals.merchant.populate("inventory.ingredient").execPopulate();
|
|
let popMerchant = res.locals.merchant.populate("inventory.ingredient").execPopulate();
|
|
|
|
|
|
|
|
- let stack = [];
|
|
|
|
|
Promise.all([Ingredient.findOne({_id: req.body.id}), popMerchant])
|
|
Promise.all([Ingredient.findOne({_id: req.body.id}), popMerchant])
|
|
|
.then((response)=>{
|
|
.then((response)=>{
|
|
|
- response[0].ingredients = req.body.ingredients;
|
|
|
|
|
|
|
+ response[0].name = req.body.name;
|
|
|
|
|
+ response[0].category = req.body.category;
|
|
|
|
|
+
|
|
|
|
|
+ //find and update ingredient on merchant
|
|
|
|
|
+ for(let i = 0; i < res.locals.merchant.inventory.length; i++){
|
|
|
|
|
+ if(res.locals.merchant.inventory[i].ingredient.toString() === req.body.id){
|
|
|
|
|
+ res.locals.merchant.inventory[i].defaultUnit = req.body.unit;
|
|
|
|
|
+
|
|
|
|
|
+ if(res.locals.merchant.inventory[i].quantity !== req.body.quantity){
|
|
|
|
|
+ new InventoryAdjustment({
|
|
|
|
|
+ date: new Date(),
|
|
|
|
|
+ merchant: req.session.owner,
|
|
|
|
|
+ ingredient: req.body.id,
|
|
|
|
|
+ quantity: req.body.quantity - res.locals.merchant.inventory[i].quantity
|
|
|
|
|
+ }).save().catch(()=>{});
|
|
|
|
|
+
|
|
|
|
|
+ res.locals.merchant.inventory[i].quantity = req.body.quantity;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return Promise.all([response[0].save(), res.locals.merchant.save()])
|
|
|
|
|
+ })
|
|
|
|
|
+ .then((response)=>{
|
|
|
|
|
+ return res.json({
|
|
|
|
|
+ ingredient: response[0],
|
|
|
|
|
+ quantity: req.body.quantity,
|
|
|
|
|
+ defaultUnit: req.body.unit
|
|
|
|
|
+ });
|
|
|
|
|
+ })
|
|
|
|
|
+ .catch((err)=>{
|
|
|
|
|
+ if(err.name === "ValidationError"){
|
|
|
|
|
+ return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
|
|
|
|
|
+ }
|
|
|
|
|
+ return res.json("ERROR: UNABLE TO UPDATE DATA");
|
|
|
|
|
+ });
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
|
|
+ /*
|
|
|
|
|
+ PUT: updates subingredients on an ingredient
|
|
|
|
|
+ req.body = {
|
|
|
|
|
+ id: String (top-level ingredient id),
|
|
|
|
|
+ ingredients: {
|
|
|
|
|
+ ingredient: String (id)
|
|
|
|
|
+ quantity: Number
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ response = Ingredient
|
|
|
|
|
+ error response = '$' delimited String
|
|
|
|
|
+ */
|
|
|
|
|
+ updateSubIngredients: function(req, res){
|
|
|
|
|
+ let stack = [];
|
|
|
|
|
+ Ingredient.findOne({_id: req.body.id})
|
|
|
|
|
+ .then((response)=>{
|
|
|
|
|
+ response.ingredients = req.body.ingredients;
|
|
|
|
|
|
|
|
// Check ingredients for circular references
|
|
// Check ingredients for circular references
|
|
|
let isCircular = (ingredient, original)=>{
|
|
let isCircular = (ingredient, original)=>{
|
|
@@ -106,39 +160,16 @@ module.exports = {
|
|
|
let ingredient = res.locals.merchant.inventory[j].ingredient;
|
|
let ingredient = res.locals.merchant.inventory[j].ingredient;
|
|
|
stack = [ingredient];
|
|
stack = [ingredient];
|
|
|
if(ingredient._id.toString() === req.body.id) throw "circular";
|
|
if(ingredient._id.toString() === req.body.id) throw "circular";
|
|
|
- if(isCircular(ingredient, response[0]) === true) throw "circular";
|
|
|
|
|
|
|
+ if(isCircular(ingredient, response) === true) throw "circular";
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- //find and update ingredient on merchant
|
|
|
|
|
- for(let i = 0; i < res.locals.merchant.inventory.length; i++){
|
|
|
|
|
- if(res.locals.merchant.inventory[i].ingredient.toString() === req.body.id){
|
|
|
|
|
- res.locals.merchant.inventory[i].defaultUnit = req.body.unit;
|
|
|
|
|
-
|
|
|
|
|
- if(res.locals.merchant.inventory[i].quantity !== req.body.quantity){
|
|
|
|
|
- new InventoryAdjustment({
|
|
|
|
|
- date: new Date(),
|
|
|
|
|
- merchant: req.session.owner,
|
|
|
|
|
- ingredient: req.body.id,
|
|
|
|
|
- quantity: req.body.quantity - res.locals.merchant.inventory[i].quantity
|
|
|
|
|
- }).save().catch(()=>{});
|
|
|
|
|
-
|
|
|
|
|
- res.locals.merchant.inventory[i].quantity = req.body.quantity;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- break;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- return Promise.all([response[0].save(), res.locals.merchant.save()])
|
|
|
|
|
|
|
+ return response.save();
|
|
|
})
|
|
})
|
|
|
- .then((response)=>{
|
|
|
|
|
- return res.json({
|
|
|
|
|
- ingredient: response[0],
|
|
|
|
|
- quantity: req.body.quantity,
|
|
|
|
|
- defaultUnit: req.body.unit
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ .then((ingredient)=>{
|
|
|
|
|
+ return res.json(ingredient);
|
|
|
})
|
|
})
|
|
|
.catch((err)=>{
|
|
.catch((err)=>{
|
|
|
if(err === "circular"){
|
|
if(err === "circular"){
|
|
@@ -158,10 +189,7 @@ module.exports = {
|
|
|
|
|
|
|
|
return res.json(string);
|
|
return res.json(string);
|
|
|
}
|
|
}
|
|
|
- if(err.name === "ValidationError"){
|
|
|
|
|
- return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
|
|
|
|
|
- }
|
|
|
|
|
- return res.json("ERROR: UNABLE TO UPDATE DATA");
|
|
|
|
|
|
|
+ console.log(err);
|
|
|
});
|
|
});
|
|
|
},
|
|
},
|
|
|
|
|
|