ingredientData.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 = helper.convertQuantityToBaseUnit(req.body.quantity, req.body.unit);
  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. createFromSpreadsheet: 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. //read file, get the correct sheet, create array from sheet
  156. let workbook = xlsx.readFile(req.file.path);
  157. fs.unlink(req.file.path, ()=>{});
  158. let sheets = Object.keys(workbook.Sheets);
  159. let sheet = {};
  160. for(let i = 0; i < sheets.length; i++){
  161. let str = sheets[i].toLowerCase();
  162. if(str === "ingredient" || str === "ingredients"){
  163. sheet = workbook.Sheets[sheets[i]];
  164. }
  165. }
  166. const array = xlsx.utils.sheet_to_json(sheet, {
  167. header: 1
  168. });
  169. //get property locations
  170. let locations = {};
  171. for(let i = 0; i < array[0].length; i++){
  172. switch(array[0][i].toLowerCase()){
  173. case "name": locations.name = i; break;
  174. case "category": locations.category = i; break;
  175. case "quantity": locations.quantity = i; break;
  176. case "unit": locations.unit = i; break;
  177. case "bottle": locations.bottle = i; break;
  178. case "bottle size": locations.bottleSize = i; break;
  179. }
  180. }
  181. //Create ingredients
  182. let ingredients = [];
  183. let merchantData = [];
  184. for(let i = 1; i < array.length; i++){
  185. let ingredient = new Ingredient({
  186. name: array[i][locations.name],
  187. category: array[i][locations.category]
  188. });
  189. let merchantItem = {
  190. ingredient: ingredient,
  191. quantity: helper.convertQuantityToBaseUnit(array[i][locations.quantity], array[i][locations.unit]),
  192. defaultUnit: array[i][locations.unit]
  193. }
  194. if(array[i][locations.bottle] === true){
  195. ingredient.unitType = "volume";
  196. ingredient.specialUnit = "bottle";
  197. ingredient.unitSize = helper.convertQuantityToBaseUnit(array[i][locations.bottleSize], array[i][locations.unit]);
  198. }else{
  199. let unitType = "";
  200. switch(array[i][locations.unit].toLowerCase()){
  201. case "g": unitType = "mass"; break;
  202. case "kg": unitType = "mass"; break;
  203. case "oz": unitType = "mass"; break;
  204. case "lb": unitType = "mass"; break;
  205. case "ml": unitType = "volume"; break;
  206. case "l": unitType = "volume"; break;
  207. case "tsp": unitType = "volume"; break;
  208. case "tbsp": unitType = "volume"; break;
  209. case "ozfl": unitType = "volume"; break;
  210. case "cup": unitType = "volume"; break;
  211. case "pt": unitType = "volume"; break;
  212. case "qt": unitType = "volume"; break;
  213. case "gal": unitType = "volume"; break;
  214. case "mm": unitType = "length"; break;
  215. case "cm": unitType = "length"; break;
  216. case "m": unitType = "length"; break;
  217. case "in": unitType = "length"; break;
  218. case "ft": unitType = "length"; break;
  219. default: unitType = "other";
  220. }
  221. ingredient.unitType = unitType;
  222. }
  223. merchantData.push(merchantItem);
  224. ingredients.push(ingredient);
  225. }
  226. //Update the database
  227. let createdIngredients = [];
  228. Ingredient.create(ingredients)
  229. .then((ingredients)=>{
  230. createdIngredients = ingredients;
  231. return Merchant.findOne({_id: req.session.user});
  232. })
  233. .then((merchant)=>{
  234. for(let i = 0; i < merchantData.length; i++){
  235. merchant.inventory.push(merchantData[i]);
  236. }
  237. return merchant.save();
  238. })
  239. .then((merchant)=>{
  240. return res.json(merchantData);
  241. })
  242. .catch((err)=>{
  243. return "ERROR: UNABLE TO CREATE YOUR INGREDIENTS";
  244. });
  245. },
  246. spreadsheetTemplate: function(req, res){
  247. if(!req.session.user){
  248. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  249. return res.redirect("/");
  250. }
  251. let workbook = xlsx.utils.book_new();
  252. workbook.SheetNames.push("Ingredients");
  253. let workbookData = [];
  254. workbookData.push(["Name", "Category", "Quantity", "Unit", "Bottle", "Bottle Size"]);
  255. workbookData.push(["Example Ingredient 1", "Produce", 100, "lbs"]);
  256. workbookData.push(["Example Ingredient Two", "Beverage", 5, "ml", "TRUE", 750]);
  257. workbook.Sheets.Ingredients = xlsx.utils.aoa_to_sheet(workbookData);
  258. xlsx.writeFile(workbook, "SublineIngredients.xlsx");
  259. return res.download("SublineIngredients.xlsx", (err)=>{
  260. fs.unlink("SublineIngredients.xlsx", ()=>{});
  261. });
  262. },
  263. //DELETE - Removes an ingredient from the merchant's inventory
  264. removeIngredient: function(req, res){
  265. if(!req.session.user){
  266. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  267. return res.redirect("/");
  268. }
  269. Merchant.findOne({_id: req.session.user})
  270. .then((merchant)=>{
  271. for(let i = 0; i < merchant.inventory.length; i++){
  272. if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
  273. merchant.inventory.splice(i, 1);
  274. break;
  275. }
  276. }
  277. return merchant.save()
  278. })
  279. .then((merchant)=>{
  280. return Ingredient.deleteOne({_id: req.params.id});
  281. })
  282. .then((ingredient)=>{
  283. return res.json({});
  284. })
  285. .catch((err)=>{
  286. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  287. });
  288. },
  289. }