merchantData.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. })
  134. .catch((err)=>{
  135. console.log(err);
  136. });
  137. },
  138. //POST - Creates a non-pos merchant from all entered data
  139. //Inputs:
  140. // req.body.data: All data from frontend in form of merchant model
  141. //Redirects to "/inventory"
  142. createMerchantNone: function(req, res){
  143. let data = JSON.parse(req.body.data);
  144. data.email = data.email.toLowerCase();
  145. let salt = bcrypt.genSaltSync(10);
  146. let hash = bcrypt.hashSync(data.password, salt);
  147. let merchant = new Merchant({
  148. name: data.name,
  149. email: data.email,
  150. password: hash,
  151. pos: "none"
  152. });
  153. for(let item of data.inventory){
  154. merchant.inventory.push({
  155. ingredient: item.ingredient.id,
  156. quantity: item.quantity
  157. });
  158. }
  159. for(let recipe of data.recipes){
  160. recipe.merchant = merchant._id;
  161. }
  162. Recipe.create(data.recipes)
  163. .then((recipes)=>{
  164. for(let recipe of recipes){
  165. merchant.recipes.push(recipe._id);
  166. }
  167. merchant.save()
  168. .then((merchant)=>{
  169. req.session.user = merchant._id;
  170. return res.redirect("/inventory");
  171. })
  172. .catch((err)=>{
  173. console.log(err);
  174. return res.render("error");
  175. });
  176. })
  177. .catch((err)=>{
  178. console.log(err);
  179. return res.render("error");
  180. });
  181. },
  182. //POST - Adds an ingredient to merchant's inventory
  183. //Inputs:
  184. // req.body: A merchant inventory item (ingredient id and quantity)
  185. //Returns:
  186. // ingredient: Newly added ingredient
  187. addMerchantIngredient: function(req, res){
  188. if(!req.session.user){
  189. return res.render("error");
  190. }
  191. Merchant.findOne({_id: req.session.user})
  192. .then((merchant)=>{
  193. merchant.inventory.push(req.body);
  194. merchant.save()
  195. .then((newMerchant)=>{
  196. Ingredient.findOne({_id: req.body.ingredient})
  197. .then((ingredient)=>{
  198. return res.json(ingredient);
  199. })
  200. .catch((err)=>{
  201. console.log(err);
  202. return res.render("error");
  203. });
  204. })
  205. .catch((err)=>{
  206. console.log(err);
  207. return res.render("error");
  208. });
  209. })
  210. .catch((err)=>{
  211. console.log(err);
  212. return res.render("error");
  213. });
  214. },
  215. //POST - Removes an ingredient from the merchant's inventory
  216. //Inputs:
  217. // ingredientId: id of ingredient to remove
  218. //Returns: Nothing
  219. removeMerchantIngredient: function(req, res){
  220. if(!req.session.user){
  221. return res.render("error");
  222. }
  223. Merchant.findOne({_id: req.session.user})
  224. .then((merchant)=>{
  225. for(let i = 0; i < merchant.inventory.length; i++){
  226. if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
  227. merchant.inventory.splice(i, 1);
  228. break;
  229. }
  230. }
  231. merchant.save()
  232. .then(()=>{
  233. return res.json();
  234. })
  235. .catch((err)=>{
  236. console.log(err);
  237. return res.render("error");
  238. });
  239. })
  240. .catch((err)=>{
  241. console.log(err);
  242. return res.render("error");
  243. });
  244. },
  245. //POST - Update the quantity for a merchant inventory item
  246. //Inputs:
  247. // req.body.ingredientId: Id of ingredient to update
  248. // req.body.quantityChange: Amount to change ingredient (not the new value)
  249. //Returns: Nothing
  250. updateMerchantIngredient: function(req, res){
  251. if(!req.session.user){
  252. return res.render("error");
  253. }
  254. Merchant.findOne({_id: req.session.user})
  255. .then((merchant)=>{
  256. let updateIngredient = merchant.inventory.find(i => i._id.toString() === req.body.ingredientId);
  257. updateIngredient.quantity += req.body.quantityChange;
  258. merchant.save()
  259. .then((merchant)=>{
  260. res.json();
  261. })
  262. .catch((err)=>{
  263. console.log(err);
  264. return res.render("error");
  265. })
  266. })
  267. .catch((err)=>{
  268. console.log(err);
  269. return res.render("error");
  270. });
  271. let invAdj = new InventoryAdjustment({
  272. date: Date.now(),
  273. merchant: req.session.user,
  274. ingredient: req.body.ingredientId,
  275. quantity: req.body.quantityChange
  276. });
  277. invAdj.save()
  278. .then((newAdjustment)=>{
  279. return;
  280. })
  281. .catch((err)=>{
  282. console.log(err);
  283. return res.render("error");
  284. });
  285. },
  286. //POST - Adds an ingredient to a recipe
  287. //Inputs:
  288. // req.body.recipeId: Id of recipe to change
  289. // req.body.item: Ingredient to add with a quantity
  290. //Returns: Nothing
  291. addRecipeIngredient: function(req, res){
  292. if(!req.session.user){
  293. return res.render("error");
  294. }
  295. Recipe.findOne({_id: req.body.recipeId})
  296. .then((recipe)=>{
  297. recipe.ingredients.push({
  298. ingredient: req.body.item.ingredient,
  299. quantity: req.body.item.quantity
  300. });
  301. recipe.save()
  302. .then((recipe)=>{
  303. return res.json();
  304. })
  305. .catch((err)=>{
  306. console.log(err);
  307. return res.render("error");
  308. });
  309. })
  310. .catch((err)=>{
  311. console.log(err);
  312. return res.render("error");
  313. });
  314. },
  315. //POST - Change quantity of a recipe's ingredient
  316. //Inputs:
  317. // req.body.recipeId: Id of recipe containing the ingredient
  318. // req.body.ingredient: The ingredient to update (_id and quantity)
  319. //Returns: Nothing
  320. updateRecipeIngredient: function(req, res){
  321. if(!req.session.user){
  322. return res.render("error");
  323. }
  324. Recipe.findOne({_id: req.body.recipeId})
  325. .then((recipe)=>{
  326. for(let ingredient of recipe.ingredients){
  327. if(ingredient._id.toString() === req.body.ingredient._id){
  328. ingredient.quantity = req.body.ingredient.quantity;
  329. recipe.save()
  330. .then((recipe)=>{
  331. return res.json();
  332. })
  333. .catch((err)=>{
  334. console.log(err);
  335. return res.render("error");
  336. });
  337. }
  338. }
  339. })
  340. .catch((err)=>{
  341. console.log(err);
  342. return res.render("error");
  343. });
  344. },
  345. //POST - Remove an ingredient from a recipe
  346. //Inputs:
  347. // req.body.ingredientId: Id of ingredient to be removed
  348. // req.body.recipeId: Id of recipe to remove ingredient from
  349. //Returns: Nothing
  350. removeRecipeIngredient: function(req, res){
  351. if(!req.session.user){
  352. return res.render("error");
  353. }
  354. Recipe.findOne({_id: req.body.recipeId})
  355. .then((recipe)=>{
  356. for(let i = 0; i < recipe.ingredients.length; i++){
  357. if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
  358. recipe.ingredients.splice(i, 1);
  359. }
  360. }
  361. recipe.save()
  362. .then((recipe)=>{
  363. return res.json();
  364. })
  365. .catch((err)=>{
  366. console.log(err);
  367. return res.render("error");
  368. });
  369. })
  370. .catch((err)=>{
  371. console.log(err);
  372. return res.render("error");
  373. });
  374. },
  375. }