Skip to content

Commit 0b2d8a7

Browse files
committed
fix: melhora o tratamento de erros e otimiza a consulta de IDs de origem no Chatwoot
1 parent 3459d61 commit 0b2d8a7

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/api/integrations/chatbot/chatwoot/utils/chatwoot-import-helper.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -177,25 +177,26 @@ class ChatwootImport {
177177
return existingSourceIdsSet;
178178
}
179179

180-
const formattedSourceIds = sourceIds.map((sourceId) => `WAID:${sourceId.replace('WAID:', '')}`); // Make sure the sourceId is always formatted as WAID:1234567890
181-
let query: string;
182-
if (conversationId) {
183-
query = 'SELECT source_id FROM messages WHERE source_id = ANY($1) AND conversation_id = $2';
184-
185-
if (!conversationId) {
186-
query = 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
187-
}
188-
180+
const formattedSourceIds = sourceIds.map((sourceId) => `WAID:${sourceId.replace('WAID:', '')}`);
189181
const pgClient = postgresClient.getChatwootConnection();
190-
const result = await pgClient.query(query, [formattedSourceIds, conversationId]);
191-
182+
183+
const params = conversationId
184+
? [formattedSourceIds, conversationId]
185+
: [formattedSourceIds];
186+
187+
const query = conversationId
188+
? 'SELECT source_id FROM messages WHERE source_id = ANY($1) AND conversation_id = $2'
189+
: 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
190+
191+
const result = await pgClient.query(query, params);
192192
for (const row of result.rows) {
193193
existingSourceIdsSet.add(row.source_id);
194194
}
195195

196196
return existingSourceIdsSet;
197197
} catch (error) {
198-
return null;
198+
this.logger.error(`Error on getExistingSourceIds: ${error.toString()}`);
199+
return new Set<string>();
199200
}
200201
}
201202

0 commit comments

Comments
 (0)