|
| 1 | +import type { Request, Response } from 'express'; |
| 2 | +import { StatusCodes } from 'http-status-codes'; |
| 3 | + |
| 4 | +import { client } from '@/lib/db'; |
| 5 | +import type { IRedisClient, IRequestMatchPayload } from '@/types'; |
| 6 | +import { createNotifSocket, queueingService } from '@/services'; |
| 7 | + |
| 8 | +let redisClient: IRedisClient; |
| 9 | +export const matchRequestController = async (req: Request, res: Response) => { |
| 10 | + const payload: Partial<IRequestMatchPayload> = req.body; |
| 11 | + const { userId, difficulty, topic } = payload; |
| 12 | + if (!userId || (!difficulty && !topic)) { |
| 13 | + return res.status(StatusCodes.UNPROCESSABLE_ENTITY).json('Malformed Request'); |
| 14 | + } |
| 15 | + |
| 16 | + if (!redisClient || !redisClient.isOpen || !redisClient.isReady) { |
| 17 | + redisClient = await client.connect(); |
| 18 | + } |
| 19 | + |
| 20 | + // TODO: Assign a proper socket to the user |
| 21 | + const socketRoom = createNotifSocket(userId); |
| 22 | + |
| 23 | + // TODO: Test if room logic works and notif socket can connect |
| 24 | + // Send socket to user first for them to subscribe |
| 25 | + res |
| 26 | + .status(StatusCodes.OK) |
| 27 | + .json({ |
| 28 | + socketPort: socketRoom, |
| 29 | + }) |
| 30 | + .end(); |
| 31 | + |
| 32 | + // TODO: Wait for user to connect to notif socket, or add a time buffer |
| 33 | + |
| 34 | + await queueingService(redisClient, { userId, difficulty, topic, socketPort: socketRoom }); |
| 35 | +}; |
0 commit comments