Skip to content

Commit 590e742

Browse files
authored
fix: duplicate chats being created for eCurrency (#526)
1 parent 6e9617e commit 590e742

File tree

3 files changed

+109
-12
lines changed

3 files changed

+109
-12
lines changed

platforms/blabsy-w3ds-auth-api/src/controllers/WebhookController.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,27 @@ export class WebhookController {
161161
// Check for existing DM (2 participants, no name) before creating
162162
const participants = mappedData.participants || [];
163163
const isDM = participants.length === 2 && !mappedData.name;
164+
const chatName = mappedData.name;
165+
166+
// For eCurrency Chat groups, check by name first
167+
if (chatName && (chatName.startsWith("eCurrency Chat") || chatName.includes("eCurrency Chat"))) {
168+
const existingChatsQuery = collection.where('name', '==', chatName);
169+
const existingChatsSnapshot = await existingChatsQuery.get();
170+
171+
if (!existingChatsSnapshot.empty) {
172+
// Use existing chat and store mapping
173+
const existingDoc = existingChatsSnapshot.docs[0];
174+
console.log(`⚠️ eCurrency Chat with name "${chatName}" already exists, using existing: ${existingDoc.id}`);
175+
docRef = collection.doc(existingDoc.id);
176+
adapter.addToLockedIds(docRef.id);
177+
adapter.addToLockedIds(globalId);
178+
await adapter.mappingDb.storeMapping({
179+
globalId: globalId,
180+
localId: docRef.id,
181+
});
182+
return; // Exit early, don't create new chat
183+
}
184+
}
164185

165186
if (isDM) {
166187
// Query for existing chats with these participants

platforms/pictique-api/src/controllers/WebhookController.ts

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -297,18 +297,53 @@ export class WebhookController {
297297
});
298298
}
299299
} else {
300-
// Group chat - always create new
301-
chat = await this.chatService.createChat(
302-
local.data.name as string,
303-
participantIds
304-
);
305-
chat.admins = admins;
306-
await this.chatService.chatRepository.save(chat);
307-
this.adapter.addToLockedIds(chat.id);
308-
await this.adapter.mappingDb.storeMapping({
309-
localId: chat.id,
310-
globalId: req.body.id,
311-
});
300+
// Group chat - check for eCurrency Chat duplicates by name
301+
const chatName = local.data.name as string;
302+
if (chatName && (chatName.startsWith("eCurrency Chat") || chatName.includes("eCurrency Chat"))) {
303+
// Check if chat with this name already exists
304+
const existingChat = await this.chatService.chatRepository.findOne({
305+
where: { name: chatName },
306+
relations: ["participants", "admins"]
307+
});
308+
309+
if (existingChat) {
310+
console.log(`⚠️ eCurrency Chat with name "${chatName}" already exists, using existing: ${existingChat.id}`);
311+
chat = existingChat;
312+
chat.admins = admins;
313+
await this.chatService.chatRepository.save(chat);
314+
this.adapter.addToLockedIds(chat.id);
315+
await this.adapter.mappingDb.storeMapping({
316+
localId: chat.id,
317+
globalId: req.body.id,
318+
});
319+
} else {
320+
// Create new chat
321+
chat = await this.chatService.createChat(
322+
chatName,
323+
participantIds
324+
);
325+
chat.admins = admins;
326+
await this.chatService.chatRepository.save(chat);
327+
this.adapter.addToLockedIds(chat.id);
328+
await this.adapter.mappingDb.storeMapping({
329+
localId: chat.id,
330+
globalId: req.body.id,
331+
});
332+
}
333+
} else {
334+
// Regular group chat - always create new
335+
chat = await this.chatService.createChat(
336+
chatName,
337+
participantIds
338+
);
339+
chat.admins = admins;
340+
await this.chatService.chatRepository.save(chat);
341+
this.adapter.addToLockedIds(chat.id);
342+
await this.adapter.mappingDb.storeMapping({
343+
localId: chat.id,
344+
globalId: req.body.id,
345+
});
346+
}
312347
}
313348
}
314349
} else if (mapping.tableName === "messages") {

platforms/pictique-api/src/services/ChatService.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,47 @@ export class ChatService {
6161
participantIds: string[] = [],
6262
creatorId?: string
6363
): Promise<Chat> {
64+
// For eCurrency Chat groups, check if a chat with the same name already exists
65+
// This prevents duplicate chat creation
66+
if (name && (name.startsWith("eCurrency Chat") || name.includes("eCurrency Chat"))) {
67+
return await AppDataSource.transaction(async (transactionalEntityManager) => {
68+
// Check if a chat with this exact name already exists
69+
const existingChat = await transactionalEntityManager.findOne(Chat, {
70+
where: { name },
71+
relations: ["participants", "admins"]
72+
});
73+
74+
if (existingChat) {
75+
console.log(`⚠️ Chat with name "${name}" already exists, returning existing chat: ${existingChat.id}`);
76+
return existingChat;
77+
}
78+
79+
// No existing chat found, create new one
80+
const participants = await transactionalEntityManager.findBy(User, {
81+
id: In(participantIds),
82+
});
83+
if (participants.length !== participantIds.length) {
84+
throw new Error("One or more participants not found");
85+
}
86+
87+
let admins: User[] = [];
88+
if (creatorId) {
89+
const creator = await transactionalEntityManager.findOneBy(User, { id: creatorId });
90+
if (creator) {
91+
admins = [creator];
92+
}
93+
}
94+
95+
const chat = transactionalEntityManager.create(Chat, {
96+
name: name,
97+
participants,
98+
admins,
99+
});
100+
return await transactionalEntityManager.save(Chat, chat);
101+
});
102+
}
103+
104+
// For non-eCurrency Chat groups, proceed normally
64105
const participants = await this.userRepository.findBy({
65106
id: In(participantIds),
66107
});

0 commit comments

Comments
 (0)