ingredientData.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. validation = Validator.quantity(req.body.ingredient.unitSize);
  46. if(validation !== true){
  47. return res.json(validation);
  48. }
  49. let ingredientPromise = Ingredient.create((req.body.ingredient));
  50. let merchantPromise = Merchant.findOne({_id: req.session.user});
  51. let newIngredient;
  52. Promise.all([ingredientPromise, merchantPromise])
  53. .then((response)=>{
  54. newIngredient = {
  55. ingredient: response[0],
  56. quantity: req.body.quantity,
  57. defaultUnit: req.body.defaultUnit
  58. }
  59. response[1].inventory.push(newIngredient);
  60. return response[1].save();
  61. })
  62. .then((response)=>{
  63. return res.json(newIngredient);
  64. })
  65. .catch((err)=>{
  66. return res.json("ERROR: UNABLE TO CREATE NEW INGREDIENT");
  67. });
  68. },
  69. /*
  70. POST - Updates data for a single ingredient
  71. req.body = {
  72. id: id of the ingredient,
  73. name: new name of the ingredient,
  74. quantity: new quantity of the unit (in grams),
  75. category: new category of the unit,
  76. defaultUnit: new default unit of the ingredient
  77. }
  78. */
  79. updateIngredient: function(req, res){
  80. if(!req.session.user){
  81. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  82. return res.redirect("/");
  83. }
  84. Ingredient.findOne({_id: req.body.id})
  85. .then((ingredient)=>{
  86. ingredient.name = req.body.name,
  87. ingredient.category = req.body.category
  88. return ingredient.save();
  89. })
  90. .then((ingredient)=>{
  91. return Merchant.findOne({_id: req.session.user})
  92. })
  93. .then((merchant)=>{
  94. for(let i = 0; i < merchant.inventory.length; i++){
  95. if(merchant.inventory[i].ingredient.toString() === req.body.id){
  96. merchant.inventory[i].quantity = req.body.quantity;
  97. merchant.inventory[i].defaultUnit = req.body.defaultUnit;
  98. new InventoryAdjustment({
  99. date: new Date(),
  100. merchant: req.session.user,
  101. ingredient: req.body.id,
  102. quantity: req.body.quantity - merchant.inventory[i].quantity
  103. }).save().catch(()=>{});
  104. }
  105. }
  106. return merchant.save();
  107. })
  108. .then((merchant)=>{
  109. return res.json({});
  110. })
  111. .catch((err)=>{
  112. return res.json("ERROR: UNABLE TO UPDATE INGREDIENT");
  113. });
  114. }
  115. }