Skip to content
Merged
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
7 changes: 7 additions & 0 deletions backend/src/models/Complaint.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ class Complaint {
* @returns {Object} Updated complaint
*/
static async join(id, userId = null, sessionId = null) {
// Check first: recording a join for an id that doesn't exist yet leaves
// a row that later makes the real complaint report "already joined".
const existing = await pool.query('SELECT 1 FROM complaints WHERE id = $1', [id]);
if (existing.rows.length === 0) {
return null;
}

const insertResult = await pool.query(
`INSERT OR IGNORE INTO complaint_joins (complaint_id, user_id, session_id) VALUES ($1, $2, $3)`,
[id, userId, sessionId]
Expand Down
25 changes: 25 additions & 0 deletions backend/tests/complaintJoin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
process.env.JWT_SECRET = process.env.JWT_SECRET || 'x'.repeat(48);
const { test, before } = require('node:test');
const assert = require('node:assert');
const initDatabase = require('../src/config/initDatabase');
const Complaint = require('../src/models/Complaint');
const pool = require('../src/config/database');

before(async () => {
await initDatabase();
});

test('joining a complaint that does not exist records nothing', async () => {
const { rows } = await pool.query('SELECT COALESCE(MAX(id), 0) AS max_id FROM complaints');
const missingId = Number(rows[0].max_id) + 100000;
const sessionId = `join-test-${Date.now()}`;

const result = await Complaint.join(missingId, null, sessionId);

assert.strictEqual(result, null);
const joins = await pool.query(
'SELECT COUNT(*) AS n FROM complaint_joins WHERE complaint_id = $1',
[missingId]
);
assert.strictEqual(Number(joins.rows[0].n), 0);
});
Loading