feat: Route to fetch all groups that the connection is part of

This commit is contained in:
Davidson Gomes 2023-06-11 11:00:40 -03:00
parent 75b8bcb853
commit ea8d6bf6c8
5 changed files with 54 additions and 0 deletions

View File

@ -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

View File

@ -65,6 +65,37 @@ export abstract class RouterBroker {
return await execute(instance, ref);
}
public async groupNoValidate<T>(args: DataValidate<T>) {
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<T>(args: DataValidate<T>) {
const { request, ClassRef, schema, execute } = args;

View File

@ -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);
}

View File

@ -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<GroupJid>({
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<GroupJid>({
request: req,

View File

@ -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);