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