ingredientData.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. if(req.body.ingredient.unitSize){
  46. validation = Validator.quantity(req.body.ingredient.unitSize);
  47. if(validation !== true){
  48. return res.json(validation);
  49. }
  50. }
  51. let ingredientPromise = Ingredient.create((req.body.ingredient));
  52. let merchantPromise = Merchant.findOne({_id: req.session.user});
  53. let newIngredient;
  54. Promise.all([ingredientPromise, merchantPromise])
  55. .then((response)=>{
  56. newIngredient = {
  57. ingredient: response[0],
  58. quantity: req.body.quantity,
  59. defaultUnit: req.body.defaultUnit
  60. }
  61. response[1].inventory.push(newIngredient);
  62. return response[1].save();
  63. })
  64. .then((response)=>{
  65. return res.json(newIngredient);
  66. })
  67. .catch((err)=>{
  68. return res.json("ERROR: UNABLE TO CREATE NEW INGREDIENT");
  69. });
  70. },
  71. /*
  72. POST - Updates data for a single ingredient
  73. req.body = {
  74. id: id of the ingredient,
  75. name: new name of the ingredient,
  76. quantity: new quantity of the unit (in grams),
  77. category: new category of the unit,
  78. defaultUnit: new default unit of the ingredient
  79. }
  80. */
  81. updateIngredient: function(req, res){
  82. if(!req.session.user){
  83. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  84. return res.redirect("/");
  85. }
  86. Ingredient.findOne({_id: req.body.id})
  87. .then((ingredient)=>{
  88. ingredient.name = req.body.name,
  89. ingredient.category = req.body.category
  90. if(ingredient.specialUnit === "bottle"){
  91. ingredient.unitSize = req.body.unitSize;
  92. }
  93. return ingredient.save();
  94. })
  95. .then((ingredient)=>{
  96. return Merchant.findOne({_id: req.session.user})
  97. })
  98. .then((merchant)=>{
  99. for(let i = 0; i < merchant.inventory.length; i++){
  100. if(merchant.inventory[i].ingredient.toString() === req.body.id){
  101. merchant.inventory[i].quantity = req.body.quantity;
  102. merchant.inventory[i].defaultUnit = req.body.defaultUnit;
  103. new InventoryAdjustment({
  104. date: new Date(),
  105. merchant: req.session.user,
  106. ingredient: req.body.id,
  107. quantity: req.body.quantity - merchant.inventory[i].quantity
  108. }).save().catch(()=>{});
  109. }
  110. }
  111. return merchant.save();
  112. })
  113. .then((merchant)=>{
  114. return res.json({});
  115. })
  116. .catch((err)=>{
  117. return res.json("ERROR: UNABLE TO UPDATE INGREDIENT");
  118. });
  119. },
  120. //POST - Removes an ingredient from the merchant's inventory
  121. removeIngredient: function(req, res){
  122. if(!req.session.user){
  123. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  124. return res.redirect("/");
  125. }
  126. Merchant.findOne({_id: req.session.user})
  127. .then((merchant)=>{
  128. for(let i = 0; i < merchant.inventory.length; i++){
  129. if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
  130. merchant.inventory.splice(i, 1);
  131. break;
  132. }
  133. }
  134. return merchant.save()
  135. })
  136. .then((merchant)=>{
  137. return Ingredient.deleteOne({_id: req.params.id});
  138. })
  139. .then((ingredient)=>{
  140. return res.json({});
  141. })
  142. .catch((err)=>{
  143. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  144. });
  145. },
  146. }