merchantData.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 = "Error: Passwords must 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 item of response.data.elements){
  73. let recipe = new Recipe({
  74. posId: item.id,
  75. merchant: merchant,
  76. name: item.name,
  77. price: item.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. Try using updating your recipes on the recipe page."
  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 necessary 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. //DELETE - removes a single recipe from the merchant
  109. removeRecipe: function(req, res){
  110. if(!req.session.user){
  111. req.session.error = "Must be logged in to do that";
  112. return res.redirect("/");
  113. }
  114. Merchant.findOne({_id: req.session.user})
  115. .then((merchant)=>{
  116. if(merchant.pos === "clover"){
  117. return res.json("Error: you must edit your recipes inside Clover");
  118. }
  119. for(let i = 0; i < merchant.recipes.length; i++){
  120. if(merchant.recipes[i].toString() === req.params.id){
  121. merchant.recipes.splice(i, 1);
  122. break;
  123. }
  124. }
  125. merchant.save()
  126. .then((updatedMerchant)=>{
  127. return res.json({});
  128. })
  129. .catch((err)=>{
  130. return res.json("Error: unable to save data")
  131. })
  132. })
  133. .catch((err)=>{
  134. return res.json("Error: unable to retrieve merchant data");
  135. });
  136. },
  137. /*
  138. //POST - Adds an ingredient to merchant's inventory
  139. req.body = [{
  140. id: ingredient id,
  141. quantity: quantity of ingredient for the merchant
  142. }]
  143. */
  144. addMerchantIngredient: function(req, res){
  145. if(!req.session.user){
  146. req.session.error = "Must be logged in to do that";
  147. return res.redirect("/");
  148. }
  149. let validation;
  150. for(let i = 0; i < req.body.length; i++){
  151. validation = Validator.quantity(req.body[i].quantity);
  152. if(validation !== true){
  153. return res.json(validation);
  154. }
  155. }
  156. Merchant.findOne({_id: req.session.user})
  157. .then((merchant)=>{
  158. for(let i = 0; i < req.body.length; i++){
  159. for(let j = 0; j < merchant.inventory.length; j++){
  160. if(merchant.inventory[j].ingredient.toString() === req.body[i].id){
  161. return res.json("Error: Duplicate ingredient detected");
  162. }
  163. }
  164. merchant.inventory.push({
  165. ingredient: req.body[i].id,
  166. quantity: req.body[i].quantity
  167. });
  168. }
  169. merchant.save()
  170. .then((newMerchant)=>{
  171. return res.json({});
  172. })
  173. .catch((err)=>{
  174. return res.json("Error: unable to save new ingredient");
  175. });
  176. })
  177. .catch((err)=>{
  178. return res.json("Error: unable to retrieve user data");
  179. });
  180. },
  181. //POST - Removes an ingredient from the merchant's inventory
  182. removeMerchantIngredient: function(req, res){
  183. if(!req.session.user){
  184. req.session.error = "Must be logged in to do that";
  185. return res.redirect("/");
  186. }
  187. Merchant.findOne({_id: req.session.user})
  188. .then((merchant)=>{
  189. for(let i = 0; i < merchant.inventory.length; i++){
  190. if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
  191. merchant.inventory.splice(i, 1);
  192. break;
  193. }
  194. }
  195. merchant.save()
  196. .then((merchant)=>{
  197. return res.json({});
  198. })
  199. .catch((err)=>{
  200. return res.json("Error: unable to save user data");
  201. });
  202. })
  203. .catch((err)=>{
  204. return res.json("Error: unable to retrieve user data");
  205. });
  206. },
  207. /*
  208. POST - Update the quantity for a merchant inventory item
  209. req.body = [{
  210. id: id of ingredient to update,
  211. quantity: change in quantity
  212. }]
  213. */
  214. updateMerchantIngredient: function(req, res){
  215. if(!req.session.user){
  216. req.session.error = "Must be logged in to do that";
  217. return res.redirect("/");
  218. }
  219. let validation = Validator.quantity(req.body.quantity);
  220. if(validation !== true){
  221. return res.json(validation);
  222. }
  223. let adjustments = [];
  224. Merchant.findOne({_id: req.session.user})
  225. .then((merchant)=>{
  226. for(let i = 0; i < req.body.length; i++){
  227. let updateIngredient;
  228. for(let j = 0; j < merchant.inventory.length; j++){
  229. if(merchant.inventory[j].ingredient.toString() === req.body[i].id){
  230. updateIngredient = merchant.inventory[j];
  231. break;
  232. }
  233. }
  234. adjustments.push(new InventoryAdjustment({
  235. date: Date.now(),
  236. merchant: req.session.user,
  237. ingredient: req.body[i].id,
  238. quantity: req.body[i].quantity - updateIngredient.quantity
  239. }));
  240. updateIngredient.quantity = req.body[i].quantity;
  241. }
  242. merchant.save()
  243. .then((newMerchant)=>{
  244. res.json({});
  245. InventoryAdjustment.create(adjustments).catch(()=>{});
  246. return;
  247. })
  248. .catch((err)=>{
  249. return res.json("Error: your data could not be saved");
  250. })
  251. })
  252. .catch((err)=>{
  253. return res.json("Error: your data could not be retrieved");
  254. });
  255. },
  256. /*
  257. POST - Changes the users password
  258. req.body = {
  259. pass: new password,
  260. confirmPass: new password confirmation,
  261. hash: hashed version of old password
  262. }
  263. */
  264. updatePassword: function(req, res){
  265. let validation = Validator.password(req.body.pass, req.body.confirmPass);
  266. if(validation !== true){
  267. return res.json(validation);
  268. }
  269. Merchant.findOne({password: req.body.hash})
  270. .then((merchant)=>{
  271. if(merchant){
  272. let salt = bcrypt.genSaltSync(10);
  273. let hash = bcrypt.hashSync(req.body.pass, salt);
  274. merchant.password = hash;
  275. return merchant.save();
  276. }else{
  277. req.session.error = "Error: unable to retrieve merchant data";
  278. return res.redirect("/");
  279. }
  280. })
  281. .then((merchant)=>{
  282. req.session.error = "Password successfully reset. Please log in";
  283. return res.redirect("/");
  284. })
  285. .catch((err)=>{});
  286. }
  287. }