merchantData.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. const axios = require("axios");
  2. const bcrypt = require("bcryptjs");
  3. const Merchant = require("../models/merchant");
  4. const Recipe = require("../models/recipe");
  5. const InventoryAdjustment = require("../models/inventoryAdjustment");
  6. const RecipeChange = require("../models/recipeChange");
  7. module.exports = {
  8. /*
  9. POST - Create a new merchant with no POS system
  10. req.body = {
  11. name: retaurant name,
  12. email: registration email,
  13. password: password,
  14. confirmPassword: confirmation password
  15. }
  16. Redirects to /dashboard
  17. */
  18. createMerchantNone: function(req, res){
  19. if(req.body.password === req.body.confirmPassword){
  20. let salt = bcrypt.genSaltSync(10);
  21. let hash = bcrypt.hashSync(req.body.password, salt);
  22. let merchant = new Merchant({
  23. name: req.body.name,
  24. email: req.body.email.toLowerCase(),
  25. password: hash,
  26. pos: "none",
  27. lastUpdatedTime: Date.now(),
  28. createdAt: Date.now(),
  29. accountStatus: "valid",
  30. inventory: [],
  31. recipes: []
  32. });
  33. merchant.save()
  34. .then((merchant)=>{
  35. req.session.user = merchant._id;
  36. return res.redirect("/dashboard");
  37. })
  38. .catch((err)=>{
  39. req.session.error = "Error: Unable to create account at this time";
  40. return res.redirect("/");
  41. });
  42. }else{
  43. req.session.error = "Error: Passwords must match";
  44. return res.redirect("/");
  45. }
  46. },
  47. /*
  48. POST - Creates new Clover merchant
  49. Redirects to /dashboard
  50. */
  51. createMerchantClover: async function(req, res){
  52. console.log("function ran")
  53. axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}?access_token=${req.session.accessToken}`)
  54. .then((response)=>{
  55. console.log("axios 1 successful");
  56. let merchant = new Merchant({
  57. name: response.data.name,
  58. pos: "clover",
  59. posId: req.session.merchantId,
  60. posAccessToken: req.session.accessToken,
  61. lastUpdatedTime: Date.now(),
  62. createdAt: Date.now(),
  63. inventory: [],
  64. recipes: []
  65. });
  66. axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}/items?access_token=${req.session.accessToken}`)
  67. .then((response)=>{
  68. console.log("axsios 2 successful");
  69. let recipes = [];
  70. for(let item of response.data.elements){
  71. let recipe = new Recipe({
  72. posId: item.id,
  73. merchant: merchant,
  74. name: item.name,
  75. price: item.price,
  76. ingredients: []
  77. });
  78. recipes.push(recipe);
  79. merchant.recipes.push(recipe);
  80. }
  81. Recipe.create(recipes)
  82. .catch((err)=>{
  83. console.log(err);
  84. req.session.error = "Error: unable to create your recipes from Clover. Try using updating your recipes on the recipe page."
  85. })
  86. merchant.save()
  87. .then((newMerchant)=>{
  88. req.session.accessToken = undefined;
  89. req.session.user = newMerchant._id;
  90. return res.redirect("/dashboard");
  91. })
  92. .catch((err)=>{
  93. console.log(err);
  94. req.session.error = "Error: unable to save data from Clover";
  95. return res.redirect("/");
  96. });
  97. })
  98. .catch((err)=>{
  99. console.log(err);
  100. req.session.error = "Error: unable to retrieve necessary data from Clover";
  101. return res.redirect("/");
  102. })
  103. })
  104. .catch((err)=>{
  105. console.log(err);
  106. req.session.error = "Error: Unable to retrieve data from Clover";
  107. return res.redirect("/");
  108. });
  109. },
  110. //DELETE - removes a single recipe from the merchant
  111. removeRecipe: function(req, res){
  112. if(!req.session.user){
  113. req.session.error = "Must be logged in to do that";
  114. return res.redirect("/");
  115. }
  116. Merchant.findOne({_id: req.session.user})
  117. .then((merchant)=>{
  118. if(merchant.pos === "clover"){
  119. return res.json("Error: you must edit your recipes inside Clover");
  120. }
  121. for(let i = 0; i < merchant.recipes.length; i++){
  122. if(merchant.recipes[i].toString() === req.params.id){
  123. merchant.recipes.splice(i, 1);
  124. break;
  125. }
  126. }
  127. merchant.save()
  128. .then((updatedMerchant)=>{
  129. return res.json({});
  130. })
  131. .catch((err)=>{
  132. return res.json("Error: unable to save data")
  133. })
  134. })
  135. .catch((err)=>{
  136. return res.json("Error: unable to retrieve merchant data");
  137. });
  138. },
  139. /*
  140. //POST - Adds an ingredient to merchant's inventory
  141. req.body = [ingredient objects]
  142. */
  143. addMerchantIngredient: function(req, res){
  144. if(!req.session.user){
  145. req.session.error = "Must be logged in to do that";
  146. return res.redirect("/");
  147. }
  148. Merchant.findOne({_id: req.session.user})
  149. .then((merchant)=>{
  150. for(let ingredient of req.body){
  151. for(let item of merchant.inventory){
  152. if(item.ingredient.toString() === ingredient.ingredient._id){
  153. return res.json("Error: Duplicate ingredient detected");
  154. }
  155. }
  156. merchant.inventory.push({
  157. ingredient: ingredient.ingredient._id,
  158. quantity: ingredient.quantity
  159. });
  160. }
  161. merchant.save()
  162. .then((newMerchant)=>{
  163. newMerchant.populate("inventory.ingredient", (err)=>{
  164. if(err){
  165. return res.json("Warning: refresh page to view updates");
  166. }else{
  167. return res.json({});
  168. }
  169. });
  170. })
  171. .catch((err)=>{
  172. return res.json("Error: unable to save new ingredient");
  173. });
  174. })
  175. .catch((err)=>{
  176. return res.json("Error: unable to retrieve user data");
  177. });
  178. },
  179. //POST - Removes an ingredient from the merchant's inventory
  180. removeMerchantIngredient: function(req, res){
  181. if(!req.session.user){
  182. req.session.error = "Must be logged in to do that";
  183. return res.redirect("/");
  184. }
  185. Merchant.findOne({_id: req.session.user})
  186. .then((merchant)=>{
  187. for(let i = 0; i < merchant.inventory.length; i++){
  188. if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
  189. merchant.inventory.splice(i, 1);
  190. break;
  191. }
  192. }
  193. merchant.save()
  194. .then((merchant)=>{
  195. return res.json({});
  196. })
  197. .catch((err)=>{
  198. return res.json("Error: unable to save user data");
  199. });
  200. })
  201. .catch((err)=>{
  202. return res.json("Error: unable to retrieve user data");
  203. });
  204. },
  205. /*
  206. POST - Update the quantity for a merchant inventory item
  207. req.body = [{
  208. id: id of ingredient to update,
  209. quantity: change in quantity
  210. }]
  211. */
  212. updateMerchantIngredient: function(req, res){
  213. if(!req.session.user){
  214. req.session.error = "Must be logged in to do that";
  215. return res.redirect("/");
  216. }
  217. let adjustments = [];
  218. Merchant.findOne({_id: req.session.user})
  219. .then((merchant)=>{
  220. for(let i = 0; i < req.body.length; i++){
  221. let updateIngredient;
  222. for(let j = 0; j < merchant.inventory.length; j++){
  223. if(merchant.inventory[j].ingredient.toString() === req.body[i].id){
  224. updateIngredient = merchant.inventory[j];
  225. break;
  226. }
  227. }
  228. adjustments.push(new InventoryAdjustment({
  229. date: Date.now(),
  230. merchant: req.session.user,
  231. ingredient: req.body[i].id,
  232. quantity: req.body[i].quantity
  233. }));
  234. updateIngredient.quantity += req.body[i].quantity;
  235. }
  236. merchant.save()
  237. .then((newMerchant)=>{
  238. res.json({});
  239. InventoryAdjustment.create(adjustments).catch(()=>{});
  240. return;
  241. })
  242. .catch((err)=>{
  243. return res.json("Error: your data could not be saved");
  244. })
  245. })
  246. .catch((err)=>{
  247. return res.json("Error: your data could not be retrieved");
  248. });
  249. },
  250. /*
  251. //POST - Update merchant password
  252. req.body = {
  253. oldPass: current merchant password (supposedly),
  254. newPass: replacement password
  255. }
  256. */
  257. updatePassword: function(req, res){
  258. if(!req.session.user){
  259. req.session.error = "Must be logged in to do that";
  260. return res.redirect("/");
  261. }
  262. Merchant.findOne({_id: req.session.user})
  263. .then((merchant)=>{
  264. bcrypt.compare(req.body.oldPass, merchant.password, (err, result)=>{
  265. if(result){
  266. let salt = bcrypt.genSaltSync(10);
  267. let hash = bcrypt.hashSync(req.body.newPass, salt);
  268. merchant.password = hash;
  269. merchant.save()
  270. .then((updatedMerchant)=>{
  271. return res.json({});
  272. })
  273. .catch((err)=>{
  274. return res.json("Error: Unable to save new password");
  275. });
  276. }else{
  277. return res.json("Error: old password does not match current password");
  278. }
  279. });
  280. })
  281. .catch((err)=>{
  282. return res.json("Error: Unable to retrieve merchant data");
  283. });
  284. }
  285. }