Explorar el Código

Merge branch 'customValidation' into development

Lee Morgan hace 5 años
padre
commit
bf8bc718bb

+ 14 - 0
controllers/helper.js

@@ -268,5 +268,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;
     }
 }

+ 28 - 42
controllers/ingredientData.js

@@ -3,25 +3,11 @@ const Ingredient = require("../models/ingredient");
 const InventoryAdjustment = require("../models/inventoryAdjustment.js");
 
 const helper = require("./helper.js");
-const validator = require("./validator.js");
 
 const xlsx = require("xlsx");
 const fs = require("fs");
 
 module.exports = {
-    //GET - gets a list of all database ingredients
-    //Returns:
-    //  ingredients: list containing all ingredients
-    getIngredients: function(req, res){
-        Ingredient.find()
-            .then((ingredients)=>{
-                return res.json(ingredients);
-            })
-            .catch((err)=>{
-                return res.json("ERROR: UNABLE TO RETRIEVE INGREDIENTS");
-            });
-    },
-
     /*
     POST - create a single ingredient and then add to the merchant
     req.body = {
@@ -42,23 +28,6 @@ module.exports = {
             return res.redirect("/");
         }
 
-        let validation = validator.ingredient(req.body.ingredient);
-        if(validation !== true){
-            return res.json(validation);
-        }
-
-        validation = validator.quantity(req.body.quantity);
-        if(validation !== true){
-            return res.json(validation);
-        }
-
-        if(req.body.ingredient.unitSize){
-            validation = validator.quantity(req.body.ingredient.unitSize);
-            if(validation !== true){
-                return res.json(validation);
-            }
-        }
-
         let newIngredient = {};
         if(req.body.ingredient.specialUnit === "bottle"){
             newIngredient = new Ingredient({
@@ -96,7 +65,13 @@ module.exports = {
                 return res.json(newIngredient);
             })
             .catch((err)=>{
-                return res.json("ERROR: UNABLE TO CREATE NEW INGREDIENT");
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
+                return res.json("ERROR: UNABLE TO CREATE THE INGREDIENT");
             });
     },
 
@@ -116,17 +91,12 @@ module.exports = {
             req.session.error = "MUST BE LOGGED IN TO DO THAT";
             return res.redirect("/");
         }
-
-        const ingredientCheck = validator.ingredient(req.body);
-        if(ingredientCheck !== true){
-            return res.json(ingredientCheck);
-        }
-
         let updatedIngredient = {};
         Ingredient.findOne({_id: req.body.id})
             .then((ingredient)=>{
                 ingredient.name = req.body.name,
                 ingredient.category = req.body.category
+                //TODO: Handle this in the class
                 if(ingredient.specialUnit === "bottle"){
                     ingredient.unitSize = req.body.unitSize;
                 }
@@ -166,7 +136,13 @@ module.exports = {
                 return res.json(updatedIngredient);
             })
             .catch((err)=>{
-                return res.json("ERROR: UNABLE TO UPDATE INGREDIENT");
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
+                return res.json("ERROR: UNABLE TO UDATE THE INGREDIENT");
             });
     },
 
@@ -226,6 +202,7 @@ module.exports = {
                 ingredient.unitSize = helper.convertQuantityToBaseUnit(array[i][locations.bottleSize], array[i][locations.unit]);
             }else{
                 let unitType = "";
+                //TODO: this should probably be in a helper
                 switch(array[i][locations.unit].toLowerCase()){
                     case "g": unitType = "mass"; break;
                     case "kg": unitType = "mass"; break;
@@ -256,11 +233,8 @@ module.exports = {
         }
 
         //Update the database
-        let createdIngredients = [];
         Ingredient.create(ingredients)
             .then((ingredients)=>{
-                createdIngredients = ingredients;
-
                 return Merchant.findOne({_id: req.session.user});
             })
             .then((merchant)=>{
@@ -274,6 +248,12 @@ module.exports = {
                 return res.json(merchantData);
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return "ERROR: UNABLE TO CREATE YOUR INGREDIENTS";
             });
     },
@@ -324,6 +304,12 @@ module.exports = {
                 return res.json({});
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
             });
     },

+ 78 - 49
controllers/merchantData.js

@@ -2,7 +2,6 @@ const Merchant = require("../models/merchant");
 const Recipe = require("../models/recipe");
 const InventoryAdjustment = require("../models/inventoryAdjustment");
 
-const validator = require("./validator.js");
 const helper = require("./helper.js");
 
 const axios = require("axios");
@@ -20,43 +19,50 @@ module.exports = {
     Redirects to /dashboard
     */
     createMerchantNone: async function(req, res){
-        let validation =  await validator.merchant(req.body);
-        if(validation !== true){
-            req.session.error = validation;
-            return res.redirect("/");
+        if(req.body.password.length < 10){
+            throw "PASSWORD MUST CONTAIN AT LEAST 10 CHARACTERS";
         }
 
-        if(req.body.password === req.body.confirmPassword){
-            let salt = bcrypt.genSaltSync(10);
-            let hash = bcrypt.hashSync(req.body.password, salt);
+        if(req.body.password !== req.body.confirmPassword){
+            throw "PASSWORDS DO NOT MATCH";
+        }
 
-            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)
-            });
+        const merchantFind = await Merchant.findOne({email: req.body.email.toLowerCase()});
+        if(merchantFind !== null){
+            throw "USER WITH THIS EMAIL ADDRESS ALREADY EXISTS";
+        }
 
-            merchant.save()
-                .then((merchant)=>{
-                    return res.redirect(`/verify/email/${merchant._id}`);
-                })
-                .catch((err)=>{
+        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(typeof(err) === "string"){
+                    req.session.error = err;
+                }else if(err.name === "ValidationError"){
+                    req.session.error = err.errors.name.properties.message;
+                }else{
                     req.session.error = "ERROR: UNABLE TO CREATE ACCOUNT AT THIS TIME";
-
-                    return res.redirect("/");
-                });
-        }else{
-            req.session.error = "PASSWORDS DO NOT MATCH";
-
-            return res.redirect("/");
-        }
+                }
+                
+                return res.redirect("/");
+            });
     },
 
     /*
@@ -106,7 +112,14 @@ module.exports = {
                 return res.redirect("/dashboard");
             })
             .catch((err)=>{
-                req.session.error = "ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER";
+                if(typeof(err) === "string"){
+                    req.session.error = err;
+                }else if(err.name === "ValidationError"){
+                    req.session.error = err.errors.name.properties.message;
+                }else{
+                    req.session.error = "ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER";
+                }
+                
                 return res.redirect("/");
             });
     },
@@ -187,7 +200,14 @@ module.exports = {
                 return res.redirect("/dashboard");
             })
             .catch((err)=>{
-                banner.createError("ERROR: UNABLE TO CREATE NEW USER AT THIS TIME");
+                if(typeof(err) === "string"){
+                    req.session.error = err;
+                }else if(err.name === "ValidationError"){
+                    req.session.error = err.errors.name.properties.message;
+                }else{
+                    req.session.error = "ERROR: UNABLE TO CREATE NEW USER";
+                }
+                return res.redirect("/");
             });
     },
 
@@ -204,15 +224,8 @@ module.exports = {
             return res.redirect("/");
         }
 
-        for(let i = 0; i < req.body.length; i++){
-            let validation = validator.quantity(req.body[i].quantity);
-            if(validation !== true){
-                return res.json(validation);
-            }
-        }
-
         let adjustments = [];
-        let changedIngredients = []
+        let changedIngredients = [];
         Merchant.findOne({_id: req.session.user})
             .populate("inventory.ingredient")
             .then((merchant)=>{
@@ -245,6 +258,12 @@ module.exports = {
                 return;
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO UPDATE DATA");
             });        
     },
@@ -258,14 +277,16 @@ module.exports = {
     }
     */
     updatePassword: function(req, res){
-        let validation = validator.password(req.body.pass, req.body.confirmPass);
-        if(validation !== true){
-            return res.json(validation);
-        }
-
         Merchant.findOne({password: req.body.hash})
             .then((merchant)=>{
                 if(merchant){
+                    if(req.body.pass.length < 10){
+                        throw "PASSWORD MUST CONTAIN AT LEAST 10 CHARACTERS";
+                    }
+                    if(req.body.pass !== req.body.confirmPass){
+                        throw "PASSWORDS DO NOT MATCH";
+                    }
+
                     let salt = bcrypt.genSaltSync(10);
                     let hash = bcrypt.hashSync(req.body.pass, salt);
 
@@ -281,6 +302,14 @@ module.exports = {
                 req.session.error = "PASSWORD SUCCESSFULLY RESET. PLEASE LOG IN";
                 return res.redirect("/");
             })
-            .catch((err)=>{});
+            .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
+                return res.json("ERROR: UNABLE TO UPDATE YOUR PASSWORD");
+            });
     }
 }

