recipeData.js 10 KB

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