ingredientData.js 8.7 KB

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