tyt-api-nest/prisma/schema.prisma

121 lines
4.6 KiB
Plaintext

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
generator client {
provider = "prisma-client"
output = "../src/generated/prisma"
}
datasource db {
provider = "postgresql"
}
enum UserRole {
SYSTEM_ADMIN
HOSPITAL_ADMIN
DIRECTOR
TEAM_LEAD
DOCTOR
ENGINEER
}
model Hospital {
id Int @id @default(autoincrement())
name String
code String @unique
departments Department[]
users User[]
patients Patient[]
engineerAssignments EngineerHospitalAssignment[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}
model Department {
id Int @id @default(autoincrement())
name String
hospitalId Int
hospital Hospital @relation(fields: [hospitalId], references: [id], onDelete: Cascade)
medicalGroups MedicalGroup[]
users User[]
patients Patient[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@unique([hospitalId, name])
}
model MedicalGroup {
id Int @id @default(autoincrement())
name String
departmentId Int
department Department @relation(fields: [departmentId], references: [id], onDelete: Cascade)
users User[]
patients Patient[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@unique([departmentId, name])
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
role UserRole @default(DOCTOR)
hospitalId Int?
departmentId Int?
medicalGroupId Int?
managerId Int?
hospital Hospital? @relation(fields: [hospitalId], references: [id], onDelete: SetNull)
department Department? @relation(fields: [departmentId], references: [id], onDelete: SetNull)
medicalGroup MedicalGroup? @relation(fields: [medicalGroupId], references: [id], onDelete: SetNull)
manager User? @relation("UserHierarchy", fields: [managerId], references: [id], onDelete: SetNull)
subordinates User[] @relation("UserHierarchy")
patients Patient[] @relation("DoctorPatients")
engineerAssignments EngineerHospitalAssignment[] @relation("EngineerAssignments")
assignedEngineerHospitals EngineerHospitalAssignment[] @relation("SystemAdminAssignments")
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@index([role])
@@index([hospitalId])
@@index([managerId])
}
model Patient {
id Int @id @default(autoincrement())
name String
hospitalId Int
departmentId Int?
medicalGroupId Int?
doctorId Int
hospital Hospital @relation(fields: [hospitalId], references: [id], onDelete: Restrict)
department Department? @relation(fields: [departmentId], references: [id], onDelete: SetNull)
medicalGroup MedicalGroup? @relation(fields: [medicalGroupId], references: [id], onDelete: SetNull)
doctor User @relation("DoctorPatients", fields: [doctorId], references: [id], onDelete: Restrict)
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
@@index([doctorId])
@@index([hospitalId])
}
model EngineerHospitalAssignment {
id Int @id @default(autoincrement())
hospitalId Int
engineerId Int
assignedById Int
hospital Hospital @relation(fields: [hospitalId], references: [id], onDelete: Cascade)
engineer User @relation("EngineerAssignments", fields: [engineerId], references: [id], onDelete: Restrict)
assignedBy User @relation("SystemAdminAssignments", fields: [assignedById], references: [id], onDelete: Restrict)
createdAt DateTime @default(now())
@@unique([hospitalId, engineerId])
@@index([engineerId])
@@index([assignedById])
}