Преглед изворни кода

Fixed an innocuous error message in the analytics strand.
It appeared when you opened the page but had no ingredients or no transactions.

Lee Morgan пре 5 година
родитељ
комит
00f4a5a672

+ 39 - 11
controllers/emailVerification.js

@@ -2,6 +2,7 @@ const Merchant = require("../models/merchant.js");
 
 const mailgun = require("mailgun-js")({apiKey: process.env.MG_SUBLINE_APIKEY, domain: "mail.thesubline.net"});
 const verifyEmail = require("../emails/verifyEmail.js");
+const { db } = require("../models/merchant.js");
 
 module.exports = {
     sendVerifyEmail: function(req, res){
@@ -13,13 +14,13 @@ module.exports = {
                     subject: "Email verification",
                     html: verifyEmail({
                         name: merchant.name,
-                        link: `${process.env.SITE}/verify/${merchant._id}`,
-                        code: merchant.verifyId
+                        link: `${process.env.SITE}/verify/${merchant._id}/${merchant.verifyId}`,
                     })
                 };
                 mailgun.messages().send(mailgunData, (err, body)=>{});
 
-                return res.redirect(`/verify/${merchant._id}`);
+
+                return res.render(`verifyPage/verify`, {id: merchant._id, email: merchant.email});
             })
             .catch((err)=>{
                 req.session.error = "ERROR: UNABLE TO SEND VERIFICATION EMAIL";
@@ -27,17 +28,40 @@ module.exports = {
             });
     },
 
