Просмотр исходного кода

Add ability to download a template spreadsheet for uploading ingredients.

Lee Morgan 5 лет назад
Родитель
Сommit
0c7ef9446a
3 измененных файлов с 23 добавлено и 1 удалено
  1. 21 0
      controllers/ingredientData.js
  2. 1 1
      controllers/recipeData.js
  3. 1 0
      routes.js

+ 21 - 0
controllers/ingredientData.js

@@ -278,6 +278,27 @@ module.exports = {
             });
     },
 
+    spreadsheetTemplate: function(req, res){
+        if(!req.session.user){
+            req.session.error = "MUST BE LOGGED IN TO DO THAT";
+            return res.redirect("/");
+        }
+
+        let workbook = xlsx.utils.book_new();
+        workbook.SheetNames.push("Ingredients");
+        let workbookData = [];
+
+        workbookData.push(["Name", "Category", "Quantity", "Unit", "Bottle", "Bottle Size"]);
+        workbookData.push(["Example Ingredient 1", "Produce", 100, "lbs"]);
+        workbookData.push(["Example Ingredient Two", "Beverage", 5, "ml", "TRUE", 750]);
+
+        workbook.Sheets.Ingredients = xlsx.utils.aoa_to_sheet(workbookData);
+        xlsx.writeFile(workbook, "SublineIngredients.xlsx");
+        return res.download("SublineIngredients.xlsx", (err)=>{
+            fs.unlink("SublineIngredients.xlsx", ()=>{});
+        });
+    },
+
     //DELETE - Removes an ingredient from the merchant's inventory
     removeIngredient: function(req, res){
         if(!req.session.user){

+ 1 - 1
controllers/recipeData.js

@@ -461,7 +461,7 @@ module.exports = {
 
                 xlsx.writeFile(workbook, "SublineRecipes.xlsx");
 
-                res.download("SublineRecipes.xlsx", (err)=>{
+                return res.download("SublineRecipes.xlsx", (err)=>{
                     fs.unlink("SublineRecipes.xlsx", ()=>{});
                 });
             })

+ 1 - 0
routes.js

@@ -30,6 +30,7 @@ module.exports = function(app){
     app.post("/ingredients/create", ingredientData.createIngredient);  //also adds to merchant
     app.put("/ingredients/update", ingredientData.updateIngredient);
     app.post("/ingredients/create/spreadsheet", upload.single("ingredients"), ingredientData.createFromSpreadsheet);
+    app.get("/ingredients/download/spreadsheet", ingredientData.spreadsheetTemplate);
     app.delete("/ingredients/remove/:id", ingredientData.removeIngredient);