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 = `
|
||||
SELECT
|
||||
m.*,
|
||||
cm.media_url,
|
||||
cm.media_type as media_file_type,
|
||||
cm.thumbnail_url
|
||||
m.*
|
||||
FROM messages m
|
||||
LEFT JOIN conversation_media cm ON m.media_id = cm.id
|
||||
WHERE m.conversation_id = $1
|
||||
AND m.deleted = FALSE
|
||||
ORDER BY m.created_at DESC
|
||||
|
|
@ -116,28 +112,27 @@ router.post("/messages", async (req, res) => {
|
|||
const client = await pool.connect();
|
||||
try {
|
||||
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 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
|
||||
// Insert Message with embedded media fields
|
||||
const insertMessageQuery = `
|
||||
INSERT INTO messages (conversation_id, sender_id, receiver_id, message_type, content, media_id)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
INSERT INTO messages (
|
||||
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 *
|
||||
`;
|
||||
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
|
||||
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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue