Эх сурвалжийг харах

Add ability to upload recipes through a spreadsheet.

Lee Morgan 5 жил өмнө
parent
commit
8432235631

+ 74 - 35
controllers/recipeData.js

@@ -3,10 +3,12 @@ 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");
+const ObjectId = require("mongoose").ObjectId;
 
 module.exports = {
     /*
@@ -307,7 +309,6 @@ module.exports = {
             return res.redirect("/");
         }
 
-        console.log(req.file);
         //read file, get the correct sheet, create array from sheet
         let workbook = xlsx.readFile(req.file.path);
         fs.unlink(req.file.path, ()=>{});
@@ -331,42 +332,80 @@ module.exports = {
             switch(array[0][i].toLowerCase()){
                 case "name": locations.name = i; break;
                 case "price": locations.price = i; break;
+                case "ingredients": locations.ingredient = i; break;
+                case "ingredient amount": locations.amount = i; break;
             }
         }
 
-        //Create Recipes
-        // let merchant = {};
-        // let newRecipes = [];
-        // return Merchant.findOne({_id: req.session.user})
-        //     .then((response)=>{
-        //         merchant = response;
-
-        //         let recipes = [];
-        //         for(let i = 1; i < array.length; i++){
-        //             recipes.push({
-        //                 name: array[i][locations.name],
-        //                 price: parseInt(array[i][locations.price] * 100),
-        //                 merchant: merchant
-        //             });
-        //         }
-
-        //         return Recipe.create(recipes)
-        //     })
-        //     .then((recipes)=>{
-        //         for(let i = 0; i < recipes.length; i++){
-        //             merchant.recipes.push(recipes[i]);
-
-        //             recipes[i].merchant = undefined;
-        //             newRecipes.push(recipes[i]);
-        //         }
-
-        //         return merchant.save();
-        //     })
-        //     .then((merchant)=>{
-        //         return newRecipes;
-        //     })
-        //     .catch((err)=>{
-        //         return "ERROR: UNABLE TO CREATE RECIPES";
-        //     });
+        let merchant = {};
+        let ingredients = [];
+        Merchant.findOne({_id: req.session.user})
+            .populate("inventory.ingredient")
+            .then((response)=>{
+                merchant = response;
+
+                for(let i = 0; i < merchant.inventory.length; i++){
+                    ingredients.push({
+                        id: merchant.inventory[i].ingredient._id,
+                        name: merchant.inventory[i].ingredient.name.toLowerCase(),
+                        unit: merchant.inventory[i].defaultUnit
+                    });
+                }
+
+                let recipes = [];
+                let currentRecipe = {};
+                for(let i = 1; i < array.length; i++){
+                    if(array[i].length === 0){
+                        continue;
+                    }
+
+                    if(array[i][locations.name] !== undefined){
+                        currentRecipe = {
+                            merchant: req.session.user,
+                            name: array[i][locations.name],
+                            price: parseInt(array[i][locations.price] * 100),
+                            ingredients: []
+                        }
+
+                        recipes.push(currentRecipe);
+                    }
+
+                    let exists = false;
+                    for(let j = 0; j < ingredients.length; j++){
+                        if(ingredients[j].name === array[i][locations.ingredient]){
+                            currentRecipe.ingredients.push({
+                                ingredient: ingredients[j].id,
+                                quantity: helper.convertQuantityToBaseUnit(array[i][locations.amount], ingredients[j].unit)
+                            });
+
+                            exists = true;
+                            break;
+                        }
+                    }
+
+                    if(exists === false){
+                        throw `CANNOT FIND INGREDIENT ${array[i][locations.ingredient]} FROM RECIPE ${array[i][locations.name]}`;
+                    }
+                }
+                
+                return Recipe.create(recipes);
+            })
+            .then((response)=>{
+                recipes = response;
+
+                for(let i = 0; i < recipes.length; i++){
+                    merchant.recipes.push(recipes[i]._id);
+                }
+
+                return merchant.save();
+            })
+            .then((merchant)=>{
+                return res.json(recipes);
+            })
+            .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+            });
     }
 }

BIN
uploads/5fab6c3092ea20ee989ee97e796327e8


+ 23 - 11
views/dashboardPage/bundle.js

@@ -125,6 +125,8 @@ class Ingredient{
 
 module.exports = Ingredient;
 },{}],2:[function(require,module,exports){
+const Recipe = require("./Recipe");
+
 class MerchantIngredient{
     constructor(ingredient, quantity){
         if(quantity < 0){
@@ -250,7 +252,7 @@ class Merchant{
                 for(let k = 0; k < this._ingredients.length; k++){
                     if(ingredient.ingredient === this._ingredients[k].ingredient.id){
                         ingredients.push({
-                            ingredient: this._ingredients[k].ingredient,
+                            ingredient: this._ingredients[k].ingredient.id,
                             quantity: ingredient.quantity
                         });
                         break;
@@ -356,7 +358,9 @@ class Merchant{
         return this._recipes;
     }
 
-    addRecipe(recipe){
+    addRecipe(id, name, price, ingredients){
+        let recipe = new Recipe(id, name, price, ingredients, this);
+
         this._recipes.push(recipe);
 
         this._modules.recipeBook.isPopulated = false;
@@ -799,7 +803,7 @@ class Merchant{
 }
 
 module.exports = Merchant;
-},{}],3:[function(require,module,exports){
+},{"./Recipe":4}],3:[function(require,module,exports){
 class OrderIngredient{
     constructor(ingredient, quantity, pricePerUnit){
         if(quantity < 0){
@@ -1127,8 +1131,9 @@ class Recipe{
         this._ingredients = [];
 
         for(let i = 0; i < ingredients.length; i++){
+            const ingredient = parent.getIngredient(ingredients[i].ingredient);
             const recipeIngredient = new RecipeIngredient(
-                ingredients[i].ingredient,
+                ingredient.ingredient,
                 ingredients[i].quantity
             );
 
@@ -2345,13 +2350,12 @@ let newRecipe = {
                         }
                     }
 
-                    merchant.addRecipe(new Recipe(
+                    merchant.addRecipe(
                         response._id,
                         response.name,
                         response.price,
-                        ingredients,
-                        merchant
-                    ));
+                        ingredients
+                    );
 
                     banner.createNotification("RECIPE CREATED");
                     controller.openStrand("recipeBook");
@@ -2367,12 +2371,11 @@ let newRecipe = {
 
     submitSpreadsheet: function(){
         event.preventDefault();
+        controller.closeModal();
 
         const file = document.getElementById("spreadsheetInput").files[0];
         let data = new FormData();
-        console.log(file);
         data.append("recipes", file);
-        console.log(data);
 
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
@@ -2386,11 +2389,20 @@ let newRecipe = {
                 if(typeof(response) === "String"){
                     banner.createError(response);
                 }else{
+                    for(let i = 0; i < response.length; i++){
+                        merchant.addRecipe(
+                            response[i]._id,
+                            response[i].name,
+                            response[i].price,
+                            response[i].ingredients
+                        );
+                    }
 
+                    banner.createNotification("ALL INGREDIENTS SUCCESSFULLY CREATED");
+                    controller.openStrand("recipeBook");
                 }
             })
             .catch((err)=>{
-                console.log(err);
                 banner.createError("UNABLE TO DISPLAY NEW RECIPES.  PLEASE REFRESH THE PAGE");
             })
             .finally(()=>{

+ 6 - 2
views/dashboardPage/js/classes/Merchant.js

@@ -1,3 +1,5 @@
+const Recipe = require("./Recipe");
+
 class MerchantIngredient{
     constructor(ingredient, quantity){
         if(quantity < 0){
@@ -123,7 +125,7 @@ class Merchant{
                 for(let k = 0; k < this._ingredients.length; k++){
                     if(ingredient.ingredient === this._ingredients[k].ingredient.id){
                         ingredients.push({
-                            ingredient: this._ingredients[k].ingredient,
+                            ingredient: this._ingredients[k].ingredient.id,
                             quantity: ingredient.quantity
                         });
                         break;
@@ -229,7 +231,9 @@ class Merchant{
         return this._recipes;
     }
 
-    addRecipe(recipe){
+    addRecipe(id, name, price, ingredients){
+        let recipe = new Recipe(id, name, price, ingredients, this);
+
         this._recipes.push(recipe);
 
         this._modules.recipeBook.isPopulated = false;

+ 2 - 1
views/dashboardPage/js/classes/Recipe.js

@@ -111,8 +111,9 @@ class Recipe{
         this._ingredients = [];
 
         for(let i = 0; i < ingredients.length; i++){
+            const ingredient = parent.getIngredient(ingredients[i].ingredient);
             const recipeIngredient = new RecipeIngredient(
-                ingredients[i].ingredient,
+                ingredient.ingredient,
                 ingredients[i].quantity
             );
 

+ 14 - 7
views/dashboardPage/js/sidebars/newRecipe.js

@@ -109,13 +109,12 @@ let newRecipe = {
                         }
                     }
 
-                    merchant.addRecipe(new Recipe(
+                    merchant.addRecipe(
                         response._id,
                         response.name,
                         response.price,
-                        ingredients,
-                        merchant
-                    ));
+                        ingredients
+                    );
 
                     banner.createNotification("RECIPE CREATED");
                     controller.openStrand("recipeBook");
@@ -131,12 +130,11 @@ let newRecipe = {
 
     submitSpreadsheet: function(){
         event.preventDefault();
+        controller.closeModal();
 
         const file = document.getElementById("spreadsheetInput").files[0];
         let data = new FormData();
-        console.log(file);
         data.append("recipes", file);
-        console.log(data);
 
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
@@ -150,11 +148,20 @@ let newRecipe = {
                 if(typeof(response) === "String"){
                     banner.createError(response);
                 }else{
+                    for(let i = 0; i < response.length; i++){
+                        merchant.addRecipe(
+                            response[i]._id,
+                            response[i].name,
+                            response[i].price,
+                            response[i].ingredients
+                        );
+                    }
 
+                    banner.createNotification("ALL INGREDIENTS SUCCESSFULLY CREATED");
+                    controller.openStrand("recipeBook");
                 }
             })
             .catch((err)=>{
-                console.log(err);
                 banner.createError("UNABLE TO DISPLAY NEW RECIPES.  PLEASE REFRESH THE PAGE");
             })
             .finally(()=>{