merchantData.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. const axios = require("axios");
  2. const bcrypt = require("bcryptjs");
  3. const Merchant = require("../models/merchant");
  4. const Recipe = require("../models/recipe");
  5. const Ingredient = require("../models/ingredient");
  6. const InventoryAdjustment = require("../models/inventoryAdjustment");
  7. const token = "b48068eb-411a-918e-ea64-52007147e42c";
  8. module.exports = {
  9. //GET - Checks clover for new or deleted recipes
  10. //Returns:
  11. // merchant: Full merchant (recipe ingredients populated)
  12. // count: Number of new recipes
  13. updateRecipes: function(req, res){
  14. if(!req.session.user){
  15. return res.render("error");
  16. }
  17. Merchant.findOne({_id: req.session.user})
  18. .populate("recipes")
  19. .then((merchant)=>{
  20. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${token}`)
  21. .then((result)=>{
  22. let deletedRecipes = merchant.recipes.slice();
  23. for(let i = 0; i < result.data.elements.length; i++){
  24. for(let j = 0; j < deletedRecipes.length; j++){
  25. if(result.data.elements[i].id === deletedRecipes[j].posId){
  26. result.data.elements.splice(i, 1);
  27. deletedRecipes.splice(j, 1);
  28. i--;
  29. break;
  30. }
  31. }
  32. }
  33. for(let recipe of deletedRecipes){
  34. for(let i = 0; i < merchant.recipes.length; i++){
  35. if(recipe._id === merchant.recipes[i]._id){
  36. merchant.recipes.splice(i, 1);
  37. break;
  38. }
  39. }
  40. }
  41. let newRecipes = []
  42. for(let recipe of result.data.elements){
  43. let newRecipe = new Recipe({
  44. posId: recipe.id,
  45. merchant: merchant._id,
  46. name: recipe.name,
  47. ingredients: []
  48. });
  49. merchant.recipes.push(newRecipe);
  50. newRecipes.push(newRecipe);
  51. }
  52. Recipe.create(newRecipes)
  53. .catch((err)=>{
  54. console.log(err);
  55. return res.render("error");
  56. });
  57. merchant.save()
  58. .then((newMerchant)=>{
  59. newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
  60. .then((newestMerchant)=>{
  61. return res.json({merchant: newestMerchant, count: result.data.elements.length});
  62. })
  63. .catch((err)=>{
  64. console.log(err);
  65. return res.render("error");
  66. });
  67. })
  68. .catch((err)=>{
  69. console.log(err);
  70. return res.render("error");
  71. });
  72. })
  73. .catch((err)=>{
  74. console.log(err);
  75. return res.render("error");
  76. });
  77. })
  78. .catch((err)=>{
  79. console.log(err);
  80. return res.render("error");
  81. });
  82. },
  83. //POST - Creates a Clover merchant from all entered data
  84. //Inputs:
  85. // req.body.data: All data from frontend in form of merchant model
  86. //Redirect to "/inventory"
  87. createMerchantClover: function(req, res){
  88. let data = JSON.parse(req.body.data);
  89. data.email = data.email.toLowerCase();
  90. if(data.password.length < 15 || data.password !== data.confirmPassword){
  91. return res.render("error");
  92. }
  93. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}?access_token=${token}`)
  94. .then((cloverMerchant)=>{
  95. req.session.posId = undefined;
  96. let salt = bcrypt.genSaltSync(10);
  97. let hash = bcrypt.hashSync(data.password, salt);
  98. let merchant = new Merchant({
  99. name: cloverMerchant.data.name,
  100. email: data.email,
  101. password: hash,
  102. pos: "clover",
  103. posId: cloverMerchant.data.id
  104. });
  105. for(let item of data.inventory){
  106. merchant.inventory.push({
  107. ingredient: item.ingredient.id,
  108. quantity: item.quantity
  109. });
  110. }
  111. for(let recipe of data.recipes){
  112. recipe.merchant = merchant._id;
  113. }
  114. Recipe.create(data.recipes)
  115. .then((recipes)=>{
  116. for(let recipe of recipes){
  117. merchant.recipes.push(recipe._id);
  118. }
  119. merchant.save()
  120. .then((merchant)=>{
  121. req.session.user = merchant._id;
  122. return res.redirect("/inventory");
  123. })
  124. .catch((err)=>{
  125. console.log(err);
  126. return res.render("error");
  127. });
  128. })
  129. .catch((err)=>{
  130. console.log(err);
  131. return res.render("error");
  132. });
  133. //WTF IS THIS???
  134. // Recipe.create(newRecipes)
  135. // .then((recipes)=>{
  136. // for(let recipe of recipes){
  137. // newMerchant.recipes.push(recipe);
  138. // }
  139. // newMerchant.save()
  140. // .then((merchant)=>{
  141. // return res.redirect("/inventory");
  142. // })
  143. // .catch((err)=>{
  144. // console.log(err);
  145. // return res.render("error");
  146. // });
  147. // })
  148. // .catch((err)=>{
  149. // console.log(err);
  150. // return res.render("error");
  151. // });
  152. })
  153. .catch((err)=>{
  154. console.log(err);
  155. });
  156. },
  157. //POST - Creates a non-pos merchant from all entered data
  158. //Inputs:
  159. // req.body.data: All data from frontend in form of merchant model
  160. //Redirects to "/inventory"
  161. createMerchantNone: function(req, res){
  162. let data = JSON.parse(req.body.data);
  163. data.email = data.email.toLowerCase();
  164. let salt = bcrypt.genSaltSync(10);
  165. let hash = bcrypt.hashSync(data.password, salt);
  166. let merchant = new Merchant({
  167. name: data.name,
  168. email: data.email,
  169. password: hash,
  170. pos: "none"
  171. });
  172. for(let item of data.inventory){
  173. merchant.inventory.push({
  174. ingredient: item.ingredient.id,
  175. quantity: item.quantity
  176. });
  177. }
  178. for(let recipe of data.recipes){
  179. recipe.merchant = merchant._id;
  180. }
  181. Recipe.create(data.recipes)
  182. .then((recipes)=>{
  183. for(let recipe of recipes){
  184. merchant.recipes.push(recipe._id);
  185. }
  186. merchant.save()
  187. .then((merchant)=>{
  188. req.session.user = merchant._id;
  189. return res.redirect("/inventory");
  190. })
  191. .catch((err)=>{
  192. console.log(err);
  193. return res.render("error");
  194. });
  195. })
  196. .catch((err)=>{
  197. console.log(err);
  198. return res.render("error");
  199. });
  200. },
  201. //POST - Adds an ingredient to merchant's inventory
  202. //Inputs:
  203. // req.body: A merchant inventory item (ingredient id and quantity)
  204. //Returns:
  205. // ingredient: Newly added ingredient
  206. addMerchantIngredient: function(req, res){
  207. if(!req.session.user){
  208. return res.render("error");
  209. }
  210. Merchant.findOne({_id: req.session.user})
  211. .then((merchant)=>{
  212. merchant.inventory.push(req.body);
  213. merchant.save()
  214. .then((newMerchant)=>{
  215. Ingredient.findOne({_id: req.body.ingredient})
  216. .then((ingredient)=>{
  217. return res.json(ingredient);
  218. })
  219. .catch((err)=>{
  220. console.log(err);
  221. return res.render("error");
  222. });
  223. })
  224. .catch((err)=>{
  225. console.log(err);
  226. return res.render("error");
  227. });
  228. })
  229. .catch((err)=>{
  230. console.log(err);
  231. return res.render("error");
  232. });
  233. },
  234. //POST - Removes an ingredient from the merchant's inventory
  235. //Inputs:
  236. // ingredientId: id of ingredient to remove
  237. //Returns: Nothing
  238. removeMerchantIngredient: function(req, res){
  239. if(!req.session.user){
  240. return res.render("error");
  241. }
  242. Merchant.findOne({_id: req.session.user})
  243. .then((merchant)=>{
  244. for(let i = 0; i < merchant.inventory.length; i++){
  245. if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
  246. merchant.inventory.splice(i, 1);
  247. break;
  248. }
  249. }
  250. merchant.save()
  251. .then(()=>{
  252. return res.json();
  253. })
  254. .catch((err)=>{
  255. console.log(err);
  256. return res.render("error");
  257. });
  258. })
  259. .catch((err)=>{
  260. console.log(err);
  261. return res.render("error");
  262. });
  263. },
  264. //POST - Update the quantity for a merchant inventory item
  265. //Inputs:
  266. // req.body.ingredientId: Id of ingredient to update
  267. // req.body.quantityChange: Amount to change ingredient (not the new value)
  268. //Returns: Nothing
  269. updateMerchantIngredient: function(req, res){
  270. if(!req.session.user){
  271. return res.render("error");
  272. }
  273. Merchant.findOne({_id: req.session.user})
  274. .then((merchant)=>{
  275. let updateIngredient = merchant.inventory.find(i => i._id.toString() === req.body.ingredientId);
  276. updateIngredient.quantity += req.body.quantityChange;
  277. merchant.save()
  278. .then((merchant)=>{
  279. res.json();
  280. })
  281. .catch((err)=>{
  282. console.log(err);
  283. return res.render("error");
  284. })
  285. })
  286. .catch((err)=>{
  287. console.log(err);
  288. return res.render("error");
  289. });
  290. let invAdj = new InventoryAdjustment({
  291. date: Date.now(),
  292. merchant: req.session.user,
  293. ingredient: req.body.ingredientId,
  294. quantity: req.body.quantityChange
  295. });
  296. invAdj.save()
  297. .then((newAdjustment)=>{
  298. return;
  299. })
  300. .catch((err)=>{
  301. console.log(err);
  302. return res.render("error");
  303. });
  304. },
  305. //POST - Adds an ingredient to a recipe
  306. //Inputs:
  307. // req.body.recipeId: Id of recipe to change
  308. // req.body.item: Ingredient to add with a quantity
  309. //Returns: Nothing
  310. addRecipeIngredient: function(req, res){
  311. if(!req.session.user){
  312. return res.render("error");
  313. }
  314. Recipe.findOne({_id: req.body.recipeId})
  315. .then((recipe)=>{
  316. recipe.ingredients.push({
  317. ingredient: req.body.item.ingredient,
  318. quantity: req.body.item.quantity
  319. });
  320. recipe.save()
  321. .then((recipe)=>{
  322. return res.json();
  323. })
  324. .catch((err)=>{
  325. console.log(err);
  326. return res.render("error");
  327. });
  328. })
  329. .catch((err)=>{
  330. console.log(err);
  331. return res.render("error");
  332. });
  333. },
  334. //POST - Change quantity of a recipe's ingredient
  335. //Inputs:
  336. // req.body.recipeId: Id of recipe containing the ingredient
  337. // req.body.ingredient: The ingredient to update (_id and quantity)
  338. //Returns: Nothing
  339. updateRecipeIngredient: function(req, res){
  340. if(!req.session.user){
  341. return res.render("error");
  342. }
  343. Recipe.findOne({_id: req.body.recipeId})
  344. .then((recipe)=>{
  345. for(let ingredient of recipe.ingredients){
  346. if(ingredient._id.toString() === req.body.ingredient._id){
  347. ingredient.quantity = req.body.ingredient.quantity;
  348. recipe.save()
  349. .then((recipe)=>{
  350. return res.json();
  351. })
  352. .catch((err)=>{
  353. console.log(err);
  354. return res.render("error");
  355. });
  356. }
  357. }
  358. })
  359. .catch((err)=>{
  360. console.log(err);
  361. return res.render("error");
  362. });
  363. },
  364. //POST - Remove an ingredient from a recipe
  365. //Inputs:
  366. // req.body.ingredientId: Id of ingredient to be removed
  367. // req.body.recipeId: Id of recipe to remove ingredient from
  368. //Returns: Nothing
  369. removeRecipeIngredient: function(req, res){
  370. if(!req.session.user){
  371. return res.render("error");
  372. }
  373. Recipe.findOne({_id: req.body.recipeId})
  374. .then((recipe)=>{
  375. for(let i = 0; i < recipe.ingredients.length; i++){
  376. if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
  377. recipe.ingredients.splice(i, 1);
  378. }
  379. }
  380. recipe.save()
  381. .then((recipe)=>{
  382. return res.json();
  383. })
  384. .catch((err)=>{
  385. console.log(err);
  386. return res.render("error");
  387. });
  388. })
  389. .catch((err)=>{
  390. console.log(err);
  391. return res.render("error");
  392. });
  393. },
  394. }