Skip to content

Commit 2545013

Browse files
committed
fix: enable sourceId exists in a conversation
1 parent 7cccda1 commit 2545013

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/api/integrations/chatbot/chatwoot/services/chatwoot.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ export class ChatwootService {
930930
quotedMsg?: MessageModel,
931931
) {
932932
if (sourceId && this.isImportHistoryAvailable()) {
933-
const messageAlreadySaved = await chatwootImport.getExistingSourceIds([sourceId]);
933+
const messageAlreadySaved = await chatwootImport.getExistingSourceIds([sourceId], conversationId);
934934
if (messageAlreadySaved) {
935935
if (messageAlreadySaved.size > 0) {
936936
this.logger.warn('Message already saved on chatwoot');

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class ChatwootImport {
169169
}
170170
}
171171

172-
public async getExistingSourceIds(sourceIds: string[]): Promise<Set<string>> {
172+
public async getExistingSourceIds(sourceIds: string[], conversationId?: number): Promise<Set<string>> {
173173
try {
174174
const existingSourceIdsSet = new Set<string>();
175175

@@ -178,9 +178,17 @@ class ChatwootImport {
178178
}
179179

180180
const formattedSourceIds = sourceIds.map((sourceId) => `WAID:${sourceId.replace('WAID:', '')}`); // Make sure the sourceId is always formatted as WAID:1234567890
181-
const query = 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
181+
let query: string;
182+
if (conversationId) {
183+
query = 'SELECT source_id FROM messages WHERE source_id = ANY($1)';
184+
}
185+
186+
if(!conversationId) {
187+
query = 'SELECT source_id FROM messages WHERE source_id = ANY($1) AND conversation_id = $2';
188+
}
189+
182190
const pgClient = postgresClient.getChatwootConnection();
183-
const result = await pgClient.query(query, [formattedSourceIds]);
191+
const result = await pgClient.query(query, [formattedSourceIds, conversationId]);
184192

185193
for (const row of result.rows) {
186194
existingSourceIdsSet.add(row.source_id);

0 commit comments

Comments
 (0)