diff --git a/index.js b/index.js index 60c48ca..d665af2 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ const fs = require('fs'); // Load environment variables securely require("dotenv").config({ path: "./config.env" }); +const { checkLimit } = require('./middleware/hubspot'); // MongoDB setup const mongoose = require('mongoose'); @@ -141,7 +142,7 @@ app.get('/', function(req, res) { res.render('pages/home'); }); -app.get('/new', ensureAuthenticated, function(req, res) { +app.get('/new', ensureAuthenticated, checkLimit, function(req, res, next) { const page = { title: "Project details", link: "projectDetails" diff --git a/middleware/hubspot.js b/middleware/hubspot.js index 5e90f0f..374882d 100644 --- a/middleware/hubspot.js +++ b/middleware/hubspot.js @@ -16,20 +16,20 @@ const checkLimit = async (req, res, next) => { require("dotenv").config({ path: "./config.env" }); // 2. Read FREE_PROJECT_LIMIT from the config.env const freeLimit = parseInt(process.env.FREE_PROJECT_LIMIT); - //console.log(freeLimit); // 3. Look up how many existing projects the user has to ensure it is below the limit const projectCount = await Project.countDocuments({ owner: userId }); //console.log(projectCount); if (projectCount >= freeLimit) { - return res.status(403).json({ message: `You have reached the limit of ${freeLimit} free projects.` }); + const error = new Error(`You have reached the limit of ${freeLimit} free projects.`); + error.status = 403; + throw error; } // If the user does not have an active membership and has not reached the project limit, proceed to the next middleware or route handler next(); } catch (error) { - console.error("Error in checkLimit middleware:", error); - res.status(500).json({ message: "Internal server error." }); + return next(error); } } diff --git a/package.json b/package.json index 8281358..709fcd3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "care.theodi.org", - "version": "2.8.2", + "version": "2.8.3", "description": "The ODI Care tool (AI enabled)", "main": "index.js", "scripts": { diff --git a/routes/project.js b/routes/project.js index 71161c4..ddf29e5 100644 --- a/routes/project.js +++ b/routes/project.js @@ -228,7 +228,7 @@ router.get('/:id', ensureAuthenticated, checkProjectAccess, loadProject, async ( } }); // POST route to create a new project -router.post('/', ensureAuthenticated, checkLimit, async (req, res) => { +router.post('/', ensureAuthenticated, checkLimit, async (req, res, next) => { try { // Set owner field to the ID of the authenticated user const user = req.session.passport.user; @@ -241,12 +241,12 @@ router.post('/', ensureAuthenticated, checkLimit, async (req, res) => { } res.status(201).json(savedProject); } catch (error) { - res.status(400).json({ message: error.message }); + next(error); } }); // PUT route to update an existing project -router.put('/:id', ensureAuthenticated, checkProjectAccess, async (req, res) => { +router.put('/:id', ensureAuthenticated, checkProjectAccess, async (req, res, next) => { const id = req.params.id; try { const updatedProject = await Project.findByIdAndUpdate(id, req.body, { new: false }); @@ -263,7 +263,7 @@ router.put('/:id', ensureAuthenticated, checkProjectAccess, async (req, res) => }); // DELETE route to delete a project -router.delete('/:id', ensureAuthenticated, checkProjectOwner, async (req, res) => { +router.delete('/:id', ensureAuthenticated, checkProjectOwner, async (req, res, next) => { const id = req.params.id; // Unset req.session.projectId if it matches the ID to be deleted if (req.session.projectId === id) { diff --git a/views/pages/scan.ejs b/views/pages/scan.ejs index af1083d..bf5b959 100644 --- a/views/pages/scan.ejs +++ b/views/pages/scan.ejs @@ -236,6 +236,7 @@ async function sendDataToServer(inputObject) { method, headers: { "Content-Type": "application/json", + "Accept": "application/json" }, body: JSON.stringify(inputObject), });