adding member to group is working
This commit is contained in:
@@ -105,7 +105,7 @@ async function createTables() {
|
||||
CREATE TABLE IF NOT EXISTS Contacts (
|
||||
contact_id SERIAL PRIMARY KEY,
|
||||
user_id INT REFERENCES Accounts(user_id) ON DELETE CASCADE,
|
||||
contact_user_id INT REFERENCES Accounts(user_id) ON DELETE CASCADE,
|
||||
contact_user_id INT NOT NULL REFERENCES Accounts(user_id) ON DELETE CASCADE,
|
||||
read BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
last_active TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT unique_contact UNIQUE (user_id, contact_user_id)
|
||||
@@ -275,6 +275,45 @@ async function addMemberToGroup(conversation_id, user_id) {
|
||||
}
|
||||
}
|
||||
|
||||
async function addMemberToGroupByUsername(conversation_id, username) {
|
||||
const query = `
|
||||
WITH user_id_query AS (
|
||||
SELECT user_id
|
||||
FROM Accounts
|
||||
WHERE username = $1
|
||||
LIMIT 1
|
||||
),
|
||||
insert_membership AS (
|
||||
INSERT INTO Memberships (conversation_id, user_id)
|
||||
SELECT $2, user_id
|
||||
FROM user_id_query
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM Memberships
|
||||
WHERE conversation_id = $2 AND user_id = (SELECT user_id FROM user_id_query)
|
||||
)
|
||||
)
|
||||
SELECT (SELECT user_id FROM user_id_query) AS added_user_id;
|
||||
`;
|
||||
|
||||
try {
|
||||
const result = await client.query(query, [username, conversation_id]);
|
||||
if (result.rows.length > 0) {
|
||||
console.log(
|
||||
`Added user with username ${username} to conversation_id ${conversation_id}`,
|
||||
);
|
||||
return result.rows[0].added_user_id;
|
||||
} else {
|
||||
console.log(
|
||||
`User with username ${username} not found or already in group.`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to add member to group by username", e);
|
||||
}
|
||||
}
|
||||
|
||||
async function getMessages(user_id, receiverUsername, limit = 50, cursor = 0) {
|
||||
let query = `
|
||||
WITH recipient AS (
|
||||
@@ -706,4 +745,6 @@ module.exports = {
|
||||
updateContactStatus,
|
||||
updateContactLastActive,
|
||||
createGroup,
|
||||
addMemberToGroup,
|
||||
addMemberToGroupByUsername,
|
||||
};
|
||||
|
||||
@@ -36,7 +36,13 @@ const {
|
||||
} = require("./utils/filter");
|
||||
const { generateJwtToken, verifyJwtToken } = require("./auth/jwt");
|
||||
const { initializeSocket } = require("./socket/socket");
|
||||
const { getContacts, insertContact, createGroup } = require("./db/db");
|
||||
const {
|
||||
getContacts,
|
||||
insertContact,
|
||||
createGroup,
|
||||
addMemberToGroup,
|
||||
addMemberToGroupByUsername,
|
||||
} = require("./db/db");
|
||||
const { extname } = require("node:path");
|
||||
|
||||
const corsOptions = {
|
||||
@@ -305,9 +311,9 @@ app.post(
|
||||
|
||||
app.post("/api/chat/groups/create", authorizeUser, async (req, res) => {
|
||||
const user_id = req.user.user_id;
|
||||
const groupname = req.body.groupname;
|
||||
const groupname = req.body.groupName;
|
||||
if (!groupname) {
|
||||
res.status(400).json({ message: "Groupname not provided" });
|
||||
return res.status(400).json({ message: "Groupname not provided" });
|
||||
}
|
||||
const group_id = await createGroup(user_id, groupname);
|
||||
if (!group_id) {
|
||||
@@ -321,9 +327,15 @@ app.post("/api/chat/groups/create", authorizeUser, async (req, res) => {
|
||||
|
||||
app.post("/api/chat/groups/add", async (req, res) => {
|
||||
const username = req.body.username;
|
||||
const group_id = req.body.group_id;
|
||||
if (!username) {
|
||||
return res.status(400).json({ message: "Username not provided" });
|
||||
}
|
||||
const result = await addMemberToGroupByUsername(group_id, username);
|
||||
if (result !== null) {
|
||||
return res.status(200).json({ message: "Successfully added member" });
|
||||
}
|
||||
res.status(500).json({ message: "Failed to add member" });
|
||||
});
|
||||
|
||||
initializeSocket(io);
|
||||
|
||||
Reference in New Issue
Block a user