Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,27 @@ export class WebhookController {
// Check for existing DM (2 participants, no name) before creating
const participants = mappedData.participants || [];
const isDM = participants.length === 2 && !mappedData.name;
const chatName = mappedData.name;

// For eCurrency Chat groups, check by name first
if (chatName && (chatName.startsWith("eCurrency Chat") || chatName.includes("eCurrency Chat"))) {
const existingChatsQuery = collection.where('name', '==', chatName);
const existingChatsSnapshot = await existingChatsQuery.get();

if (!existingChatsSnapshot.empty) {
// Use existing chat and store mapping
const existingDoc = existingChatsSnapshot.docs[0];
console.log(`⚠️ eCurrency Chat with name "${chatName}" already exists, using existing: ${existingDoc.id}`);
docRef = collection.doc(existingDoc.id);
adapter.addToLockedIds(docRef.id);
adapter.addToLockedIds(globalId);
await adapter.mappingDb.storeMapping({
globalId: globalId,
localId: docRef.id,
});
return; // Exit early, don't create new chat
}
}

if (isDM) {
// Query for existing chats with these participants
Expand Down
59 changes: 47 additions & 12 deletions platforms/pictique-api/src/controllers/WebhookController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,18 +297,53 @@ export class WebhookController {
});
}
} else {
// Group chat - always create new
chat = await this.chatService.createChat(
local.data.name as string,
participantIds
);
chat.admins = admins;
await this.chatService.chatRepository.save(chat);
this.adapter.addToLockedIds(chat.id);
await this.adapter.mappingDb.storeMapping({
localId: chat.id,
globalId: req.body.id,
});
// Group chat - check for eCurrency Chat duplicates by name
const chatName = local.data.name as string;
if (chatName && (chatName.startsWith("eCurrency Chat") || chatName.includes("eCurrency Chat"))) {
// Check if chat with this name already exists
const existingChat = await this.chatService.chatRepository.findOne({
where: { name: chatName },
relations: ["participants", "admins"]
});

if (existingChat) {
console.log(`⚠️ eCurrency Chat with name "${chatName}" already exists, using existing: ${existingChat.id}`);
chat = existingChat;
chat.admins = admins;
await this.chatService.chatRepository.save(chat);
this.adapter.addToLockedIds(chat.id);
await this.adapter.mappingDb.storeMapping({
localId: chat.id,
globalId: req.body.id,
});
} else {
// Create new chat
chat = await this.chatService.createChat(
chatName,
participantIds
);
chat.admins = admins;
await this.chatService.chatRepository.save(chat);
this.adapter.addToLockedIds(chat.id);
await this.adapter.mappingDb.storeMapping({
localId: chat.id,
globalId: req.body.id,
});
}
} else {
// Regular group chat - always create new
chat = await this.chatService.createChat(
chatName,
participantIds
);
chat.admins = admins;
await this.chatService.chatRepository.save(chat);
this.adapter.addToLockedIds(chat.id);
await this.adapter.mappingDb.storeMapping({
localId: chat.id,
globalId: req.body.id,
});
}
}
}
} else if (mapping.tableName === "messages") {
Expand Down
41 changes: 41 additions & 0 deletions platforms/pictique-api/src/services/ChatService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,47 @@ export class ChatService {
participantIds: string[] = [],
creatorId?: string
): Promise<Chat> {
// For eCurrency Chat groups, check if a chat with the same name already exists
// This prevents duplicate chat creation
if (name && (name.startsWith("eCurrency Chat") || name.includes("eCurrency Chat"))) {
return await AppDataSource.transaction(async (transactionalEntityManager) => {
// Check if a chat with this exact name already exists
const existingChat = await transactionalEntityManager.findOne(Chat, {
where: { name },
relations: ["participants", "admins"]
});

if (existingChat) {
console.log(`⚠️ Chat with name "${name}" already exists, returning existing chat: ${existingChat.id}`);
return existingChat;
}

// No existing chat found, create new one
const participants = await transactionalEntityManager.findBy(User, {
id: In(participantIds),
});
if (participants.length !== participantIds.length) {
throw new Error("One or more participants not found");
}

let admins: User[] = [];
if (creatorId) {
const creator = await transactionalEntityManager.findOneBy(User, { id: creatorId });
if (creator) {
admins = [creator];
}
}

const chat = transactionalEntityManager.create(Chat, {
name: name,
participants,
admins,
});
return await transactionalEntityManager.save(Chat, chat);
});
}

// For non-eCurrency Chat groups, proceed normally
const participants = await this.userRepository.findBy({
id: In(participantIds),
});
Expand Down