Merge pull request #1481 from AlexSzefezuk/fix-chatwoot-import

Fix-chatwoot-import
This commit is contained in:
Davidson Gomes 2025-05-21 13:54:51 -03:00 committed by GitHub
commit fcde1f9acc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 4 deletions

View File

@ -930,7 +930,7 @@ export class ChatwootService {
quotedMsg?: MessageModel,
) {
if (sourceId && this.isImportHistoryAvailable()) {
const messageAlreadySaved = await chatwootImport.getExistingSourceIds([sourceId]);
const messageAlreadySaved = await chatwootImport.getExistingSourceIds([sourceId], conversationId);
if (messageAlreadySaved) {
if (messageAlreadySaved.size > 0) {
this.logger.warn('Message already saved on chatwoot');

View File

@ -169,7 +169,7 @@ class ChatwootImport {
}
}
public async getExistingSourceIds(sourceIds: string[]): Promise<Set<string>> {
public async getExistingSourceIds(sourceIds: string[], conversationId?: number): Promise<Set<string>> {
try {
const existingSourceIdsSet = new Set<string>();
@ -178,9 +178,17 @@ class ChatwootImport {
}
const formattedSourceIds = sourceIds.map((sourceId) => `WAID:${sourceId.replace('WAID:', '')}`); // Make sure the sourceId is always formatted as WAID:1234567890
const query = 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
let query: string;
if (conversationId) {
query = 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
}
if(!conversationId) {
query = 'SELECT source_id FROM messages WHERE source_id = ANY($1) AND conversation_id = $2';
}
const pgClient = postgresClient.getChatwootConnection();
const result = await pgClient.query(query, [formattedSourceIds]);
const result = await pgClient.query(query, [formattedSourceIds, conversationId]);
for (const row of result.rows) {
existingSourceIdsSet.add(row.source_id);