ingredientData.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 * response[0].unitSize;
  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. unit: new default unit of the ingredient,
  95. unitSize: unit size for special unit, if any
  96. }
  97. */
  98. updateIngredient: function(req, res){
  99. if(!req.session.user){
  100. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  101. return res.redirect("/");
  102. }
  103. const ingredientCheck = Validator.ingredient(req.body);
  104. if(ingredientCheck !== true){
  105. return res.json(ingredientCheck);
  106. }
  107. let updatedIngredient = {};
  108. Ingredient.findOne({_id: req.body.id})
  109. .then((ingredient)=>{
  110. ingredient.name = req.body.name,
  111. ingredient.category = req.body.category
  112. if(ingredient.specialUnit === "bottle"){
  113. ingredient.unitSize = req.body.unitSize;
  114. }
  115. return ingredient.save();
  116. })
  117. .then((ingredient)=>{
  118. updatedIngredient.ingredient = ingredient;
  119. return Merchant.findOne({_id: req.session.user});
  120. })
  121. .then((merchant)=>{
  122. for(let i = 0; i < merchant.inventory.length; i++){
  123. if(merchant.inventory[i].ingredient.toString() === req.body.id){
  124. merchant.inventory[i].defaultUnit = req.body.unit;
  125. if(merchant.inventory[i].quantity !== req.body.quantity){
  126. new InventoryAdjustment({
  127. date: new Date(),
  128. merchant: req.session.user,
  129. ingredient: req.body.id,
  130. quantity: req.body.quantity - merchant.inventory[i].quantity
  131. }).save().catch(()=>{});
  132. merchant.inventory[i].quantity = req.body.quantity;
  133. }
  134. updatedIngredient.quantity = req.body.quantity;
  135. updatedIngredient.unit = req.body.unit;
  136. break;
  137. }
  138. }
  139. return merchant.save();
  140. })
  141. .then((merchant)=>{
  142. return res.json(updatedIngredient);
  143. })
  144. .catch((err)=>{
  145. return res.json("ERROR: UNABLE TO UPDATE INGREDIENT");
  146. });
  147. },
  148. //DELETE - Removes an ingredient from the merchant's inventory
  149. removeIngredient: function(req, res){
  150. if(!req.session.user){
  151. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  152. return res.redirect("/");
  153. }
  154. Merchant.findOne({_id: req.session.user})
  155. .then((merchant)=>{
  156. for(let i = 0; i < merchant.inventory.length; i++){
  157. if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
  158. merchant.inventory.splice(i, 1);
  159. break;
  160. }
  161. }
  162. return merchant.save()
  163. })
  164. .then((merchant)=>{
  165. return Ingredient.deleteOne({_id: req.params.id});
  166. })
  167. .then((ingredient)=>{
  168. return res.json({});
  169. })
  170. .catch((err)=>{
  171. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  172. });
  173. },
  174. }