recipeData.js 10 KB

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