Skip to content

Commit 6fccffc

Browse files
committed
adding cancel endpoint for matching
1 parent 16cb511 commit 6fccffc

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Request, Response } from 'express';
2+
3+
import { client as redisClient } from '../lib/db';
4+
5+
export const cancelMatchRequestController = async (req: Request, res: Response) => {
6+
const { userId } = req.body; // Only check for userId
7+
8+
if (!userId) {
9+
return res.status(400).json({ message: 'User ID is required' }); // No need for roomId
10+
}
11+
12+
try {
13+
if (!redisClient.isOpen) {
14+
await redisClient.connect();
15+
}
16+
17+
// Check pending status using only userId
18+
const exists = await redisClient.exists(`match:${userId}`);
19+
20+
if (exists) {
21+
console.log(`Redis key for user ${userId} exists.`);
22+
} else {
23+
console.error(`Redis key for user ${userId} does NOT exist!`);
24+
}
25+
26+
const pendingStatus = await redisClient.hGet(`match:${userId}`, 'pending');
27+
console.log(pendingStatus);
28+
const data = await redisClient.hGetAll(`match:${userId}`);
29+
console.log(`Data stored in Redis for user ${userId}:`, data);
30+
31+
if (pendingStatus === 'true') {
32+
// Allow cancellation and remove from queue based on userId
33+
await redisClient.del(`match:${userId}`);
34+
console.log(`User ${userId} has canceled their match.`);
35+
return res.status(200).json({ message: 'Match canceled successfully' });
36+
} else {
37+
// If the match is no longer pending, deny cancellation
38+
console.log(`User ${userId} is no longer pending and cannot cancel.`);
39+
return res
40+
.status(403)
41+
.json({ message: 'Match cancellation failed: Request is not pending.' });
42+
}
43+
} catch (error) {
44+
console.error('Error canceling match:', error);
45+
return res.status(500).json({ message: 'Server error, please try again later' });
46+
}
47+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import { Router } from 'express';
22

3+
import { cancelMatchRequestController } from '@/controllers/cancel-request';
34
import { matchRequestController } from '@/controllers/match-request';
45

56
const route = Router();
67

78
route.post('/request', matchRequestController);
9+
route.post('/cancel', cancelMatchRequestController);
810

911
export default route;

backend/matching/src/workers/matcher.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ async function processMatch(
5858
continue;
5959
}
6060

61+
const setPendingToFalse = async (userId: string) => {
62+
await redisClient.hSet(`match:${userId}`, 'pending', 'false');
63+
console.log(`User ${userId} is now in MATCHING state, pending set to false.`);
64+
};
65+
66+
await Promise.all([
67+
setPendingToFalse(requestorUserId), // Set pending to false for requester
68+
setPendingToFalse(matchedUserId), // Set pending to false for matched user
69+
]);
70+
6171
// To block cancellation
6272
sendNotif([matchedSocketPort], MATCHING_EVENT.MATCHING);
6373

@@ -81,7 +91,6 @@ async function processMatch(
8191
matchedUserId
8292
);
8393
logger.info(`Generated Match - ${JSON.stringify(matchItems)}`);
84-
8594
sendNotif([requestorSocketPort, matchedSocketPort], MATCHING_EVENT.SUCCESS, matchItems);
8695
sendNotif([requestorSocketPort, matchedSocketPort], MATCHING_EVENT.DISCONNECT);
8796

package-lock.json

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)