Просмотр исходного кода

Add fully working front and backend email uniqueness validation.

Lee Morgan 6 лет назад
Родитель
Сommit
34f25d1896

+ 14 - 2
controllers/merchantData.js

@@ -132,10 +132,16 @@ module.exports = {
     //Inputs:
     //Inputs:
     //  req.body.data: All data from frontend in form of merchant model
     //  req.body.data: All data from frontend in form of merchant model
     //Redirect to "/inventory"
     //Redirect to "/inventory"
-    createMerchantClover: function(req, res){
+    createMerchantClover: async function(req, res){
         let data = JSON.parse(req.body.data);
         let data = JSON.parse(req.body.data);
         data.email = data.email.toLowerCase();
         data.email = data.email.toLowerCase();
 
 
+        let merchant = await Merchant.findOne({email: data.email});
+        if(merchant){
+            req.session.error = "Email already in use";
+            return res.redirect("/merchant/new/clover");
+        }
+
         if(data.password.length < 15 || data.password !== data.confirmPassword){
         if(data.password.length < 15 || data.password !== data.confirmPassword){
             req.session.error = "Passwords must match and contain at least 15 characters";
             req.session.error = "Passwords must match and contain at least 15 characters";
             return res.redirect("/");
             return res.redirect("/");
@@ -219,10 +225,16 @@ module.exports = {
     //Inputs:
     //Inputs:
     //  req.body.data: All data from frontend in form of merchant model
     //  req.body.data: All data from frontend in form of merchant model
     //Redirects to "/inventory"
     //Redirects to "/inventory"
-    createMerchantNone: function(req, res){
+    createMerchantNone: async function(req, res){
         let data = JSON.parse(req.body.data);
         let data = JSON.parse(req.body.data);
         data.email = data.email.toLowerCase();
         data.email = data.email.toLowerCase();
 
 
+        let merchantExists = await Merchant.findOne({email: data.email});
+        if(merchantExists){
+            req.session.error = "Email already in use";
+            return res.redirect("/merchant/new/none");
+        }
+
         if(data.password.length < 15 || data.password !== data.confirmPassword){
         if(data.password.length < 15 || data.password !== data.confirmPassword){
             req.session.error = "Passwords must match and contain at least 15 characters";
             req.session.error = "Passwords must match and contain at least 15 characters";
             return res.redirect("/");
             return res.redirect("/");

+ 18 - 2
controllers/renderer.js

@@ -136,12 +136,20 @@ module.exports = {
     //Renders merchantSetupPage
     //Renders merchantSetupPage
     merchantSetupClover: function(req, res){
     merchantSetupClover: function(req, res){
         req.session.posId = "YHVPCQMVB1P81";
         req.session.posId = "YHVPCQMVB1P81";
+
+        let errorMessage = {};
+        if(req.session.error){
+            errorMessage = req.session.error;
+            req.session.error = undefined;
+        }else{
+            errorMessage = undefined;
+        }
         
         
         Ingredient.find()
         Ingredient.find()
             .then((ingredients)=>{
             .then((ingredients)=>{
                 axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}/items?access_token=${token}`)
                 axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}/items?access_token=${token}`)
                     .then((recipes)=>{
                     .then((recipes)=>{
-                        return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
+                        return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data, error: errorMessage});
                     })
                     })
                     .catch((err)=>{
                     .catch((err)=>{
                         req.session.error = "We were unable to retrieve your data from Clover"
                         req.session.error = "We were unable to retrieve your data from Clover"
@@ -174,9 +182,17 @@ module.exports = {
     //  recipes: null (to signify non-post client)
     //  recipes: null (to signify non-post client)
     //Renders merchantSetupPage
     //Renders merchantSetupPage
     merchantSetupNone: function(req, res){
     merchantSetupNone: function(req, res){
+        let errorMessage = {};
+        if(req.session.error){
+            errorMessage = req.session.error;
+            req.session.error = undefined;
+        }else{
+            errorMessage = undefined;
+        }
+
         Ingredient.find()
         Ingredient.find()
             .then((ingredients)=>{
             .then((ingredients)=>{
-                return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null});
+                return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null, error: errorMessage});
             })
             })
             .catch((err)=>{
             .catch((err)=>{
                 req.session.error = "Data for new merchants could not be retrieved";
                 req.session.error = "Data for new merchants could not be retrieved";

+ 4 - 0
views/merchantSetupPage/controller.js

@@ -9,6 +9,10 @@ let controller = {
     createRecipesStrand: document.querySelector("#createRecipesStrand"),
     createRecipesStrand: document.querySelector("#createRecipesStrand"),
 
 
     onStart: function(){
     onStart: function(){
+        if(error){
+            banner.createError(error);
+        }
+        
         basicInfoObj.display();
         basicInfoObj.display();
     },
     },
 
 

+ 7 - 0
views/merchantSetupPage/merchantSetup.ejs

@@ -114,6 +114,13 @@
             <button id="previous" onclick="createRecipesObj.changeRecipe(-1)">Previous Recipe</button>
             <button id="previous" onclick="createRecipesObj.changeRecipe(-1)">Previous Recipe</button>
         </div>
         </div>
 
 
+        <script>
+            <% if(locals.error){ %>
+                let error = <%- JSON.stringify(error) %>;
+            <% }else{ %>
+                let error = undefined;
+            <% } %>
+        </script>
         <script>
         <script>
             let ingredients = <%- JSON.stringify(ingredients) %>;
             let ingredients = <%- JSON.stringify(ingredients) %>;
             let recipes = <%- JSON.stringify(recipes) %>;
             let recipes = <%- JSON.stringify(recipes) %>;