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

Update ingredientData.js to display validation error messages to user.

Lee Morgan 5 лет назад
Родитель
Сommit
9942852c16
2 измененных файлов с 25 добавлено и 35 удалено
  1. 25 34
      controllers/ingredientData.js
  2. 0 1
      routes.js

+ 25 - 34
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");
             });
     },
 
@@ -164,7 +139,10 @@ module.exports = {
                 if(typeof(err) === "string"){
                     return res.json(err);
                 }
-                return res.json(err.errors.name.properties.message);
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors.name.properties.message);
+                }
+                return res.json("ERROR: UNABLE TO UDATE THE INGREDIENT");
             });
     },
 
@@ -193,7 +171,13 @@ module.exports = {
                 return res.json({});
             })
             .catch((err)=>{
-                return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
+                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 THE INGREDIENT");
             });
     },
 
@@ -253,6 +237,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;
@@ -298,6 +283,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";
             });
     }

+ 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.delete("/ingredients/remove/:id", ingredientData.removeIngredient);