1- import { Request , Response } from 'express' ;
1+ import type { Request , Response } from 'express' ;
2+ import { StatusCodes } from 'http-status-codes' ;
23
3- import { client as redisClient } from '../lib/db' ;
4+ import { client as redisClient , logQueueStatus } from '@/lib/db' ;
5+ import { STREAM_NAME } from '@/lib/db/constants' ;
6+ import { getPoolKey , getStreamId , logger } from '@/lib/utils' ;
7+ import { io } from '@/server' ;
8+ import { MATCHING_EVENT } from '@/ws/events' ;
49
510export const cancelMatchRequestController = async ( req : Request , res : Response ) => {
611 const { userId } = req . body ; // Only check for userId
712
813 if ( ! userId ) {
9- return res . status ( 400 ) . json ( { message : 'User ID is required' } ) ; // No need for roomId
14+ return res . status ( StatusCodes . UNPROCESSABLE_ENTITY ) . json ( { message : 'User ID is required' } ) ; // No need for roomId
1015 }
1116
1217 try {
@@ -15,20 +20,52 @@ export const cancelMatchRequestController = async (req: Request, res: Response)
1520 }
1621
1722 // Check pending status using only userId
18- const pendingStatus = await redisClient . hGet ( `match:${ userId } ` , 'pending' ) ;
19- console . log ( pendingStatus ) ;
20-
21- if ( pendingStatus === 'true' ) {
22- // Allow cancellation and remove from queue based on userId
23- await redisClient . del ( `match:${ userId } ` ) ;
24- console . log ( `User ${ userId } has canceled their match.` ) ;
25- return res . status ( 200 ) . json ( { message : 'Match canceled successfully' } ) ;
26- } else {
27- // If the match is no longer pending, deny cancellation
28- console . log ( `User ${ userId } is no longer pending and cannot cancel.` ) ;
29- return res
30- . status ( 403 )
31- . json ( { message : 'Match cancellation failed: Request is not pending.' } ) ;
23+ const result = await redisClient
24+ . hGetAll ( getPoolKey ( userId ) )
25+ . then ( async ( value ) => {
26+ if ( value . pending === 'true' ) {
27+ const timestamp = value . timestamp ;
28+ await Promise . all ( [
29+ redisClient . del ( getPoolKey ( userId ) ) ,
30+ timestamp
31+ ? redisClient . xDel ( STREAM_NAME , getStreamId ( value . timestamp ) )
32+ : Promise . resolve ( ) ,
33+ ] ) ;
34+ await logQueueStatus (
35+ logger ,
36+ redisClient ,
37+ 'Queue Status after cancelling request: <PLACEHOLDER>'
38+ ) ;
39+ logger . info ( `Request cancellation successful` ) ;
40+ const room = value . socketPort ;
41+
42+ if ( room ) {
43+ io . sockets . in ( room ) . socketsLeave ( room ) ;
44+ }
45+
46+ return {
47+ success : true ,
48+ } ;
49+ }
50+
51+ return {
52+ success : false ,
53+ error : `Match in ${ MATCHING_EVENT . MATCHING } state.` ,
54+ } ;
55+ } )
56+ . catch ( ( reason ) => {
57+ if ( reason ) {
58+ return {
59+ success : false ,
60+ error : reason ,
61+ } ;
62+ }
63+ } ) ;
64+
65+ if ( result ?. success ) {
66+ return res . status ( StatusCodes . OK ) . end ( ) ;
67+ } else if ( ! result ?. success ) {
68+ return res . status ( StatusCodes . FORBIDDEN ) . json ( result ?. error ) ;
3269 }
3370 } catch ( error ) {
3471 console . error ( 'Error canceling match:' , error ) ;
0 commit comments