ingredientData.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. const fs = require("fs");
  8. module.exports = {
  9. //GET - gets a list of all database ingredients
  10. //Returns:
  11. // ingredients: list containing all ingredients
  12. getIngredients: function(req, res){
  13. Ingredient.find()
  14. .then((ingredients)=>{
  15. return res.json(ingredients);
  16. })
  17. .catch((err)=>{
  18. return res.json("ERROR: UNABLE TO RETRIEVE INGREDIENTS");
  19. });
  20. },
  21. /*
  22. POST - create a single ingredient and then add to the merchant
  23. req.body = {
  24. ingredient: {
  25. name: name of ingredient,
  26. category: category of ingredient,
  27. unitType: category for the unit (mass, volume, length)
  28. },
  29. quantity: quantity of ingredient for current merchant,
  30. defaultUnit: default unit of measurement to display
  31. }
  32. Returns:
  33. Same as above, with the _id
  34. */
  35. createIngredient: function(req, res){
  36. if(!req.session.user){
  37. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  38. return res.redirect("/");
  39. }
  40. let validation = Validator.ingredient(req.body.ingredient);
  41. if(validation !== true){
  42. return res.json(validation);
  43. }
  44. validation = Validator.quantity(req.body.quantity);
  45. if(validation !== true){
  46. return res.json(validation);
  47. }
  48. if(req.body.ingredient.unitSize){
  49. validation = Validator.quantity(req.body.ingredient.unitSize);
  50. if(validation !== true){
  51. return res.json(validation);
  52. }
  53. }
  54. let newIngredient = {};
  55. if(req.body.ingredient.specialUnit === "bottle"){
  56. newIngredient = new Ingredient({
  57. name: req.body.ingredient.name,
  58. category: req.body.ingredient.category,
  59. unitType: req.body.ingredient.unitType,
  60. specialUnit: req.body.ingredient.specialUnit,
  61. unitSize: Helper.convertQuantityToBaseUnit(req.body.ingredient.unitSize, req.body.defaultUnit)
  62. });
  63. }else{
  64. newIngredient = new Ingredient(req.body.ingredient);
  65. }
  66. let ingredientPromise = newIngredient.save();
  67. let merchantPromise = Merchant.findOne({_id: req.session.user});
  68. Promise.all([ingredientPromise, merchantPromise])
  69. .then((response)=>{
  70. newIngredient = {
  71. ingredient: response[0],
  72. defaultUnit: req.body.defaultUnit
  73. }
  74. if(response[0].specialUnit === "bottle"){
  75. newIngredient.quantity = req.body.quantity * response[0].unitSize;
  76. }else{
  77. newIngredient.quantity = Helper.convertQuantityToBaseUnit(req.body.quantity, req.body.defaultUnit);
  78. }
  79. response[1].inventory.push(newIngredient);
  80. return response[1].save();
  81. })
  82. .then((response)=>{
  83. return res.json(newIngredient);
  84. })
  85. .catch((err)=>{
  86. return res.json("ERROR: UNABLE TO CREATE NEW INGREDIENT");
  87. });
  88. },
  89. /*
  90. POST - Updates data for a single ingredient
  91. req.body = {
  92. id: id of the ingredient,
  93. name: new name of the ingredient,
  94. quantity: new quantity of the unit (in grams),
  95. category: new category of the unit,
  96. unit: new default unit of the ingredient,
  97. unitSize: unit size for special unit, if any
  98. }
  99. */
  100. updateIngredient: function(req, res){
  101. if(!req.session.user){
  102. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  103. return res.redirect("/");
  104. }
  105. const ingredientCheck = Validator.ingredient(req.body);
  106. if(ingredientCheck !== true){
  107. return res.json(ingredientCheck);
  108. }
  109. let updatedIngredient = {};
  110. Ingredient.findOne({_id: req.body.id})
  111. .then((ingredient)=>{
  112. ingredient.name = req.body.name,
  113. ingredient.category = req.body.category
  114. if(ingredient.specialUnit === "bottle"){
  115. ingredient.unitSize = req.body.unitSize;
  116. }
  117. return ingredient.save();
  118. })
  119. .then((ingredient)=>{
  120. updatedIngredient.ingredient = ingredient;
  121. return Merchant.findOne({_id: req.session.user});
  122. })
  123. .then((merchant)=>{
  124. for(let i = 0; i < merchant.inventory.length; i++){
  125. if(merchant.inventory[i].ingredient.toString() === req.body.id){
  126. merchant.inventory[i].defaultUnit = req.body.unit;
  127. if(merchant.inventory[i].quantity !== req.body.quantity){
  128. new InventoryAdjustment({
  129. date: new Date(),
  130. merchant: req.session.user,
  131. ingredient: req.body.id,
  132. quantity: req.body.quantity - merchant.inventory[i].quantity
  133. }).save().catch(()=>{});
  134. merchant.inventory[i].quantity = req.body.quantity;
  135. }
  136. updatedIngredient.quantity = req.body.quantity;
  137. updatedIngredient.unit = req.body.unit;
  138. break;
  139. }
  140. }
  141. return merchant.save();
  142. })
  143. .then((merchant)=>{
  144. return res.json(updatedIngredient);
  145. })
  146. .catch((err)=>{
  147. return res.json("ERROR: UNABLE TO UPDATE INGREDIENT");
  148. });
  149. },
  150. //DELETE - Removes an ingredient from the merchant's inventory
  151. removeIngredient: function(req, res){
  152. if(!req.session.user){
  153. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  154. return res.redirect("/");
  155. }
  156. Merchant.findOne({_id: req.session.user})
  157. .then((merchant)=>{
  158. for(let i = 0; i < merchant.inventory.length; i++){
  159. if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
  160. merchant.inventory.splice(i, 1);
  161. break;
  162. }
  163. }
  164. return merchant.save()
  165. })
  166. .then((merchant)=>{
  167. return Ingredient.deleteOne({_id: req.params.id});
  168. })
  169. .then((ingredient)=>{
  170. return res.json({});
  171. })
  172. .catch((err)=>{
  173. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  174. });
  175. },
  176. createFromSpreadsheet: function(req, res){
  177. if(!req.session.user){
  178. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  179. return res.redirect("/");
  180. }
  181. let workbook = xlsx.readFile(req.file.path);
  182. fs.unlink(req.file.path, ()=>{});
  183. let array = xlsx.utils.sheet_to_json(workbook.Sheets.Sheet1, {
  184. header: 1
  185. });
  186. //get property locations
  187. let locations = {};
  188. for(let i = 0; i < array[0].length; i++){
  189. switch(array[0][i].toLowerCase()){
  190. case "name": locations.name = i; break;
  191. case "category": locations.category = i; break;
  192. case "quantity": locations.quantity = i; break;
  193. case "unit": locations.unit = i; break;
  194. case "bottle": locations.bottle = i; break;
  195. case "bottle size": locations.bottleSize = i; break;
  196. }
  197. }
  198. //Create ingredients
  199. let ingredients = [];
  200. let merchantData = [];
  201. for(let i = 1; i < array.length; i++){
  202. let ingredient = new Ingredient({
  203. name: array[i][locations.name],
  204. category: array[i][locations.category]
  205. });
  206. let merchantItem = {
  207. ingredient: array[i][locations.name],
  208. quantity: Helper.convertQuantityToBaseUnit(array[i][locations.quantity], array[i][locations.unit]),
  209. defaultUnit: array[i][locations.unit]
  210. }
  211. if(array[i][locations.bottle] === true){
  212. ingredient.unitType = "volume";
  213. ingredient.specialUnit = "bottle";
  214. ingredient.unitSize = Helper.convertQuantityToBaseUnit(array[i][locations.bottleSize], array[i][locations.unit]);
  215. }else{
  216. let unitType = "";
  217. switch(array[i][locations.unit].toLowerCase()){
  218. case "g": unitType = "mass"; break;
  219. case "kg": unitType = "mass"; break;
  220. case "oz": unitType = "mass"; break;
  221. case "lb": unitType = "mass"; break;
  222. case "ml": unitType = "volume"; break;
  223. case "l": unitType = "volume"; break;
  224. case "tsp": unitType = "volume"; break;
  225. case "tbsp": unitType = "volume"; break;
  226. case "ozfl": unitType = "volume"; break;
  227. case "cup": unitType = "volume"; break;
  228. case "pt": unitType = "volume"; break;
  229. case "qt": unitType = "volume"; break;
  230. case "gal": unitType = "volume"; break;
  231. case "mm": unitType = "length"; break;
  232. case "cm": unitType = "length"; break;
  233. case "m": unitType = "length"; break;
  234. case "in": unitType = "length"; break;
  235. case "ft": unitType = "length"; break;
  236. default: unitType = "other";
  237. }
  238. ingredient.unitType = unitType;
  239. }
  240. merchantData.push(merchantItem);
  241. ingredients.push(ingredient);
  242. }
  243. //Update the database
  244. let createdIngredients = [];
  245. Ingredient.create(ingredients)
  246. .then((ingredients)=>{
  247. createdIngredients = ingredients;
  248. return Merchant.findOne({_id: req.session.user});
  249. })
  250. .then((merchant)=>{
  251. for(let i = 0; i < merchantData.length; i++){
  252. for(let j = 0; j < createdIngredients.length; j++){
  253. if(merchantData[i].ingredient === createdIngredients[j].name){
  254. merchantData[i].ingredient = createdIngredients[j];
  255. merchant.inventory.push(merchantData[i]);
  256. break;
  257. }
  258. }
  259. }
  260. return merchant.save();
  261. })
  262. .then((merchant)=>{
  263. return res.json(merchantData);
  264. })
  265. .catch((err)=>{
  266. return res.json("ERROR: UNABLE TO CREATE YOUR INGREDIENTS");
  267. });
  268. }
  269. }