ingredientData.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. const Merchant = require("../models/merchant");
  2. const Ingredient = require("../models/ingredient");
  3. const InventoryAdjustment = require("../models/inventoryAdjustment.js");
  4. const Validator = require("./validator.js");
  5. module.exports = {
  6. //GET - gets a list of all database ingredients
  7. //Returns:
  8. // ingredients: list containing all ingredients
  9. getIngredients: function(req, res){
  10. Ingredient.find()
  11. .then((ingredients)=>{
  12. return res.json(ingredients);
  13. })
  14. .catch((err)=>{
  15. return res.json("ERROR: UNABLE TO RETRIEVE INGREDIENTS");
  16. });
  17. },
  18. /*
  19. POST - create a single ingredient and then add to the merchant
  20. req.body = {
  21. ingredient: {
  22. name: name of ingredient,
  23. category: category of ingredient,
  24. unitType: category for the unit (mass, volume, length)
  25. },
  26. quantity: quantity of ingredient for current merchant,
  27. defaultUnit: default unit of measurement to display
  28. }
  29. Returns:
  30. Same as above, with the _id
  31. */
  32. createIngredient: function(req, res){
  33. if(!req.session.user){
  34. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  35. return res.redirect("/");
  36. }
  37. let validation = Validator.ingredient(req.body.ingredient);
  38. if(validation !== true){
  39. return res.json(validation);
  40. }
  41. validation = Validator.quantity(req.body.quantity);
  42. if(validation !== true){
  43. return res.json(validation);
  44. }
  45. let ingredientPromise = Ingredient.create((req.body.ingredient));
  46. let merchantPromise = Merchant.findOne({_id: req.session.user});
  47. let newIngredient;
  48. Promise.all([ingredientPromise, merchantPromise])
  49. .then((response)=>{
  50. newIngredient = {
  51. ingredient: response[0],
  52. quantity: req.body.quantity,
  53. defaultUnit: req.body.defaultUnit
  54. }
  55. response[1].inventory.push(newIngredient);
  56. return response[1].save();
  57. })
  58. .then((response)=>{
  59. return res.json(newIngredient);
  60. })
  61. .catch((err)=>{
  62. return res.json("ERROR: UNABLE TO CREATE NEW INGREDIENT");
  63. });
  64. },
  65. /*
  66. POST - Updates data for a single ingredient
  67. req.body = {
  68. id: id of the ingredient,
  69. name: new name of the ingredient,
  70. quantity: new quantity of the unit (in grams),
  71. category: new category of the unit,
  72. defaultUnit: new default unit of the ingredient
  73. }
  74. */
  75. updateIngredient: function(req, res){
  76. if(!req.session.user){
  77. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  78. return res.redirect("/");
  79. }
  80. Ingredient.findOne({_id: req.body.id})
  81. .then((ingredient)=>{
  82. ingredient.name = req.body.name,
  83. ingredient.category = req.body.category
  84. return ingredient.save();
  85. })
  86. .then((ingredient)=>{
  87. return Merchant.findOne({_id: req.session.user})
  88. })
  89. .then((merchant)=>{
  90. for(let i = 0; i < merchant.inventory.length; i++){
  91. if(merchant.inventory[i].ingredient.toString() === req.body.id){
  92. merchant.inventory[i].quantity = req.body.quantity;
  93. merchant.inventory[i].defaultUnit = req.body.defaultUnit;
  94. new InventoryAdjustment({
  95. date: new Date(),
  96. merchant: req.session.user,
  97. ingredient: req.body.id,
  98. quantity: req.body.quantity - merchant.inventory[i].quantity
  99. }).save().catch(()=>{});
  100. }
  101. }
  102. return merchant.save();
  103. })
  104. .then((merchant)=>{
  105. return res.json({});
  106. })
  107. .catch((err)=>{
  108. return res.json("ERROR: UNABLE TO UPDATE INGREDIENT");
  109. });
  110. }
  111. }