recipeData.js 10 KB

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