recipeData.js 11 KB

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