ingredientData.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. const Ingredient = require("../models/ingredient");
  2. const helper = require("./helper.js");
  3. const xlsx = require("xlsx");
  4. const fs = require("fs");
  5. module.exports = {
  6. /*
  7. POST - create a single ingredient and then add to the merchant
  8. req.body = {
  9. ingredient: {
  10. name: name of ingredient,
  11. category: category of ingredient,
  12. convert: {
  13. toMass: Number
  14. toVolume: Number
  15. toLength: Number
  16. }
  17. unit: String
  18. }
  19. quantity: quantity of ingredient for current merchant,
  20. }
  21. Returns:
  22. Same as above, with the _id
  23. */
  24. createIngredient: function(req, res){
  25. let newIngredient = new Ingredient({
  26. name: req.body.ingredient.name,
  27. category: req.body.ingredient.category,
  28. unit: req.body.ingredient.unit,
  29. convert: req.body.ingredient.convert,
  30. ingredients: []
  31. });
  32. newIngredient.save()
  33. .then((ingredient)=>{
  34. newIngredient = {
  35. ingredient: ingredient,
  36. quantity: req.body.quantity,
  37. }
  38. res.locals.merchant.inventory.push(newIngredient);
  39. return res.locals.merchant.save();
  40. })
  41. .then((response)=>{
  42. return res.json(newIngredient);
  43. })
  44. .catch((err)=>{
  45. if(typeof(err) === "string") return res.json(err);
  46. if(err.name === "ValidationError"){
  47. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  48. }
  49. return res.json("ERROR: UNABLE TO CREATE THE INGREDIENT");
  50. });
  51. },
  52. /*
  53. PUT: Updates data for a single ingredient
  54. req.body = {
  55. ingredient: {
  56. id: String (id of Ingredient)
  57. name: String
  58. category: String
  59. unit: String
  60. convert: {
  61. toMass: Number
  62. toVolume: Number
  63. toLength: Number
  64. }
  65. }
  66. quantity: Number
  67. }
  68. response = Ingredient
  69. */
  70. updateIngredient: function(req, res){
  71. Ingredient.findOne({_id: req.body.ingredient.id})
  72. .then((ingredient)=>{
  73. ingredient.name = req.body.ingredient.name;
  74. ingredient.category = req.body.ingredient.category;
  75. ingredient.convert = req.body.ingredient.convert;
  76. ingredient.unit = req.body.ingredient.unit;
  77. //find and update ingredient on merchant
  78. for(let i = 0; i < res.locals.merchant.inventory.length; i++){
  79. if(res.locals.merchant.inventory[i].ingredient.toString() === req.body.ingredient.id){
  80. res.locals.merchant.inventory[i].quantity = req.body.quantity;
  81. break;
  82. }
  83. }
  84. return Promise.all([ingredient.save(), res.locals.merchant.save()])
  85. })
  86. .then((response)=>{
  87. return res.json({
  88. ingredient: response[0],
  89. quantity: req.body.quantity
  90. });
  91. })
  92. .catch((err)=>{
  93. if(err.name === "ValidationError"){
  94. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  95. }
  96. return res.json("ERROR: UNABLE TO UPDATE DATA");
  97. });
  98. },
  99. /*
  100. PUT: updates subingredients on an ingredient
  101. req.body = {
  102. id: String (top-level ingredient id),
  103. ingredients: [{
  104. ingredient: String (id)
  105. quantity: Number
  106. unit: String
  107. }]
  108. }
  109. response = Ingredient
  110. error response = '$' delimited String
  111. */
  112. updateSubIngredients: function(req, res){
  113. let popMerchant = res.locals.merchant.populate("inventory.ingredient").execPopulate();
  114. let stack = [];
  115. Promise.all([Ingredient.findOne({_id: req.body.id}), popMerchant])
  116. .then((response)=>{
  117. response[0].ingredients = req.body.ingredients;
  118. // Check ingredients for circular references
  119. let isCircular = (ingredient, original)=>{
  120. if(ingredient.ingredients.length === 0) {
  121. stack.pop();
  122. return false;
  123. }
  124. for(let i = 0; i < ingredient.ingredients.length; i++){
  125. for(let j = 0; j < res.locals.merchant.inventory.length; j++){
  126. if(res.locals.merchant.inventory[j].ingredient._id.toString() === ingredient.ingredients[i].ingredient.toString()){
  127. let next = res.locals.merchant.inventory[j].ingredient;
  128. stack.push(next);
  129. if(next._id.toString() === original._id.toString()) return true;
  130. return isCircular(next, original);
  131. }
  132. }
  133. }
  134. }
  135. for(let i = 0; i < req.body.ingredients.length; i++){
  136. for(let j = 0; j < res.locals.merchant.inventory.length; j++){
  137. if(res.locals.merchant.inventory[j].ingredient._id.toString() === req.body.ingredients[i].ingredient){
  138. let ingredient = res.locals.merchant.inventory[j].ingredient;
  139. stack = [ingredient];
  140. if(ingredient._id.toString() === req.body.id) throw "circular";
  141. if(isCircular(ingredient, response[0]) === true) throw "circular";
  142. break;
  143. }
  144. }
  145. }
  146. return Promise.all([response[0].save(), res.locals.merchant.save()])
  147. })
  148. .then((response)=>{
  149. return res.json(response[0]);
  150. })
  151. .catch((err)=>{
  152. if(err === "circular"){
  153. let string = "YOU ATTEMPTED TO MAKE A CIRCULAR REFERENCE";
  154. if(stack.length === 1){
  155. string += `$${stack[0].name} CONTAINS ${stack[0].name}`;
  156. }else{
  157. for(let i = 0; i < stack.length; i++){
  158. if(i === stack.length - 1){
  159. string += `$${stack[i].name} CONTAINS ${stack[0].name}`;
  160. break;
  161. }
  162. string += `$${stack[i].name} CONTAINS ${stack[i+1].name}`;
  163. }
  164. }
  165. return res.json(string);
  166. }
  167. return res.json("ERROR: UNABLE TO UPDATE YOUR SUB-INGREDIENTS");
  168. });
  169. },
  170. //DELETE - Removes an ingredient from the merchant's inventory
  171. removeIngredient: function(req, res){
  172. for(let i = 0; i < res.locals.merchant.inventory.length; i++){
  173. if(req.params.id === res.locals.merchant.inventory[i].ingredient._id.toString()){
  174. res.locals.merchant.inventory.splice(i, 1);
  175. break;
  176. }
  177. }
  178. Promise.all([res.locals.merchant.save(), Ingredient.deleteOne({_id: req.params.id})])
  179. .then((response)=>{
  180. return res.json({});
  181. })
  182. .catch((err)=>{
  183. if(typeof(err) === "string"){
  184. return res.json(err);
  185. }
  186. if(err.name === "ValidationError"){
  187. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  188. }
  189. return res.json("ERROR: UNABLE TO RETRIEVE DATA");
  190. });
  191. }
  192. }