Skip to content

Commit ddaf32b

Browse files
committed
fix: melhora a formatação e tratamento de erros na função getExistingSourceIds
2 parents 9cda6a2 + 0b2d8a7 commit ddaf32b

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,24 +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-
} else {
185-
query = 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
186-
}
187-
180+
const formattedSourceIds = sourceIds.map((sourceId) => `WAID:${sourceId.replace('WAID:', '')}`);
188181
const pgClient = postgresClient.getChatwootConnection();
189-
const result = await pgClient.query(query, [formattedSourceIds, conversationId]);
190-
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);
191192
for (const row of result.rows) {
192193
existingSourceIdsSet.add(row.source_id);
193194
}
194195

195196
return existingSourceIdsSet;
196197
} catch (error) {
197-
return null;
198+
this.logger.error(`Error on getExistingSourceIds: ${error.toString()}`);
199+
return new Set<string>();
198200
}
199201
}
200202

@@ -336,7 +338,6 @@ class ChatwootImport {
336338

337339
this.deleteHistoryMessages(instance);
338340
this.deleteRepositoryMessagesCache(instance);
339-
return 0;
340341
}
341342
}
342343

0 commit comments

Comments
 (0)