feat: Added returning or non-returning participants option in fetchAllGroups

This commit is contained in:
Davidson Gomes 2023-07-13 07:19:32 -03:00
parent db54f247a2
commit be782ba512
7 changed files with 99 additions and 9 deletions

View File

@ -3,12 +3,15 @@
### Features ### Features
* Native integration with chatwoot * Native integration with chatwoot
* Added returning or non-returning participants option in fetchAllGroups
### Fixed ### Fixed
* Adjusts in docker-compose files * Adjusts in docker-compose files
* Adjusts in number validation for AR and MX numbers * Adjusts in number validation for AR and MX numbers
* Adjusts in env files, removed save old_messages * Adjusts in env files, removed save old_messages
* Fix when sending a message to a group I don't belong returns a bad request * 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) # 1.1.5 (2023-07-12 07:17)

View File

@ -700,6 +700,16 @@ export const groupJidSchema: JSONSchema7 = {
...isNotEmpty('groupJid'), ...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 = { export const groupSendInviteSchema: JSONSchema7 = {
$id: v4(), $id: v4(),
type: 'object', type: 'object',

View File

@ -5,7 +5,7 @@ import { validate } from 'jsonschema';
import { BadRequestException } from '../../exceptions'; import { BadRequestException } from '../../exceptions';
import 'express-async-errors'; import 'express-async-errors';
import { Logger } from '../../config/logger.config'; import { Logger } from '../../config/logger.config';
import { GroupInvite, GroupJid } from '../dto/group.dto'; import { GetParticipant, GroupInvite, GroupJid } from '../dto/group.dto';
type DataValidate<T> = { type DataValidate<T> = {
request: Request; request: Request;
@ -181,4 +181,47 @@ export abstract class RouterBroker {
return await execute(instance, ref); return await execute(instance, ref);
} }
public async getParticipantsValidate<T>(args: DataValidate<T>) {
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);
}
} }

View File

@ -1,5 +1,6 @@
import { import {
CreateGroupDto, CreateGroupDto,
GetParticipant,
GroupDescriptionDto, GroupDescriptionDto,
GroupInvite, GroupInvite,
GroupJid, GroupJid,
@ -59,11 +60,13 @@ export class GroupController {
return await this.waMonitor.waInstances[instance.instanceName].findGroup(groupJid); return await this.waMonitor.waInstances[instance.instanceName].findGroup(groupJid);
} }
public async fetchAllGroups(instance: InstanceDto) { public async fetchAllGroups(instance: InstanceDto, getPaticipants: GetParticipant) {
logger.verbose( logger.verbose(
'requested fetchAllGroups from ' + instance.instanceName + ' instance', '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) { public async inviteCode(instance: InstanceDto, groupJid: GroupJid) {

View File

@ -23,6 +23,10 @@ export class GroupJid {
groupJid: string; groupJid: string;
} }
export class GetParticipant {
getParticipants: string;
}
export class GroupInvite { export class GroupInvite {
inviteCode: string; inviteCode: string;
} }

View File

@ -10,6 +10,7 @@ import {
updateGroupDescriptionSchema, updateGroupDescriptionSchema,
groupInviteSchema, groupInviteSchema,
groupSendInviteSchema, groupSendInviteSchema,
getParticipantsSchema,
} from '../../validate/validate.schema'; } from '../../validate/validate.schema';
import { RouterBroker } from '../abstract/abstract.router'; import { RouterBroker } from '../abstract/abstract.router';
import { import {
@ -23,6 +24,7 @@ import {
GroupUpdateSettingDto, GroupUpdateSettingDto,
GroupToggleEphemeralDto, GroupToggleEphemeralDto,
GroupSendInvite, GroupSendInvite,
GetParticipant,
} from '../dto/group.dto'; } from '../dto/group.dto';
import { groupController } from '../whatsapp.module'; import { groupController } from '../whatsapp.module';
import { HttpStatus } from './index.router'; import { HttpStatus } from './index.router';
@ -123,11 +125,11 @@ export class GroupRouter extends RouterBroker {
logger.verbose('request query: '); logger.verbose('request query: ');
logger.verbose(req.query); logger.verbose(req.query);
const response = await this.groupNoValidate<GroupJid>({ const response = await this.getParticipantsValidate<GetParticipant>({
request: req, request: req,
schema: {}, schema: getParticipantsSchema,
ClassRef: GroupJid, ClassRef: GetParticipant,
execute: (instance) => groupController.fetchAllGroups(instance), execute: (instance, data) => groupController.fetchAllGroups(instance, data),
}); });
res.status(HttpStatus.OK).json(response); res.status(HttpStatus.OK).json(response);

View File

@ -109,6 +109,7 @@ import {
GroupSubjectDto, GroupSubjectDto,
GroupDescriptionDto, GroupDescriptionDto,
GroupSendInvite, GroupSendInvite,
GetParticipant,
} from '../dto/group.dto'; } from '../dto/group.dto';
import { MessageUpQuery } from '../repository/messageUp.repository'; import { MessageUpQuery } from '../repository/messageUp.repository';
import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-db'; 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'); this.logger.verbose('Fetching all groups');
try { 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) { } catch (error) {
throw new NotFoundException('Error fetching group', error.toString()); throw new NotFoundException('Error fetching group', error.toString());
} }