feat: Implement real-time messaging module with REST API endpoints and WebSocket gateway.

This commit is contained in:
pradeepkumar
2026-02-08 22:44:16 +05:30
parent 895a106d1b
commit 3b1a7b999e
14 changed files with 1274 additions and 34 deletions

View File

@@ -62,6 +62,19 @@ enum ConnectionStatus {
REJECTED
}
enum MessageType {
TEXT
FILE
IMAGE
SYSTEM // For system messages like "connection accepted"
}
enum MessageStatus {
SENT
DELIVERED
READ
}
// ===========================================
// USER - Authentication Only
@@ -94,6 +107,10 @@ model User {
updatedAt DateTime @updatedAt
lastLoginAt DateTime?
// Online Status (for messaging)
isOnline Boolean @default(false)
lastSeenAt DateTime?
// Relations - Profile based on role
userProfile UserProfile?
agentProfile AgentProfile?
@@ -104,9 +121,14 @@ model User {
// Connection Requests
connectionRequests ConnectionRequest[]
// Messaging Relations
conversations Conversation[]
messages Message[]
@@index([email])
@@index([role])
@@index([status])
@@index([isOnline])
@@map("users")
}
@@ -204,6 +226,7 @@ model AgentProfile {
agentType AgentType? @relation(fields: [agentTypeId], references: [id])
fieldValues AgentProfileFieldValue[]
connectionRequests ConnectionRequest[]
conversations Conversation[]
@@index([userId])
@@index([agentTypeId])
@@ -409,3 +432,63 @@ model ConnectionRequest {
@@index([status])
@@map("connection_requests")
}
// ===========================================
// MESSAGING SYSTEM
// ===========================================
model Conversation {
id String @id @default(uuid())
userId String // Regular user in the conversation
agentProfileId String // Agent in the conversation
lastMessageAt DateTime?
lastMessageText String? @db.VarChar(255)
userUnreadCount Int @default(0) // Unread count for the user
agentUnreadCount Int @default(0) // Unread count for the agent
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
agentProfile AgentProfile @relation(fields: [agentProfileId], references: [id], onDelete: Cascade)
messages Message[]
@@unique([userId, agentProfileId]) // One conversation per user-agent pair
@@index([userId, lastMessageAt])
@@index([agentProfileId, lastMessageAt])
@@map("conversations")
}
model Message {
id String @id @default(uuid())
conversationId String
senderId String // User ID of the sender (can be user or agent's user)
content String @db.Text
messageType MessageType @default(TEXT)
// File attachment fields
fileUrl String?
fileName String?
fileSize Int? // File size in bytes
mimeType String?
// Message status
status MessageStatus @default(SENT)
deliveredAt DateTime?
readAt DateTime?
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
conversation Conversation @relation(fields: [conversationId], references: [id], onDelete: Cascade)
sender User @relation(fields: [senderId], references: [id], onDelete: Cascade)
@@index([conversationId, createdAt])
@@index([senderId])
@@index([status])
@@map("messages")
}