Browse Source

Recipes can now be uploaded via admin page.

Lee Morgan 5 years ago
parent
commit
90e19bfc0a
2 changed files with 44 additions and 1 deletions
  1. 43 1
      controllers/admin.js
  2. 1 0
      tmp/tmp-3-1626615775947

+ 43 - 1
controllers/admin.js

@@ -1,5 +1,6 @@
 const Merchant = require("../models/merchant.js");
 const Ingredient = require("../models/ingredient.js");
+const Recipe = require("../models/recipe.js");
 const helper = require("./helper.js");
 
 const fs = require("fs");
@@ -20,6 +21,7 @@ module.exports = {
         if(req.body.password !== process.env.ADMIN_PASS) return res.json("bad password");
 
         Merchant.findOne({_id: req.body.id})
+            .populate("inventory.ingredient")
             .then((merchant)=>{
                 //Ingredients
                 let newIngredients = [];
@@ -56,9 +58,49 @@ module.exports = {
                     }
                 }
 
+                let indexIngredients = ()=>{
+                    let ingredients = {};
+                    for(let i = 0; i < merchant.inventory.length; i++){
+                        ingredients[merchant.inventory[i].ingredient.name] = merchant.inventory[i].ingredient;
+                    }
+                    return ingredients;
+                }
+
+                //Recipes
+                let newRecipes = [];
+                if(req.files.recipes !== undefined){
+                    let recipeData = fs.readFileSync(req.files.recipes.tempFilePath).toString();
+                    fs.unlink(req.files.recipes.tempFilePath, ()=>{});
+                    recipeData = recipeData.split("\n");
+
+                    let ingredientIndices = indexIngredients();
+                    for(let i = 0; i < recipeData.length; i++){
+                        if(recipeData[i] === "") continue;
+                        let data = recipeData[i].split(",");
+                        let recipe = new Recipe({
+                            merchant: merchant._id,
+                            name: data[0],
+                            price: parseInt(parseFloat(data[1]) * 100),
+                            category: data[2],
+                            ingredients: []
+                        });
+
+                        for(let j = 3; j < data.length; j+=3){
+                            recipe.ingredients.push({
+                                ingredient: ingredientIndices[data[j]],
+                                quantity: helper.convertQuantityToBaseUnit(parseFloat(data[j+1]), data[j+2])
+                            });
+                        }
+
+                        merchant.recipes.push(recipe);
+                        newRecipes.push(recipe);
+                    }
+                }
+
                 return Promise.all([
                     merchant.save(),
-                    Ingredient.create(newIngredients)
+                    Ingredient.create(newIngredients),
+                    Recipe.create(newRecipes)
                 ]);
             })
             .then((response)=>{

+ 1 - 0
tmp/tmp-3-1626615775947

@@ -0,0 +1 @@
+Recipe One,1.11,Stuffs,ingredient one,2,ml,5