Skip to content

Commit 545e15c

Browse files
Jean Limaclaude
andcommitted
fix: improve type safety by removing 'as any' casts
- Add IMessageRaw interface to properly type prepareMessage() return - Replace unsafe 'key as any' casts with ExtendedIMessageKey - Add MessageBodyWithExtendedKey interface for chatwoot service - Improve code readability with descriptive variable names (pollKey, receivedKey) Addresses Sourcery code review feedback regarding type safety in remoteJidAlt handling. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent cd800f2 commit 545e15c

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ export interface ExtendedIMessageKey extends proto.IMessageKey {
162162
isViewOnce?: boolean;
163163
}
164164

165+
export interface IMessageRaw {
166+
key: ExtendedIMessageKey;
167+
pushName?: string;
168+
status?: string;
169+
message: any;
170+
contextInfo?: any;
171+
messageType: string;
172+
messageTimestamp: number;
173+
instanceId: string;
174+
source: string;
175+
}
176+
165177
const groupMetadataCache = new CacheService(new CacheEngine(configService, 'groups').getEngine());
166178

167179
// Adicione a função getVideoDuration no início do arquivo
@@ -1234,22 +1246,23 @@ export class BaileysStartupService extends ChannelStartupService {
12341246
}
12351247

12361248
if (pollVote.encPayload && pollEncKey) {
1249+
const pollKey = pollMessage.key as ExtendedIMessageKey;
12371250
const creatorCandidates = [
12381251
this.instance.wuid,
12391252
this.client.user?.lid,
1240-
pollMessage.key.participant,
1241-
(pollMessage.key as any).participantAlt,
1242-
pollMessage.key.remoteJid,
1253+
pollKey.participant,
1254+
pollKey.participantAlt,
1255+
pollKey.remoteJid,
12431256
];
12441257

1245-
const key = received.key as any;
1258+
const receivedKey = received.key as ExtendedIMessageKey;
12461259
const voterCandidates = [
12471260
this.instance.wuid,
12481261
this.client.user?.lid,
1249-
key.participant,
1250-
key.participantAlt,
1251-
key.remoteJidAlt,
1252-
key.remoteJid,
1262+
receivedKey.participant,
1263+
receivedKey.participantAlt,
1264+
receivedKey.remoteJidAlt,
1265+
receivedKey.remoteJid,
12531266
];
12541267

12551268
const uniqueCreators = [
@@ -4649,7 +4662,7 @@ export class BaileysStartupService extends ChannelStartupService {
46494662
return obj;
46504663
}
46514664

4652-
private prepareMessage(message: proto.IWebMessageInfo): any {
4665+
private prepareMessage(message: proto.IWebMessageInfo): IMessageRaw {
46534666
const contentType = getContentType(message.message);
46544667
const contentMsg = message?.message[contentType] as any;
46554668

src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import i18next from '@utils/i18n';
2424
import { sendTelemetry } from '@utils/sendTelemetry';
2525
import axios from 'axios';
2626
import { WAMessageContent, WAMessageKey } from 'baileys';
27+
import { ExtendedIMessageKey } from '../../channel/whatsapp/whatsapp.baileys.service';
2728
import dayjs from 'dayjs';
2829
import FormData from 'form-data';
2930
import { Jimp, JimpMime } from 'jimp';
@@ -41,6 +42,11 @@ interface ChatwootMessage {
4142
isRead?: boolean;
4243
}
4344

45+
interface MessageBodyWithExtendedKey {
46+
key: ExtendedIMessageKey;
47+
[key: string]: any;
48+
}
49+
4450
export class ChatwootService {
4551
private readonly logger = new Logger('ChatwootService');
4652

@@ -629,7 +635,7 @@ export class ChatwootService {
629635
return filterPayload;
630636
}
631637

632-
public async createConversation(instance: InstanceDto, body: any) {
638+
public async createConversation(instance: InstanceDto, body: MessageBodyWithExtendedKey) {
633639
const isLid = body.key.addressingMode === 'lid';
634640
const isGroup = body.key.remoteJid.endsWith('@g.us');
635641
const phoneNumber = isLid && !isGroup ? body.key.remoteJidAlt : body.key.remoteJid;

0 commit comments

Comments
 (0)