merchantData.js 15 KB

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