|
1 |
| -import { sql } from 'drizzle-orm'; |
2 | 1 | import type { Request, Response } from 'express';
|
3 | 2 | import { StatusCodes } from 'http-status-codes';
|
4 | 3 |
|
5 |
| -import { db, rooms } from '@/lib/db'; |
| 4 | +import { logger } from '@/lib/utils'; |
| 5 | +import { roomAuthService } from '@/service/get/room-auth-service'; |
6 | 6 |
|
7 |
| -export async function authCheck(req: Request, res: Response) { |
8 |
| - const roomId = req.query.roomId as string | undefined; |
9 |
| - const userId = req.query.userId as string | undefined; |
10 |
| - const questionId = req.query.questionId as string | undefined; |
| 7 | +type QueryParams = { |
| 8 | + roomId: string; |
| 9 | + userId: string; |
| 10 | +}; |
11 | 11 |
|
12 |
| - if (!roomId || !userId || !questionId) { |
13 |
| - return { |
14 |
| - code: StatusCodes.UNPROCESSABLE_ENTITY, |
15 |
| - error: { |
16 |
| - message: 'Malformed', |
17 |
| - }, |
18 |
| - }; |
| 12 | +// Returns the questionId if valid. |
| 13 | +export async function authCheck( |
| 14 | + req: Request<unknown, unknown, unknown, Partial<QueryParams>>, |
| 15 | + res: Response |
| 16 | +) { |
| 17 | + const { roomId, userId } = req.query; |
| 18 | + |
| 19 | + if (!roomId || !userId) { |
| 20 | + return res.status(StatusCodes.UNPROCESSABLE_ENTITY).json('Malformed request'); |
19 | 21 | }
|
20 | 22 |
|
21 | 23 | try {
|
22 |
| - const room = await db |
23 |
| - .select() |
24 |
| - .from(rooms) |
25 |
| - .where(sql`${rooms.roomId} = ${roomId} and ${rooms.questionId} = ${questionId}`) |
26 |
| - .limit(1); |
27 |
| - |
28 |
| - if (room.length === 0) { |
29 |
| - return res.status(StatusCodes.NOT_FOUND).json({ |
30 |
| - error: { |
31 |
| - message: 'Room not found', |
32 |
| - }, |
33 |
| - }); |
34 |
| - } |
35 |
| - |
36 |
| - const { userId1, userId2 } = room[0]; |
| 24 | + const response = await roomAuthService({ |
| 25 | + roomId, |
| 26 | + userId, |
| 27 | + }); |
37 | 28 |
|
38 |
| - if (userId !== userId1 && userId !== userId2) { |
39 |
| - return res.status(StatusCodes.FORBIDDEN).json({ |
40 |
| - code: StatusCodes.FORBIDDEN, |
41 |
| - error: { message: 'User is not authorized to access this room' }, |
42 |
| - }); |
| 29 | + if (response.data) { |
| 30 | + return res.status(response.code).json(response.data); |
43 | 31 | }
|
44 | 32 |
|
45 |
| - return res.status(StatusCodes.OK).json({ |
46 |
| - code: StatusCodes.OK, |
47 |
| - data: { roomId }, |
48 |
| - }); |
| 33 | + return res |
| 34 | + .status(response.code) |
| 35 | + .json({ error: response.error || { message: 'An error occurred.' } }); |
49 | 36 | } catch (error) {
|
50 |
| - console.error('Error authenticating room:', error); |
| 37 | + const { name, stack, cause, message } = error as Error; |
| 38 | + logger.error('Error authenticating room: ' + JSON.stringify({ name, stack, message, cause })); |
51 | 39 | return res.status(StatusCodes.INTERNAL_SERVER_ERROR).json({
|
52 |
| - code: StatusCodes.INTERNAL_SERVER_ERROR, |
53 | 40 | error: { message: 'An error occurred while authenticating the room' },
|
54 | 41 | });
|
55 | 42 | }
|
|
0 commit comments