merchantData.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const Merchant = require("../models/merchant");
  2. const InventoryAdjustment = require("../models/inventoryAdjustment");
  3. const helper = require("./helper.js");
  4. const bcrypt = require("bcryptjs");
  5. module.exports = {
  6. /*
  7. POST - Create a new merchant with no POS system1
  8. req.body = {
  9. name: retaurant name,
  10. email: registration email,
  11. password: password,
  12. confirmPassword: confirmation password
  13. }
  14. Redirects to /dashboard
  15. */
  16. createMerchantNone: async function(req, res){
  17. if(req.body.password.length < 10){
  18. req.session.error = "PASSWORD MUST CONTAIN AT LEAST 10 CHARACTERS";
  19. return res.redirect("/register");
  20. }
  21. if(req.body.password !== req.body.confirmPassword){
  22. req.session.error = "PASSWORDS DO NOT MATCH";
  23. return res.redirect("/register");
  24. }
  25. const merchantFind = await Merchant.findOne({email: req.body.email.toLowerCase()});
  26. if(merchantFind !== null){
  27. req.session.error = "USER WITH THIS EMAIL ADDRESS ALREADY EXISTS";
  28. return res.redirect("/register");
  29. }
  30. let salt = bcrypt.genSaltSync(10);
  31. let hash = bcrypt.hashSync(req.body.password, salt);
  32. let expirationDate = new Date();
  33. expirationDate.setDate(expirationDate.getDate() + 90);
  34. let merchant = new Merchant({
  35. name: req.body.name,
  36. email: req.body.email.toLowerCase(),
  37. password: hash,
  38. pos: "none",
  39. lastUpdatedTime: Date.now(),
  40. createdAt: Date.now(),
  41. status: ["unverified"],
  42. inventory: [],
  43. recipes: [],
  44. verifyId: helper.generateId(15),
  45. session: {
  46. sessionId: helper.generateId(25),
  47. expiration: expirationDate
  48. }
  49. });
  50. merchant.save()
  51. .then((merchant)=>{
  52. return res.redirect(`/verify/email/${merchant._id}`);
  53. })
  54. .catch((err)=>{
  55. if(typeof(err) === "string"){
  56. req.session.error = err;
  57. }else if(err.name === "ValidationError"){
  58. req.session.error = err.errors[Object.keys(err.errors)[0]].properties.message;
  59. }else{
  60. req.session.error = "ERROR: UNABLE TO CREATE ACCOUNT AT THIS TIME";
  61. }
  62. return res.redirect("/");
  63. });
  64. },
  65. /*
  66. POST - Update the quantity for a merchant inventory item
  67. req.body = [{
  68. id: id of ingredient to update,
  69. quantity: change in quantity
  70. }]
  71. */
  72. updateMerchantIngredient: function(req, res){
  73. let adjustments = [];
  74. let changedIngredients = [];
  75. res.locals.merchant
  76. .populate("inventory.ingredient")
  77. .execPopulate()
  78. .then((merchant)=>{
  79. for(let i = 0; i < req.body.length; i++){
  80. let updateIngredient;
  81. for(let j = 0; j < merchant.inventory.length; j++){
  82. if(merchant.inventory[j].ingredient._id.toString() === req.body[i].id){
  83. updateIngredient = merchant.inventory[j];
  84. break;
  85. }
  86. }
  87. adjustments.push(new InventoryAdjustment({
  88. date: Date.now(),
  89. merchant: req.session.user,
  90. ingredient: req.body[i].id,
  91. quantity: req.body[i].quantity - updateIngredient.quantity,
  92. }));
  93. updateIngredient.quantity = helper.convertQuantityToBaseUnit(req.body[i].quantity, updateIngredient.defaultUnit);
  94. changedIngredients.push(updateIngredient);
  95. }
  96. return merchant.save();
  97. })
  98. .then((newMerchant)=>{
  99. res.json(changedIngredients);
  100. InventoryAdjustment.create(adjustments).catch(()=>{});
  101. return;
  102. })
  103. .catch((err)=>{
  104. if(typeof(err) === "string"){
  105. return res.json(err);
  106. }
  107. if(err.name === "ValidationError"){
  108. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  109. }
  110. return res.json("ERROR: UNABLE TO UPDATE DATA");
  111. });
  112. },
  113. /*
  114. POST - Changes the users password
  115. req.body = {
  116. pass: new password,
  117. confirmPass: new password confirmation,
  118. hash: hashed version of old password
  119. }
  120. */
  121. updatePassword: function(req, res){
  122. Merchant.findOne({password: req.body.hash})
  123. .then((merchant)=>{
  124. if(merchant){
  125. if(req.body.pass.length < 10){
  126. throw "PASSWORD MUST CONTAIN AT LEAST 10 CHARACTERS";
  127. }
  128. if(req.body.pass !== req.body.confirmPass){
  129. throw "PASSWORDS DO NOT MATCH";
  130. }
  131. let salt = bcrypt.genSaltSync(10);
  132. let hash = bcrypt.hashSync(req.body.pass, salt);
  133. merchant.password = hash;
  134. return merchant.save();
  135. }else{
  136. req.session.error = "ERROR: UNABLE TO RETRIEVE USER DATA";
  137. return res.redirect("/");
  138. }
  139. })
  140. .then((merchant)=>{
  141. req.session.success = "PASSWORD SUCCESSFULLY RESET. PLEASE LOG IN";
  142. return res.redirect("/login");
  143. })
  144. .catch((err)=>{
  145. if(typeof(err) === "string"){
  146. return res.json(err);
  147. }
  148. if(err.name === "ValidationError"){
  149. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  150. }
  151. return res.json("ERROR: UNABLE TO UPDATE YOUR PASSWORD");
  152. });
  153. }
  154. }