ingredientData.js 4.3 KB

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