merchantData.js 11 KB

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