-    verifyPage: function(req, res){
-        return res.render("verifyPage/verify", {id: req.params.id});
+    resendEmail: function(req, res){
+        Merchant.findOne({email: req.body.email.toLowerCase()})
+            .then((merchant)=>{
+                if(merchant){
+                    throw "USER WITH THIS EMAIL ADDRESS ALREADY EXISTS";
+                }
+
+                return Merchant.findOne({_id: req.body.id});
+            })
+            .then((merchant)=>{
+                merchant.email = req.body.email.toLowerCase();
+
+                return merchant.save();
+            })
+            .then((merchant)=>{
+                return res.redirect(`/verify/email/${merchant._id}`);
+            })
+            .catch((err)=>{
+                if(typeof(err) === "string"){
+                    req.session.error = err;
+                }else if(err.name === "ValidationError"){
+                    req.session.error = err.errors[Object.keys(err.errors)[0]].properties.message;
+                }else{
+                    req.session.error = "ERROR: UNABLE TO CHANGE YOUR EMAIL ADDRESS";
+                }
+                return res.redirect("/");
+            });
     },
 
     verify: function(req, res){
-        Merchant.findOne({_id: req.body.id})
+        Merchant.findOne({_id: req.params.id})
             .then((merchant)=>{
-                if(req.body.code !== merchant.verifyId){
-                    req.session.error = "INCORRECT CODE";
-                    
-                    return res.redirect(`/verify/${merchant._id}`);
+                if(req.params.code !== merchant.verifyId){
+                    throw "UNABLE TO VERIFY EMAIL ADDRESS.  INCORRECT LINK";
                 }
 
                 merchant.verifyId = undefined;
@@ -60,7 +84,11 @@ module.exports = {
                 return res.redirect("/dashboard");
             })
             .catch((err)=>{
-                req.session.error = "ERROR: UNABLE TO VERIFY EMAIL ADDRESS";
+                if(typeof(err) === "string"){
+                    req.session.error = err;
+                }else{
+                    req.session.error = "ERROR: UNABLE TO VERIFY EMAIL ADDRESS"
+                }
 
                 return res.redirect("/");
             });

+ 2 - 0
controllers/recipeData.js

@@ -2,6 +2,8 @@ const Recipe = require("../models/recipe.js");
 const Merchant = require("../models/merchant.js");
 const ArchivedRecipe = require("../models/archivedRecipe.js");
 
+const helper = require("./helper.js");
+
 const axios = require("axios");
 const xlsx = require("xlsx");
 const fs = require("fs");

+ 2 - 4
emails/verifyEmail.js

@@ -8,10 +8,8 @@ module.exports = (data)=>{
             </header>
 
             <h1 style="text-align:center;">Email Verification for ${data.name}</h1>
-
-            <p>Please enter the following code on the previous page.  Or use the link below to reopen the page.</p>
-
-            <p>CODE: ${data.code}</p>
+            
+            <p>Use the following link to verify your email:</p>
 
             <p>${data.link}</p>
         </div>

+ 2 - 2
routes.js

@@ -75,8 +75,8 @@ module.exports = function(app){
 
     //Email verification
     app.get("/verify/email/:id", emailVerification.sendVerifyEmail);
-    app.get("/verify/:id", emailVerification.verifyPage);
-    app.post("/verify", emailVerification.verify);
+    app.post("/verify/resend", emailVerification.resendEmail);
+    app.get("/verify/:id/:code", emailVerification.verify);
 
     //Password reset
     app.get("/reset/email", passwordReset.enterEmail);

+ 18 - 5
views/dashboardPage/bundle.js

@@ -3018,8 +3018,8 @@ module.exports = transactionFilter;
 },{}],19:[function(require,module,exports){
 let analytics = {
     isPopulated: false,
-    ingredient: {},
-    recipe: {},
+    ingredient: undefined,
+    recipe: undefined,
     transactionsByDate: [],
 
     display: function(Transaction){
@@ -3038,9 +3038,15 @@ let analytics = {
 
             this.populateButtons();
 
-            this.ingredient = merchant.ingredients[0].ingredient;
-            this.recipe = merchant.recipes[0];
+            let hasIngredients
 
+            if(merchant.ingredients.length > 0){
+                this.ingredient = merchant.ingredients[0].ingredient;
+            }
+            if(merchant.recipes.length > 0){
+                this.recipe = merchant.recipes[0];
+            }
+            
             this.newDates(Transaction);
             
             this.isPopulated = true;
@@ -3115,6 +3121,9 @@ let analytics = {
     },
 
     displayIngredient: function(ingredient){
+        if(this.ingredient === undefined  || this.transactionsByDate.length === 0){
+            return;
+        }
         //break down data into dates and quantities
         let dates = [];
         let quantities = [];
@@ -3187,6 +3196,10 @@ let analytics = {
     },
 
     displayRecipe: function(){
+        if(this.recipes === undefined || this.transactionsByDate.length === 0){
+            return;
+        }
+
         //break down data into dates and quantities
         let dates = [];
         let quantities = [];
@@ -3235,7 +3248,7 @@ let analytics = {
         avg = avg / quantities.length;
 
         document.getElementById("recipeAvgUse").innerText = avg.toFixed(2);
-        document.getElementById("recipeAvgRevenue").innerText = `$${(avg * this.recipe.price).toFixed(2)}`
+        document.getElementById("recipeAvgRevenue").innerText = `$${(avg * this.recipe.price).toFixed(2)}`;
     },
 
     switchDisplay: function(){

+ 18 - 5
views/dashboardPage/js/strands/analytics.js

@@ -1,7 +1,7 @@
 let analytics = {
     isPopulated: false,
-    ingredient: {},
-    recipe: {},
+    ingredient: undefined,
+    recipe: undefined,
     transactionsByDate: [],
 
     display: function(Transaction){
@@ -20,9 +20,15 @@ let analytics = {
 
             this.populateButtons();
 
-            this.ingredient = merchant.ingredients[0].ingredient;
-            this.recipe = merchant.recipes[0];
+            let hasIngredients
 
+            if(merchant.ingredients.length > 0){
+                this.ingredient = merchant.ingredients[0].ingredient;
+            }
+            if(merchant.recipes.length > 0){
+                this.recipe = merchant.recipes[0];
+            }
+            
             this.newDates(Transaction);
             
             this.isPopulated = true;
@@ -97,6 +103,9 @@ let analytics = {
     },
 
     displayIngredient: function(ingredient){
+        if(this.ingredient === undefined  || this.transactionsByDate.length === 0){
+            return;
+        }
         //break down data into dates and quantities
         let dates = [];
         let quantities = [];
@@ -169,6 +178,10 @@ let analytics = {
     },
 
     displayRecipe: function(){
+        if(this.recipes === undefined || this.transactionsByDate.length === 0){
+            return;
+        }
+
         //break down data into dates and quantities
         let dates = [];
         let quantities = [];
@@ -217,7 +230,7 @@ let analytics = {
         avg = avg / quantities.length;
 
         document.getElementById("recipeAvgUse").innerText = avg.toFixed(2);
-        document.getElementById("recipeAvgRevenue").innerText = `$${(avg * this.recipe.price).toFixed(2)}`
+        document.getElementById("recipeAvgRevenue").innerText = `$${(avg * this.recipe.price).toFixed(2)}`;
     },
 
     switchDisplay: function(){

+ 1 - 1
views/verifyPage/verify.css

@@ -19,7 +19,7 @@ body{
     padding: 0;
 }
 
-    .code{
+    .email{
         font-size: 30px;
         margin-bottom: 15px;
         text-align: center;

+ 5 - 9
views/verifyPage/verify.ejs

@@ -12,21 +12,17 @@
     <body>
         <% include ../shared/header %>
 
-        <h1 class="title">Verify your email address</h1>
+        <h1 class="title">An email has been sent to <%=email%>.  Use the link in the email to verify your account.</h1>
 
-        <p class="text">Please enter the 15 character code sent to your email address</p>
+        <p class="text">If you did not recieve an email or need to change your email address, enter it below and click submit.</p>
 
-        <form class="form" action="/verify" method="post">
-            <input class="code" name="code" type="text">
+        <form class="form" action="/verify/resend" method="post">
+            <input class="email" name="email" type="email" required>
 
             <input name="id" type="hidden" value="<%=id%>">
 
-            <input class="button" type="submit" value="VERIFY">
+            <input class="button" type="submit" value="SUBMIT">
         </form>
-
-        <p class="resend">Didn't recieve an email?</p>
-
-        <a href="/verify/email/<%=id%>">Resend email</a>
     </body>
 
     <script>