From 68e847d10e2e6cd7429f914b20dbd0a10c3e29fc Mon Sep 17 00:00:00 2001 From: foqc Date: Mon, 21 Jul 2025 12:21:45 -0500 Subject: [PATCH] feature: add endpoint to retrieve chat data by phone number --- src/api/controllers/chat.controller.ts | 4 ++++ src/api/routes/chat.router.ts | 10 ++++++++++ src/api/services/channel.service.ts | 10 ++++++++++ 3 files changed, 24 insertions(+) diff --git a/src/api/controllers/chat.controller.ts b/src/api/controllers/chat.controller.ts index 207d8ba5..22e90b9f 100644 --- a/src/api/controllers/chat.controller.ts +++ b/src/api/controllers/chat.controller.ts @@ -70,6 +70,10 @@ export class ChatController { return await this.waMonitor.waInstances[instanceName].fetchChats(query); } + public async findChatByRemoteJid({ instanceName }: InstanceDto, remoteJid: string) { + return await this.waMonitor.waInstances[instanceName].findChatByRemoteJid(remoteJid); + } + public async sendPresence({ instanceName }: InstanceDto, data: SendPresenceDto) { return await this.waMonitor.waInstances[instanceName].sendPresence(data); } diff --git a/src/api/routes/chat.router.ts b/src/api/routes/chat.router.ts index a1094b5e..e99e8900 100644 --- a/src/api/routes/chat.router.ts +++ b/src/api/routes/chat.router.ts @@ -191,6 +191,16 @@ export class ChatRouter extends RouterBroker { return res.status(HttpStatus.OK).json(response); }) + .get(this.routerPath('findChatByRemoteJid'), ...guards, async (req, res) => { + const instance = req.params as unknown as InstanceDto; + const { remoteJid } = req.query as unknown as { remoteJid: string }; + if (!remoteJid) { + return res.status(HttpStatus.BAD_REQUEST).json({ error: "remoteJid is a required query parameter" }); + } + const response = await chatController.findChatByRemoteJid(instance, remoteJid); + + return res.status(HttpStatus.OK).json(response); + }) // Profile routes .post(this.routerPath('fetchBusinessProfile'), ...guards, async (req, res) => { const response = await this.dataValidate({ diff --git a/src/api/services/channel.service.ts b/src/api/services/channel.service.ts index cd94d2d2..dc754ab6 100644 --- a/src/api/services/channel.service.ts +++ b/src/api/services/channel.service.ts @@ -696,6 +696,16 @@ export class ChannelStartupService { }); } + public async findChatByRemoteJid(remoteJid: string) { + if (!remoteJid) return null; + return await this.prismaRepository.chat.findFirst({ + where: { + instanceId: this.instanceId, + remoteJid: remoteJid, + }, + }); + } + public async fetchChats(query: any) { const remoteJid = query?.where?.remoteJid ? query?.where?.remoteJid.includes('@')