From 2c31ebf2c2b3d6478f9b7b97517d43f675665e12 Mon Sep 17 00:00:00 2001 From: Ofek Itscovits Date: Thu, 12 Jun 2025 11:23:19 +0300 Subject: [PATCH] feat: add search messages route --- apps/api/src/modules/chat/chat.route.ts | 61 ++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/apps/api/src/modules/chat/chat.route.ts b/apps/api/src/modules/chat/chat.route.ts index 2184f38..f486972 100644 --- a/apps/api/src/modules/chat/chat.route.ts +++ b/apps/api/src/modules/chat/chat.route.ts @@ -1,5 +1,5 @@ import { z } from "zod"; -import { and, count, desc, eq, ne, notExists } from "drizzle-orm"; +import { and, count, desc, eq, ilike, ne, notExists } from "drizzle-orm"; import { createTypiRouter, createTypiRoute, @@ -305,6 +305,65 @@ const chatRouter = createTypiRouter({ }, }), }), + "/:chatId/messages/search": createTypiRoute({ + get: createTypiRouteHandler("/:chatId/messages/search", { + input: { + query: z.object({ + searchTerm: z.string(), + }), + }, + middlewares: [authMiddleware], + handler: async (ctx) => { + const chatId = parseInt(ctx.input.path.chatId); + const userId = ctx.data.userId; + + const isParticipant = await db.query.chatParticipants.findFirst({ + where: and( + eq(chatParticipants.userId, userId), + eq(chatParticipants.chatId, chatId) + ), + }); + if (!isParticipant) + return ctx.error( + "UNAUTHORIZED", + "You are not a participant of this chat" + ); + + const chat = await db.query.chats.findFirst({ + where: eq(chats.id, chatId), + }); + if (!chat) return ctx.error("NOT_FOUND", "Chat not found."); + const chatMessages = await db.query.messages.findMany({ + where: and( + eq(messages.chatId, chatId), + ilike(messages.content, `%${ctx.input.query.searchTerm}%`) + ), + orderBy: [desc(messages.createdAt)], + with: { + sender: true, + readReceipnts: true, + attachments: true, + }, + }); + + const blockerIds = await getUserBlockers(userId); + + const processedMessages = chatMessages.map((message) => { + if (blockerIds.has(message.senderId)) { + return { + ...message, + sender: unavailableUserData(), + }; + } + return message; + }); + + return ctx.success({ + messages: processedMessages, + }); + }, + }), + }), }); export default chatRouter;