Updated chat routes for new DB schema
This commit is contained in:
parent
913e60f25d
commit
7534605f19
|
|
@ -89,12 +89,8 @@ router.get("/conversations/:conversationId/messages", async (req, res) => {
|
||||||
|
|
||||||
const queryText = `
|
const queryText = `
|
||||||
SELECT
|
SELECT
|
||||||
m.*,
|
m.*
|
||||||
cm.media_url,
|
|
||||||
cm.media_type as media_file_type,
|
|
||||||
cm.thumbnail_url
|
|
||||||
FROM messages m
|
FROM messages m
|
||||||
LEFT JOIN conversation_media cm ON m.media_id = cm.id
|
|
||||||
WHERE m.conversation_id = $1
|
WHERE m.conversation_id = $1
|
||||||
AND m.deleted = FALSE
|
AND m.deleted = FALSE
|
||||||
ORDER BY m.created_at DESC
|
ORDER BY m.created_at DESC
|
||||||
|
|
@ -116,28 +112,27 @@ router.post("/messages", async (req, res) => {
|
||||||
const client = await pool.connect();
|
const client = await pool.connect();
|
||||||
try {
|
try {
|
||||||
await client.query("BEGIN");
|
await client.query("BEGIN");
|
||||||
const { conversation_id, sender_id, receiver_id, content, message_type = 'text', media } = req.body;
|
const { conversation_id, sender_id, receiver_id, content, message_type = 'text', media_url, media_type, media_metadata } = req.body;
|
||||||
|
|
||||||
let media_id = null;
|
// Insert Message with embedded media fields
|
||||||
|
|
||||||
// Insert Media if present and message type allows it
|
|
||||||
if ((message_type === 'media' || message_type === 'both') && media) {
|
|
||||||
const insertMediaQuery = `
|
|
||||||
INSERT INTO conversation_media (media_type, media_url, thumbnail_url)
|
|
||||||
VALUES ($1, $2, $3)
|
|
||||||
RETURNING id
|
|
||||||
`;
|
|
||||||
const mediaResult = await client.query(insertMediaQuery, [media.media_type, media.media_url, media.thumbnail_url]);
|
|
||||||
media_id = mediaResult.rows[0].id;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Insert Message
|
|
||||||
const insertMessageQuery = `
|
const insertMessageQuery = `
|
||||||
INSERT INTO messages (conversation_id, sender_id, receiver_id, message_type, content, media_id)
|
INSERT INTO messages (
|
||||||
VALUES ($1, $2, $3, $4, $5, $6)
|
conversation_id, sender_id, receiver_id, message_type, content,
|
||||||
|
message_media, media_type, media_metadata
|
||||||
|
)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||||
RETURNING *
|
RETURNING *
|
||||||
`;
|
`;
|
||||||
const messageResult = await client.query(insertMessageQuery, [conversation_id, sender_id, receiver_id, message_type, content, media_id]);
|
const messageResult = await client.query(insertMessageQuery, [
|
||||||
|
conversation_id,
|
||||||
|
sender_id,
|
||||||
|
receiver_id,
|
||||||
|
message_type,
|
||||||
|
content,
|
||||||
|
media_url || null,
|
||||||
|
media_type || null,
|
||||||
|
media_metadata || null
|
||||||
|
]);
|
||||||
|
|
||||||
// Update conversation timestamp
|
// Update conversation timestamp
|
||||||
const updateConvQuery = `UPDATE conversations SET updated_at = NOW() WHERE id = $1`;
|
const updateConvQuery = `UPDATE conversations SET updated_at = NOW() WHERE id = $1`;
|
||||||
|
|
@ -193,4 +188,48 @@ router.put("/messages/:messageId/read", async (req, res) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 6. POST /communications (Log Call/Communication)
|
||||||
|
router.post("/communications", async (req, res) => {
|
||||||
|
const client = await pool.connect();
|
||||||
|
try {
|
||||||
|
const {
|
||||||
|
conversation_id,
|
||||||
|
buyer_id,
|
||||||
|
seller_id,
|
||||||
|
communication_type,
|
||||||
|
call_status,
|
||||||
|
duration_seconds,
|
||||||
|
call_recording_url
|
||||||
|
} = req.body;
|
||||||
|
|
||||||
|
await client.query("BEGIN");
|
||||||
|
|
||||||
|
const insertQuery = `
|
||||||
|
INSERT INTO communication_records (
|
||||||
|
conversation_id, buyer_id, seller_id,
|
||||||
|
communication_type, call_status, duration_seconds, call_recording_url
|
||||||
|
)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||||
|
RETURNING *
|
||||||
|
`;
|
||||||
|
const result = await client.query(insertQuery, [
|
||||||
|
conversation_id, buyer_id, seller_id,
|
||||||
|
communication_type, call_status, duration_seconds || 0, call_recording_url
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Trigger updates conversation via DB trigger, but we might want to emit socket event?
|
||||||
|
// For now, just return success.
|
||||||
|
|
||||||
|
await client.query("COMMIT");
|
||||||
|
res.status(201).json(result.rows[0]);
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
await client.query("ROLLBACK");
|
||||||
|
console.error("Error logging communication:", err);
|
||||||
|
res.status(500).json({ error: "Internal server error" });
|
||||||
|
} finally {
|
||||||
|
client.release();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue