merchantData.js 11 KB

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