merchantData.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.findOne({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 = [{
  139. id: ingredient id,
  140. quantity: quantity of ingredient for the merchant
  141. }]
  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 i = 0; i < req.body.length; i++){
  151. for(let j = 0; j < merchant.inventory.length; j++){
  152. if(merchant.inventory[j].ingredient.toString() === req.body[i].id){
  153. return res.json("Error: Duplicate ingredient detected");
  154. }
  155. }
  156. merchant.inventory.push({
  157. ingredient: req.body[i].id,
  158. quantity: req.body[i].quantity
  159. });
  160. }
  161. merchant.save()
  162. .then((newMerchant)=>{
  163. return res.json({});
  164. })
  165. .catch((err)=>{
  166. return res.json("Error: unable to save new ingredient");
  167. });
  168. })
  169. .catch((err)=>{
  170. return res.json("Error: unable to retrieve user data");
  171. });
  172. },
  173. //POST - Removes an ingredient from the merchant's inventory
  174. removeMerchantIngredient: function(req, res){
  175. if(!req.session.user){
  176. req.session.error = "Must be logged in to do that";
  177. return res.redirect("/");
  178. }
  179. Merchant.findOne({_id: req.session.user})
  180. .then((merchant)=>{
  181. for(let i = 0; i < merchant.inventory.length; i++){
  182. if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
  183. merchant.inventory.splice(i, 1);
  184. break;
  185. }
  186. }
  187. merchant.save()
  188. .then((merchant)=>{
  189. return res.json({});
  190. })
  191. .catch((err)=>{
  192. return res.json("Error: unable to save user data");
  193. });
  194. })
  195. .catch((err)=>{
  196. return res.json("Error: unable to retrieve user data");
  197. });
  198. },
  199. /*
  200. POST - Update the quantity for a merchant inventory item
  201. req.body = [{
  202. id: id of ingredient to update,
  203. quantity: change in quantity
  204. }]
  205. */
  206. updateMerchantIngredient: function(req, res){
  207. if(!req.session.user){
  208. req.session.error = "Must be logged in to do that";
  209. return res.redirect("/");
  210. }
  211. let adjustments = [];
  212. Merchant.findOne({_id: req.session.user})
  213. .then((merchant)=>{
  214. for(let i = 0; i < req.body.length; i++){
  215. let updateIngredient;
  216. for(let j = 0; j < merchant.inventory.length; j++){
  217. if(merchant.inventory[j].ingredient.toString() === req.body[i].id){
  218. updateIngredient = merchant.inventory[j];
  219. break;
  220. }
  221. }
  222. adjustments.push(new InventoryAdjustment({
  223. date: Date.now(),
  224. merchant: req.session.user,
  225. ingredient: req.body[i].id,
  226. quantity: req.body[i].quantity
  227. }));
  228. updateIngredient.quantity += req.body[i].quantity;
  229. }
  230. merchant.save()
  231. .then((newMerchant)=>{
  232. res.json({});
  233. InventoryAdjustment.create(adjustments).catch(()=>{});
  234. return;
  235. })
  236. .catch((err)=>{
  237. return res.json("Error: your data could not be saved");
  238. })
  239. })
  240. .catch((err)=>{
  241. return res.json("Error: your data could not be retrieved");
  242. });
  243. },
  244. /*
  245. //POST - Update merchant password
  246. req.body = {
  247. oldPass: current merchant password (supposedly),
  248. newPass: replacement password
  249. }
  250. */
  251. updatePassword: function(req, res){
  252. if(!req.session.user){
  253. req.session.error = "Must be logged in to do that";
  254. return res.redirect("/");
  255. }
  256. Merchant.findOne({_id: req.session.user})
  257. .then((merchant)=>{
  258. bcrypt.compare(req.body.oldPass, merchant.password, (err, result)=>{
  259. if(result){
  260. let salt = bcrypt.genSaltSync(10);
  261. let hash = bcrypt.hashSync(req.body.newPass, salt);
  262. merchant.password = hash;
  263. merchant.save()
  264. .then((updatedMerchant)=>{
  265. return res.json({});
  266. })
  267. .catch((err)=>{
  268. return res.json("Error: Unable to save new password");
  269. });
  270. }else{
  271. return res.json("Error: old password does not match current password");
  272. }
  273. });
  274. })
  275. .catch((err)=>{
  276. return res.json("Error: Unable to retrieve merchant data");
  277. });
  278. }
  279. }