ingredientData.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. const Merchant = require("../models/merchant");
  2. const Ingredient = require("../models/ingredient");
  3. const InventoryAdjustment = require("../models/inventoryAdjustment.js");
  4. const Helper = require("./helper.js");
  5. const Validator = require("./validator.js");
  6. module.exports = {
  7. //GET - gets a list of all database ingredients
  8. //Returns:
  9. // ingredients: list containing all ingredients
  10. getIngredients: function(req, res){
  11. Ingredient.find()
  12. .then((ingredients)=>{
  13. return res.json(ingredients);
  14. })
  15. .catch((err)=>{
  16. return res.json("ERROR: UNABLE TO RETRIEVE INGREDIENTS");
  17. });
  18. },
  19. /*
  20. POST - create a single ingredient and then add to the merchant
  21. req.body = {
  22. ingredient: {
  23. name: name of ingredient,
  24. category: category of ingredient,
  25. unitType: category for the unit (mass, volume, length)
  26. },
  27. quantity: quantity of ingredient for current merchant,
  28. defaultUnit: default unit of measurement to display
  29. }
  30. Returns:
  31. Same as above, with the _id
  32. */
  33. createIngredient: function(req, res){
  34. if(!req.session.user){
  35. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  36. return res.redirect("/");
  37. }
  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. if(req.body.ingredient.unitSize){
  47. validation = Validator.quantity(req.body.ingredient.unitSize);
  48. if(validation !== true){
  49. return res.json(validation);
  50. }
  51. }
  52. let newIngredient = {};
  53. if(req.body.ingredient.specialUnit === "bottle"){
  54. newIngredient = new Ingredient({
  55. name: req.body.ingredient.name,
  56. category: req.body.ingredient.category,
  57. unitType: req.body.ingredient.unitType,
  58. specialUnit: req.body.ingredient.specialUnit,
  59. unitSize: Helper.convertQuantityToBaseUnit(req.body.ingredient.unitSize, req.body.defaultUnit)
  60. });
  61. }else{
  62. newIngredient = new Ingredient(req.body.ingredient);
  63. }
  64. let ingredientPromise = newIngredient.save();
  65. let merchantPromise = Merchant.findOne({_id: req.session.user});
  66. Promise.all([ingredientPromise, merchantPromise])
  67. .then((response)=>{
  68. newIngredient = {
  69. ingredient: response[0],
  70. defaultUnit: req.body.defaultUnit
  71. }
  72. if(response[0].specialUnit === "bottle"){
  73. newIngredient.quantity = req.body.quantity;
  74. }else{
  75. newIngredient.quantity = Helper.convertQuantityToBaseUnit(req.body.quantity, req.body.defaultUnit);
  76. }
  77. response[1].inventory.push(newIngredient);
  78. return response[1].save();
  79. })
  80. .then((response)=>{
  81. return res.json(newIngredient);
  82. })
  83. .catch((err)=>{
  84. return res.json("ERROR: UNABLE TO CREATE NEW INGREDIENT");
  85. });
  86. },
  87. /*
  88. POST - Updates data for a single ingredient
  89. req.body = {
  90. id: id of the ingredient,
  91. name: new name of the ingredient,
  92. quantity: new quantity of the unit (in grams),
  93. category: new category of the unit,
  94. defaultUnit: new default unit of the ingredient
  95. }
  96. */
  97. updateIngredient: function(req, res){
  98. if(!req.session.user){
  99. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  100. return res.redirect("/");
  101. }
  102. Ingredient.findOne({_id: req.body.id})
  103. .then((ingredient)=>{
  104. ingredient.name = req.body.name,
  105. ingredient.category = req.body.category
  106. if(ingredient.specialUnit === "bottle"){
  107. ingredient.unitSize = req.body.unitSize;
  108. }
  109. return ingredient.save();
  110. })
  111. .then((ingredient)=>{
  112. return Merchant.findOne({_id: req.session.user})
  113. })
  114. .then((merchant)=>{
  115. for(let i = 0; i < merchant.inventory.length; i++){
  116. if(merchant.inventory[i].ingredient.toString() === req.body.id){
  117. merchant.inventory[i].quantity = req.body.quantity;
  118. merchant.inventory[i].defaultUnit = req.body.defaultUnit;
  119. new InventoryAdjustment({
  120. date: new Date(),
  121. merchant: req.session.user,
  122. ingredient: req.body.id,
  123. quantity: req.body.quantity - merchant.inventory[i].quantity
  124. }).save().catch(()=>{});
  125. }
  126. }
  127. return merchant.save();
  128. })
  129. .then((merchant)=>{
  130. return res.json({});
  131. })
  132. .catch((err)=>{
  133. return res.json("ERROR: UNABLE TO UPDATE INGREDIENT");
  134. });
  135. },
  136. //POST - Removes an ingredient from the merchant's inventory
  137. removeIngredient: function(req, res){
  138. if(!req.session.user){
  139. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  140. return res.redirect("/");
  141. }
  142. Merchant.findOne({_id: req.session.user})
  143. .then((merchant)=>{
  144. for(let i = 0; i < merchant.inventory.length; i++){
  145. if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
  146. merchant.inventory.splice(i, 1);
  147. break;
  148. }
  149. }
  150. return merchant.save()
  151. })
  152. .then((merchant)=>{
  153. return Ingredient.deleteOne({_id: req.params.id});
  154. })
  155. .then((ingredient)=>{
  156. return res.json({});
  157. })
  158. .catch((err)=>{
  159. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  160. });
  161. },
  162. }