Prechádzať zdrojové kódy

Reorganize code to handle all data in one file.

Lee Morgan 5 rokov pred
rodič
commit
b2349b7f5f

+ 18 - 25
controllers/ingredientData.js

@@ -2,11 +2,11 @@ const Merchant = require("../models/merchant");
 const Ingredient = require("../models/ingredient");
 const InventoryAdjustment = require("../models/inventoryAdjustment.js");
 
-const Helper = require("./helper.js");
-const Validator = require("./validator.js");
+const helper = require("./helper.js");
+const validator = require("./validator.js");
+
+const xlsxUtils = require("xlsx").utils;
 
-const xlsx = require("xlsx");
-const fs = require("fs");
 module.exports = {
     //GET - gets a list of all database ingredients
     //Returns:
@@ -41,18 +41,18 @@ module.exports = {
             return res.redirect("/");
         }
 
-        let validation = Validator.ingredient(req.body.ingredient);
+        let validation = validator.ingredient(req.body.ingredient);
         if(validation !== true){
             return res.json(validation);
         }
 
-        validation = Validator.quantity(req.body.quantity);
+        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);
+            validation = validator.quantity(req.body.ingredient.unitSize);
             if(validation !== true){
                 return res.json(validation);
             }
@@ -65,7 +65,7 @@ module.exports = {
                 category: req.body.ingredient.category,
                 unitType: req.body.ingredient.unitType,
                 specialUnit: req.body.ingredient.specialUnit,
-                unitSize: Helper.convertQuantityToBaseUnit(req.body.ingredient.unitSize, req.body.defaultUnit)
+                unitSize: helper.convertQuantityToBaseUnit(req.body.ingredient.unitSize, req.body.defaultUnit)
             });
         }else{
             newIngredient = new Ingredient(req.body.ingredient);
@@ -84,7 +84,7 @@ module.exports = {
                 if(response[0].specialUnit === "bottle"){
                     newIngredient.quantity = req.body.quantity * response[0].unitSize;
                 }else{
-                    newIngredient.quantity = Helper.convertQuantityToBaseUnit(req.body.quantity, req.body.defaultUnit);
+                    newIngredient.quantity = helper.convertQuantityToBaseUnit(req.body.quantity, req.body.defaultUnit);
                 }
 
                 response[1].inventory.push(newIngredient);
@@ -116,7 +116,7 @@ module.exports = {
             return res.redirect("/");
         }
 
-        const ingredientCheck = Validator.ingredient(req.body);
+        const ingredientCheck = validator.ingredient(req.body);
         if(ingredientCheck !== true){
             return res.json(ingredientCheck);
         }
@@ -198,15 +198,8 @@ module.exports = {
             });
     },
 
