recipeData.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. const Recipe = require("../models/recipe.js");
  2. const Merchant = require("../models/merchant.js");
  3. const ArchivedRecipe = require("../models/archivedRecipe.js");
  4. const helper = require("./helper.js");
  5. const axios = require("axios");
  6. const xlsx = require("xlsx");
  7. const fs = require("fs");
  8. module.exports = {
  9. /*
  10. POST - creates a single new recipe
  11. req.body = {
  12. name: name of recipe,
  13. price: price of the recipe,
  14. ingredients: [{
  15. id: id of ingredient,
  16. quantity: quantity of ingredient in recipe
  17. }]
  18. }
  19. Return = newly created recipe in same form as above, with _id
  20. */
  21. createRecipe: function(req, res){
  22. let recipe = new Recipe({
  23. merchant: res.locals.merchant._id,
  24. name: req.body.name,
  25. price: Math.round(req.body.price * 100),
  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 RECIPE");
  82. });
  83. },
  84. //DELETE - removes a single recipe from the merchant and the database
  85. removeRecipe: function(req, res){
  86. if(res.locals.merchant.pos === "clover"){
  87. return res.json("YOU MUST EDIT YOUR RECIPES INSIDE CLOVER");
  88. }
  89. for(let i = 0; i < res.locals.merchant.recipes.length; i++){
  90. if(res.locals.merchant.recipes[i].toString() === req.params.id){
  91. res.locals.merchant.recipes.splice(i, 1);
  92. break;
  93. }
  94. }
  95. Promise.all([Recipe.deleteOne({_id: req.params.id}), res.locals.merchant.save()])
  96. .then((response)=>{
  97. return res.json({});
  98. })
  99. .catch((err)=>{
  100. if(typeof(err) === "string"){
  101. return res.json(err);
  102. }
  103. if(err.name === "ValidationError"){
  104. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  105. }
  106. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  107. });
  108. },
  109. createFromSpreadsheet: function(req, res){
  110. //read file, get the correct sheet, create array from sheet
  111. let workbook = xlsx.readFile(req.file.path);
  112. fs.unlink(req.file.path, ()=>{});
  113. let sheets = Object.keys(workbook.Sheets);
  114. let sheet = {};
  115. for(let i = 0; i < sheets.length; i++){
  116. let str = sheets[i].toLowerCase();
  117. if(str === "recipe" || str === "recipes"){
  118. sheet = workbook.Sheets[sheets[i]];
  119. }
  120. }
  121. const array = xlsx.utils.sheet_to_json(sheet, {
  122. header: 1
  123. });
  124. //get property locations
  125. let locations = {};
  126. for(let i = 0; i < array[0].length; i++){
  127. switch(array[0][i].toLowerCase()){
  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].length === 0){
  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 === array[i][locations.ingredient]){
  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. if(!req.session.user){
  204. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  205. return res.redirect("/");
  206. }
  207. Merchant.findOne({_id: req.session.user})
  208. .populate("inventory.ingredient")
  209. .then((merchant)=>{
  210. let workbook = xlsx.utils.book_new();
  211. workbook.SheetNames.push("Recipes");
  212. let workbookData = [];
  213. workbookData.push(["Name", "Price", "Ingredients", "Ingredient Amount", "", "Ingredients Reference", "Ingredient Unit"]);
  214. for(let i = 0; i < merchant.inventory.length; i++){
  215. workbookData.push(["", "", "", "", "", merchant.inventory[i].ingredient.name, merchant.inventory[i].defaultUnit]);
  216. }
  217. if(workbookData.length < 5){
  218. for(let i = workbookData.length - 1; i < 5; i++){
  219. workbookData.push(["", "", "", ""]);
  220. }
  221. }
  222. workbookData[1][0] = "Example Recipe 1";
  223. workbookData[1][1] = 10.98;
  224. workbookData[1][2] = "Example Ingredient 1";
  225. workbookData[1][3] = 1.2;
  226. workbookData[2][2] = "Example Ingredient 2";
  227. workbookData[2][3] = 0.55;
  228. workbookData[3][0] = "Example Recipe 2";
  229. workbookData[3][1] = 5.54;
  230. workbookData[3][2] = "Example Ingredient 3";
  231. workbookData[3][3] = 1;
  232. workbookData[4][2] = "Example Ingredient 4";
  233. workbookData[4][3] = 1.53;
  234. workbook.Sheets.Recipes = xlsx.utils.aoa_to_sheet(workbookData);
  235. xlsx.writeFile(workbook, "SublineRecipes.xlsx");
  236. return res.download("SublineRecipes.xlsx", (err)=>{
  237. fs.unlink("SublineRecipes.xlsx", ()=>{});
  238. });
  239. })
  240. .catch((err)=>{});
  241. }
  242. }