merchantData.js 11 KB

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