diff --git a/CHANGELOG.md b/CHANGELOG.md index c0d822a6..bb334735 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,15 @@ ### Features * Native integration with chatwoot +* Added returning or non-returning participants option in fetchAllGroups + ### Fixed * Adjusts in docker-compose files * Adjusts in number validation for AR and MX numbers * Adjusts in env files, removed save old_messages * Fix when sending a message to a group I don't belong returns a bad request +* Fits the format on return from the fetchAllGroups endpoint # 1.1.5 (2023-07-12 07:17) diff --git a/src/validate/validate.schema.ts b/src/validate/validate.schema.ts index 03b01a4a..1fbb331e 100644 --- a/src/validate/validate.schema.ts +++ b/src/validate/validate.schema.ts @@ -700,6 +700,16 @@ export const groupJidSchema: JSONSchema7 = { ...isNotEmpty('groupJid'), }; +export const getParticipantsSchema: JSONSchema7 = { + $id: v4(), + type: 'object', + properties: { + getParticipants: { type: 'string', enum: ['true', 'false'] }, + }, + required: ['getParticipants'], + ...isNotEmpty('getParticipants'), +}; + export const groupSendInviteSchema: JSONSchema7 = { $id: v4(), type: 'object', diff --git a/src/whatsapp/abstract/abstract.router.ts b/src/whatsapp/abstract/abstract.router.ts index e0ed588c..e657c2c0 100644 --- a/src/whatsapp/abstract/abstract.router.ts +++ b/src/whatsapp/abstract/abstract.router.ts @@ -5,7 +5,7 @@ import { validate } from 'jsonschema'; import { BadRequestException } from '../../exceptions'; import 'express-async-errors'; import { Logger } from '../../config/logger.config'; -import { GroupInvite, GroupJid } from '../dto/group.dto'; +import { GetParticipant, GroupInvite, GroupJid } from '../dto/group.dto'; type DataValidate = { request: Request; @@ -181,4 +181,47 @@ export abstract class RouterBroker { return await execute(instance, ref); } + + public async getParticipantsValidate(args: DataValidate) { + const { request, ClassRef, schema, execute } = args; + + const getParticipants = request.query as unknown as GetParticipant; + + if (!getParticipants?.getParticipants) { + throw new BadRequestException( + 'The getParticipants needs to be informed in the query', + ); + } + + const instance = request.params as unknown as InstanceDto; + const body = request.body; + + const ref = new ClassRef(); + + Object.assign(body, getParticipants); + Object.assign(ref, body); + + const v = validate(ref, schema); + + console.log(v, '@checkei aqui'); + + 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); + } } diff --git a/src/whatsapp/controllers/group.controller.ts b/src/whatsapp/controllers/group.controller.ts index e0254796..f4d381ce 100644 --- a/src/whatsapp/controllers/group.controller.ts +++ b/src/whatsapp/controllers/group.controller.ts @@ -1,5 +1,6 @@ import { CreateGroupDto, + GetParticipant, GroupDescriptionDto, GroupInvite, GroupJid, @@ -59,11 +60,13 @@ export class GroupController { return await this.waMonitor.waInstances[instance.instanceName].findGroup(groupJid); } - public async fetchAllGroups(instance: InstanceDto) { + public async fetchAllGroups(instance: InstanceDto, getPaticipants: GetParticipant) { logger.verbose( 'requested fetchAllGroups from ' + instance.instanceName + ' instance', ); - return await this.waMonitor.waInstances[instance.instanceName].fetchAllGroups(); + return await this.waMonitor.waInstances[instance.instanceName].fetchAllGroups( + getPaticipants, + ); } public async inviteCode(instance: InstanceDto, groupJid: GroupJid) { diff --git a/src/whatsapp/dto/group.dto.ts b/src/whatsapp/dto/group.dto.ts index fba86ae2..bc36e27f 100644 --- a/src/whatsapp/dto/group.dto.ts +++ b/src/whatsapp/dto/group.dto.ts @@ -23,6 +23,10 @@ export class GroupJid { groupJid: string; } +export class GetParticipant { + getParticipants: string; +} + export class GroupInvite { inviteCode: string; } diff --git a/src/whatsapp/routers/group.router.ts b/src/whatsapp/routers/group.router.ts index 8cb3032e..4c1b3023 100644 --- a/src/whatsapp/routers/group.router.ts +++ b/src/whatsapp/routers/group.router.ts @@ -10,6 +10,7 @@ import { updateGroupDescriptionSchema, groupInviteSchema, groupSendInviteSchema, + getParticipantsSchema, } from '../../validate/validate.schema'; import { RouterBroker } from '../abstract/abstract.router'; import { @@ -23,6 +24,7 @@ import { GroupUpdateSettingDto, GroupToggleEphemeralDto, GroupSendInvite, + GetParticipant, } from '../dto/group.dto'; import { groupController } from '../whatsapp.module'; import { HttpStatus } from './index.router'; @@ -123,11 +125,11 @@ export class GroupRouter extends RouterBroker { logger.verbose('request query: '); logger.verbose(req.query); - const response = await this.groupNoValidate({ + const response = await this.getParticipantsValidate({ request: req, - schema: {}, - ClassRef: GroupJid, - execute: (instance) => groupController.fetchAllGroups(instance), + schema: getParticipantsSchema, + ClassRef: GetParticipant, + execute: (instance, data) => groupController.fetchAllGroups(instance, data), }); res.status(HttpStatus.OK).json(response); diff --git a/src/whatsapp/services/whatsapp.service.ts b/src/whatsapp/services/whatsapp.service.ts index e21e57c2..9d56730c 100644 --- a/src/whatsapp/services/whatsapp.service.ts +++ b/src/whatsapp/services/whatsapp.service.ts @@ -109,6 +109,7 @@ import { GroupSubjectDto, GroupDescriptionDto, GroupSendInvite, + GetParticipant, } from '../dto/group.dto'; import { MessageUpQuery } from '../repository/messageUp.repository'; import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-db'; @@ -2567,10 +2568,34 @@ export class WAStartupService { } } - public async fetchAllGroups() { + public async fetchAllGroups(getParticipants: GetParticipant) { this.logger.verbose('Fetching all groups'); try { - return await this.client.groupFetchAllParticipating(); + const fetch = Object.values(await this.client.groupFetchAllParticipating()); + + const groups = fetch.map((group) => { + const result = { + id: group.id, + subject: group.subject, + subjectOwner: group.subjectOwner, + subjectTime: group.subjectTime, + size: group.size, + creation: group.creation, + owner: group.owner, + desc: group.desc, + descId: group.descId, + restrict: group.restrict, + announce: group.announce, + }; + + if (getParticipants.getParticipants == 'true') { + result['participants'] = group.participants; + } + + return result; + }); + + return groups; } catch (error) { throw new NotFoundException('Error fetching group', error.toString()); }