Skip to content

Commit be782ba

Browse files
committed
feat: Added returning or non-returning participants option in fetchAllGroups
1 parent db54f24 commit be782ba

File tree

7 files changed

+99
-9
lines changed

7 files changed

+99
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
### Features
44

55
* Native integration with chatwoot
6+
* Added returning or non-returning participants option in fetchAllGroups
7+
68
### Fixed
79

810
* Adjusts in docker-compose files
911
* Adjusts in number validation for AR and MX numbers
1012
* Adjusts in env files, removed save old_messages
1113
* Fix when sending a message to a group I don't belong returns a bad request
14+
* Fits the format on return from the fetchAllGroups endpoint
1215

1316
# 1.1.5 (2023-07-12 07:17)
1417

src/validate/validate.schema.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,16 @@ export const groupJidSchema: JSONSchema7 = {
700700
...isNotEmpty('groupJid'),
701701
};
702702

703+
export const getParticipantsSchema: JSONSchema7 = {
704+
$id: v4(),
705+
type: 'object',
706+
properties: {
707+
getParticipants: { type: 'string', enum: ['true', 'false'] },
708+
},
709+
required: ['getParticipants'],
710+
...isNotEmpty('getParticipants'),
711+
};
712+
703713
export const groupSendInviteSchema: JSONSchema7 = {
704714
$id: v4(),
705715
type: 'object',

src/whatsapp/abstract/abstract.router.ts

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { validate } from 'jsonschema';
55
import { BadRequestException } from '../../exceptions';
66
import 'express-async-errors';
77
import { Logger } from '../../config/logger.config';
8-
import { GroupInvite, GroupJid } from '../dto/group.dto';
8+
import { GetParticipant, GroupInvite, GroupJid } from '../dto/group.dto';
99

1010
type DataValidate<T> = {
1111
request: Request;
@@ -181,4 +181,47 @@ export abstract class RouterBroker {
181181

182182
return await execute(instance, ref);
183183
}
184+
185+
public async getParticipantsValidate<T>(args: DataValidate<T>) {
186+
const { request, ClassRef, schema, execute } = args;
187+
188+
const getParticipants = request.query as unknown as GetParticipant;
189+
190+
if (!getParticipants?.getParticipants) {
191+
throw new BadRequestException(
192+
'The getParticipants needs to be informed in the query',
193+
);
194+
}
195+
196+
const instance = request.params as unknown as InstanceDto;
197+
const body = request.body;
198+
199+
const ref = new ClassRef();
200+
201+
Object.assign(body, getParticipants);
202+
Object.assign(ref, body);
203+
204+
const v = validate(ref, schema);
205+
206+
console.log(v, '@checkei aqui');
207+
208+
if (!v.valid) {
209+
const message: any[] = v.errors.map(({ property, stack, schema }) => {
210+
let message: string;
211+
if (schema['description']) {
212+
message = schema['description'];
213+
} else {
214+
message = stack.replace('instance.', '');
215+
}
216+
return {
217+
property: property.replace('instance.', ''),
218+
message,
219+
};
220+
});
221+
logger.error([...message]);
222+
throw new BadRequestException(...message);
223+
}
224+
225+
return await execute(instance, ref);
226+
}
184227
}

src/whatsapp/controllers/group.controller.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
CreateGroupDto,
3+
GetParticipant,
34
GroupDescriptionDto,
45
GroupInvite,
56
GroupJid,
@@ -59,11 +60,13 @@ export class GroupController {
5960
return await this.waMonitor.waInstances[instance.instanceName].findGroup(groupJid);
6061
}
6162

62-
public async fetchAllGroups(instance: InstanceDto) {
63+
public async fetchAllGroups(instance: InstanceDto, getPaticipants: GetParticipant) {
6364
logger.verbose(
6465
'requested fetchAllGroups from ' + instance.instanceName + ' instance',
6566
);
66-
return await this.waMonitor.waInstances[instance.instanceName].fetchAllGroups();
67+
return await this.waMonitor.waInstances[instance.instanceName].fetchAllGroups(
68+
getPaticipants,
69+
);
6770
}
6871

6972
public async inviteCode(instance: InstanceDto, groupJid: GroupJid) {

src/whatsapp/dto/group.dto.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ export class GroupJid {
2323
groupJid: string;
2424
}
2525

26+
export class GetParticipant {
27+
getParticipants: string;
28+
}
29+
2630
export class GroupInvite {
2731
inviteCode: string;
2832
}

src/whatsapp/routers/group.router.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
updateGroupDescriptionSchema,
1111
groupInviteSchema,
1212
groupSendInviteSchema,
13+
getParticipantsSchema,
1314
} from '../../validate/validate.schema';
1415
import { RouterBroker } from '../abstract/abstract.router';
1516
import {
@@ -23,6 +24,7 @@ import {
2324
GroupUpdateSettingDto,
2425
GroupToggleEphemeralDto,
2526
GroupSendInvite,
27+
GetParticipant,
2628
} from '../dto/group.dto';
2729
import { groupController } from '../whatsapp.module';
2830
import { HttpStatus } from './index.router';
@@ -123,11 +125,11 @@ export class GroupRouter extends RouterBroker {
123125

124126
logger.verbose('request query: ');
125127
logger.verbose(req.query);
126-
const response = await this.groupNoValidate<GroupJid>({
128+
const response = await this.getParticipantsValidate<GetParticipant>({
127129
request: req,
128-
schema: {},
129-
ClassRef: GroupJid,
130-
execute: (instance) => groupController.fetchAllGroups(instance),
130+
schema: getParticipantsSchema,
131+
ClassRef: GetParticipant,
132+
execute: (instance, data) => groupController.fetchAllGroups(instance, data),
131133
});
132134

133135
res.status(HttpStatus.OK).json(response);

src/whatsapp/services/whatsapp.service.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ import {
109109
GroupSubjectDto,
110110
GroupDescriptionDto,
111111
GroupSendInvite,
112+
GetParticipant,
112113
} from '../dto/group.dto';
113114
import { MessageUpQuery } from '../repository/messageUp.repository';
114115
import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-db';
@@ -2567,10 +2568,34 @@ export class WAStartupService {
25672568
}
25682569
}
25692570

2570-
public async fetchAllGroups() {
2571+
public async fetchAllGroups(getParticipants: GetParticipant) {
25712572
this.logger.verbose('Fetching all groups');
25722573
try {
2573-
return await this.client.groupFetchAllParticipating();
2574+
const fetch = Object.values(await this.client.groupFetchAllParticipating());
2575+
2576+
const groups = fetch.map((group) => {
2577+
const result = {
2578+
id: group.id,
2579+
subject: group.subject,
2580+
subjectOwner: group.subjectOwner,
2581+
subjectTime: group.subjectTime,
2582+
size: group.size,
2583+
creation: group.creation,
2584+
owner: group.owner,
2585+
desc: group.desc,
2586+
descId: group.descId,
2587+
restrict: group.restrict,
2588+
announce: group.announce,
2589+
};
2590+
2591+
if (getParticipants.getParticipants == 'true') {
2592+
result['participants'] = group.participants;
2593+
}
2594+
2595+
return result;
2596+
});
2597+
2598+
return groups;
25742599
} catch (error) {
25752600
throw new NotFoundException('Error fetching group', error.toString());
25762601
}

0 commit comments

Comments
 (0)