Skip to content
Merged

Test #46

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pids
*.pid
*.seed
*.pid.lock

package-lock.json
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

Expand Down Expand Up @@ -134,3 +134,4 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.idea
14 changes: 13 additions & 1 deletion documentation/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,16 @@ $$ LANGUAGE plpgsql;
CREATE TRIGGER update_user_settings_updated_at
BEFORE UPDATE ON user_settings
FOR EACH ROW
EXECUTE FUNCTION update_updated_at_column();
EXECUTE FUNCTION update_updated_at_column();




ALTER TABLE user_settings ADD COLUMN daily_message_limit INTEGER DEFAULT 100;
CREATE TABLE user_message_logs (
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL,
message_count INTEGER DEFAULT 0,
date DATE DEFAULT CURRENT_DATE,
CONSTRAINT unique_user_date UNIQUE (username, date)
);
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ router.use((err, req, res, next) => {
res.render('templates/Error/500', { error: err });
});

const port = 3130;
const port = 3030;

// Start the router
router.listen(port, () => {
Expand Down
56 changes: 3 additions & 53 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions routes/checkMessageLimit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { pool } from "./pool.js";

export const checkMessageLimit = async (req, res, next) => {
try {
const username = req.session.user.username;
const role = req.session.user.role;

console.log(`[checkMessageLimit] User: ${username}, Role: ${role}`);

// SuperAdmin has unlimited messages
if (role === "SuperAdmin") {
console.log(`[checkMessageLimit] User ${username} is a SuperAdmin. Skipping message limit check.`);
return next();
}

// Fetch user's daily message limit
console.log(`[checkMessageLimit] Fetching daily message limit for user: ${username}`);
const userSettings = await pool.query(
`SELECT daily_message_limit FROM user_settings WHERE username = $1`,
[username]
);

const dailyLimit = userSettings.rows[0]?.daily_message_limit || 100; // Default to 100 if not set
console.log(`[checkMessageLimit] Daily message limit for user ${username}: ${dailyLimit}`);

// Check today's message count
const today = new Date().toISOString().split("T")[0]; // Get current date in YYYY-MM-DD format
console.log(`[checkMessageLimit] Checking message count for user ${username} on date: ${today}`);
const messageLog = await pool.query(
`SELECT message_count FROM user_message_logs WHERE username = $1 AND date = $2`,
[username, today]
);

const messageCount = messageLog.rows[0]?.message_count || 0;
console.log(`[checkMessageLimit] Current message count for user ${username} on ${today}: ${messageCount}`);

if (messageCount >= dailyLimit) {
console.warn(`[checkMessageLimit] User ${username} has reached the daily message limit of ${dailyLimit}.`);
return res.status(403).json({ message: "Daily message limit reached." });
}

// Increment message count for the user
if (messageLog.rows.length > 0) {
console.log(`[checkMessageLimit] Incrementing message count for user ${username} on ${today}.`);
await pool.query(
`UPDATE user_message_logs SET message_count = message_count + 1 WHERE username = $1 AND date = $2`,
[username, today]
);
} else {
console.log(`[checkMessageLimit] Creating new message log entry for user ${username} on ${today}.`);
await pool.query(
`INSERT INTO user_message_logs (username, message_count, date) VALUES ($1, $2, $3)`,
[username, 1, today]
);
}

console.log(`[checkMessageLimit] Message count updated successfully for user ${username}.`);
next();
} catch (error) {
console.error("[checkMessageLimit] Error in middleware:", error);
res.status(500).json({ message: "Internal server error." });
}
};
Loading