ingredientData.js 7.7 KB

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