conversation.controller.ts75 lines, 3.0 KB
| 1 | import { asyncHandler, HttpError, type AsyncHandler } from '@chatapp/common'; |
| 2 | import type { RequestHandler } from 'express'; |
| 3 | import { conversationService } from '@/services/conversation.service'; |
| 4 | import { |
| 5 | createConversationSchema, |
| 6 | listConversationsQuerySchema, |
| 7 | } from '@/validation/conversation.schema'; |
| 8 | import { conversationIdParamsSchema } from '@/validation/shared.schema'; |
| 9 | import { getAuthenticatedUser } from '@/utils/auth'; |
| 10 | import { createMessageBodySchema, listMessagesQuerySchema } from '@/validation/message.schema'; |
| 11 | import { messageService } from '@/services/message.service'; |
| 12 | |
| 13 | const parsedConversation = (params: unknown) => { |
| 14 | const { id } = conversationIdParamsSchema.parse(params); |
| 15 | return id; |
| 16 | }; |
| 17 | |
| 18 | export const createConversationHandler: RequestHandler = asyncHandler(async (req, res) => { |
| 19 | const user = getAuthenticatedUser(req); |
| 20 | const payload = createConversationSchema.parse(req.body); |
| 21 | const uniqueParticipantIds = Array.from(new Set([...payload.participantIds, user.id])); |
| 22 | |
| 23 | if (uniqueParticipantIds.length < 2) { |
| 24 | throw new HttpError(400, 'Conversation must atleast include one other participant'); |
| 25 | } |
| 26 | |
| 27 | const conversation = await conversationService.createConversation({ |
| 28 | title: payload.title, |
| 29 | participantIds: uniqueParticipantIds, |
| 30 | }); |
| 31 | res.status(201).json({ data: conversation }); |
| 32 | }); |
| 33 | |
| 34 | export const listConversationHandler: RequestHandler = asyncHandler(async (req, res) => { |
| 35 | const user = getAuthenticatedUser(req); |
| 36 | const filter = listConversationsQuerySchema.parse(req.query); |
| 37 | if (filter.participantId && filter.participantId !== user.id) { |
| 38 | throw new HttpError(403, 'Unauthorized'); |
| 39 | } |
| 40 | |
| 41 | const conversations = await conversationService.listConversation({ participantId: user.id }); |
| 42 | res.status(201).json({ data: conversations }); |
| 43 | }); |
| 44 | |
| 45 | export const getConversationHandler: RequestHandler = asyncHandler(async (req, res) => { |
| 46 | const user = getAuthenticatedUser(req); |
| 47 | const conversationId = parsedConversation(req.params); |
| 48 | const conversation = await conversationService.getConversationById(conversationId); |
| 49 | |
| 50 | if (!conversation.participantIds.includes(user.id)) { |
| 51 | throw new HttpError(403, 'Unauthorized'); |
| 52 | } |
| 53 | |
| 54 | res.status(201).json({ data: conversation }); |
| 55 | }); |
| 56 | |
| 57 | export const createMessageHandler: RequestHandler = asyncHandler(async (req, res) => { |
| 58 | const user = getAuthenticatedUser(req); |
| 59 | const conversationId = parsedConversation(req.params); |
| 60 | const payload = createMessageBodySchema.parse(req.body); |
| 61 | const message = await messageService.createMessage(conversationId, user.id, payload.body); |
| 62 | res.status(201).json({ data: message }); |
| 63 | }); |
| 64 | |
| 65 | export const listMessageHandler: RequestHandler = asyncHandler(async (req, res) => { |
| 66 | const user = getAuthenticatedUser(req); |
| 67 | const conversationId = parsedConversation(req.params); |
| 68 | const query = listMessagesQuerySchema.parse(req.query); |
| 69 | const after = query.after ? new Date(query.after) : undefined; |
| 70 | const messages = await messageService.listMessages(conversationId, user.id, { |
| 71 | limit: query.limit, |
| 72 | after, |
| 73 | }); |
| 74 | res.json({ data: messages }); |
| 75 | }); |