Ver Fonte

Merge branch 'accountStrand' into development

Lee Morgan há 6 anos atrás
pai
commit
d2234989a3

+ 70 - 1
.gitignore

@@ -251,7 +251,12 @@ module.exports = {
             name: data.name,
             email: data.email,
             password: hash,
-            pos: "none"
+            pos: "none",
+            accountStatus: {
+                status: "valid",
+                //working here
+            }
+
         });
 
         for(let item of data.inventory){
@@ -686,5 +691,69 @@ module.exports = {
 
                 return res.json(errorMessage);
             });
+    },
+
+    //POST - Update merchant information
+    //Inputs:
+    //  req.body.name: name update
+    //  req.body.email: email update
+    //Returns: Nothing
+    updateMerchant: function(req, res){
+        if(!req.session.user){
+            req.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                merchant.name = req.body.name;
+                merchant.email = req.body.email;
+                merchant.save()
+                    .then((updatedMerchant)=>{
+                        return res.json({});
+                    })
+                    .catch((err)=>{
+                        return res.json("Error: unable to save merchant data");
+                    });
+            })
+            .catch((err)=>{
+                return res.json("Error: unable to retrieve merchant data");
+            });
+    },
+
+    //POST - Update merchant password
+    //Inputs:
+    //  req.body.oldPass:  current merchant password (supposedly)
+    //  req.body.newPass:  replacement password
+    //Returns: Nothing
+    updatePassword: function(req, res){
+        if(!req.session.user){
+            req.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                bcrypt.compare(req.body.oldPass, merchant.password, (err, result)=>{
+                    if(result){
+                        let salt = bcrypt.genSaltSync(10);
+                        let hash = bcrypt.hashSync(req.body.newPass, salt);
+
+                        merchant.password = hash;
+                        merchant.save()
+                            .then((updatedMerchant)=>{
+                                return res.json({});
+                            })
+                            .catch((err)=>{
+                                return res.json("Error: Unable to save new password");
+                            });
+                    }else{
+                        return res.json("Error: old password does not match current password");
+                    }
+                });
+            })
+            .catch((err)=>{
+                return res.json("Error: Unable to retrieve merchant data");
+            });
     }
 }

+ 2 - 0
controllers/otherData.js

@@ -22,6 +22,8 @@ module.exports = function(app){
     app.post("/merchant/recipes/ingredients/create", merchantData.addRecipeIngredient);
     app.post("/merchant/recipes/ingredients/update", merchantData.updateRecipeIngredient);
     app.post("/merchant/recipes/ingredients/remove", merchantData.removeRecipeIngredient);
+    app.post("/merchant/update", merchantData.updateMerchant);
+    app.post("/merchant/password", merchantData.updatePassword);
 
     //Ingredients
     app.get("/ingredients", ingredientData.getIngredients);

+ 88 - 0
views/informationPage/information.css

@@ -2,5 +2,93 @@ window.accountObj = {
     display: function(){
         clearScreen();
         document.querySelector("#accountStrand").style.display = "flex";
+    },
+
+    editAccount: function(){
+        event.preventDefault();
+
+        document.querySelector("#accountDisplay").style.display = "none";
+        document.querySelector("#accountEdit").style.display = "flex";
+    },
+
+    updateAccount: function(){
+        event.preventDefault();
+
+        let data = {
+            name: document.querySelector("#accountName").value,
+            email: document.querySelector("#accountEmail").value
+        }
+
+        if(validator.isSanitary(name)){
+            axios.post("/merchant/update", data)
+                .then((response)=>{
+                    if(typeof(response.data) === "string"){
+                        banner.createError(response.data);
+                    }else{
+                        merchant.name = data.name;
+                        merchant.email = data.email;
+
+                        document.querySelector("#accountDisplay").style.display = "flex";
+                        document.querySelector("#accountEdit").style.display = "none";
+
+                        let labels = document.querySelectorAll("#accountDisplay label");
+                        labels[0].children[0].innerText = merchant.name;
+                        labels[1].children[0].innerText = merchant.email;
+                    }
+                })
+                .catch((err)=>{
+                    banner.createError("Error: Your data could not be updated");
+                });
+        }
+    },
+
+    editAccountCancel: function(){
+        event.preventDefault();
+
+        document.querySelector("#accountDisplay").style.display = "flex";
+        document.querySelector("#accountEdit").style.display = "none";
+    },
+
+    editPassword: function(){
+        document.querySelector("#passwordEdit").style.display = "flex";
+        document.querySelector("#accountStrand > button").style.display = "none";
+    },
+
+    updatePassword: function(){
+        event.preventDefault();
+
+        let oldPass = document.querySelector("#oldPass").value;
+        let newPass = document.querySelector("#newPass").value;
+        let confirmNewPass = document.querySelector("#confirmNewPass").value;
+
+        if(validator.merchant.password(newPass, confirmNewPass)){
+            axios.post("/merchant/password", {oldPass: oldPass, newPass: newPass})
+                .then((response)=>{
+                    if(typeof(response.data) === "string"){
+                        banner.createError(response.data);
+                    }
+
+                    document.querySelector("#oldPass").value = "";
+                    document.querySelector("#newPass").value = "";
+                    document.querySelector("#confirmNewPass").value = "";
+
+                    document.querySelector("#passwordEdit").style.display = "none";
+                    document.querySelector("#accountStrand > button").style.display = "block";
+                })
+                .catch((err)=>{
+                    banner.createError("Error: please refresh page to check for updates");
+                });
+        }
+    },
+
+    editPasswordCancel: function(){
+        event.preventDefault();
+
+        document.querySelector("#oldPass").value = "";
+        document.querySelector("#newPass").value = "";
+        document.querySelector("#confirmNewPass").value = "";
+
+        document.querySelector("#passwordEdit").style.display = "none";
+        document.querySelector("#accountStrand > button").style.display = "block";
     }
 }

+ 8 - 0
views/inventoryPage/addIngredient.js

@@ -87,6 +87,14 @@
         display: flex;
     }
 
