conversation.controller.ts71 lines, 2.7 KB
| 1 | import type { RequestHandler } from 'express'; |
| 2 | |
| 3 | import { chatProxyService } from '@/services/chat-proxy.service'; |
| 4 | import { getAuthenticatedUser } from '@/utils/auth'; |
| 5 | import { |
| 6 | createConversationBodySchema, |
| 7 | listConversationsQuerySchema, |
| 8 | conversationIdParamsSchema, |
| 9 | } from '@/validation/conversation.schema'; |
| 10 | import { asyncHandler, HttpError } from '@chatapp/common'; |
| 11 | import { createMessageBodySchema, listMessagesQuerySchema } from '@/validation/message.schema'; |
| 12 | |
| 13 | export const createConversationHandler: RequestHandler = asyncHandler(async (req, res) => { |
| 14 | const user = getAuthenticatedUser(req); |
| 15 | const payload = createConversationBodySchema.parse(req.body); |
| 16 | |
| 17 | const uniqueParticipantIds = Array.from(new Set([...payload.participantIds, user.id])); |
| 18 | |
| 19 | if (uniqueParticipantIds.length < 2) { |
| 20 | throw new HttpError(400, 'Conversation must atleast include one other participant'); |
| 21 | } |
| 22 | |
| 23 | const conversation = await chatProxyService.createConversation(user.id, { |
| 24 | title: payload.title, |
| 25 | participantIds: uniqueParticipantIds, |
| 26 | }); |
| 27 | |
| 28 | res.status(201).json({ data: conversation }); |
| 29 | }); |
| 30 | |
| 31 | export const listConversationsHandler: RequestHandler = asyncHandler(async (req, res) => { |
| 32 | const user = getAuthenticatedUser(req); |
| 33 | const { participantId } = listConversationsQuerySchema.parse(req.query); |
| 34 | |
| 35 | if (participantId && participantId !== user.id) { |
| 36 | throw new HttpError(403, 'Cannot list conversations for another user'); |
| 37 | } |
| 38 | |
| 39 | const conversations = await chatProxyService.listConversations(user.id); |
| 40 | res.json({ data: conversations }); |
| 41 | }); |
| 42 | |
| 43 | export const getConversationHandler: RequestHandler = asyncHandler(async (req, res) => { |
| 44 | const user = getAuthenticatedUser(req); |
| 45 | const { id } = conversationIdParamsSchema.parse(req.params); |
| 46 | const conversation = await chatProxyService.getConversation(id, user.id); |
| 47 | |
| 48 | if (!conversation.participantIds.includes(user.id)) { |
| 49 | throw new HttpError(403, 'You are not a participant in this conversation'); |
| 50 | } |
| 51 | |
| 52 | res.json({ data: conversation }); |
| 53 | }); |
| 54 | |
| 55 | export const createMessageHandler: RequestHandler = asyncHandler(async (req, res) => { |
| 56 | const user = getAuthenticatedUser(req); |
| 57 | const { id } = conversationIdParamsSchema.parse(req.params); |
| 58 | const payload = createMessageBodySchema.parse(req.body); |
| 59 | const message = await chatProxyService.createMessage(id, user.id, { |
| 60 | body: payload.body, |
| 61 | }); |
| 62 | res.status(201).json({ data: message }); |
| 63 | }); |
| 64 | |
| 65 | export const listMessagesHandler: RequestHandler = asyncHandler(async (req, res) => { |
| 66 | const user = getAuthenticatedUser(req); |
| 67 | const { id } = conversationIdParamsSchema.parse(req.params); |
| 68 | const query = listMessagesQuerySchema.parse(req.query); |
| 69 | const messages = await chatProxyService.listMessages(id, user.id, query); |
| 70 | res.json({ data: messages }); |
| 71 | }); |