Преглед на файлове

Update removing ingredient to work with new session id.
Change the location of the middleware to better apply to specific routes.

Lee Morgan преди 5 години
родител
ревизия
ae4d06fafd
променени са 4 файла, в които са добавени 49 реда и са изтрити 68 реда
  1. 0 47
      app.js
  2. 7 15
      controllers/ingredientData.js
  3. 7 6
      routes.js
  4. 35 0
      verifySession.js

+ 0 - 47
app.js

@@ -4,16 +4,6 @@ const mongoose = require("mongoose");
 const compression = require("compression");
 const https = require("https");
 const fs = require("fs");
-const Merchant = require("./models/merchant.js");
-const helper = require("./controllers/helper.js");
-
-let protectedRoutes = [
-    "/dashboard",
-    "/ingredients/create",
-    "/ingredients/update",
-    "/ingredients/create/spreadsheet",
-    "/ingredients/download/spreadsheet"
-];
 
 const app = express();
 
@@ -45,43 +35,6 @@ app.use(session({
     saveUninitialized: true,
     resave: false
 }));
-app.use((req, res, next)=>{
-    if(protectedRoutes.includes(req.url)){
-        if(req.session.user === undefined) {
-            req.session.error = "PLEASE LOG IN";
-            return res.redirect("/");
-        }
-
-        Merchant.findOne({"session.sessionId": req.session.user})
-            .then((merchant)=>{
-                if(merchant === null){
-                    throw "no merchant";
-                }
-
-                if(merchant.session.date < new Date()){
-                    let newExpiration = new Date();
-                    newExpiration.setDate(newExpiration.getDate() + 90);
-
-                    merchant.session.sessionId = helper.generateId(25);
-                    merchant.session.date = newExpiration;
-                    merchant.save();
-                    return res.redirect("/");
-                }
-
-                res.locals.merchant = merchant;
-                return next();
-            })
-            .catch((err)=>{
-                if(err === "no merchant"){
-                    req.session.error = "PLEASE LOG IN";
-                    return res.redirect("/");
-                }
-                return res.json("ERROR: UNABLE TO RETRIEVE DATA");
-            });
-    }else{
-        return next();
-    }
-});
 app.use(express.urlencoded({extended: true}));
 app.use(express.json());
 

+ 7 - 15
controllers/ingredientData.js

@@ -1,4 +1,3 @@
-const Merchant = require("../models/merchant");
 const Ingredient = require("../models/ingredient");
 const InventoryAdjustment = require("../models/inventoryAdjustment.js");
 
@@ -28,6 +27,7 @@ module.exports = {
             newIngredient.ingredient.unitSize = helper.convertQuantityToBaseUnit(newIngredient.ingredient.unitSize, newIngredient.ingredient.unitType);
         }
 
+        
         newIngredient = new Ingredient(newIngredient.ingredient);
         
         newIngredient.save()
@@ -210,22 +210,14 @@ module.exports = {
 
     //DELETE - Removes an ingredient from the merchant's inventory
     removeIngredient: function(req, res){
-        if(!req.session.user){
-            req.session.error = "MUST BE LOGGED IN TO DO THAT";
-            return res.redirect("/");
+        for(let i = 0; i < res.locals.merchant.inventory.length; i++){
+            if(req.params.id === res.locals.merchant.inventory[i].ingredient._id.toString()){
+                res.locals.merchant.inventory.splice(i, 1);
+                break;
+            }
         }
 
-        Merchant.findOne({_id: req.session.user})
-            .then((merchant)=>{
-                for(let i = 0; i < merchant.inventory.length; i++){
-                    if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
-                        merchant.inventory.splice(i, 1);
-                        break;
-                    }
-                }
-
-                return Promise.all([merchant.save(), Ingredient.deleteOne({_id: req.params.id})]);
-            })
+        Promise.all([res.locals.merchant.save(), Ingredient.deleteOne({_id: req.params.id})])
             .then((response)=>{
                 return res.json({});
             })

+ 7 - 6
routes.js

@@ -8,6 +8,7 @@ const orderData = require("./controllers/orderData.js");
 const informationPages = require("./controllers/informationPages.js");
 const emailVerification = require("./controllers/emailVerification.js");
 const passwordReset = require("./controllers/passwordReset.js");
+const session = require("./verifySession.js");
 
 const multer = require("multer");
 const upload = multer({dest: "uploads/"});
@@ -15,7 +16,7 @@ const upload = multer({dest: "uploads/"});
 module.exports = function(app){
     //Render page
     app.get("/", renderer.landingPage);
-    app.get("/dashboard", renderer.displayDashboard);
+    app.get("/dashboard", session, renderer.displayDashboard);
     app.get("/resetpassword/*", renderer.displayPassReset);
 
     //Merchant
@@ -26,11 +27,11 @@ module.exports = function(app){
     app.post("/merchant/password", merchantData.updatePassword);
 
     //Ingredients
-    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);
+    app.post("/ingredients/create", session, ingredientData.createIngredient);  //also adds to merchant
+    app.put("/ingredients/update", session, ingredientData.updateIngredient);
+    app.post("/ingredients/create/spreadsheet", session, upload.single("ingredients"), ingredientData.createFromSpreadsheet);
+    app.get("/ingredients/download/spreadsheet", session, ingredientData.spreadsheetTemplate);
+    app.delete("/ingredients/remove/:id", session, ingredientData.removeIngredient);
 
     //Recipes
     app.post("/recipe/create", recipeData.createRecipe);

+ 35 - 0
verifySession.js

@@ -0,0 +1,35 @@
+const Merchant = require("./models/merchant.js")
+
+module.exports = function(req, res, next){
+    if(req.session.user === undefined) {
+        req.session.error = "PLEASE LOG IN";
+        return res.redirect("/");
+    }
+
+    Merchant.findOne({"session.sessionId": req.session.user})
+        .then((merchant)=>{
+            if(merchant === null){
+                throw "no merchant";
+            }
+
+            if(merchant.session.date < new Date()){
+                let newExpiration = new Date();
+                newExpiration.setDate(newExpiration.getDate() + 90);
+
+                merchant.session.sessionId = helper.generateId(25);
+                merchant.session.date = newExpiration;
+                merchant.save();
+                return res.redirect("/");
+            }
+
+            res.locals.merchant = merchant;
+            return next();
+        })
+        .catch((err)=>{
+            if(err === "no merchant"){
+                req.session.error = "PLEASE LOG IN";
+                return res.redirect("/");
+            }
+            return res.json("ERROR: UNABLE TO RETRIEVE DATA");
+        });
+}