recipeData.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. const Recipe = require("../models/recipe.js");
  2. const ArchivedRecipe = require("../models/archivedRecipe.js");
  3. const helper = require("./helper.js");
  4. const xlsx = require("xlsx");
  5. const fs = require("fs");
  6. module.exports = {
  7. /*
  8. POST - creates a single new recipe
  9. req.body = {
  10. name: name of recipe,
  11. price: price of the recipe,
  12. category: String
  13. ingredients: [{
  14. id: id of ingredient,
  15. quantity: quantity of ingredient in recipe,
  16. unit: String
  17. }]
  18. }
  19. Return = newly created recipe in same form as above, with _id
  20. */
  21. createRecipe: function(req, res){
  22. let recipe = new Recipe({
  23. merchant: res.locals.merchant._id,
  24. name: req.body.name,
  25. price: req.body.price,
  26. category: req.body.category,
  27. ingredients: req.body.ingredients
  28. });
  29. recipe.save()
  30. .then((newRecipe)=>{
  31. res.locals.merchant.recipes.push(recipe);
  32. res.locals.merchant.save().catch((err)=>{throw err});
  33. return res.json(newRecipe);
  34. })
  35. .catch((err)=>{
  36. if(typeof(err) === "string"){
  37. return res.json(err);
  38. }
  39. if(err.name === "ValidationError"){
  40. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  41. }
  42. return res.json("ERROR: UNABLE TO SAVE INGREDIENT");
  43. });
  44. },
  45. /*
  46. PUT - Update a single recipe
  47. req.body = {
  48. id: id of recipe,
  49. name: name of recipe,
  50. price: price of recipe,
  51. category: String
  52. ingredients: [{
  53. ingredient: id of ingredient,
  54. quantity: quantity of ingredient in recipe
  55. unit: String
  56. }]
  57. }
  58. */
  59. updateRecipe: function(req, res){
  60. Recipe.findOne({_id: req.body.id})
  61. .then((recipe)=>{
  62. new ArchivedRecipe({
  63. merchant: res.locals.merchant._id,
  64. name: recipe.name,
  65. price: recipe.price,
  66. category: recipe.category,
  67. date: new Date(),
  68. ingredients: recipe.ingredients
  69. }).save().catch(()=>{});
  70. recipe.name = req.body.name;
  71. recipe.price = req.body.price;
  72. recipe.category = req.body.category;
  73. recipe.ingredients = req.body.ingredients;
  74. return recipe.save();
  75. })
  76. .then((recipe)=>{
  77. res.json(recipe);
  78. })
  79. .catch((err)=>{
  80. if(typeof(err) === "string") return res.json(err);
  81. if(err.name === "ValidationError"){
  82. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  83. }
  84. return res.json("ERROR: UNABLE TO UPDATE DATA");
  85. });
  86. },
  87. /*
  88. DELETE: removes a single recipe from the merchant and the database
  89. req.params.id = String (recipe id)
  90. response = {}
  91. */
  92. removeRecipe: function(req, res){
  93. if(res.locals.merchant.pos === "square") return res.json("YOU MUST EDIT YOUR RECIPES INSIDE SQUARE");
  94. for(let i = 0; i < res.locals.merchant.recipes.length; i++){
  95. if(res.locals.merchant.recipes[i].toString() === req.params.id){
  96. res.locals.merchant.recipes.splice(i, 1);
  97. break;
  98. }
  99. }
  100. res.locals.merchant.save()
  101. .then(()=>{
  102. return res.json({});
  103. })
  104. .catch((err)=>{
  105. return res.json("ERROR: UNABLE TO DELETE RECIPE");
  106. });
  107. },
  108. createFromSpreadsheet: function(req, res){
  109. //read file, get the correct sheet, create array from sheet
  110. let workbook = xlsx.readFile(req.file.path);
  111. fs.unlink(req.file.path, ()=>{});
  112. let sheets = Object.keys(workbook.Sheets);
  113. let sheet = {};
  114. for(let i = 0; i < sheets.length; i++){
  115. let str = sheets[i].toLowerCase();
  116. if(str === "recipe" || str === "recipes"){
  117. sheet = workbook.Sheets[sheets[i]];
  118. }
  119. }
  120. const array = xlsx.utils.sheet_to_json(sheet, {
  121. header: 1
  122. });
  123. //get property locations
  124. let locations = {};
  125. for(let i = 0; i < array[0].length; i++){
  126. let title = "";
  127. try{title = array[0][i].toLowerCase()}
  128. catch{title = ""}
  129. switch(title){
  130. case "name": locations.name = i; break;
  131. case "price": locations.price = i; break;
  132. case "ingredients": locations.ingredient = i; break;
  133. case "ingredient amount": locations.amount = i; break;
  134. }
  135. }
  136. let merchant = {};
  137. let ingredients = [];
  138. res.locals.merchant
  139. .populate("inventory.ingredient")
  140. .execPopulate()
  141. .then((response)=>{
  142. merchant = response;
  143. for(let i = 0; i < merchant.inventory.length; i++){
  144. ingredients.push({
  145. id: merchant.inventory[i].ingredient._id,
  146. name: merchant.inventory[i].ingredient.name.toLowerCase(),
  147. unit: merchant.inventory[i].defaultUnit,
  148. specialUnit: merchant.inventory[i].specialUnit,
  149. unitSize: merchant.inventory[i].unitSize
  150. });
  151. }
  152. let recipes = [];
  153. let currentRecipe = {};
  154. for(let i = 1; i < array.length; i++){
  155. if(array[i][locations.ingredient] === undefined){
  156. continue;
  157. }
  158. if(array[i][locations.name] !== undefined){
  159. currentRecipe = {
  160. merchant: res.locals.merchant._id,
  161. name: array[i][locations.name],
  162. price: parseInt(array[i][locations.price] * 100),
  163. ingredients: []
  164. }
  165. recipes.push(currentRecipe);
  166. }
  167. let exists = false;
  168. for(let j = 0; j < ingredients.length; j++){
  169. if(ingredients[j].name.toLowerCase() === array[i][locations.ingredient].toLowerCase()){
  170. currentRecipe.ingredients.push({
  171. ingredient: ingredients[j].id,
  172. quantity: helper.convertQuantityToBaseUnit(array[i][locations.amount], ingredients[j].unit)
  173. });
  174. exists = true;
  175. break;
  176. }
  177. }
  178. if(exists === false){
  179. throw `CANNOT FIND INGREDIENT ${array[i][locations.ingredient]} FROM RECIPE ${array[i][locations.name]}`;
  180. }
  181. }
  182. return Recipe.create(recipes);
  183. })
  184. .then((response)=>{
  185. recipes = response;
  186. for(let i = 0; i < recipes.length; i++){
  187. merchant.recipes.push(recipes[i]._id);
  188. }
  189. return merchant.save();
  190. })
  191. .then((merchant)=>{
  192. return res.json(recipes);
  193. })
  194. .catch((err)=>{
  195. if(typeof(err) === "string"){
  196. return res.json(err);
  197. }
  198. if(err.name === "ValidationError"){
  199. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  200. }
  201. return res.json("ERROR: UNABLE TO CREATE YOUR RECIPES");
  202. });
  203. },
  204. spreadsheetTemplate: function(req, res){
  205. res.locals.merchant
  206. .populate("inventory.ingredient")
  207. .execPopulate()
  208. .then((merchant)=>{
  209. let workbook = xlsx.utils.book_new();
  210. workbook.SheetNames.push("Recipes");
  211. let workbookData = [];
  212. workbookData.push(["Name", "Price", "Ingredients", "Ingredient Amount", "", "Ingredients Reference", "Ingredient Unit"]);
  213. for(let i = 0; i < merchant.inventory.length; i++){
  214. workbookData.push(["", "", "", "", "", merchant.inventory[i].ingredient.name, merchant.inventory[i].defaultUnit]);
  215. }
  216. if(workbookData.length < 5){
  217. for(let i = workbookData.length - 1; i < 5; i++){
  218. workbookData.push(["", "", "", ""]);
  219. }
  220. }
  221. workbookData[1][0] = "Example Recipe 1";
  222. workbookData[1][1] = 10.98;
  223. workbookData[1][2] = "Example Ingredient 1";
  224. workbookData[1][3] = 1.2;
  225. workbookData[2][2] = "Example Ingredient 2";
  226. workbookData[2][3] = 0.55;
  227. workbookData[3][0] = "Example Recipe 2";
  228. workbookData[3][1] = 5.54;
  229. workbookData[3][2] = "Example Ingredient 3";
  230. workbookData[3][3] = 1;
  231. workbookData[4][2] = "Example Ingredient 4";
  232. workbookData[4][3] = 1.53;
  233. workbook.Sheets.Recipes = xlsx.utils.aoa_to_sheet(workbookData);
  234. xlsx.writeFile(workbook, "SublineRecipes.xlsx");
  235. return res.download("SublineRecipes.xlsx", (err)=>{
  236. fs.unlink("SublineRecipes.xlsx", ()=>{});
  237. });
  238. })
  239. .catch((err)=>{});
  240. },
  241. /*
  242. GET: toggles the hidden property of a recipe
  243. req.params.id = String (id of recipe)
  244. response = {}
  245. */
  246. hideRecipe: function(req, res){
  247. Recipe.findOne({_id: req.params.id})
  248. .then((recipe)=>{
  249. if(recipe.merchant.toString() !== res.locals.merchant._id.toString()) throw "unauthorized";
  250. recipe.hidden = (recipe.hidden === true) ? false : true;
  251. return recipe.save();
  252. })
  253. .then((recipe)=>{
  254. return res.json({});
  255. })
  256. .catch((err)=>{
  257. if(err === "unauthorized") return res.json("YOU DO NOT HAVE PERMISSION TO EDIT THAT RECIPE");
  258. return res.json("ERROR: UNABLE TO HIDE/UNHIDE THE RECIPE");
  259. });
  260. },
  261. /*
  262. POST: gets a list of individual recipes
  263. req.body = [String] (ids)
  264. response = [Recipe]
  265. */
  266. findRecipes: function(req, res){
  267. Recipe.find(req.body)
  268. .then((recipes)=>{
  269. return res.json(recipes);
  270. })
  271. .catch((err)=>{
  272. return res.json("ERROR: UNABLE TO FIND DELETED RECIPES TO MATCH WITH TRANSACTIONS");
  273. });
  274. }
  275. }