merchantData.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. let validation = Validate.quantity(req.body.quantity);
  151. if(validation !== true){
  152. return res.json(validation);
  153. }
  154. Merchant.findOne({_id: req.session.user})
  155. .then((merchant)=>{
  156. for(let i = 0; i < req.body.length; i++){
  157. for(let j = 0; j < merchant.inventory.length; j++){
  158. if(merchant.inventory[j].ingredient.toString() === req.body[i].id){
  159. return res.json("Error: Duplicate ingredient detected");
  160. }
  161. }
  162. merchant.inventory.push({
  163. ingredient: req.body[i].id,
  164. quantity: req.body[i].quantity
  165. });
  166. }
  167. merchant.save()
  168. .then((newMerchant)=>{
  169. return res.json({});
  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 validation = Validator.quantity(req.body.quantity);
  218. if(validation !== true){
  219. return res.json(validation);
  220. }
  221. let adjustments = [];
  222. Merchant.findOne({_id: req.session.user})
  223. .then((merchant)=>{
  224. for(let i = 0; i < req.body.length; i++){
  225. let updateIngredient;
  226. for(let j = 0; j < merchant.inventory.length; j++){
  227. if(merchant.inventory[j].ingredient.toString() === req.body[i].id){
  228. updateIngredient = merchant.inventory[j];
  229. break;
  230. }
  231. }
  232. adjustments.push(new InventoryAdjustment({
  233. date: Date.now(),
  234. merchant: req.session.user,
  235. ingredient: req.body[i].id,
  236. quantity: req.body[i].quantity - updateIngredient.quantity
  237. }));
  238. updateIngredient.quantity = req.body[i].quantity;
  239. }
  240. merchant.save()
  241. .then((newMerchant)=>{
  242. res.json({});
  243. InventoryAdjustment.create(adjustments).catch(()=>{});
  244. return;
  245. })
  246. .catch((err)=>{
  247. return res.json("Error: your data could not be saved");
  248. })
  249. })
  250. .catch((err)=>{
  251. return res.json("Error: your data could not be retrieved");
  252. });
  253. },
  254. /*
  255. POST - Changes the users password
  256. req.body = {
  257. pass: new password,
  258. confirmPass: new password confirmation,
  259. hash: hashed version of old password
  260. }
  261. */
  262. updatePassword: function(req, res){
  263. let validation = Validator.password(req.body.pass, req.body.confirmPass);
  264. if(validation !== true){
  265. return res.json(validation);
  266. }
  267. Merchant.findOne({password: req.body.hash})
  268. .then((merchant)=>{
  269. if(merchant){
  270. let salt = bcrypt.genSaltSync(10);
  271. let hash = bcrypt.hashSync(req.body.pass, salt);
  272. merchant.password = hash;
  273. return merchant.save();
  274. }else{
  275. req.session.error = "Error: unable to retrieve merchant data";
  276. return res.redirect("/");
  277. }
  278. })
  279. .then((merchant)=>{
  280. req.session.error = "Password successfully reset. Please log in";
  281. return res.redirect("/");
  282. })
  283. .catch((err)=>{});
  284. }
  285. }