Bläddra i källkod

Bug fix: server was trying to send circular data in json.
Ingredients and recipes now successfully added and sent back to frontend.

Lee Morgan 5 år sedan
förälder
incheckning
b9ef221c43

+ 2 - 8
controllers/ingredientData.js

@@ -226,7 +226,7 @@ module.exports = {
             });
 
             let merchantItem = {
-                ingredient: array[i][locations.name],
+                ingredient: ingredient,
                 quantity: helper.convertQuantityToBaseUnit(array[i][locations.quantity], array[i][locations.unit]),
                 defaultUnit: array[i][locations.unit]
             }
@@ -276,13 +276,7 @@ module.exports = {
             })
             .then((merchant)=>{
                 for(let i = 0; i < merchantData.length; i++){
-                    for(let j = 0; j < createdIngredients.length; j++){
-                        if(merchantData[i].ingredient === createdIngredients[j].name){
-                            merchantData[i].ingredient = createdIngredients[j];
-                            merchant.inventory.push(merchantData[i]);
-                            break;
-                        }
-                    }
+                    merchant.inventory.push(merchantData[i]);
                 }
 
                 return merchant.save();

+ 2 - 0
controllers/otherData.js

@@ -168,9 +168,11 @@ module.exports = {
         for(let i = 0; i < sheets.length; i++){
             switch(sheets[i].toLocaleLowerCase()){
                 case "ingredients":
+                    console.log("ingredients");
                     data.ingredients = await ingredientData.createFromSpreadsheet(workbook.Sheets[sheets[i]], req.session.user);
                     break;
                 case "recipes":
+                    console.log("recipes");
                     data.recipes = await recipeData.createFromSpreadsheet(workbook.Sheets[sheets[i]], req.session.user);
                     break;
             }

+ 45 - 2
controllers/recipeData.js

@@ -5,6 +5,7 @@ const ArchivedRecipe = require("../models/archivedRecipe.js");
 const validator = require("./validator.js");
 
 const axios = require("axios");
+const xlsxUtils = require("xlsx").utils;
 
 module.exports = {
     /*
@@ -299,12 +300,54 @@ module.exports = {
             });
     },
 
-    createFromSpreadSheet: function(sheet, user){
+    createFromSpreadsheet: function(sheet, user){
         const array = xlsxUtils.sheet_to_json(sheet, {
             header: 1
         });
 
         //get property locations
-        
+        let locations = {};
+        for(let i = 0; i < array[0].length; i++){
+            switch(array[0][i].toLowerCase()){
+                case "name": locations.name = i; break;
+                case "price": locations.price = i; break;
+            }
+        }
+
+        //Create Recipes
+        let merchant = {};
+        let newRecipes = [];
+        return Merchant.findOne({_id: 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)=>{
+                console.log(err);
+                return "ERROR: UNABLE TO CREATE RECIPES";
+            });
     }
 }

+ 3 - 5
views/dashboardPage/bundle.js

@@ -2026,12 +2026,10 @@ let newIngredient = {
             .then((response)=>{
                 if(typeof(response) === "string"){
                     banner.createError(response);
+                }else{
+                    console.log(response);
                 }
-                for(let i = 0; i < response.length; i++){
-                    merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
-                }
-
-                controller.openStrand("ingredients");
+                
             })
             .catch((err)=>{
                 banner.createError("SOMETHING WENT WRONG.  TRY REFRESHING THE PAGE");

+ 3 - 5
views/dashboardPage/js/sidebars/newIngredient.js

@@ -94,12 +94,10 @@ let newIngredient = {
             .then((response)=>{
                 if(typeof(response) === "string"){
                     banner.createError(response);
+                }else{
+                    console.log(response);
                 }
-                for(let i = 0; i < response.length; i++){
-                    merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
-                }
-
-                controller.openStrand("ingredients");
+                
             })
             .catch((err)=>{
                 banner.createError("SOMETHING WENT WRONG.  TRY REFRESHING THE PAGE");