ingredientData.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 xlsxUtils = require("xlsx").utils;
  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(sheet, user){
  176. const array = xlsxUtils.sheet_to_json(sheet, {
  177. header: 1
  178. });
  179. //get property locations
  180. let locations = {};
  181. for(let i = 0; i < array[0].length; i++){
  182. switch(array[0][i].toLowerCase()){
  183. case "name": locations.name = i; break;
  184. case "category": locations.category = i; break;
  185. case "quantity": locations.quantity = i; break;
  186. case "unit": locations.unit = i; break;
  187. case "bottle": locations.bottle = i; break;
  188. case "bottle size": locations.bottleSize = i; break;
  189. }
  190. }
  191. //Create ingredients
  192. let ingredients = [];
  193. let merchantData = [];
  194. for(let i = 1; i < array.length; i++){
  195. let ingredient = new Ingredient({
  196. name: array[i][locations.name],
  197. category: array[i][locations.category]
  198. });
  199. let merchantItem = {
  200. ingredient: ingredient,
  201. quantity: helper.convertQuantityToBaseUnit(array[i][locations.quantity], array[i][locations.unit]),
  202. defaultUnit: array[i][locations.unit]
  203. }
  204. if(array[i][locations.bottle] === true){
  205. ingredient.unitType = "volume";
  206. ingredient.specialUnit = "bottle";
  207. ingredient.unitSize = helper.convertQuantityToBaseUnit(array[i][locations.bottleSize], array[i][locations.unit]);
  208. }else{
  209. let unitType = "";
  210. switch(array[i][locations.unit].toLowerCase()){
  211. case "g": unitType = "mass"; break;
  212. case "kg": unitType = "mass"; break;
  213. case "oz": unitType = "mass"; break;
  214. case "lb": unitType = "mass"; break;
  215. case "ml": unitType = "volume"; break;
  216. case "l": unitType = "volume"; break;
  217. case "tsp": unitType = "volume"; break;
  218. case "tbsp": unitType = "volume"; break;
  219. case "ozfl": unitType = "volume"; break;
  220. case "cup": unitType = "volume"; break;
  221. case "pt": unitType = "volume"; break;
  222. case "qt": unitType = "volume"; break;
  223. case "gal": unitType = "volume"; break;
  224. case "mm": unitType = "length"; break;
  225. case "cm": unitType = "length"; break;
  226. case "m": unitType = "length"; break;
  227. case "in": unitType = "length"; break;
  228. case "ft": unitType = "length"; break;
  229. default: unitType = "other";
  230. }
  231. ingredient.unitType = unitType;
  232. }
  233. merchantData.push(merchantItem);
  234. ingredients.push(ingredient);
  235. }
  236. //Update the database
  237. let createdIngredients = [];
  238. return Ingredient.create(ingredients)
  239. .then((ingredients)=>{
  240. createdIngredients = ingredients;
  241. return Merchant.findOne({_id: user});
  242. })
  243. .then((merchant)=>{
  244. for(let i = 0; i < merchantData.length; i++){
  245. merchant.inventory.push(merchantData[i]);
  246. }
  247. return merchant.save();
  248. })
  249. .then((merchant)=>{
  250. return merchantData;
  251. })
  252. .catch((err)=>{
  253. return "ERROR: UNABLE TO CREATE YOUR INGREDIENTS";
  254. });
  255. }
  256. }