From ea8d6bf6c8e82f3bc8b54c14fbae9c45b85d3c25 Mon Sep 17 00:00:00 2001 From: Davidson Gomes Date: Sun, 11 Jun 2023 11:00:40 -0300 Subject: [PATCH] feat: Route to fetch all groups that the connection is part of --- CHANGELOG.md | 1 + src/whatsapp/abstract/abstract.router.ts | 31 ++++++++++++++++++++ src/whatsapp/controllers/group.controller.ts | 4 +++ src/whatsapp/routers/group.router.ts | 10 +++++++ src/whatsapp/services/whatsapp.service.ts | 8 +++++ 5 files changed, 54 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3800afc..6d6ea723 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * Improved fetch instances endpoint, now it also fetch other instances even if they are not connected * Added conversion of audios for sending recorded audio, now it is possible to send mp3 audios and not just ogg +* Route to fetch all groups that the connection is part of ### Fixed diff --git a/src/whatsapp/abstract/abstract.router.ts b/src/whatsapp/abstract/abstract.router.ts index 3da5d43d..e0ed588c 100644 --- a/src/whatsapp/abstract/abstract.router.ts +++ b/src/whatsapp/abstract/abstract.router.ts @@ -65,6 +65,37 @@ export abstract class RouterBroker { return await execute(instance, ref); } + public async groupNoValidate(args: DataValidate) { + const { request, ClassRef, schema, execute } = args; + + const instance = request.params as unknown as InstanceDto; + + const ref = new ClassRef(); + + Object.assign(ref, request.body); + + const v = validate(ref, schema); + + if (!v.valid) { + const message: any[] = v.errors.map(({ property, stack, schema }) => { + let message: string; + if (schema['description']) { + message = schema['description']; + } else { + message = stack.replace('instance.', ''); + } + return { + property: property.replace('instance.', ''), + message, + }; + }); + logger.error([...message]); + throw new BadRequestException(...message); + } + + return await execute(instance, ref); + } + public async groupValidate(args: DataValidate) { const { request, ClassRef, schema, execute } = args; diff --git a/src/whatsapp/controllers/group.controller.ts b/src/whatsapp/controllers/group.controller.ts index 7fd16f2d..764e26cd 100644 --- a/src/whatsapp/controllers/group.controller.ts +++ b/src/whatsapp/controllers/group.controller.ts @@ -27,6 +27,10 @@ export class GroupController { return await this.waMonitor.waInstances[instance.instanceName].findGroup(groupJid); } + public async fetchAllGroups(instance: InstanceDto) { + return await this.waMonitor.waInstances[instance.instanceName].fetchAllGroups(); + } + public async inviteCode(instance: InstanceDto, groupJid: GroupJid) { return await this.waMonitor.waInstances[instance.instanceName].inviteCode(groupJid); } diff --git a/src/whatsapp/routers/group.router.ts b/src/whatsapp/routers/group.router.ts index 7803a3d6..aaf0a60c 100644 --- a/src/whatsapp/routers/group.router.ts +++ b/src/whatsapp/routers/group.router.ts @@ -55,6 +55,16 @@ export class GroupRouter extends RouterBroker { res.status(HttpStatus.OK).json(response); }) + .get(this.routerPath('fetchAllGroups'), ...guards, async (req, res) => { + const response = await this.groupNoValidate({ + request: req, + schema: {}, + ClassRef: GroupJid, + execute: (instance) => groupController.fetchAllGroups(instance), + }); + + res.status(HttpStatus.OK).json(response); + }) .get(this.routerPath('participants'), ...guards, async (req, res) => { const response = await this.groupValidate({ request: req, diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index 324bbabe..4441302b 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -1610,6 +1610,14 @@ export class WAStartupService { } } + public async fetchAllGroups() { + try { + return await this.client.groupFetchAllParticipating(); + } catch (error) { + throw new NotFoundException('Error fetching group', error.toString()); + } + } + public async inviteCode(id: GroupJid) { try { const code = await this.client.groupInviteCode(id.groupJid);