Files
backend/prisma/schema.prisma

350 lines
9.8 KiB
Plaintext
Raw Normal View History

// Real Estate Agent Platform - Database Schema
2026-01-11 20:59:12 +05:30
// Milestone 1 & 2: Foundation, Authentication & Agent Features
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
}
// ===========================================
// ENUMS
// ===========================================
enum UserRole {
USER
AGENT
ADMIN
}
enum UserStatus {
ACTIVE
INACTIVE
SUSPENDED
PENDING_VERIFICATION
}
enum AuthProvider {
LOCAL
GOOGLE
FACEBOOK
TWITTER
}
2026-01-24 03:33:54 +05:30
enum FieldType {
TEXT // Single line text input
TEXTAREA // Multi-line text area
CHECKBOX // Single checkbox (Yes/No)
CHECKBOX_GROUP // Multiple checkboxes in grid (select multiple)
RADIO // Radio buttons (select one)
SELECT // Dropdown (select one)
MULTI_SELECT // Dropdown with multi-select
RANGE // Slider with min/max
NUMBER // Number input
DATE // Date picker
TAG_INPUT // Tag input (add custom tags)
}
2026-01-11 20:59:12 +05:30
// ===========================================
// USER - Authentication Only
// ===========================================
model User {
id String @id @default(uuid())
email String @unique
password String? // Null for social login users
role UserRole @default(USER)
status UserStatus @default(ACTIVE)
emailVerified Boolean @default(false)
emailVerifiedAt DateTime?
2026-01-11 20:59:12 +05:30
avatar String? // Profile picture URL
// Social Auth
authProvider AuthProvider @default(LOCAL)
googleId String? @unique
facebookId String? @unique
twitterId String? @unique
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
lastLoginAt DateTime?
// Relations - Profile based on role
userProfile UserProfile?
agentProfile AgentProfile?
// Auth Relations
sessions Session[]
@@index([email])
@@index([role])
@@index([status])
@@map("users")
}
// ===========================================
// USER PROFILE - Normal Users
// ===========================================
model UserProfile {
id String @id @default(uuid())
userId String @unique
firstName String?
lastName String?
phone String?
avatar String?
// Location
city String?
state String?
country String?
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
@@map("user_profiles")
}
// ===========================================
// AGENT PROFILE - Agents/Subscribers
// ===========================================
model AgentProfile {
id String @id @default(uuid())
userId String @unique
slug String @unique
agentTypeId String?
// Basic Info
firstName String?
lastName String?
phone String?
avatar String?
2026-01-11 20:59:12 +05:30
bio String? @db.Text
headline String?
// Location
city String?
state String?
country String?
address String?
2026-01-11 20:59:12 +05:30
zipCode String?
latitude Float?
longitude Float?
// Professional Info
yearsOfExperience Int?
licenseNumber String?
companyName String?
2026-01-11 20:59:12 +05:30
website String?
// Social Links
facebookUrl String?
twitterUrl String?
linkedinUrl String?
instagramUrl String?
// Verification Status
isVerified Boolean @default(false)
verifiedAt DateTime?
// Profile Status
isProfileComplete Boolean @default(false)
profileCompleteness Int @default(0)
2026-01-11 20:59:12 +05:30
isPublic Boolean @default(true)
isFeatured Boolean @default(false)
// Stats (denormalized for performance)
totalReviews Int @default(0)
averageRating Float @default(0)
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
2026-01-11 20:59:12 +05:30
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
agentType AgentType? @relation(fields: [agentTypeId], references: [id])
2026-01-24 03:33:54 +05:30
fieldValues AgentProfileFieldValue[]
@@index([userId])
@@index([agentTypeId])
@@index([slug])
@@index([city, state])
2026-01-11 20:59:12 +05:30
@@index([isVerified])
@@index([isPublic])
@@index([isFeatured])
@@map("agent_profiles")
}
2026-01-11 20:59:12 +05:30
// ===========================================
// AGENT TYPE - Dynamic agent types (Professional, Lender, etc.)
2026-01-11 20:59:12 +05:30
// ===========================================
model AgentType {
id String @id @default(uuid())
name String @unique
description String?
icon String?
isActive Boolean @default(true)
sortOrder Int @default(0)
2026-01-11 20:59:12 +05:30
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
2026-01-11 20:59:12 +05:30
// Relations
2026-01-24 03:33:54 +05:30
agents AgentProfile[]
agentTypeSections AgentTypeSection[]
2026-01-11 20:59:12 +05:30
@@index([isActive])
@@map("agent_types")
2026-01-11 20:59:12 +05:30
}
2026-01-24 03:33:54 +05:30
// ===========================================
// DYNAMIC PROFILE FIELD SYSTEM
// ===========================================
// Profile Sections - Groups of related fields
model ProfileSection {
id String @id @default(uuid())
name String // e.g., "Location", "Experience", "Specialization"
slug String @unique // URL-friendly identifier
description String?
icon String? // Icon name or URL
sortOrder Int @default(0)
isActive Boolean @default(true)
isGlobal Boolean @default(false) // If true, applies to ALL agent types
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
fields ProfileField[]
agentTypeSections AgentTypeSection[]
@@index([isActive])
@@index([sortOrder])
@@map("profile_sections")
}
// Many-to-Many: Which sections belong to which agent types
model AgentTypeSection {
id String @id @default(uuid())
agentTypeId String
sectionId String
sortOrder Int @default(0) // Order specific to this agent type
isRequired Boolean @default(false) // Is this section required for this type
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
agentType AgentType @relation(fields: [agentTypeId], references: [id], onDelete: Cascade)
section ProfileSection @relation(fields: [sectionId], references: [id], onDelete: Cascade)
@@unique([agentTypeId, sectionId])
@@index([agentTypeId])
@@index([sectionId])
@@map("agent_type_sections")
}
// Profile Fields - Individual form fields
model ProfileField {
id String @id @default(uuid())
sectionId String
name String // e.g., "State", "Years in Business"
slug String // Unique within section: e.g., "state", "years_in_business"
fieldType FieldType
description String? // Help text shown to user
placeholder String? // Placeholder text for input
defaultValue String? // Default value (JSON for complex types)
sortOrder Int @default(0)
isActive Boolean @default(true)
isRequired Boolean @default(false)
// Validation rules (JSON)
validation Json? // { min, max, minLength, maxLength, pattern, etc. }
// For SELECT, RADIO, MULTI_SELECT, CHECKBOX_GROUP
options Json? // [{ value: "...", label: "...", sortOrder: 0 }]
// For RANGE/slider
rangeConfig Json? // { min: 0, max: 100, step: 1 }
// UI Configuration
uiConfig Json? // { columns: 2, showInPreview: true, etc. }
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
section ProfileSection @relation(fields: [sectionId], references: [id], onDelete: Cascade)
fieldValues AgentProfileFieldValue[]
@@unique([sectionId, slug])
@@index([sectionId])
@@index([isActive])
@@map("profile_fields")
}
// Agent Profile Field Values - Stores actual agent data
model AgentProfileFieldValue {
id String @id @default(uuid())
agentProfileId String
fieldId String
// Value storage - use appropriate column based on field type
textValue String? @db.Text // For TEXT, TEXTAREA
numberValue Float? // For NUMBER, RANGE
booleanValue Boolean? // For single CHECKBOX
jsonValue Json? // For MULTI_SELECT, RADIO, complex data
dateValue DateTime? // For DATE
// Timestamps
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
agentProfile AgentProfile @relation(fields: [agentProfileId], references: [id], onDelete: Cascade)
field ProfileField @relation(fields: [fieldId], references: [id], onDelete: Cascade)
@@unique([agentProfileId, fieldId])
@@index([agentProfileId])
@@index([fieldId])
@@map("agent_profile_field_values")
}
// ===========================================
// SESSION - JWT Token Management
// ===========================================
model Session {
id String @id @default(uuid())
userId String
token String @unique
refreshToken String? @unique
userAgent String?
ipAddress String?
expiresAt DateTime
createdAt DateTime @default(now())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
@@index([token])
@@map("sessions")
}