diff --git a/database/migrations/010_chat_notifications.sql b/database/migrations/010_chat_notifications.sql new file mode 100644 index 0000000..b96c3fd --- /dev/null +++ b/database/migrations/010_chat_notifications.sql @@ -0,0 +1,4 @@ +ALTER TABLE notifications ADD COLUMN IF NOT EXISTS context_id UUID; +ALTER TABLE notifications ADD COLUMN IF NOT EXISTS context_type VARCHAR(50); + +CREATE INDEX IF NOT EXISTS idx_notifications_context ON notifications(context_type, context_id) WHERE context_type IS NOT NULL; diff --git a/database/migrations/run_all.sql b/database/migrations/run_all.sql index b1add20..ededccc 100644 --- a/database/migrations/run_all.sql +++ b/database/migrations/run_all.sql @@ -31,6 +31,9 @@ \echo '=== Running 009_settings.sql (Company Settings + User Preferences) ===' \i 009_settings.sql +\echo '=== Running 010_chat_notifications.sql (Chat Message Notifications) ===' +\i 010_chat_notifications.sql + \echo '=== Migration Complete ===' \echo '' \echo 'Test accounts created:' diff --git a/src/app/api/conversations/[id]/messages/route.ts b/src/app/api/conversations/[id]/messages/route.ts index f395322..d1ee0c0 100644 --- a/src/app/api/conversations/[id]/messages/route.ts +++ b/src/app/api/conversations/[id]/messages/route.ts @@ -84,13 +84,28 @@ export async function POST( ) const msg = result.rows[0] + const senderName = `${user.firstName} ${user.lastName}` + + const otherResult = await query( + `SELECT user_id FROM conversation_participants + WHERE conversation_id = $1 AND user_id != $2`, + [id, user.id], + ) + + for (const row of otherResult.rows) { + await query( + `INSERT INTO notifications (user_id, type, title, description, link, context_id, context_type) + VALUES ($1, 'chat_message', 'New Message', $2, '/chats', $3, 'conversation')`, + [row.user_id, `${senderName} sent a message`, id], + ) + } return NextResponse.json({ message: { id: msg.id, conversationId: id, senderId: user.id, - senderName: `${user.firstName} ${user.lastName}`, + senderName, senderAvatar: user.avatar, content: content.trim(), timestamp: formatTime(new Date(msg.created_at)), diff --git a/src/app/api/conversations/[id]/read/route.ts b/src/app/api/conversations/[id]/read/route.ts index 758fd90..e5357e8 100644 --- a/src/app/api/conversations/[id]/read/route.ts +++ b/src/app/api/conversations/[id]/read/route.ts @@ -19,6 +19,12 @@ export async function POST( [id, user.id], ) + await query( + `UPDATE notifications SET is_read = TRUE + WHERE user_id = $1 AND context_type = 'conversation' AND context_id = $2 AND is_read = FALSE`, + [user.id, id], + ) + return NextResponse.json({ success: true }) } catch (error) { console.error("Mark read error:", error) diff --git a/src/app/api/notifications/route.ts b/src/app/api/notifications/route.ts index d8c2c00..cecfda7 100644 --- a/src/app/api/notifications/route.ts +++ b/src/app/api/notifications/route.ts @@ -8,7 +8,7 @@ export async function GET() { if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }) const result = await query( - `SELECT id, type, title, description, link, is_read, created_at + `SELECT id, type, title, description, link, is_read, created_at, context_id, context_type FROM notifications WHERE user_id = $1 ORDER BY created_at DESC @@ -24,6 +24,8 @@ export async function GET() { link: r.link, read: r.is_read, timestamp: r.created_at, + contextId: r.context_id, + contextType: r.context_type, })) const unreadResult = await query(