Explorar el Código

Add updating of merchant data on the backend.
Right now, only the email address is available for updating.

Lee Morgan hace 5 años
padre
commit
cc1a680023

+ 39 - 1
controllers/merchantData.js

@@ -2,12 +2,14 @@ const Merchant = require("../models/merchant");
 const InventoryAdjustment = require("../models/inventoryAdjustment");
 
 const helper = require("./helper.js");
+const verifyEmail = require("../emails/verifyEmail.js");
 
 const bcrypt = require("bcryptjs");
+const mailgun = require("mailgun-js")({apiKey: process.env.MG_SUBLINE_APIKEY, domain: "mail.thesubline.net"});
 
 module.exports = {
     /*
-    POST - Create a new merchant with no POS system1
+    POST - Create a new merchant with no POS system
     req.body = {
         name: retaurant name,
         email: registration email,
@@ -169,5 +171,41 @@ module.exports = {
                 }
                 return res.json("ERROR: UNABLE TO UPDATE YOUR PASSWORD");
             });
+    },
+
+    /*
+    PUT: Update merchant data
+    req.body = {
+        email: String (merchant email address)
+    },
+    response = Merchant
+    */
+    updateData: function(req, res){
+        if(req.body.email !== res.locals.merchant.email){
+            res.locals.merchant.email = req.body.email;
+            res.locals.merchant.status.push("unverified");
+
+            const mailgunData = {
+                from: "The Subline <clientsupport@thesubline.net>",
+                to: res.locals.merchant.email,
+                subject: "Email Verification",
+                html: verifyEmail({
+                    name: res.locals.merchant.name,
+                    link: `${process.env.SITE}/verify/${res.locals.merchant._id}/${res.locals.merchant.verifyId}`
+                })
+            };
+            mailgun.messages().send(mailgunData, (err, body)=>{console.log(err)});
+        }
+
+        res.locals.merchant.save()
+            .then((merchant)=>{
+                return res.json(merchant);
+            })
+            .catch((err)=>{
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
+                }
+                return res.json("ERROR: UNABLE TO UPDATE YOUR PASSWORD");
+            });
     }
 }

+ 1 - 0
routes.js

@@ -26,6 +26,7 @@ module.exports = function(app){
     app.post("/merchant/create/none", merchantData.createMerchantNone);
     app.put("/merchant/ingredients/update", session, merchantData.updateIngredientQuantities); //also updates some data in ingredients
     app.post("/merchant/password", merchantData.updatePassword); //TODO: change to work with session
+    app.put("/merchant/update", session, merchantData.updateData);
 
     //Ingredients
     app.post("/ingredients/create", session, ingredientData.createIngredient);  //also adds to merchant

+ 2 - 2
views/dashboardPage/bundle.js

@@ -2975,14 +2975,14 @@ let account = {
 
     updateData: function(){
         let data = {
-            email: document.getElementById("accountemail").value
+            email: document.getElementById("accountEmail").value
         }
 
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
 
         fetch("/merchant/update", {
-            method: "post",
+            method: "put",
             headers: {
                 "Content-Type": "application/json;charset=utf-8"
             },

+ 2 - 2
views/dashboardPage/js/strands/account.js

@@ -25,14 +25,14 @@ let account = {
 
     updateData: function(){
         let data = {
-            email: document.getElementById("accountemail").value
+            email: document.getElementById("accountEmail").value
         }
 
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
 
         fetch("/merchant/update", {
-            method: "post",
+            method: "put",
             headers: {
                 "Content-Type": "application/json;charset=utf-8"
             },