ingredientData.js 11 KB

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