|
|
@@ -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";
|
|
|
});
|
|
|
}
|