recipeData.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. const Recipe = require("../models/recipe.js");
  2. const Merchant = require("../models/merchant.js");
  3. const ArchivedRecipe = require("../models/archivedRecipe.js");
  4. const validator = require("./validator.js");
  5. const axios = require("axios");
  6. const xlsxUtils = require("xlsx").utils;
  7. module.exports = {
  8. /*
  9. POST - creates a single new recipe
  10. req.body = {
  11. name: name of recipe,
  12. price: price of the recipe,
  13. ingredients: [{
  14. id: id of ingredient,
  15. quantity: quantity of ingredient in recipe
  16. }]
  17. }
  18. Return = newly created recipe in same form as above, with _id
  19. */
  20. createRecipe: function(req, res){
  21. if(!req.session.user){
  22. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  23. return res.redirect("/");
  24. }
  25. let validation = validator.recipe(req.body);
  26. if(validation !== true){
  27. return res.json(validation);
  28. }
  29. let recipe = new Recipe({
  30. merchant: req.session.user,
  31. name: req.body.name,
  32. price: Math.round(req.body.price * 100),
  33. ingredients: req.body.ingredients
  34. });
  35. Merchant.findOne({_id: req.session.user})
  36. .then((merchant)=>{
  37. merchant.recipes.push(recipe);
  38. merchant.save()
  39. .catch((err)=>{
  40. return res.json("ERROR: UNABLE TO SAVE RECIPE");
  41. });
  42. })
  43. .catch((err)=>{
  44. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  45. });
  46. recipe.save()
  47. .then((newRecipe)=>{
  48. return res.json(newRecipe);
  49. })
  50. .catch((err)=>{
  51. return res.json("ERROR: UNABLE TO SAVE INGREDIENT");
  52. });
  53. },
  54. /*
  55. PUT - Update a single recipe
  56. req.body = {
  57. id: id of recipe,
  58. name: name of recipe,
  59. price: price of recipe,
  60. ingredients: [{
  61. ingredient: id of ingredient,
  62. quantity: quantity of ingredient in recipe
  63. }]
  64. }
  65. */
  66. updateRecipe: function(req, res){
  67. if(!req.session.user){
  68. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  69. return res.redirect("/");
  70. }
  71. let validation = validator.recipe(req.body);
  72. if(validation !== true){
  73. return res.json(validation);
  74. }
  75. Recipe.findOne({_id: req.body.id})
  76. .then((recipe)=>{
  77. new ArchivedRecipe({
  78. merchant: req.session.user,
  79. name: recipe.name,
  80. price: recipe.price,
  81. date: new Date(),
  82. ingredients: recipe.ingredients
  83. }).save().catch(()=>{});
  84. recipe.name = req.body.name;
  85. recipe.price = req.body.price;
  86. recipe.ingredients = req.body.ingredients;
  87. return recipe.save();
  88. })
  89. .then((recipe)=>{
  90. res.json(recipe);
  91. })
  92. .catch((err)=>{
  93. return res.json("ERROR: UNABLE TO UPDATE RECIPE");
  94. });
  95. },
  96. //DELETE - removes a single recipe from the merchant and the database
  97. removeRecipe: function(req, res){
  98. if(!req.session.user){
  99. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  100. return res.redirect("/");
  101. }
  102. Merchant.findOne({_id: req.session.user})
  103. .then((merchant)=>{
  104. if(merchant.pos === "clover"){
  105. return res.json("YOU MUST EDIT YOUR RECIPES INSIDE CLOVER");
  106. }
  107. for(let i = 0; i < merchant.recipes.length; i++){
  108. if(merchant.recipes[i].toString() === req.params.id){
  109. merchant.recipes.splice(i, 1);
  110. break;
  111. }
  112. }
  113. merchant.save()
  114. .catch((err)=>{
  115. return res.json("ERROR: UNABLE TO SAVE DATA");
  116. })
  117. return Recipe.deleteOne({_id: req.params.id});
  118. })
  119. .then((response)=>{
  120. return res.json({});
  121. })
  122. .catch((err)=>{
  123. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  124. });
  125. },
  126. //GET - Checks clover for new or deleted recipes
  127. //Returns:
  128. // merchant: Full merchant (recipe ingredients populated)
  129. // count: Number of new recipes
  130. updateRecipesClover: function(req, res){
  131. if(!req.session.user){
  132. req.session.error = "Must be logged in to do that";
  133. return res.redirect("/");
  134. }
  135. let merchant = {};
  136. let newRecipes = [];
  137. let deletedRecipes = []
  138. Merchant.findOne({_id: req.session.user})
  139. .populate("recipes")
  140. .then((response)=>{
  141. merchant = response;
  142. return axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${merchant.posAccessToken}`);
  143. })
  144. .then((result)=>{
  145. deletedRecipes = merchant.recipes.slice();
  146. for(let i = 0; i < result.data.elements.length; i++){
  147. for(let j = 0; j < deletedRecipes.length; j++){
  148. if(result.data.elements[i].id === deletedRecipes[j].posId){
  149. result.data.elements.splice(i, 1);
  150. deletedRecipes.splice(j, 1);
  151. i--;
  152. break;
  153. }
  154. }
  155. }
  156. for(let i = 0; i < deletedRecipes.length; i++){
  157. for(let j = 0; j < merchant.recipes.length; j++){
  158. if(deletedRecipes[i]._id === merchant.recipes[j]._id){
  159. merchant.recipes.splice(j, 1);
  160. break;
  161. }
  162. }
  163. }
  164. for(let i = 0; i < result.data.elements.length; i++){
  165. let newRecipe = new Recipe({
  166. posId: result.data.elements[i].id,
  167. merchant: merchant._id,
  168. name: result.data.elements[i].name,
  169. ingredients: [],
  170. price: result.data.elements[i].price
  171. });
  172. merchant.recipes.push(newRecipe);
  173. newRecipes.push(newRecipe);
  174. }
  175. Recipe.create(newRecipes).catch((err)=>{});
  176. return merchant.save();
  177. })
  178. .then((newMerchant)=>{
  179. return res.json({new: newRecipes, removed: deletedRecipes});
  180. })
  181. .catch((err)=>{
  182. return res.json("ERROR: UNABLE TO RETRIEVE MERCHANT DATA");
  183. });
  184. },
  185. updateRecipesSquare: function(req, res){
  186. if(!req.session.user){
  187. req.session.error = "Must be logged in to do that";
  188. return res.redirect("/");
  189. }
  190. let merchant = {};
  191. let merchantRecipes = [];
  192. let newRecipes = [];
  193. Merchant.findOne({_id: req.session.user})
  194. .populate("recipes")
  195. .then((fetchedMerchant)=>{
  196. merchant = fetchedMerchant;
  197. return axios.post(`${process.env.SQUARE_ADDRESS}/v2/catalog/search`, {
  198. object_types: ["ITEM"]
  199. }, {
  200. headers: {
  201. Authorization: `Bearer ${merchant.posAccessToken}`
  202. }
  203. });
  204. })
  205. .then((response)=>{
  206. merchantRecipes = merchant.recipes.slice();
  207. for(let i = 0; i < response.data.objects.length; i++){
  208. let itemData = response.data.objects[i].item_data;
  209. for(let j = 0; j < itemData.variations.length; j++){
  210. let isFound = false;
  211. for(let k = 0; k < merchantRecipes.length; k++){
  212. if(itemData.variations[j].id === merchantRecipes[k].posId){
  213. merchantRecipes.splice(k, 1);
  214. k--;
  215. isFound = true;
  216. break;
  217. }
  218. }
  219. if(!isFound){
  220. let newRecipe = new Recipe({
  221. posId: itemData.variations[j].id,
  222. merchant: merchant._id,
  223. name: "",
  224. price: itemData.variations[j].item_variation_data.price_money.amount,
  225. ingredients: []
  226. });
  227. if(itemData.variations.length > 1){
  228. newRecipe.name = `${itemData.name} '${itemData.variations[j].item_variation_data.name}'`;
  229. }else{
  230. newRecipe.name = itemData.name;
  231. }
  232. newRecipes.push(newRecipe);
  233. merchant.recipes.push(newRecipe);
  234. }
  235. }
  236. }
  237. let ids = [];
  238. for(let i = 0; i < merchantRecipes.length; i++){
  239. ids.push(merchantRecipes[i]._id);
  240. for(let j = 0; j < merchant.recipes.length; j++){
  241. if(merchantRecipes[i]._id.toString() === merchant.recipes[j]._id.toString()){
  242. merchant.recipes.splice(j, 1);
  243. j--;
  244. break;
  245. }
  246. }
  247. }
  248. if(newRecipes.length > 0){
  249. Recipe.create(newRecipes);
  250. }
  251. if(merchantRecipes.length > 0){
  252. Recipe.deleteMany({_id: {$in: ids}});
  253. }
  254. return merchant.save();
  255. })
  256. .then((merchant)=>{
  257. return res.json({new: newRecipes, removed: merchantRecipes});
  258. })
  259. .catch((err)=>{
  260. return "ERROR: UNABLE TO RETRIEVE RECIPE DATA FROM SQUARE";
  261. });
  262. },
  263. createFromSpreadsheet: function(sheet, user){
  264. const array = xlsxUtils.sheet_to_json(sheet, {
  265. header: 1
  266. });
  267. //get property locations
  268. let locations = {};
  269. for(let i = 0; i < array[0].length; i++){
  270. switch(array[0][i].toLowerCase()){
  271. case "name": locations.name = i; break;
  272. case "price": locations.price = i; break;
  273. }
  274. }
  275. //Create Recipes
  276. let merchant = {};
  277. let newRecipes = [];
  278. return Merchant.findOne({_id: user})
  279. .then((response)=>{
  280. merchant = response;
  281. let recipes = [];
  282. for(let i = 1; i < array.length; i++){
  283. recipes.push({
  284. name: array[i][locations.name],
  285. price: parseInt(array[i][locations.price] * 100),
  286. merchant: merchant
  287. });
  288. }
  289. return Recipe.create(recipes)
  290. })
  291. .then((recipes)=>{
  292. for(let i = 0; i < recipes.length; i++){
  293. merchant.recipes.push(recipes[i]);
  294. recipes[i].merchant = undefined;
  295. newRecipes.push(recipes[i]);
  296. }
  297. return merchant.save();
  298. })
  299. .then((merchant)=>{
  300. return newRecipes;
  301. })
  302. .catch((err)=>{
  303. console.log(err);
  304. return "ERROR: UNABLE TO CREATE RECIPES";
  305. });
  306. }
  307. }