Skip to content

Commit ab5eb80

Browse files
committed
Refactor JID creation and fetch chats
- Introduced a new utility function `createJid` to standardize JID creation across the application, replacing the previous method in `ChannelStartupService`. - Updated multiple services to utilize the new `createJid` function for improved consistency and maintainability. - Added a `cleanMessageData` method in `ChannelStartupService` to sanitize message objects before processing. - Updated CHANGELOG to reflect the refactor on chat fetching logic and the introduction of the new JID utility.
1 parent b0219e5 commit ab5eb80

File tree

6 files changed

+262
-176
lines changed

6 files changed

+262
-176
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
* Correction of webhook global
1111
* Fixed send audio with whatsapp cloud api
12+
* Refactor on fetch chats
1213

1314
# 2.2.0 (2024-10-18 10:00)
1415

src/api/integrations/channel/evolution/evolution.channel.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { ChannelStartupService } from '@api/services/channel.service';
77
import { Events, wa } from '@api/types/wa.types';
88
import { Chatwoot, ConfigService, Openai } from '@config/env.config';
99
import { BadRequestException, InternalServerErrorException } from '@exceptions';
10+
import { createJid } from '@utils/createJid';
1011
import { status } from '@utils/renderStatus';
1112
import { isURL } from 'class-validator';
1213
import EventEmitter2 from 'eventemitter2';
@@ -57,7 +58,7 @@ export class EvolutionStartupService extends ChannelStartupService {
5758
}
5859

