merchantData.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. req.session.error = validation;
  22. return res.redirect("/");
  23. }
  24. if(req.body.password === req.body.confirmPassword){
  25. let salt = bcrypt.genSaltSync(10);
  26. let hash = bcrypt.hashSync(req.body.password, salt);
  27. let merchant = new Merchant({
  28. name: req.body.name,
  29. email: req.body.email.toLowerCase(),
  30. password: hash,
  31. pos: "none",
  32. lastUpdatedTime: Date.now(),
  33. createdAt: Date.now(),
  34. accountStatus: "valid",
  35. inventory: [],
  36. recipes: []
  37. });
  38. merchant.save()
  39. .then((merchant)=>{
  40. req.session.user = merchant._id;
  41. return res.redirect("/dashboard");
  42. })
  43. .catch((err)=>{
  44. req.session.error = "ERROR: UNABLE TO CREATE ACCOUNT AT THIS TIME";
  45. return res.redirect("/");
  46. });
  47. }else{
  48. req.session.error = "PASSWORDS DO NOT MATCH";
  49. return res.redirect("/");
  50. }
  51. },
  52. /*
  53. POST - Creates new Clover merchant
  54. Redirects to /dashboard
  55. */
  56. createMerchantClover: async function(req, res){
  57. axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}?access_token=${req.session.accessToken}`)
  58. .then((response)=>{
  59. let merchant = new Merchant({
  60. name: response.data.name,
  61. pos: "clover",
  62. posId: req.session.merchantId,
  63. posAccessToken: req.session.accessToken,
  64. lastUpdatedTime: Date.now(),
  65. createdAt: Date.now(),
  66. inventory: [],
  67. recipes: []
  68. });
  69. axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}/items?access_token=${req.session.accessToken}`)
  70. .then((response)=>{
  71. let recipes = [];
  72. for(let i = 0; i < response.data.elements.length; i++){
  73. let recipe = new Recipe({
  74. posId: response.data.elements[i].id,
  75. merchant: merchant,
  76. name: response.data.elements[i].name,
  77. price: response.data.elements[i].price,
  78. ingredients: []
  79. });
  80. recipes.push(recipe);
  81. merchant.recipes.push(recipe);
  82. }
  83. Recipe.create(recipes)
  84. .catch((err)=>{
  85. req.session.error = "ERROR: UNABLE TO CREATE YOUR RECIPES FROM CLOVER."
  86. })
  87. merchant.save()
  88. .then((newMerchant)=>{
  89. req.session.accessToken = undefined;
  90. req.session.user = newMerchant._id;
  91. return res.redirect("/dashboard");
  92. })
  93. .catch((err)=>{
  94. req.session.error = "ERROR: UNABLE TO SAVE DATA FROM CLOVER";
  95. return res.redirect("/");
  96. });
  97. })
  98. .catch((err)=>{
  99. req.session.error = "ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER";
  100. return res.redirect("/");
  101. })
  102. })
  103. .catch((err)=>{
  104. req.session.error = "ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER";
  105. return res.redirect("/");
  106. });
  107. },
  108. createMerchantSquare: function(req, res){
  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("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 USER DATA");
  137. });
  138. },
  139. /*
  140. //POST - Adds an ingredient to merchant's inventory
  141. req.body = [{
  142. id: ingredient id,
  143. quantity: quantity of ingredient for the merchant
  144. defaultUnit: default unit of measurement to display
  145. }]
  146. */
  147. addMerchantIngredient: function(req, res){
  148. if(!req.session.user){
  149. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  150. return res.redirect("/");
  151. }
  152. let validation;
  153. for(let i = 0; i < req.body.length; i++){
  154. validation = Validator.quantity(req.body[i].quantity);
  155. if(validation !== true){
  156. return res.json(validation);
  157. }
  158. }
  159. Merchant.findOne({_id: req.session.user})
  160. .then((merchant)=>{
  161. for(let i = 0; i < req.body.length; i++){
  162. for(let j = 0; j < merchant.inventory.length; j++){
  163. if(merchant.inventory[j].ingredient.toString() === req.body[i].id){
  164. return res.json("ERROR: DUPLICATE INGREDIENT DETECTED");
  165. }
  166. }
  167. merchant.inventory.push({
  168. ingredient: req.body[i].id,
  169. quantity: req.body[i].quantity,
  170. defaultUnit: req.body[i].defaultUnit
  171. });
  172. }
  173. merchant.save()
  174. .then((newMerchant)=>{
  175. return res.json({});
  176. })
  177. .catch((err)=>{
  178. return res.json("ERROR: UNABLE TO SAVE NEW INGREDIENT");
  179. });
  180. })
  181. .catch((err)=>{
  182. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  183. });
  184. },
  185. //POST - Removes an ingredient from the merchant's inventory
  186. removeMerchantIngredient: function(req, res){
  187. if(!req.session.user){
  188. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  189. return res.redirect("/");
  190. }
  191. Merchant.findOne({_id: req.session.user})
  192. .then((merchant)=>{
  193. for(let i = 0; i < merchant.inventory.length; i++){
  194. if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
  195. merchant.inventory.splice(i, 1);
  196. break;
  197. }
  198. }
  199. merchant.save()
  200. .then((merchant)=>{
  201. return res.json({});
  202. })
  203. .catch((err)=>{
  204. return res.json("ERROR: UNABLE TO SAVE USER DATA");
  205. });
  206. })
  207. .catch((err)=>{
  208. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  209. });
  210. },
  211. //PUT - Update the default unit for a single ingredient
  212. ingredientDefaultUnit: 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. Merchant.findOne({_id: req.session.user})
  218. .then((merchant)=>{
  219. for(let i = 0; i < merchant.inventory.length; i++){
  220. if(merchant.inventory[i].ingredient.toString() === req.params.id){
  221. merchant.inventory[i].defaultUnit =req.params.unit;
  222. }
  223. }
  224. return merchant.save()
  225. })
  226. .then((merchant)=>{
  227. return res.json({});
  228. })
  229. .catch((err)=>{
  230. return res.json("ERROR: UNABLE TO UPDATE DEFAULT UNIT");
  231. });
  232. },
  233. /*
  234. POST - Update the quantity for a merchant inventory item
  235. req.body = [{
  236. id: id of ingredient to update,
  237. quantity: change in quantity
  238. }]
  239. */
  240. updateMerchantIngredient: function(req, res){
  241. if(!req.session.user){
  242. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  243. return res.redirect("/");
  244. }
  245. for(let i = 0; i < req.body.length; i++){
  246. let validation = Validator.quantity(req.body[i].quantity);
  247. if(validation !== true){
  248. return res.json(validation);
  249. }
  250. }
  251. let adjustments = [];
  252. Merchant.findOne({_id: req.session.user})
  253. .then((merchant)=>{
  254. for(let i = 0; i < req.body.length; i++){
  255. let updateIngredient;
  256. for(let j = 0; j < merchant.inventory.length; j++){
  257. if(merchant.inventory[j].ingredient.toString() === req.body[i].id){
  258. updateIngredient = merchant.inventory[j];
  259. break;
  260. }
  261. }
  262. adjustments.push(new InventoryAdjustment({
  263. date: Date.now(),
  264. merchant: req.session.user,
  265. ingredient: req.body[i].id,
  266. quantity: req.body[i].quantity - updateIngredient.quantity
  267. }));
  268. updateIngredient.quantity = req.body[i].quantity;
  269. }
  270. merchant.save()
  271. .then((newMerchant)=>{
  272. res.json({});
  273. InventoryAdjustment.create(adjustments).catch(()=>{});
  274. return;
  275. })
  276. .catch((err)=>{
  277. return res.json("ERROR: UNABLE TO SAVE DATA");
  278. })
  279. })
  280. .catch((err)=>{
  281. return res.json("ERROR: UNABLE TO RETRIEVE DATA");
  282. });
  283. },
  284. /*
  285. POST - Changes the users password
  286. req.body = {
  287. pass: new password,
  288. confirmPass: new password confirmation,
  289. hash: hashed version of old password
  290. }
  291. */
  292. updatePassword: function(req, res){
  293. let validation = Validator.password(req.body.pass, req.body.confirmPass);
  294. if(validation !== true){
  295. return res.json(validation);
  296. }
  297. Merchant.findOne({password: req.body.hash})
  298. .then((merchant)=>{
  299. if(merchant){
  300. let salt = bcrypt.genSaltSync(10);
  301. let hash = bcrypt.hashSync(req.body.pass, salt);
  302. merchant.password = hash;
  303. return merchant.save();
  304. }else{
  305. req.session.error = "ERROR: UNABLE TO RETRIEVE USER DATA";
  306. return res.redirect("/");
  307. }
  308. })
  309. .then((merchant)=>{
  310. req.session.error = "PASSWORD SUCCESSFULLY RESET. PLEASE LOG IN";
  311. return res.redirect("/");
  312. })
  313. .catch((err)=>{});
  314. }
  315. }