fixed insertContact again
This commit is contained in:
@@ -514,6 +514,64 @@ async function insertContactById(senderId, conversation_id, read) {
|
||||
|
||||
async function insertContact(initiatorId, receiverId, contactUsername, read) {
|
||||
try {
|
||||
let conversation_id;
|
||||
|
||||
// Check if the user is creating a contact for themselves
|
||||
const isSelfContact = initiatorId === receiverId;
|
||||
|
||||
if (isSelfContact) {
|
||||
// For self-contacts, check if a conversation already exists for the user alone
|
||||
const findSelfConversationQuery = `
|
||||
SELECT c.conversation_id
|
||||
FROM Conversations c
|
||||
JOIN Memberships m ON c.conversation_id = m.conversation_id
|
||||
WHERE c.conversation_type = 'direct'
|
||||
AND m.user_id = $1
|
||||
AND (
|
||||
SELECT COUNT(*)
|
||||
FROM Memberships
|
||||
WHERE conversation_id = c.conversation_id
|
||||
) = 1
|
||||
LIMIT 1;
|
||||
`;
|
||||
|
||||
const selfConversationResult = await client.query(
|
||||
findSelfConversationQuery,
|
||||
[initiatorId],
|
||||
);
|
||||
conversation_id = selfConversationResult.rows[0]?.conversation_id;
|
||||
|
||||
if (!conversation_id) {
|
||||
// Create a new conversation for the user alone
|
||||
const createConversationQuery = `
|
||||
INSERT INTO Conversations (conversation_type)
|
||||
VALUES ('direct')
|
||||
RETURNING conversation_id;
|
||||
`;
|
||||
const newConversationResult = await client.query(
|
||||
createConversationQuery,
|
||||
);
|
||||
|
||||
if (!newConversationResult) {
|
||||
console.error("Failed to create new conversation for self-contact");
|
||||
return null;
|
||||
}
|
||||
|
||||
conversation_id = newConversationResult.rows[0].conversation_id;
|
||||
|
||||
// Add the user as the only member of the conversation
|
||||
const createMembershipQuery = `
|
||||
INSERT INTO Memberships (conversation_id, user_id)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (conversation_id, user_id) DO NOTHING;
|
||||
`;
|
||||
await client.query(createMembershipQuery, [
|
||||
conversation_id,
|
||||
initiatorId,
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
// For regular contacts, check if a conversation already exists between the two users
|
||||
const findConversationQuery = `
|
||||
SELECT c.conversation_id
|
||||
FROM Conversations c
|
||||
@@ -525,27 +583,25 @@ async function insertContact(initiatorId, receiverId, contactUsername, read) {
|
||||
OR
|
||||
(m1.user_id = $2 AND m2.user_id = $1)
|
||||
)
|
||||
AND (
|
||||
SELECT COUNT(DISTINCT user_id)
|
||||
FROM Memberships
|
||||
WHERE conversation_id = c.conversation_id
|
||||
) = 1
|
||||
LIMIT 1
|
||||
LIMIT 1;
|
||||
`;
|
||||
|
||||
const conversationResult = await client.query(findConversationQuery, [
|
||||
initiatorId,
|
||||
receiverId,
|
||||
]);
|
||||
let conversation_id = conversationResult.rows[0]?.conversation_id;
|
||||
conversation_id = conversationResult.rows[0]?.conversation_id;
|
||||
|
||||
// Create new conversation if none exists
|
||||
if (!conversation_id) {
|
||||
// Create a new conversation for the two users
|
||||
const createConversationQuery = `
|
||||
INSERT INTO Conversations (conversation_type)
|
||||
VALUES ('direct')
|
||||
RETURNING conversation_id
|
||||
RETURNING conversation_id;
|
||||
`;
|
||||
const newConversationResult = await client.query(createConversationQuery);
|
||||
const newConversationResult = await client.query(
|
||||
createConversationQuery,
|
||||
);
|
||||
|
||||
if (!newConversationResult) {
|
||||
console.error("Failed to create new conversation");
|
||||
@@ -553,11 +609,12 @@ async function insertContact(initiatorId, receiverId, contactUsername, read) {
|
||||
}
|
||||
|
||||
conversation_id = newConversationResult.rows[0].conversation_id;
|
||||
// Create memberships for users
|
||||
|
||||
// Add both users as members of the conversation
|
||||
const createMembershipsQuery = `
|
||||
INSERT INTO Memberships (conversation_id, user_id)
|
||||
VALUES ($1, $2), ($1, $3)
|
||||
ON CONFLICT (conversation_id, user_id) DO NOTHING
|
||||
ON CONFLICT (conversation_id, user_id) DO NOTHING;
|
||||
`;
|
||||
await client.query(createMembershipsQuery, [
|
||||
conversation_id,
|
||||
@@ -565,7 +622,9 @@ async function insertContact(initiatorId, receiverId, contactUsername, read) {
|
||||
receiverId,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert the contact for the initiator
|
||||
const contact = await insertContactById(initiatorId, conversation_id, read);
|
||||
if (!contact.contact_id) {
|
||||
console.error("Failed to insert contact by id");
|
||||
|
||||
Reference in New Issue
Block a user