+ 24 - 10
controllers/orderData.js

@@ -2,11 +2,6 @@ const Order = require("../models/order.js");
 const Merchant = require("../models/merchant.js");
 
 const ObjectId = require("mongoose").Types.ObjectId;
-const Validator = require("./validator.js");
-const helper = require("./helper.js");
-
-const xlsx = require("xlsx");
-const fs = require("fs");
 
 module.exports = {
     /*
@@ -45,6 +40,12 @@ module.exports = {
                 return res.json(orders);
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO RETRIEVE YOUR ORDERS");
             });
     },
@@ -91,6 +92,12 @@ module.exports = {
                 return res.json(orders);
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO RETRIEVE YOUR TRANSACTIONS");
             });
     },
@@ -113,11 +120,6 @@ module.exports = {
             return res.redirect("/");
         }
 
-        let validation = Validator.order(req.body);
-        if(validation !== true){
-            return res.json(validation);
-        }
-
         let newOrder = new Order(req.body);
         newOrder.merchant = req.session.user;
         newOrder.save()
@@ -125,6 +127,12 @@ module.exports = {
                 res.json(response);
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO SAVE ORDER");
             });
 
@@ -329,6 +337,12 @@ module.exports = {
                 return merchant.save();
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO REMOVE ORDER");
             });
     }

+ 0 - 5
controllers/otherData.js

@@ -1,13 +1,8 @@
 const Merchant = require("../models/merchant");
 
-const ingredientData = require("./ingredientData.js");
-const recipeData = require("./recipeData.js");
-
 const bcrypt = require("bcryptjs");
 const axios = require("axios");
 const path = require("path");
-const fs = require("fs");
-const xlsx = require("xlsx");
 
 module.exports = {
     /*

+ 7 - 1
controllers/passwordReset.js

@@ -89,7 +89,13 @@ module.exports = {
                 }
             })
             .catch((err)=>{
-                req.session.error = "ERROR: UNABLE TO UPDATE YOUR PASSWORD AT THIS TIME";
+                if(typeof(err) === "string"){
+                    req.session.error = err;
+                }else if(err.name === "ValidationError"){
+                    req.session.error = err.errors.name.properties.message;
+                }else{
+                    req.session.error = "ERROR: UNABLE TO UPDATE YOUR PASSWORD AT THIS TIME";
+                }
                 return res.redirect("/");
             });
     }

+ 38 - 28
controllers/recipeData.js

@@ -2,9 +2,6 @@ const Recipe = require("../models/recipe.js");
 const Merchant = require("../models/merchant.js");
 const ArchivedRecipe = require("../models/archivedRecipe.js");
 
-const validator = require("./validator.js");
-const helper = require("./helper.js");
-
 const axios = require("axios");
 const xlsx = require("xlsx");
 const fs = require("fs");
@@ -28,11 +25,6 @@ module.exports = {
             return res.redirect("/");
         }
 
-        let validation = validator.recipe(req.body);
-        if(validation !== true){
-            return res.json(validation);
-        }
-
         let recipe = new Recipe({
             merchant: req.session.user,
             name: req.body.name,
@@ -40,26 +32,26 @@ module.exports = {
             ingredients: req.body.ingredients
         });
 
-
-        Merchant.findOne({_id: req.session.user})
-            .then((merchant)=>{
-                merchant.recipes.push(recipe);
-                merchant.save()
-                    .catch((err)=>{
-                        return res.json("ERROR: UNABLE TO SAVE RECIPE");
-                    });
-            })
-            .catch((err)=>{
-                return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
-            });
-
         recipe.save()
             .then((newRecipe)=>{
                 return res.json(newRecipe);
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO SAVE INGREDIENT");
             });
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                merchant.recipes.push(recipe);
+                return merchant.save();
+            })
+            .catch((err)=>{});
     },
 
     /*
@@ -80,11 +72,6 @@ module.exports = {
             return res.redirect("/");
         }
 
-        let validation = validator.recipe(req.body);
-        if(validation !== true){
-            return res.json(validation);
-        }
-
         Recipe.findOne({_id: req.body.id})
             .then((recipe)=>{
                 new ArchivedRecipe({
@@ -105,6 +92,12 @@ module.exports = {
                 res.json(recipe);
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO UPDATE RECIPE");
             });
     },
@@ -140,6 +133,12 @@ module.exports = {
                 return res.json({});
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
             });
     },
@@ -206,12 +205,17 @@ module.exports = {
                 return res.json({new: newRecipes, removed: deletedRecipes});
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO RETRIEVE MERCHANT DATA");
             });
     },
 
     updateRecipesSquare: function(req, res){
-        
         if(!req.session.user){
             req.session.error = "Must be logged in to do that";
             return res.redirect("/");
@@ -298,7 +302,13 @@ module.exports = {
                 return res.json({new: newRecipes, removed: merchantRecipes});
             })
             .catch((err)=>{
-                return "ERROR: UNABLE TO RETRIEVE RECIPE DATA FROM SQUARE";
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
+                return res.json("ERROR: UNABLE TO RETRIEVE RECIPE DATA FROM SQUARE");
             });
     },
 

+ 12 - 0
controllers/transactionData.js

@@ -90,6 +90,12 @@ module.exports = {
                 return res.json(response);
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO CREATE NEW TRANSACTION");
             });
     },
@@ -274,6 +280,12 @@ module.exports = {
                 return merchant.save();
             })
             .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
                 return res.json("ERROR: UNABLE TO DELETE THE TRANSACTION");
             });
     },

+ 0 - 161
controllers/validator.js

@@ -1,161 +0,0 @@
-const Merchant = require("../models/merchant.js");
-
-module.exports = {
-    merchant: async function(merchant){
-        if(!this.isSanitary([merchant.name])){
-            return "NAME CONTAINS ILLEGAL CHARACTERS";
-        }
-
-        if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(merchant.email)){
-            return "INVALID EMAIL ADDRESS";
-        }
-
-        let checkMerchant = await Merchant.findOne({email: merchant.email});
-
-        if(checkMerchant){
-            return "AN ACCOUNT WITH THAT EMAIL ADDRESS ALREADY EXISTS";
-        }
-
-        let checkPassword = this.password(merchant.password, merchant.confirmPassword);
-        if(this.password(checkPassword !== true)){
-            return checkPassword;
-        }
-
-        return true;
-    },
-
-    password: function(password, confirmPassword){
-        if(password.length < 10){
-            return "PASSWORD MUST CONTAIN AT LEAST 10 CHARACTERS";
-        }
-
-        if(password !== confirmPassword){
-            return "PASSWORDS DO NOT MATCH";
-        }
-
-        return true;
-    },
-
-    quantity: function(num){
-        if(isNaN(num) || num === ""){
-            return "QUANTITY MUST BE A NUMBER";
-        }
-
-        if(num < 0){
-            return "QUANTITY CANNOT BE A NEGATIVE NUMBER";
-        }
-
-        return true;
-    },
-
-    price: function(price){
-        if(price < 0){
-            return "PRICE CANNOT BE A NEGATIVE NUMBER";
-        }
-
-        if(isNaN(price) || price === ""){
-            return "PRICE MUST BE A NUMBER";
-        }
-
-        return true;
-    },
-
-    /*
-    ingredient = {
-        name: required,
-        category: required,
-        unit: required,
-        quantity: required,
-        specialUnit: optional,
-        unitSize: optional
-    }
-    */
-    ingredient: function(ingredient){
-        if(!this.isSanitary([ingredient.name, ingredient.category])){
-            return "INGREDIENT CONTAINS ILLEGAL CHARACTERS";
-        }
-
-        if(ingredient.specialUnit === "bottle"){
-            let quantityCheck = this.quantity(ingredient.unitSize);
-            if(quantityCheck !== true){
-                return "BOTTLE SIZE MUST BE A NON-NEGATIVE NUMBER";
-            }
-        }
-
-        return true;
-    },
-
-    recipe: function(recipe){
-        if(!this.isSanitary([recipe.name])){
-            return "INGREDIENT CONTAINS ILLEGAL CHARACTERS";
-        }
-
-        let priceCheck = this.price(recipe.price);
-        if(priceCheck !== true){
-            return priceCheck;
-        }
-
-        for(let i = 0; i < recipe.ingredients.length; i++){
-            let checkQuantity = this.quantity(recipe.ingredients[i].quantity);
-            if(checkQuantity !== true){
-                return checkQuantity;
-            }
-        }
-
-        for(let i = 0; i < recipe.ingredients.length; i++){
-            for(let j = i + 1; j < recipe.ingredients.length; j++){
-                if(recipe.ingredients[i].ingredient === recipe.ingredients[j].ingredient){
-                    return "RECIPE CANNOT CONTAIN DUPLICATE INGREDIENTS";
-                }
-            }
-        }
-
-        return true;
-    },
-
-    order: function(order){
-        if(!this.isSanitary([order.name])){
-            return "ORDER NAME CONTAINS ILLEGAL CHARACTERS";
-        }
-
-        if(new Date(order.date) > new Date()){
-            return "DATE CANNOT BE IN THE FUTURE";
-        }
-
-        if(this.quantity(order.taxes) !== true){
-            return "TAXES MUST BE A NON NEGATIVE NUMBER";
-        }
-
-        if(this.quantity(order.fees) !== true){
-            return "FEES MUST BE A NON NEGATIVE NUMBER";
-        }
-
-        for(let i = 0; i < order.ingredients; i++){
-            let quantityCheck = this.quantity(order.ingredients[i].quantity);
-            if(quantityCheck !== true){
-                return quantityCheck;
-            }
-
-            let priceCheck = this.price(order.ingredients[i].price);
-            if(priceCheck !== true){
-                return priceCheck;
-            }
-        }
-
-        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;
-    }
-}

