mirror of
https://github.com/EvolutionAPI/evolution-api.git
synced 2025-07-14 01:41:24 -06:00
feat: Added returning or non-returning participants option in fetchAllGroups
This commit is contained in:
parent
db54f247a2
commit
be782ba512
@ -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)
|
||||
|
||||
|
@ -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',
|
||||
|
@ -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<T> = {
|
||||
request: Request;
|
||||
@ -181,4 +181,47 @@ export abstract class RouterBroker {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -23,6 +23,10 @@ export class GroupJid {
|
||||
groupJid: string;
|
||||
}
|
||||
|
||||
export class GetParticipant {
|
||||
getParticipants: string;
|
||||
}
|
||||
|
||||
export class GroupInvite {
|
||||
inviteCode: string;
|
||||
}
|
||||
|
@ -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<GroupJid>({
|
||||
const response = await this.getParticipantsValidate<GetParticipant>({
|
||||
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);
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user