recipeData.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. if(!req.session.user){
  111. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  112. return res.redirect("/");
  113. }
  114. //read file, get the correct sheet, create array from sheet
  115. let workbook = xlsx.readFile(req.file.path);
  116. fs.unlink(req.file.path, ()=>{});
  117. let sheets = Object.keys(workbook.Sheets);
  118. let sheet = {};
  119. for(let i = 0; i < sheets.length; i++){
  120. let str = sheets[i].toLowerCase();
  121. if(str === "recipe" || str === "recipes"){
  122. sheet = workbook.Sheets[sheets[i]];
  123. }
  124. }
  125. const array = xlsx.utils.sheet_to_json(sheet, {
  126. header: 1
  127. });
  128. //get property locations
  129. let locations = {};
  130. for(let i = 0; i < array[0].length; i++){
  131. switch(array[0][i].toLowerCase()){
  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. Merchant.findOne({_id: req.session.user})
  141. .populate("inventory.ingredient")
  142. .then((response)=>{
  143. merchant = response;
  144. for(let i = 0; i < merchant.inventory.length; i++){
  145. ingredients.push({
  146. id: merchant.inventory[i].ingredient._id,
  147. name: merchant.inventory[i].ingredient.name.toLowerCase(),
  148. unit: merchant.inventory[i].defaultUnit,
  149. specialUnit: merchant.inventory[i].specialUnit,
  150. unitSize: merchant.inventory[i].unitSize
  151. });
  152. }
  153. let recipes = [];
  154. let currentRecipe = {};
  155. for(let i = 1; i < array.length; i++){
  156. if(array[i].length === 0){
  157. continue;
  158. }
  159. if(array[i][locations.name] !== undefined){
  160. currentRecipe = {
  161. merchant: req.session.user,
  162. name: array[i][locations.name],
  163. price: parseInt(array[i][locations.price] * 100),
  164. ingredients: []
  165. }
  166. recipes.push(currentRecipe);
  167. }
  168. let exists = false;
  169. for(let j = 0; j < ingredients.length; j++){
  170. if(ingredients[j].name === array[i][locations.ingredient]){
  171. currentRecipe.ingredients.push({
  172. ingredient: ingredients[j].id,
  173. quantity: helper.convertQuantityToBaseUnit(array[i][locations.amount], ingredients[j].unit)
  174. });
  175. exists = true;
  176. break;
  177. }
  178. }
  179. if(exists === false){
  180. throw `CANNOT FIND INGREDIENT ${array[i][locations.ingredient]} FROM RECIPE ${array[i][locations.name]}`;
  181. }
  182. }
  183. return Recipe.create(recipes);
  184. })
  185. .then((response)=>{
  186. recipes = response;
  187. for(let i = 0; i < recipes.length; i++){
  188. merchant.recipes.push(recipes[i]._id);
  189. }
  190. return merchant.save();
  191. })
  192. .then((merchant)=>{
  193. return res.json(recipes);
  194. })
  195. .catch((err)=>{
  196. if(typeof(err) === "string"){
  197. return res.json(err);
  198. }
  199. if(err.name === "ValidationError"){
  200. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  201. }
  202. return res.json("ERROR: UNABLE TO CREATE YOUR RECIPES");
  203. });
  204. },
  205. spreadsheetTemplate: function(req, res){
  206. if(!req.session.user){
  207. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  208. return res.redirect("/");
  209. }
  210. Merchant.findOne({_id: req.session.user})
  211. .populate("inventory.ingredient")
  212. .then((merchant)=>{
  213. let workbook = xlsx.utils.book_new();
  214. workbook.SheetNames.push("Recipes");
  215. let workbookData = [];
  216. workbookData.push(["Name", "Price", "Ingredients", "Ingredient Amount", "", "Ingredients Reference", "Ingredient Unit"]);
  217. for(let i = 0; i < merchant.inventory.length; i++){
  218. workbookData.push(["", "", "", "", "", merchant.inventory[i].ingredient.name, merchant.inventory[i].defaultUnit]);
  219. }
  220. if(workbookData.length < 5){
  221. for(let i = workbookData.length - 1; i < 5; i++){
  222. workbookData.push(["", "", "", ""]);
  223. }
  224. }
  225. workbookData[1][0] = "Example Recipe 1";
  226. workbookData[1][1] = 10.98;
  227. workbookData[1][2] = "Example Ingredient 1";
  228. workbookData[1][3] = 1.2;
  229. workbookData[2][2] = "Example Ingredient 2";
  230. workbookData[2][3] = 0.55;
  231. workbookData[3][0] = "Example Recipe 2";
  232. workbookData[3][1] = 5.54;
  233. workbookData[3][2] = "Example Ingredient 3";
  234. workbookData[3][3] = 1;
  235. workbookData[4][2] = "Example Ingredient 4";
  236. workbookData[4][3] = 1.53;
  237. workbook.Sheets.Recipes = xlsx.utils.aoa_to_sheet(workbookData);
  238. xlsx.writeFile(workbook, "SublineRecipes.xlsx");
  239. return res.download("SublineRecipes.xlsx", (err)=>{
  240. fs.unlink("SublineRecipes.xlsx", ()=>{});
  241. });
  242. })
  243. .catch((err)=>{});
  244. }
  245. }