Skip to content

Commit 17fd407

Browse files
Merge pull request #1494 from KokeroO/develop
fix: melhora a formatação e tratamento de erros na função getExistingSourceIds
2 parents 9cda6a2 + 0cdc67e commit 17fd407

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -177,24 +177,25 @@ 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+
// Ensure all sourceIds are consistently prefixed with 'WAID:' as required by downstream systems and database queries.
181+
const formattedSourceIds = sourceIds.map((sourceId) => `WAID:${sourceId.replace('WAID:', '')}`);
188182
const pgClient = postgresClient.getChatwootConnection();
189-
const result = await pgClient.query(query, [formattedSourceIds, conversationId]);
190183

184+
const params = conversationId ? [formattedSourceIds, conversationId] : [formattedSourceIds];
185+
186+
const query = conversationId
187+
? 'SELECT source_id FROM messages WHERE source_id = ANY($1) AND conversation_id = $2'
188+
: 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
189+
190+
const result = await pgClient.query(query, params);
191191
for (const row of result.rows) {
192192
existingSourceIdsSet.add(row.source_id);
193193
}
194194

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

@@ -336,7 +337,6 @@ class ChatwootImport {
336337

337338
this.deleteHistoryMessages(instance);
338339
this.deleteRepositoryMessagesCache(instance);
339-
return 0;
340340
}
341341
}
342342

0 commit comments

Comments
 (0)