Skip to content

Commit 3bd25ec

Browse files
committed
matching service notification
1 parent 80a208c commit 3bd25ec

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

backend/matching/src/controllers/match-request.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,34 @@ export const matchRequestController = async (req: Request, res: Response) => {
4343

4444
logQueueStatus(logger, redisClient, `Queue Status Before Matching: <PLACEHOLDER>`);
4545
};
46+
47+
export const cancelMatchController = async (req: Request, res: Response) => {
48+
const { requestId, userId } = req.body;
49+
50+
if (!requestId && !userId) {
51+
return res.status(StatusCodes.UNPROCESSABLE_ENTITY).json('Malformed Request');
52+
}
53+
54+
if (!redisClient || !redisClient.isOpen || !redisClient.isReady) {
55+
redisClient = await client.connect();
56+
}
57+
58+
try {
59+
// Remove the user from the match queue
60+
const queueKey = `matchQueue:${requestId || userId}`;
61+
const result = await redisClient.del(queueKey);
62+
63+
if (result > 0) {
64+
logger.info(`Cancelled match request with requestId: ${requestId} or userId: ${userId}`);
65+
res.status(StatusCodes.OK).json({ message: 'Match request cancelled successfully.' });
66+
} else {
67+
logger.warn(`No match request found for requestId: ${requestId} or userId: ${userId}`);
68+
res.status(StatusCodes.NOT_FOUND).json({ error: 'No active match request found to cancel.' });
69+
}
70+
} catch (error) {
71+
logger.error('Error cancelling match request:', error);
72+
res
73+
.status(StatusCodes.INTERNAL_SERVER_ERROR)
74+
.json({ error: 'Failed to cancel match request.' });
75+
}
76+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { matchRequestController } from '@/controllers/match-request';
2+
import { cancelMatchController } from '@/controllers/match-request';
23
import { Router } from 'express';
34

45
const route = Router();
56

67
route.post('/request', matchRequestController);
8+
route.post('/cancel', cancelMatchController);
79

810
export default route;
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export const createNotifSocket = (userId: string) => {
2-
// TODO: Assign proper socket room
3-
return userId;
2+
const dateString = Date.now().toString(36);
3+
const roomId = `${userId}_${dateString}`;
4+
return roomId;
45
};

backend/matching/src/ws.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,33 @@ export const createWs = (server: ReturnType<(typeof http)['createServer']>) => {
77
io.on('connection', (socket) => {
88
logger.info(`${socket.id} connected`);
99

10-
socket.on('joinRoom', (room) => {
11-
socket.join(room);
12-
logger.info(`Client joined room: ${room}`);
13-
socket.to(room).emit('message', `A new user has joined room: ${room}`);
10+
socket.on('joinRoom', (roomId) => {
11+
if (!roomId) {
12+
logger.warn('joinRoom event received without a roomId');
13+
return;
14+
}
15+
socket.join(roomId);
16+
logger.info(`Socket ${socket.id} joined room: ${roomId}`);
17+
socket.emit('joinedRoom', roomId);
1418
});
1519
// socket.on('create', (room) => {
1620
// socket.join(room);
1721
// });
22+
socket.on('cancelRoom', (roomId) => {
23+
if (roomId) {
24+
io.in(roomId).socketsLeave(roomId);
25+
logger.info(`Room ${roomId} has been cancelled and closed.`);
26+
socket.emit('roomCancelled', roomId);
27+
} else {
28+
logger.warn('No room ID provided for cancellation');
29+
}
30+
});
1831
socket.on('leave', (room) => {
1932
socket.leave(room);
2033
});
34+
socket.on('disconnect', () => {
35+
logger.info(`Client disconnected: ${socket.id}`);
36+
});
2137
});
2238
return io;
2339
};

0 commit comments

Comments
 (0)