Skip to content

Commit ade248d

Browse files
committed
refactor: implement sendDataWithRetry method for improved error handling and retry logic
1 parent af675f0 commit ade248d

1 file changed

Lines changed: 74 additions & 3 deletions

File tree

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

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,77 @@ export class ChatwootService {
10171017
return data;
10181018
} catch (error) {
10191019
this.logger.error(error);
1020+
throw error;
1021+
}
1022+
}
1023+
1024+
/**
1025+
* Envia mídia para o Chatwoot. Tenta uma vez; se receber 404,
1026+
* remove a chave de cache, cria uma nova conversa e tenta de novo.
1027+
*/
1028+
private async sendDataWithRetry(
1029+
conversationId: number,
1030+
fileStream: Readable,
1031+
fileName: string,
1032+
messageType: 'incoming' | 'outgoing' | undefined,
1033+
content?: string,
1034+
instance?: InstanceDto,
1035+
messageBody?: any,
1036+
sourceId?: string,
1037+
quotedMsg?: MessageModel,
1038+
maxRetries = 1,
1039+
) {
1040+
try {
1041+
const conversationId = await this.createConversation(instance, messageBody);
1042+
if (!conversationId) return null;
1043+
1044+
return await this.sendData(
1045+
conversationId,
1046+
fileStream,
1047+
fileName,
1048+
messageType,
1049+
content,
1050+
instance,
1051+
messageBody,
1052+
sourceId,
1053+
quotedMsg,
1054+
);
1055+
} catch (err: any) {
1056+
if (
1057+
err?.response?.status === 404 &&
1058+
err?.response?.data?.error === 'Resource could not be found' &&
1059+
maxRetries > 0
1060+
) {
1061+
const isLid = messageBody.key.remoteJid.includes('@lid') && messageBody.key.senderPn;
1062+
const remoteJid = isLid ? messageBody.key.senderPn : messageBody.key.remoteJid;
1063+
const cacheKey = `${instance.instanceName}:createConversation-${remoteJid}`;
1064+
1065+
this.logger.warn(`Conversation não existe mais. Limpando "${cacheKey}" e tentando novamente…`);
1066+
await this.cache.delete(cacheKey);
1067+
1068+
try {
1069+
const newConversationId = await this.createConversation(instance, messageBody);
1070+
if (!newConversationId) return null;
1071+
1072+
return await this.sendData(
1073+
newConversationId,
1074+
fileStream,
1075+
fileName,
1076+
messageType,
1077+
content,
1078+
instance,
1079+
messageBody,
1080+
sourceId,
1081+
quotedMsg,
1082+
);
1083+
} catch (e) {
1084+
this.logger.error(`Retry falhou: ${e}`);
1085+
return null;
1086+
}
1087+
}
1088+
1089+
this.logger.error(`Failed to send message: ${err}`);
1090+
return null;
10201091
}
10211092
}
10221093

@@ -2020,7 +2091,7 @@ export class ChatwootService {
20202091
content = `${bodyMessage}`;
20212092
}
20222093

2023-
const send = await this.sendData(
2094+
const send = await this.sendDataWithRetry(
20242095
getConversation,
20252096
fileStream,
20262097
nameFile,
@@ -2039,7 +2110,7 @@ export class ChatwootService {
20392110

20402111
return send;
20412112
} else {
2042-
const send = await this.sendData(
2113+
const send = await this.sendDataWithRetry(
20432114
getConversation,
20442115
fileStream,
20452116
nameFile,
@@ -2119,7 +2190,7 @@ export class ChatwootService {
21192190
const title = truncStr(adsMessage.title, 40);
21202191
const description = truncStr(adsMessage?.body, 75);
21212192

2122-
const send = await this.sendData(
2193+
const send = await this.sendDataWithRetry(
21232194
getConversation,
21242195
fileStream,
21252196
nameFile,

0 commit comments

Comments
 (0)