فهرست منبع

Update backend ingredient creation.

Lee Morgan 5 سال پیش
والد
کامیت
de71c949df
3فایلهای تغییر یافته به همراه19 افزوده شده و 34 حذف شده
  1. 17 23
      controllers/ingredientData.js
  2. 1 10
      models/ingredient.js
  3. 1 1
      models/merchant.js

+ 17 - 23
controllers/ingredientData.js

@@ -13,38 +13,34 @@ module.exports = {
         ingredient: {
             name: name of ingredient,
             category: category of ingredient,
-            unitType: category for the unit (mass, volume, length)
-        },
-        quantity: quantity of ingredient for current merchant,
-        defaultUnit: default unit of measurement to display
-        convert: {
-            toMass: Number
-            toVolume: Number
-            toLength: Number
+            convert: {
+                toMass: Number
+                toVolume: Number
+                toLength: Number
+            }
         }
+            quantity: quantity of ingredient for current merchant,
+            unit: String
     }
     Returns:
         Same as above, with the _id
     */
     createIngredient: function(req, res){
-        let newIngredient = {...req.body};
-        if(req.body.defaultUnit === "bottle"){
-            newIngredient.ingredient.unitSize = newIngredient.ingredient.unitSize;
-        }
-
-        newIngredient = new Ingredient(newIngredient.ingredient);
-        newIngredient.convert = req.body.convert;
-        newIngredient.ingredients = [];
+        let newIngredient = new Ingredient({
+            name: req.body.ingredient.name,
+            category: req.body.ingredient.category,
+            convert: req.body.ingredient.category,
+            ingredients: []
+        })
         
         newIngredient.save()
             .then((ingredient)=>{
                 newIngredient = {
-                    ingredient: ingredient,
-                    defaultUnit: req.body.defaultUnit
+                    ingredient: ingredient._id,
+                    quantity: req.body.quantity,
+                    unit: req.body.unit
                 }
 
-                newIngredient.quantity = req.body.quantity, req.body.defaultUnit;
-
                 res.locals.merchant.inventory.push(newIngredient);
 
                 return res.locals.merchant.save();
@@ -53,9 +49,7 @@ module.exports = {
                 return res.json(newIngredient);
             })
             .catch((err)=>{
-                if(typeof(err) === "string"){
-                    return res.json(err);
-                }
+                if(typeof(err) === "string") return res.json(err);
                 if(err.name === "ValidationError"){
                     return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
                 }

+ 1 - 10
models/ingredient.js

@@ -21,15 +21,6 @@ const IngredientSchema = new mongoose.Schema({
             message: "INGREDIENT CATEGORY CONTAINS ILLEGAL CHARACTERS"
         }
     },
-    unitType: {
-        type: String,
-        required: [true, "UNIT TYPE IS REQUIRED"]
-    },
-    unitSize: {
-        type: Number,
-        min: [0, "SIZE CANNOT BE A NEGATIVE NUMBER"],
-        required: false
-    },
     convert: {
         toMass: Number,
         toVolume: Number,
@@ -41,7 +32,7 @@ const IngredientSchema = new mongoose.Schema({
             ref: "Ingredient",
             required: true
         },
-        quantity: { //quantity per base unit of parent
+        quantity: {
             type: Number,
             required: true,
             min: 0

+ 1 - 1
models/merchant.js

@@ -46,7 +46,7 @@ const MerchantSchema = new mongoose.Schema({
             type: Number,
             required: [true, "INGREDIENT QUANTITY IS REQUIRED"]
         },
-        defaultUnit: {
+        unit: {
             type: String,
             required: [true, "INGREDIENT UNIT IS REQUIRED"]
         }