-    createFromSpreadsheet: function(req, res){
-        if(!req.session.user){
-            req.session.error = "MUST BE LOGGED IN TO DO THAT";
-            return res.redirect("/");
-        }
-
-        let workbook = xlsx.readFile(req.file.path);
-        fs.unlink(req.file.path, ()=>{});
-        let array = xlsx.utils.sheet_to_json(workbook.Sheets.Sheet1, {
+    createFromSpreadsheet: function(sheet, user){
+        const array = xlsxUtils.sheet_to_json(sheet, {
             header: 1
         });
 
@@ -234,14 +227,14 @@ module.exports = {
 
             let merchantItem = {
                 ingredient: array[i][locations.name],
-                quantity: Helper.convertQuantityToBaseUnit(array[i][locations.quantity], array[i][locations.unit]),
+                quantity: helper.convertQuantityToBaseUnit(array[i][locations.quantity], array[i][locations.unit]),
                 defaultUnit: array[i][locations.unit]
             }
 
             if(array[i][locations.bottle] === true){
                 ingredient.unitType = "volume";
                 ingredient.specialUnit = "bottle";
-                ingredient.unitSize = Helper.convertQuantityToBaseUnit(array[i][locations.bottleSize], array[i][locations.unit]);
+                ingredient.unitSize = helper.convertQuantityToBaseUnit(array[i][locations.bottleSize], array[i][locations.unit]);
             }else{
                 let unitType = "";
                 switch(array[i][locations.unit].toLowerCase()){
@@ -275,11 +268,11 @@ module.exports = {
 
         //Update the database
         let createdIngredients = [];
-        Ingredient.create(ingredients)
+        return Ingredient.create(ingredients)
             .then((ingredients)=>{
                 createdIngredients = ingredients;
 
-                return Merchant.findOne({_id: req.session.user});
+                return Merchant.findOne({_id: user});
             })
             .then((merchant)=>{
                 for(let i = 0; i < merchantData.length; i++){
@@ -295,10 +288,10 @@ module.exports = {
                 return merchant.save();
             })
             .then((merchant)=>{
-                return res.json(merchantData);
+                return merchantData;
             })
             .catch((err)=>{
-                return res.json("ERROR: UNABLE TO CREATE YOUR INGREDIENTS");
+                return "ERROR: UNABLE TO CREATE YOUR INGREDIENTS";
             });
     }
 }

+ 32 - 4
controllers/otherData.js

@@ -1,10 +1,13 @@
+const Merchant = require("../models/merchant");
+
+const ingredientData = require("./ingredientData.js");
+const recipeData = require("./recipeData.js");
+
 const bcrypt = require("bcryptjs");
 const axios = require("axios");
 const path = require("path");
-const mailgun = require("mailgun-js")({apiKey: process.env.MG_SUBLINE_APIKEY, domain: "mail.thesubline.net"});
-const VerifyEmail = require("../emails/verifyEmail.js");
-
-const Merchant = require("../models/merchant");
+const fs = require("fs");
+const xlsx = require("xlsx");
 
 module.exports = {
     /*
@@ -149,5 +152,30 @@ module.exports = {
 
     logo: function(req, res){
         return res.sendFile(path.resolve("./views/shared/images/logo.png"));
+    },
+
+    readSpreadsheet: async function(req, res){
+        if(!req.session.user){
+            req.session.error = "MUST BE LOGGED IN TO DO THAT";
+            return res.redirect("/");
+        }
+
+        let workbook = xlsx.readFile(req.file.path);
+        fs.unlink(req.file.path, ()=>{});
+        let sheets = Object.keys(workbook.Sheets);
+
+        let data = {};
+        for(let i = 0; i < sheets.length; i++){
+            switch(sheets[i].toLocaleLowerCase()){
+                case "ingredients":
+                    data.ingredients = await ingredientData.createFromSpreadsheet(workbook.Sheets[sheets[i]], req.session.user);
+                    break;
+                case "recipes":
+                    data.recipes = await recipeData.createFromSpreadsheet(workbook.Sheets[sheets[i]], req.session.user);
+                    break;
+            }
+        }
+
+        return res.json(data);
     }
 }

+ 15 - 5
controllers/recipeData.js

@@ -1,9 +1,10 @@
-const axios = require("axios");
-
 const Recipe = require("../models/recipe.js");
 const Merchant = require("../models/merchant.js");
 const ArchivedRecipe = require("../models/archivedRecipe.js");
-const Validator = require("./validator.js");
+
+const validator = require("./validator.js");
+
+const axios = require("axios");
 
 module.exports = {
     /*
@@ -24,7 +25,7 @@ module.exports = {
             return res.redirect("/");
         }
 
-        let validation = Validator.recipe(req.body);
+        let validation = validator.recipe(req.body);
         if(validation !== true){
             return res.json(validation);
         }
@@ -76,7 +77,7 @@ module.exports = {
             return res.redirect("/");
         }
 
-        let validation = Validator.recipe(req.body);
+        let validation = validator.recipe(req.body);
         if(validation !== true){
             return res.json(validation);
         }
@@ -296,5 +297,14 @@ module.exports = {
             .catch((err)=>{
                 return "ERROR: UNABLE TO RETRIEVE RECIPE DATA FROM SQUARE";
             });
+    },
+
+    createFromSpreadSheet: function(sheet, user){
+        const array = xlsxUtils.sheet_to_json(sheet, {
+            header: 1
+        });
+
+        //get property locations
+        
     }
 }

+ 1 - 1
routes.js

@@ -31,7 +31,6 @@ module.exports = function(app){
     app.post("/ingredients/create", ingredientData.createIngredient);  //also adds to merchant
     app.put("/ingredients/update", ingredientData.updateIngredient);
     app.delete("/ingredients/remove/:id", ingredientData.removeIngredient);
-    app.post("/ingredients/create/spreadsheet", upload.single("spreadsheet"), ingredientData.createFromSpreadsheet);
 
     //Recipes
     app.post("/recipe/create", recipeData.createRecipe);
@@ -61,6 +60,7 @@ module.exports = function(app){
     app.get("/cloverauth*", otherData.cloverAuth);
     app.get("/squareauth", otherData.squareAuth);
     app.get("/logo", otherData.logo);
+    app.post("/spreadsheet", upload.single("spreadsheet"), otherData.readSpreadsheet);
 
     //Information Pages
     app.get("/privacy", informationPages.privacy);

BIN
uploads/2bad78d311821368b93594411b50d85b


BIN
uploads/7b581bf3c758561ad5c131e27893743a


BIN
uploads/e647f504e0ea56d6a78f7e522b1459c0


+ 1 - 1
views/dashboardPage/bundle.js

@@ -2018,7 +2018,7 @@ let newIngredient = {
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
 
-        fetch("/ingredients/create/spreadsheet", {
+        fetch("/spreadsheet", {
             method: "post",
             body: data,
         })

+ 1 - 1
views/dashboardPage/js/sidebars/newIngredient.js

@@ -86,7 +86,7 @@ let newIngredient = {
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
 
-        fetch("/ingredients/create/spreadsheet", {
+        fetch("/spreadsheet", {
             method: "post",
             body: data,
         })