Skip to content

Commit

Permalink
Updating free limit error checking code
Browse files Browse the repository at this point in the history
  • Loading branch information
davetaz committed Nov 22, 2024
1 parent 94c0279 commit a60bbd9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 10 deletions.
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions middleware/hubspot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
8 changes: 4 additions & 4 deletions routes/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 });
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions views/pages/scan.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ async function sendDataToServer(inputObject) {
method,
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: JSON.stringify(inputObject),
});
Expand Down

0 comments on commit a60bbd9

Please sign in to comment.