ingredientData.js 7.1 KB

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