Quellcode durchsuchen

Add custom mongoose validation for merchant.

Lee Morgan vor 5 Jahren
Ursprung
Commit
103b2ec703
5 geänderte Dateien mit 89 neuen und 57 gelöschten Zeilen
  1. 14 0
      controllers/helper.js
  2. 38 30
      controllers/merchantData.js
  3. 0 14
      controllers/validator.js
  4. 13 10
      models/ingredient.js
  5. 24 3
      models/merchant.js

+ 14 - 0
controllers/helper.js

@@ -244,5 +244,19 @@ module.exports = {
         }
         
         return result;
+    },
+
+    isSanitary: function(strings){
+        let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
+
+        for(let i = 0; i < strings.length; i++){
+            for(let j = 0; j < disallowed.length; j++){
+                if(strings[i].includes(disallowed[j])){
+                    return false;
+                }
+            }
+        }
+
+        return true;
     }
 }

+ 38 - 30
controllers/merchantData.js

@@ -20,43 +20,51 @@ module.exports = {
     Redirects to /dashboard
     */
     createMerchantNone: async function(req, res){
-        let validation =  await validator.merchant(req.body);
-        if(validation !== true){
-            req.session.error = validation;
+        if(req.body.password.length < 10){
+            req.session.error = "PASSWORD MUST CONTAIN AT LEAST 10 CHARACTERS";
             return res.redirect("/");
         }
 
-        if(req.body.password === req.body.confirmPassword){
-            let salt = bcrypt.genSaltSync(10);
-            let hash = bcrypt.hashSync(req.body.password, salt);
-
-            let merchant = new Merchant({
-                name: req.body.name,
-                email: req.body.email.toLowerCase(),
-                password: hash,
-                pos: "none",
-                lastUpdatedTime: Date.now(),
-                createdAt: Date.now(),
-                status: ["unverified"],
-                inventory: [],
-                recipes: [],
-                verifyId: helper.generateId(15)
-            });
-
-            merchant.save()
-                .then((merchant)=>{
-                    return res.redirect(`/verify/email/${merchant._id}`);
-                })
-                .catch((err)=>{
-                    req.session.error = "ERROR: UNABLE TO CREATE ACCOUNT AT THIS TIME";
-
-                    return res.redirect("/");
-                });
-        }else{
+        if(req.body.password !== req.body.confirmPassword){
             req.session.error = "PASSWORDS DO NOT MATCH";
+            return res.redirect("/");
+        }
 
+        const merchantFind = await Merchant.findOne({email: req.body.email.toLowerCase()});
+        if(merchantFind !== null){
+            req.session.error = "USER WITH THIS EMAIL ADDRESS ALREADY EXISTS";
             return res.redirect("/");
         }
+
+        let salt = bcrypt.genSaltSync(10);
+        let hash = bcrypt.hashSync(req.body.password, salt);
+
+        let merchant = new Merchant({
+            name: req.body.name,
+            email: req.body.email.toLowerCase(),
+            password: hash,
+            pos: "none",
+            lastUpdatedTime: Date.now(),
+            createdAt: Date.now(),
+            status: ["unverified"],
+            inventory: [],
+            recipes: [],
+            verifyId: helper.generateId(15)
+        });
+
+        merchant.save()
+            .then((merchant)=>{
+                return res.redirect(`/verify/email/${merchant._id}`);
+            })
+            .catch((err)=>{
+                if(err.errors.name.properties.type === "user defined"){
+                    req.session.error = err.errors.name.properties.message;
+                }else{
+                    req.session.error = "ERROR: UNABLE TO CREATE ACCOUNT AT THIS TIME";
+                }
+                
+                return res.redirect("/");
+            });
     },
 
     /*

+ 0 - 14
controllers/validator.js

@@ -142,20 +142,6 @@ module.exports = {
             }
         }
 
-        return true;
-    },
-
-    isSanitary: function(strings){
-        let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
-
-        for(let i = 0; i < strings.length; i++){
-            for(let j = 0; j < disallowed.length; j++){
-                if(strings[i].includes(disallowed[j])){
-                    return false;
-                }
-            }
-        }
-
         return true;
     }
 }

+ 13 - 10
models/ingredient.js

@@ -1,28 +1,31 @@
+const helper = require("../controllers/helper.js");
+
 const mongoose = require("mongoose");
 
+let sanitary = (value)=>{
+    return helper.isSanitary(value);
+}
+
 const IngredientSchema = new mongoose.Schema({
     name: {
         type: String,
         minlength: 2,
-        required: true
+        required: true,
+        validate: {
+
+        }
     },
     category: {
         type: String,
-        minlength: 3,
+        minlength: 2,
         required: true
     },
     unitType: {
         type: String,
         required: true
     },
-    specialUnit: {
-        type: String,
-        required: false
-    },
-    unitSize:{
-        type: Number,
-        required: false
-    }
+    specialUnit: String,
+    unitSize: String
 });
 
 module.exports = mongoose.model("Ingredient", IngredientSchema);

+ 24 - 3
models/merchant.js

@@ -1,14 +1,35 @@
+const helper = require("../controllers/helper.js");
+
 const mongoose = require("mongoose");
 
+let nameSanitary = (value)=>{
+    return helper.isSanitary(value);
+}
+
+let emailValid = (value)=>{
+    /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value);
+}
+
 const MerchantSchema = new mongoose.Schema({
     name: {
         type: String,
-        required: true
+        required: true,
+        validate: {
+            validator: nameSanitary,
+            message: "NAME CONTAINS ILLEGAL CHARACTERS"
+        }
+    },
+    email: {
+        type: String,
+        required: true,
+        validate: {
+            validator: emailValid,
+            message: "INVALID EMAIL ADDRESS"
+        }
     },
-    email: String,
     password: {
         type: String,
-        minlength: 15
+        required: true,
     },
     pos: {
         type: String,