diff --git a/src/api/abstract/abstract.router.ts b/src/api/abstract/abstract.router.ts index 3ff633d58b..cf81d2ae6a 100644 --- a/src/api/abstract/abstract.router.ts +++ b/src/api/abstract/abstract.router.ts @@ -110,7 +110,9 @@ export abstract class RouterBroker { const { request, ClassRef, schema, execute } = args; const instance = request.params as unknown as InstanceDto; - const body = request.body; + // A GET without "Content-Type: application/json" arrives with request.body undefined, + // because express.json() only populates the body on JSON requests. + const body = request.body ?? {}; let groupJid = body?.groupJid; @@ -169,7 +171,8 @@ export abstract class RouterBroker { } const instance = request.params as unknown as InstanceDto; - const body = request.body; + // A GET without "Content-Type: application/json" arrives with request.body undefined. + const body = request.body ?? {}; const ref = new ClassRef(); @@ -208,7 +211,8 @@ export abstract class RouterBroker { } const instance = request.params as unknown as InstanceDto; - const body = request.body; + // A GET without "Content-Type: application/json" arrives with request.body undefined. + const body = request.body ?? {}; const ref = new ClassRef(); diff --git a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts index 22839fd451..5a0725ccd0 100644 --- a/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts +++ b/src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts @@ -4920,14 +4920,14 @@ export class BaileysStartupService extends ChannelStartupService { subjectOwner: group.subjectOwner, subjectTime: group.subjectTime, pictureUrl: picture.profilePictureUrl, - size: group.participants.length, + size: group.participants?.length ?? 0, creation: group.creation, owner: group.owner, desc: group.desc, descId: group.descId, restrict: group.restrict, announce: group.announce, - participants: group.participants, + participants: group.participants ?? [], isCommunity: group.isCommunity, isCommunityAnnounce: group.isCommunityAnnounce, linkedParent: group.linkedParent, @@ -4941,10 +4941,16 @@ export class BaileysStartupService extends ChannelStartupService { } public async fetchAllGroups(getParticipants: GetParticipant) { - const fetch = Object.values(await this?.client?.groupFetchAllParticipating()); + // groupFetchAllParticipating() can resolve to undefined/null (for example when the Baileys + // store is not synced yet right after a reconnection), which made Object.values throw + // "Cannot convert undefined or null to object". + const allGroups = await this?.client?.groupFetchAllParticipating(); + const fetch = allGroups ? Object.values(allGroups) : []; let groups = []; for (const group of fetch) { + if (!group) continue; + const picture = await this.profilePicture(group.id); const result = { @@ -4953,7 +4959,7 @@ export class BaileysStartupService extends ChannelStartupService { subjectOwner: group.subjectOwner, subjectTime: group.subjectTime, pictureUrl: picture?.profilePictureUrl, - size: group.participants.length, + size: group.participants?.length ?? 0, creation: group.creation, owner: group.owner, desc: group.desc, @@ -4966,7 +4972,7 @@ export class BaileysStartupService extends ChannelStartupService { }; if (getParticipants.getParticipants == 'true') { - result['participants'] = group.participants; + result['participants'] = group.participants ?? []; } groups = [...groups, result];