Browse Source

Add backend for uploading square items as recipes.

Lee Morgan 5 years ago
parent
commit
0d83d3003c
2 changed files with 32 additions and 2 deletions
  1. 1 1
      controllers/helper.js
  2. 31 1
      controllers/recipeData.js

+ 1 - 1
controllers/helper.js

@@ -240,7 +240,7 @@ module.exports = {
     },
 
     isSanitary: function(strings){
-        let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
+        let disallowed = ["\\", "<", ">", "$", "{", "}", "."];
 
         for(let i = 0; i < strings.length; i++){
             for(let j = 0; j < disallowed.length; j++){

+ 31 - 1
controllers/recipeData.js

@@ -229,7 +229,37 @@ module.exports = {
     },
 
     spreadsheetUpSquare: function(req, res){
-        console.log("butt stuff");
+        let workbook = xlsx.readFile(req.file.path);
+        fs.unlink(req.file.path, ()=>{});
+        let array = xlsx.utils.sheet_to_json(workbook.Sheets.Items, {header: 1});
+
+        let recipes = [];
+        for(let i = 1; i < array.length; i++){
+            let name = array[i][1];
+            if(array[i][6] !== "") name = `${name} (${array[i][6]})`;
+
+            let recipe = new Recipe({
+                posId: array[i][0],
+                merchant: res.locals.merchant._id,
+                name: name,
+                price: parseInt(array[i][7] * 100) || 0,
+                ingredients: []
+            });
+
+            recipes.push(recipe);
+            res.locals.merchant.recipes.push(recipe);
+        }
+
+
+        Promise.all([Recipe.create(recipes), res.locals.merchant.save()])
+            .then((response)=>{
+                return res.json(response[0]);
+            })
+            .catch((err)=>{
+                console.log(err);
+                if(err.name === "ValidationError") return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
+                return res.json("ERROR: UNABLE TO CREATE YOUR RECIPES");
+            });
     },
 
     spreadsheetTemplate: function(req, res){