+ 17 - 13
models/ingredient.js

@@ -1,28 +1,32 @@
+const isSanitary = require("../controllers/helper.js").isSanitary;
+
 const mongoose = require("mongoose");
 
 const IngredientSchema = new mongoose.Schema({
     name: {
         type: String,
-        minlength: 2,
-        required: true
+        minlength: [2, "INGREDIENT NAME MUST CONTAIN AT LEAST 2 CHARACTERS"],
+        required: [true, "INGREDIENT NAME IS REQUIRED"],
+        validate: {
+            validator: isSanitary,
+            message: "INGREDIENT NAME CONTAINS ILLEGAL CHARACTERS"
+        }
     },
     category: {
         type: String,
-        minlength: 3,
-        required: true
+        minlength: [2, "INGREDIENT CATEGORY MUST CONTAIN AT LEAST 2 CHARACTERS"],
+        required: [true, "INGREDIENT CATEGORY IS REQUIRED"],
+        validate: {
+            validator: isSanitary,
+            message: "INGREDIENT CATEGORY CONTAINS ILLEGAL CHARACTERS"
+        }
     },
     unitType: {
         type: String,
-        required: true
-    },
-    specialUnit: {
-        type: String,
-        required: false
+        required: [true, "UNIT TYPE IS REQUIRED"]
     },
-    unitSize:{
-        type: Number,
-        required: false
-    }
+    specialUnit: String,
+    unitSize: String
 });
 
 module.exports = mongoose.model("Ingredient", IngredientSchema);

