Forráskód Böngészése

Add ability to change the owner/business name.

Lee Morgan 5 éve
szülő
commit
304b75e06b

+ 10 - 3
controllers/merchantData.js

@@ -287,10 +287,12 @@ module.exports = {
     /*
     PUT: Update merchant data
     req.body = {
-        email: String (merchant email address)
+        email: String
+        name: String
     },
     response = {
-        email: String (merchant)
+        email: String
+        name: String
     }
     */
     updateData: async function(req, res){
@@ -313,9 +315,14 @@ module.exports = {
             mailgun.messages().send(mailgunData, (err, body)=>{});
         }
 
+        res.locals.owner.name = req.body.name;
+
         res.locals.owner.save()
             .then((owner)=>{
-                return res.json({email: res.locals.owner.email});
+                return res.json({
+                    email: res.locals.owner.email,
+                    name: res.locals.owner.name
+                });
             })
             .catch((err)=>{
                 if(err.name === "ValidationError") return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);

+ 9 - 1
models/owner.js

@@ -1,6 +1,7 @@
 const mongoose = require("mongoose");
 
 let emailValid = require("../validator.js").emailValid;
+let isSanitary = require("../validator.js").isSanitary;
 
 const OwnerSchema = new mongoose.Schema({
     email: {
@@ -12,7 +13,14 @@ const OwnerSchema = new mongoose.Schema({
         },
         index: true
     },
-    name: String,
+    name: {
+        type: String,
+        required: [true, "MUST PROVIDE AN OWNER NAME"],
+        validate: {
+            validator: isSanitary,
+            message: "OWNER NAME CONTAINS ILLEGAL CHARACTERS"
+        }
+    },
     password: {
         type: String,
         required: true

+ 5 - 0
views/dashboardPage/dashboard.css

@@ -609,6 +609,11 @@ ACCOUNT STRAND
                         margin-right: 15px;
                     }
 
+            .settingsSection label{
+                margin-bottom: 10px;
+                display: flex;
+            }
+
 
     #accountUpdate{
         max-width: 500px;

+ 6 - 1
views/dashboardPage/ejs/strands/account.ejs

@@ -8,7 +8,12 @@
             <div class="settingsSectionHeader">
                 <h2>ACCOUNT INFORMATION</h2>
             </div>
-            <label>EMAIL
+
+            <label>BUSINESS NAME:
+                <input id="accountOwnerName" type="text">
+            </label>
+
+            <label>EMAIL:
                 <input id="accountEmail" type="email">
             </label>
 

+ 11 - 3
views/dashboardPage/js/strands/account.js

@@ -4,6 +4,7 @@ let account = {
     display: function(){
         document.getElementById("accountStrandTitle").innerText = merchant.owner.name;
         document.getElementById("accountEmail").value = merchant.owner.email;
+        document.getElementById("accountOwnerName").value = merchant.owner.name;
 
         document.getElementById("accountUpdate").onclick = ()=>{this.updateData()};
         document.getElementById("deleteMerchant").onclick = ()=>{controller.openModal("confirmDeleteMerchant")};
@@ -46,7 +47,8 @@ let account = {
 
     updateData: function(){
         let data = {
-            email: document.getElementById("accountEmail").value
+            email: document.getElementById("accountEmail").value,
+            name: document.getElementById("accountOwnerName").value
         }
 
         let loader = document.getElementById("loaderContainer");
@@ -65,9 +67,15 @@ let account = {
                     controller.createBanner(response, "error");
                 }else{
                     controller.createBanner("DATA UPDATED", "success");
-                    controller.createBanner("YOU MUST VALIDATE YOUR NEW EMAIL ADDRESS BEFORE YOU CAN LOG IN AGAIN", "alert");
+                    if(merchant.owner.email !== response.email){
+                        controller.createBanner("YOU MUST VALIDATE YOUR NEW EMAIL ADDRESS BEFORE YOU CAN LOG IN AGAIN", "alert");
+                        merchant.owner.email = response.email;
+                    }
 
-                    merchant.owner.email = response.email;
+                    merchant.owner.name = response.name;
+
+                    document.getElementById("accountOwnerName").value = merchant.owner.name;
+                    document.getElementById("accountStrandTitle").innerText = merchant.owner.name;
                     document.getElementById("accountEmail").value = merchant.owner.email;
                 }
             })