refactor(backend): add status constants and update code to use them

- Create constants/statuses.js with MATCH_STATUS, SUGGESTION_STATUS
- Update routes/dashboard.js to use MATCH_STATUS
- Update routes/matches.js to use MATCH_STATUS
- Update routes/events.js to use SUGGESTION_STATUS
- Update services/matching.js to use SUGGESTION_STATUS
- Update tests to use constants
This commit is contained in:
Radosław Gierwiało
2025-11-23 22:40:54 +01:00
parent 408317b974
commit 0ca79b6c7d
9 changed files with 56 additions and 18 deletions

View File

@@ -7,8 +7,10 @@ const {
cmdEventsParticipants, cmdEventsParticipants,
cmdMatchesList, cmdMatchesList,
} = require('../cli/index'); } = require('../cli/index');
const { MATCH_STATUS } = require('../constants');
jest.mock('../utils/db', () => { jest.mock('../utils/db', () => {
const { MATCH_STATUS } = require('../constants');
const mock = { const mock = {
user: { user: {
findMany: jest.fn().mockResolvedValue([ findMany: jest.fn().mockResolvedValue([
@@ -64,7 +66,7 @@ jest.mock('../utils/db', () => {
}, },
match: { match: {
findMany: jest.fn().mockResolvedValue([ findMany: jest.fn().mockResolvedValue([
{ id: 301, slug: 'm1', status: 'pending', createdAt: new Date('2025-05-01'), event: { id: 200, slug: 'warsaw-dance-2025', name: 'Warsaw Dance 2025' }, user1: { id: 10, username: 'john_doe' }, user2: { id: 11, username: 'sarah' } }, { id: 301, slug: 'm1', status: MATCH_STATUS.PENDING, createdAt: new Date('2025-05-01'), event: { id: 200, slug: 'warsaw-dance-2025', name: 'Warsaw Dance 2025' }, user1: { id: 10, username: 'john_doe' }, user2: { id: 11, username: 'sarah' } },
]), ]),
}, },
}; };

View File

@@ -9,6 +9,7 @@ const request = require('supertest');
const app = require('../app'); const app = require('../app');
const { PrismaClient } = require('@prisma/client'); const { PrismaClient } = require('@prisma/client');
const { generateToken } = require('../utils/auth'); const { generateToken } = require('../utils/auth');
const { MATCH_STATUS } = require('../constants');
const prisma = new PrismaClient(); const prisma = new PrismaClient();
@@ -238,7 +239,7 @@ describe('Dashboard API', () => {
user2Id: testUser2.id, user2Id: testUser2.id,
eventId: testEvent.id, eventId: testEvent.id,
roomId: matchRoom.id, roomId: matchRoom.id,
status: 'accepted', status: MATCH_STATUS.ACCEPTED,
}, },
}); });
@@ -262,7 +263,7 @@ describe('Dashboard API', () => {
id: testEvent.id, id: testEvent.id,
name: testEvent.name, name: testEvent.name,
}, },
status: 'accepted', status: MATCH_STATUS.ACCEPTED,
}); });
}); });
@@ -344,7 +345,7 @@ describe('Dashboard API', () => {
user1Id: testUser2.id, user1Id: testUser2.id,
user2Id: testUser.id, user2Id: testUser.id,
eventId: testEvent.id, eventId: testEvent.id,
status: 'pending', status: MATCH_STATUS.PENDING,
}, },
}); });
@@ -388,7 +389,7 @@ describe('Dashboard API', () => {
user1Id: testUser.id, user1Id: testUser.id,
user2Id: testUser3.id, user2Id: testUser3.id,
eventId: testEvent.id, eventId: testEvent.id,
status: 'pending', status: MATCH_STATUS.PENDING,
}, },
}); });
@@ -473,7 +474,7 @@ describe('Dashboard API', () => {
where: { where: {
user1Id: testUser.id, user1Id: testUser.id,
user2Id: testUser2.id, user2Id: testUser2.id,
status: 'accepted', status: MATCH_STATUS.ACCEPTED,
}, },
include: { room: true }, include: { room: true },
}); });

View File

@@ -4,6 +4,7 @@ const { Server } = require('socket.io');
const Client = require('socket.io-client'); const Client = require('socket.io-client');
const { generateToken } = require('../utils/auth'); const { generateToken } = require('../utils/auth');
const { prisma } = require('../utils/db'); const { prisma } = require('../utils/db');
const { MATCH_STATUS } = require('../constants');
describe('Socket.IO WebRTC Signaling', () => { describe('Socket.IO WebRTC Signaling', () => {
let httpServer; let httpServer;
@@ -71,7 +72,7 @@ describe('Socket.IO WebRTC Signaling', () => {
user2Id: testUser2.id, user2Id: testUser2.id,
eventId: testEvent.id, eventId: testEvent.id,
roomId: chatRoom.id, roomId: chatRoom.id,
status: 'accepted', status: MATCH_STATUS.ACCEPTED,
}, },
}); });
@@ -209,7 +210,7 @@ describe('Socket.IO WebRTC Signaling', () => {
user2Id: testUser2.id, // Both same user user2Id: testUser2.id, // Both same user
eventId: testEvent.id, eventId: testEvent.id,
roomId: unauthorizedRoom.id, roomId: unauthorizedRoom.id,
status: 'accepted', status: MATCH_STATUS.ACCEPTED,
}, },
}); });

View File

@@ -0,0 +1,6 @@
const { MATCH_STATUS, SUGGESTION_STATUS } = require('./statuses');
module.exports = {
MATCH_STATUS,
SUGGESTION_STATUS,
};

View File