+    #accountEdit{
+        display: none;
+    }
+
+    #passwordEdit{
+        display: none;
+    }
+
 /* Add Ingredient Action */
 #addIngredientAction{
     flex-direction: column;

+ 49 - 1
views/inventoryPage/inventory.ejs

@@ -16,6 +16,7 @@
 
         <strand-selector></strand-selector>
 
+
         <div id="inventoryStrand" class="strand">
             <div class="options">
                 <button class="button" onclick="addIngredientObj.display()">Add Ingredient</button>
@@ -43,6 +44,7 @@
             </table>
         </div>
 
+
         <div id="recipesStrand" class="strand">
             <div>
                 <% if(merchant.pos !== "none"){ %>
@@ -53,8 +55,9 @@
             <div id="recipesContainer"></div>
         </div>
 
+
         <div id="accountStrand" class="strand">
-            <form onsubmit="accountObj.submitForm()">
+            <form id="accountDisplay" onsubmit="accountObj.editAccount()">
                 <label>Name:&nbsp;&nbsp;
                     <p><%=merchant.name%></p>
                 </label>
@@ -62,9 +65,51 @@
                 <label>Email:&nbsp;&nbsp;
                     <p><%=merchant.email%></p>
                 </label>
+
+                <button class="button" onclick="accountObj.editAccount()">Edit</button>
+            </form>
+
+            <form id="accountEdit" onsubmit="accountObj.updateAccount()">
+                <label>Name:&nbsp;&nbsp;
+                    <input id="accountName" type="text" value="<%=merchant.name%>">
+                </label>
+
+                <label>Email:&nbsp;&nbsp;
+                    <input id="accountEmail" type="email" value="<%=merchant.email%>">
+                </label>
+
+                <div class="buttonBox">
+                    <button class="button" onclick="accountObj.updateAccount()">Save</button>
+
+                    <button class="button" onclick="accountObj.editAccountCancel()">Cancel</button>
+                </div>
+            </form>
+
+            <form id="passwordEdit" onsubmit="accountObj.updatePassword()">
+                <label>Old password:
+                    <input id="oldPass" type="password" required>
+                </label>
+
+                <label>New password:
+                    <input id="newPass" type="password" required>
+                </label>
+
+                <label>Confirm new password:
+                    <input id="confirmNewPass" type="password" required>
+                </label>
+
+
+                <div class="buttonBox">
+                    <input class="button" type="submit" value="Submit">
+
+                    <button class="button" onclick="accountObj.editPasswordCancel()">Cancel</button>
+                </div>
             </form>
+
+            <button class="button" onclick="accountObj.editPassword()">Change password</button>
         </div>
 
+
         <div id="addIngredientAction" class="action">
             <div class="container">
                 <form onsubmit="addIngredientObj.submitAdd()">
@@ -105,6 +150,7 @@
             </div>
         </div>
 
+
         <div id="enterTransactionsAction" class="action">
             <h1>Enter all sales</h1>
             <h3>Last updated: <span id="updated"></span></h3>
@@ -122,6 +168,7 @@
             <button class="button" onclick="enterTransactionsObj.submit()">Submit</button>
         </div>
 
+
         <div id="enterPurchaseAction" class="action">
             <h1>Enter Purchases</h1>
             
@@ -138,6 +185,7 @@
             <button class="button" onclick="enterPurchaseObj.submit()">Submit</button>
         </div>
 
+
         <div id="singleRecipeAction" class="action">
             <h2 id="recipeName"></h2>
 

+ 5 - 0
views/inventoryPage/inventory.js

@@ -91,6 +91,11 @@ form{
     transition: 0.3s; 
 }
 
+.buttonBox{
+    display: flex;
+    justify-content: space-around;
+}
+
     .button-small:hover{
         background: rgb(0, 27, 45);
         color: rgb(240, 252, 255);

+ 0 - 0
views/shared/validation.js