fix: enable sourceId exists in a conversation

This commit is contained in:
Alex Szefezuk 2025-05-21 10:36:26 -03:00
parent 7cccda10bb
commit 2545013040
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);