@@ -0,0 +1,24 @@
/**
* Match status constants
*/
const MATCH_STATUS = {
PENDING: 'pending',
ACCEPTED: 'accepted',
REJECTED: 'rejected',
COMPLETED: 'completed',
};
/**
* Suggestion status constants (for auto-matching)
*/
const SUGGESTION_STATUS = {
PENDING: 'pending',
ACCEPTED: 'accepted',
REJECTED: 'rejected',
NOT_FOUND: 'not_found',
};
module.exports = {
MATCH_STATUS,
SUGGESTION_STATUS,
};

View File

@@ -8,6 +8,7 @@ const router = express.Router();
const { authenticate } = require('../middleware/auth'); const { authenticate } = require('../middleware/auth');
const { PrismaClient } = require('@prisma/client'); const { PrismaClient } = require('@prisma/client');
const { getEventsOnlineCounts } = require('../socket'); const { getEventsOnlineCounts } = require('../socket');
const { MATCH_STATUS } = require('../constants');
const prisma = new PrismaClient(); const prisma = new PrismaClient();
@@ -101,7 +102,7 @@ router.get('/', authenticate, async (req, res, next) => {
{ user1Id: userId }, { user1Id: userId },
{ user2Id: userId }, { user2Id: userId },
], ],
status: 'accepted', status: MATCH_STATUS.ACCEPTED,
}, },
include: { include: {
user1: { user1: {
@@ -219,7 +220,7 @@ router.get('/', authenticate, async (req, res, next) => {
const incomingRequests = await prisma.match.findMany({ const incomingRequests = await prisma.match.findMany({
where: { where: {
user2Id: userId, user2Id: userId,
status: 'pending', status: MATCH_STATUS.PENDING,
}, },
include: { include: {
user1: { user1: {
@@ -298,7 +299,7 @@ router.get('/', authenticate, async (req, res, next) => {
const outgoingRequests = await prisma.match.findMany({ const outgoingRequests = await prisma.match.findMany({
where: { where: {
user1Id: userId, user1Id: userId,
status: 'pending', status: MATCH_STATUS.PENDING,
}, },
include: { include: {
user2: { user2: {

View File

@@ -3,6 +3,7 @@ const { prisma } = require('../utils/db');
const { authenticate } = require('../middleware/auth'); const { authenticate } = require('../middleware/auth');
const { getIO } = require('../socket'); const { getIO } = require('../socket');
const matchingService = require('../services/matching'); const matchingService = require('../services/matching');
const { SUGGESTION_STATUS } = require('../constants');
const router = express.Router(); const router = express.Router();
@@ -1122,8 +1123,8 @@ router.post('/:slug/run-matching', authenticate, async (req, res, next) => {
const count = await matchingService.saveMatchingResults(event.id, suggestions); const count = await matchingService.saveMatchingResults(event.id, suggestions);
// Get statistics // Get statistics
const notFoundCount = suggestions.filter(s => s.status === 'not_found').length; const notFoundCount = suggestions.filter(s => s.status === SUGGESTION_STATUS.NOT_FOUND).length;
const matchedCount = suggestions.filter(s => s.status === 'pending').length; const matchedCount = suggestions.filter(s => s.status === SUGGESTION_STATUS.PENDING).length;
res.json({ res.json({
success: true, success: true,

View File

@@ -2,6 +2,7 @@ const express = require('express');
const { prisma } = require('../utils/db'); const { prisma } = require('../utils/db');
const { authenticate } = require('../middleware/auth'); const { authenticate } = require('../middleware/auth');
const { getIO } = require('../socket'); const { getIO } = require('../socket');
const { MATCH_STATUS } = require('../constants');
const router = express.Router(); const router = express.Router();
@@ -98,7 +99,7 @@ router.post('/', authenticate, async (req, res, next) => {
user1Id: requesterId, user1Id: requesterId,
user2Id: targetUserId, user2Id: targetUserId,
eventId: event.id, eventId: event.id,
status: 'pending', status: MATCH_STATUS.PENDING,
}, },
include: { include: {
user1: { user1: {
@@ -534,7 +535,7 @@ router.put('/:slug/accept', authenticate, async (req, res, next) => {
const updated = await tx.match.update({ const updated = await tx.match.update({
where: { slug }, where: { slug },
data: { data: {
status: 'accepted', status: MATCH_STATUS.ACCEPTED,
roomId: chatRoom.id, roomId: chatRoom.id,
}, },
include: { include: {
@@ -828,7 +829,7 @@ router.post('/:slug/ratings', authenticate, async (req, res, next) => {
// Both users have rated - mark match as completed // Both users have rated - mark match as completed
await prisma.match.update({ await prisma.match.update({
where: { id: match.id }, where: { id: match.id },
data: { status: 'completed' }, data: { status: MATCH_STATUS.COMPLETED },
}); });
} }

View File

@@ -10,6 +10,7 @@
*/ */
const { prisma } = require('../utils/db'); const { prisma } = require('../utils/db');
const { SUGGESTION_STATUS } = require('../constants');
// Constants // Constants
const MAX_RECORDINGS_PER_PERSON = 3; const MAX_RECORDINGS_PER_PERSON = 3;
@@ -267,7 +268,7 @@ async function runMatching(eventId) {
eventId, eventId,
heatId: heat.id, heatId: heat.id,
recorderId: null, recorderId: null,
status: 'not_found', status: SUGGESTION_STATUS.NOT_FOUND,
}); });
continue; continue;
} }
@@ -285,7 +286,7 @@ async function runMatching(eventId) {
eventId, eventId,
heatId: heat.id, heatId: heat.id,
recorderId: best.recorder.userId, recorderId: best.recorder.userId,
status: 'pending', status: SUGGESTION_STATUS.PENDING,
}); });
// Update assignment count // Update assignment count