+ 24 - 6
models/merchant.js

@@ -1,14 +1,31 @@
+const isSanitary = require("../controllers/helper.js").isSanitary;
+
 const mongoose = require("mongoose");
 
+let emailValid = (value)=>{
+    /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value);
+}
+
 const MerchantSchema = new mongoose.Schema({
     name: {
         type: String,
-        required: true
+        required: [true, "MERCHANT NAME IS REQUIRED"],
+        validate: {
+            validator: isSanitary,
+            message: "NAME CONTAINS ILLEGAL CHARACTERS"
+        }
+    },
+    email: {
+        type: String,
+        required: [true, "EMAIL IS REQUIRED"],
+        validate: {
+            validator: emailValid,
+            message: "INVALID EMAIL ADDRESS"
+        }
     },
-    email: String,
     password: {
         type: String,
-        minlength: 15
+        required: [true, "MUST PROVIDE A PASSWORD"],
     },
     pos: {
         type: String,
@@ -30,15 +47,16 @@ const MerchantSchema = new mongoose.Schema({
         ingredient: {
             type: mongoose.Schema.Types.ObjectId,
             ref: "Ingredient",
-            required: true
+            required: [true, "MUST PROVIDE THE INGREDIENT"]
         },
         quantity: {
             type: Number,
-            required: true,
+            required: [true, "INGREDIENT QUANTITY IS REQUIRED"],
+            min: [0, "QUANTITY CANNOT BE A NEGATIVE NUMBER"]
         },
         defaultUnit: {
             type: String,
-            required: true
+            required: [true, "INGREDIENT UNIT IS REQUIRED"]
         }
     }],
     recipes: [{

+ 18 - 15
models/order.js

@@ -1,39 +1,42 @@
+const isSanitary = require("../controllers/helper.js").isSanitary;
+
 const mongoose = require("mongoose");
 
 const OrderSchema = new mongoose.Schema({
     merchant: {
         type: mongoose.Schema.Types.ObjectId,
         ref: "Merchant",
-        required: true
+        required: [true, "MUST PROVIDE THE MERCHANT"]
+    },
+    name: {
+        type: String,
+        minlength: [2, "ORDER NAME MUST CONTAIN AT LEAST 2 CHARACTERS"],
+        validate: {
+            validator: isSanitary,
+            message: "ORDER NAME CONTAINS ILLEGAL CHARACTERS"
+        }
     },
-    name: String,
     date: {
         type: Date,
         default: Date.now,
-        required: true
-    },
-    taxes: {
-        type: Number,
-        required: true
-    },
-    fees: {
-        type: Number,
-        required: true
+        required: [true, "MUST PROVIDE A DATE FOR THE ORDER"]
     },
+    taxes: Number,
+    fees: Number,
     ingredients: [{
         ingredient: {
             type: mongoose.Schema.Types.ObjectId,
             ref: "Ingredient",
-            required: true
+            required: [true, "MUST PROVIDE THE INGREDIENT"]
         },
         quantity: {
             type: Number,
-            required: true,
-            min: 0
+            required: [true, "MUST PROVIDE THE QUANTITY FOR EVERY INGREDIENT IN THE ORDER"],
+            min: [0, "INGREDIENT QUANTITY IN AN ORDER CANNOT BE LESS THAN 0"]
         },
         pricePerUnit: {
             type: Number,
-            min: 0
+            min: [0, "PRICE PER UNIT CANNOT BE A NEGATIVE NUMBER"]
         }
     }]
 });

+ 14 - 6
models/recipe.js

@@ -1,3 +1,5 @@
+const isSanitary = require("../controllers/helper.js").isSanitary;
+
 const mongoose = require("mongoose");
 
 const RecipeSchema = new mongoose.Schema({
@@ -5,26 +7,32 @@ const RecipeSchema = new mongoose.Schema({
     merchant: {
         type: mongoose.Schema.Types.ObjectId,
         ref: "Merchant",
-        required: true
+        required: [true, "MERCHANT IS REQUIRED"]
     },
     name: {
         type: String,
-        required: true,
+        minlength: [2, "RECIPE NAME MUST CONTAIN AT LEAST 2 CHARACTERS"],
+        validate: {
+            validator: isSanitary,
+            message: "RECIPE NAME CONTAINS ILLEGAL CHARACTERS"
+        },
+        required: true
     },
     price: {
         type: Number,
-        min: 0
+        min: [0, "PRICE OF RECIPE CANNOT BE A NEGATIVE NUMBER"],
+        required: [true, "RECIPE PRICE IS REQUIRED"]
     },
     ingredients: [{
         ingredient: {
             type: mongoose.Schema.Types.ObjectId,
             ref: "Ingredient",
-            required: [true, "Must provide ingredient"]
+            required: [true, "INGREDIENT IS REQUIRED"]
         },
         quantity: {
             type: Number,
-            min: [0, "Cannot have a negative quantity"],
-            required: [true, "Must provide a quantity"]
+            min: [0, "QUANTITY OF INGREDIENTS CANNOT BE A NEGATIVE NUMBER"],
+            required: [true, "MUST PROVED A QUANTITY FOR ALL INGREDIENTS"]
         }
     }]
 });

+ 0 - 1
routes.js

@@ -26,7 +26,6 @@ module.exports = function(app){
     app.post("/merchant/password", merchantData.updatePassword);
 
     //Ingredients
-    app.get("/ingredients", ingredientData.getIngredients);
     app.post("/ingredients/create", ingredientData.createIngredient);  //also adds to merchant
     app.put("/ingredients/update", ingredientData.updateIngredient);
     app.post("/ingredients/create/spreadsheet", upload.single("ingredients"), ingredientData.createFromSpreadsheet);