merchantData.js 11 KB

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