5960
public async profilePicture(number: string) {
60-
const jid = this.createJid(number);
61+
const jid = createJid(number);
6162

6263
return {
6364
wuid: jid,

src/api/integrations/channel/meta/whatsapp.business.service.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { ChannelStartupService } from '@api/services/channel.service';
2222
import { Events, wa } from '@api/types/wa.types';
2323
import { Chatwoot, ConfigService, Database, Openai, S3, WaBusiness } from '@config/env.config';
2424
import { BadRequestException, InternalServerErrorException } from '@exceptions';
25+
import { createJid } from '@utils/createJid';
2526
import { status } from '@utils/renderStatus';
2627
import axios from 'axios';
2728
import { arrayUnique, isURL } from 'class-validator';
@@ -88,7 +89,7 @@ export class BusinessStartupService extends ChannelStartupService {
8889
}
8990

9091
public async profilePicture(number: string) {
91-
const jid = this.createJid(number);
92+
const jid = createJid(number);
9293

9394
return {
9495
wuid: jid,
@@ -132,9 +133,7 @@ export class BusinessStartupService extends ChannelStartupService {
132133

133134
this.eventHandler(content);
134135

135-
this.phoneNumber = this.createJid(
136-
content.messages ? content.messages[0].from : content.statuses[0]?.recipient_id,
137-
);
136+
this.phoneNumber = createJid(content.messages ? content.messages[0].from : content.statuses[0]?.recipient_id);
138137
} catch (error) {
139138
this.logger.error(error);
140139
throw new InternalServerErrorException(error?.toString());
@@ -231,7 +230,7 @@ export class BusinessStartupService extends ChannelStartupService {
231230
}
232231

233232
if (!contact.phones[0]?.wa_id) {
234-
contact.phones[0].wa_id = this.createJid(contact.phones[0].phone);
233+
contact.phones[0].wa_id = createJid(contact.phones[0].phone);
235234
}
236235

237236
result +=
@@ -915,7 +914,7 @@ export class BusinessStartupService extends ChannelStartupService {
915914
}
916915

917916
const messageRaw: any = {
918-
key: { fromMe: true, id: messageSent?.messages[0]?.id, remoteJid: this.createJid(number) },
917+
key: { fromMe: true, id: messageSent?.messages[0]?.id, remoteJid: createJid(number) },
919918
message: this.convertMessageToRaw(message, content),
920919
messageType: this.renderMessageType(content.type),
921920
messageTimestamp: (messageSent?.messages[0]?.timestamp as number) || Math.round(new Date().getTime() / 1000),
@@ -1274,7 +1273,7 @@ export class BusinessStartupService extends ChannelStartupService {
12741273
}
12751274

12761275
if (!contact.wuid) {
1277-
contact.wuid = this.createJid(contact.phoneNumber);
1276+
contact.wuid = createJid(contact.phoneNumber);
12781277
}
12791278

12801279
result += `item1.TEL;waid=${contact.wuid}:${contact.phoneNumber}\n` + 'item1.X-ABLabel:Celular\n' + 'END:VCARD';

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ import ffmpegPath from '@ffmpeg-installer/ffmpeg';
7878
import { Boom } from '@hapi/boom';
7979
import { createId as cuid } from '@paralleldrive/cuid2';
8080
import { Instance } from '@prisma/client';
81+
import { createJid } from '@utils/createJid';
8182
import { makeProxyAgent } from '@utils/makeProxyAgent';
8283
import { getOnWhatsappCache, saveOnWhatsappCache } from '@utils/onWhatsappCache';
8384
import { status } from '@utils/renderStatus';
@@ -1323,17 +1324,21 @@ export class BaileysStartupService extends ChannelStartupService {
13231324

13241325
if (this.localWebhook.enabled) {
13251326
if (isMedia && this.localWebhook.webhookBase64) {
1326-
const buffer = await downloadMediaMessage(
1327-
{ key: received.key, message: received?.message },
1328-
'buffer',
1329-
{},
1330-
{
1331-
logger: P({ level: 'error' }) as any,
1332-
reuploadRequest: this.client.updateMediaMessage,
1333-
},
1334-
);
1327+
try {
1328+
const buffer = await downloadMediaMessage(
1329+
{ key: received.key, message: received?.message },
1330+
'buffer',
1331+
{},
1332+
{
1333+
logger: P({ level: 'error' }) as any,
1334+
reuploadRequest: this.client.updateMediaMessage,
1335+
},
1336+
);
13351337

1336-
messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined;
1338+
messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined;
1339+
} catch (error) {
1340+
this.logger.error(['Error converting media to base64', error?.message]);
1341+
}
13371342
}
13381343
}
13391344

@@ -1809,7 +1814,7 @@ export class BaileysStartupService extends ChannelStartupService {
18091814
}
18101815

18111816
public async profilePicture(number: string) {
1812-
const jid = this.createJid(number);
1817+
const jid = createJid(number);
18131818

18141819
try {
18151820
const profilePictureUrl = await this.client.profilePictureUrl(jid, 'image');
@@ -1827,7 +1832,7 @@ export class BaileysStartupService extends ChannelStartupService {
18271832
}
18281833

18291834
public async getStatus(number: string) {
1830-
const jid = this.createJid(number);
1835+
const jid = createJid(number);
18311836

18321837
try {
18331838
return {
@@ -1843,7 +1848,7 @@ export class BaileysStartupService extends ChannelStartupService {
18431848
}
18441849

18451850
public async fetchProfile(instanceName: string, number?: string) {
1846-
const jid = number ? this.createJid(number) : this.client?.user?.id;
1851+
const jid = number ? createJid(number) : this.client?.user?.id;
18471852

18481853
const onWhatsapp = (await this.whatsappNumber({ numbers: [jid] }))?.shift();
18491854

@@ -1899,7 +1904,7 @@ export class BaileysStartupService extends ChannelStartupService {
18991904
}
19001905

19011906
public async offerCall({ number, isVideo, callDuration }: OfferCallDto) {
1902-
const jid = this.createJid(number);
1907+
const jid = createJid(number);
19031908

19041909
try {
19051910
const call = await this.client.offerCall(jid, isVideo);
@@ -2170,7 +2175,7 @@ export class BaileysStartupService extends ChannelStartupService {
21702175
mentions = group.participants.map((participant) => participant.id);
21712176
} else if (options?.mentioned?.length) {
21722177
mentions = options.mentioned.map((mention) => {
2173-
const jid = this.createJid(mention);
2178+
const jid = createJid(mention);
21742179
if (isJidGroup(jid)) {
21752180
return null;
21762181
}
@@ -2292,17 +2297,21 @@ export class BaileysStartupService extends ChannelStartupService {
22922297

22932298
if (this.localWebhook.enabled) {
22942299
if (isMedia && this.localWebhook.webhookBase64) {
2295-
const buffer = await downloadMediaMessage(
2296-
{ key: messageRaw.key, message: messageRaw?.message },
2297-
'buffer',
2298-
{},
2299-
{
2300-
logger: P({ level: 'error' }) as any,
2301-
reuploadRequest: this.client.updateMediaMessage,
2302-
},
2303-
);
2300+
try {
2301+
const buffer = await downloadMediaMessage(
2302+
{ key: messageRaw.key, message: messageRaw?.message },
2303+
'buffer',
2304+
{},
2305+
{
2306+
logger: P({ level: 'error' }) as any,
2307+
reuploadRequest: this.client.updateMediaMessage,
2308+
},
2309+
);
23042310

2305-
messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined;
2311+
messageRaw.message.base64 = buffer ? buffer.toString('base64') : undefined;
2312+
} catch (error) {
2313+
this.logger.error(['Error converting media to base64', error?.message]);
2314+
}
23062315
}
23072316
}
23082317

@@ -3240,7 +3249,7 @@ export class BaileysStartupService extends ChannelStartupService {
32403249
}
32413250

32423251
if (!contact.wuid) {
3243-
contact.wuid = this.createJid(contact.phoneNumber);
3252+
contact.wuid = createJid(contact.phoneNumber);
32443253
}
32453254

32463255
result += `item1.TEL;waid=${contact.wuid}:${contact.phoneNumber}\n` + 'item1.X-ABLabel:Celular\n' + 'END:VCARD';
@@ -3290,7 +3299,7 @@ export class BaileysStartupService extends ChannelStartupService {
32903299
};
32913300

32923301
data.numbers.forEach((number) => {
3293-
const jid = this.createJid(number);
3302+
const jid = createJid(number);
32943303

32953304
if (isJidGroup(jid)) {
32963305
jids.groups.push({ number, jid });
@@ -3483,7 +3492,7 @@ export class BaileysStartupService extends ChannelStartupService {
34833492
archive: data.archive,
34843493
lastMessages: [last_message],
34853494
},
3486-
this.createJid(number),
3495+
createJid(number),
34873496
);
34883497

34893498
return {
@@ -3520,7 +3529,7 @@ export class BaileysStartupService extends ChannelStartupService {
35203529
markRead: false,
35213530
lastMessages: [last_message],
35223531
},
3523-
this.createJid(number),
3532+
createJid(number),
35243533
);
35253534

35263535
return {
@@ -3725,7 +3734,7 @@ export class BaileysStartupService extends ChannelStartupService {
37253734

37263735
public async fetchBusinessProfile(number: string): Promise<NumberBusiness> {
37273736
try {
3728-
const jid = number ? this.createJid(number) : this.instance.wuid;
3737+
const jid = number ? createJid(number) : this.instance.wuid;
37293738

37303739
const profile = await this.client.getBusinessProfile(jid);
37313740

@@ -3873,7 +3882,7 @@ export class BaileysStartupService extends ChannelStartupService {
38733882
}
38743883

38753884
public async updateMessage(data: UpdateMessageDto) {
3876-
const jid = this.createJid(data.number);
3885+
const jid = createJid(data.number);
38773886

38783887
const options = await this.formatUpdateMessage(data);
38793888

@@ -4163,7 +4172,7 @@ export class BaileysStartupService extends ChannelStartupService {
41634172

41644173
const inviteUrl = inviteCode.inviteUrl;
41654174

4166-
const numbers = id.numbers.map((number) => this.createJid(number));
4175+
const numbers = id.numbers.map((number) => createJid(number));
41674176
const description = id.description ?? '';
41684177

41694178
const msg = `${description}\n\n${inviteUrl}`;
@@ -4234,7 +4243,7 @@ export class BaileysStartupService extends ChannelStartupService {
42344243

42354244
public async updateGParticipant(update: GroupUpdateParticipantDto) {
42364245
try {
4237-
const participants = update.participants.map((p) => this.createJid(p));
4246+
const participants = update.participants.map((p) => createJid(p));
42384247
const updateParticipants = await this.client.groupParticipantsUpdate(
42394248
update.groupJid,
42404249
participants,

0 commit comments

Comments
 (0)