recipeData.js 11 KB

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