1 | generator client {
|
---|
2 | provider = "prisma-client-js"
|
---|
3 | }
|
---|
4 |
|
---|
5 | datasource db {
|
---|
6 | provider = "postgresql"
|
---|
7 | url = env("DATABASE_URL")
|
---|
8 | }
|
---|
9 |
|
---|
10 | model Client {
|
---|
11 | id String @id @default(uuid())
|
---|
12 | tenantId String @map("tenant_id")
|
---|
13 | name String
|
---|
14 | email String @unique
|
---|
15 | address Json
|
---|
16 | logoUrl String? @map("logo_url")
|
---|
17 | phoneNumber String? @map("phone_number")
|
---|
18 | vatNumber String? @map("vat_number")
|
---|
19 | companyNumber String? @map("company_number")
|
---|
20 | representative String
|
---|
21 | status ClientStatus @default(active)
|
---|
22 | createdAt DateTime @default(now()) @map("created_at")
|
---|
23 | updatedAt DateTime @updatedAt @map("updated_at")
|
---|
24 |
|
---|
25 | tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
|
---|
26 | invoicesReceived Invoice[] @relation("InvoiceTo")
|
---|
27 |
|
---|
28 | // Note: Advanced indexes (partial, functional, case-insensitive)
|
---|
29 | // are handled via raw SQL in sql/02_client_email_index.sql
|
---|
30 |
|
---|
31 | // Basic indexes that Prisma can handle
|
---|
32 | @@index([email]) // Basic email index
|
---|
33 | @@index([email, tenantId]) // Composite email + tenant index
|
---|
34 | @@index([status]) // Status index
|
---|
35 | @@index([tenantId]) // Tenant index for multi-tenant queries
|
---|
36 | }
|
---|
37 |
|
---|
38 | model Service {
|
---|
39 | id String @id @default(uuid())
|
---|
40 | name String
|
---|
41 | sprint Float
|
---|
42 | hour Float
|
---|
43 | month Float
|
---|
44 | tenantId String @map("tenant_id")
|
---|
45 | tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
|
---|
46 |
|
---|
47 | lineItems LineItem[]
|
---|
48 | }
|
---|
49 |
|
---|
50 | model Invoice {
|
---|
51 | id String @id @default(uuid())
|
---|
52 | sent Int?
|
---|
53 | dueDate DateTime @map("due_date")
|
---|
54 | status InvoiceStatus
|
---|
55 | currency Currency
|
---|
56 | quantityType QuantityType @map("quantity_type")
|
---|
57 | subTotal Float @map("sub_total")
|
---|
58 | issueDate DateTime @map("issue_date")
|
---|
59 | month Month
|
---|
60 | discount Float?
|
---|
61 | taxes Float?
|
---|
62 | totalAmount Float @map("total_amount")
|
---|
63 | invoiceNumber String @unique @map("invoice_number")
|
---|
64 | pdfRef String? @map("pdf_ref")
|
---|
65 | createdAt DateTime @default(now()) @map("created_at")
|
---|
66 | updatedAt DateTime @updatedAt @map("updated_at")
|
---|
67 |
|
---|
68 | invoiceFromId String @map("tenant_id")
|
---|
69 | invoiceFrom Tenant @relation(fields: [invoiceFromId], references: [id], onDelete: Cascade)
|
---|
70 |
|
---|
71 | invoiceToId String @map("client_id")
|
---|
72 | invoiceTo Client @relation("InvoiceTo", fields: [invoiceToId], references: [id], onDelete: Cascade)
|
---|
73 |
|
---|
74 | items LineItem[]
|
---|
75 |
|
---|
76 | // Note: Complex trigger for status updates is handled via raw SQL
|
---|
77 | // in sql/01_invoice_status_trigger.sql
|
---|
78 |
|
---|
79 | // Performance indexes for common queries
|
---|
80 | @@index([status]) // Status-based queries
|
---|
81 | @@index([invoiceFromId]) // Tenant-based queries
|
---|
82 | @@index([invoiceToId]) // Client-based queries
|
---|
83 | @@index([issueDate]) // Date-based queries
|
---|
84 | @@index([dueDate]) // Due date queries
|
---|
85 | @@index([status, invoiceFromId]) // Combined status + tenant
|
---|
86 | @@index([status, dueDate]) // Overdue invoice queries
|
---|
87 | }
|
---|
88 |
|
---|
89 | model LineItem {
|
---|
90 | id String @id @default(uuid())
|
---|
91 | title String
|
---|
92 | price Float
|
---|
93 | total Float
|
---|
94 | quantity Int
|
---|
95 | description String?
|
---|
96 | serviceId String @map("service_id")
|
---|
97 | service Service @relation(fields: [serviceId], references: [id], onDelete: Cascade)
|
---|
98 | invoiceId String @map("invoice_id")
|
---|
99 | invoice Invoice @relation(fields: [invoiceId], references: [id], onDelete: Cascade)
|
---|
100 | }
|
---|
101 |
|
---|
102 | model Tenant {
|
---|
103 | id String @id @default(cuid())
|
---|
104 | name String
|
---|
105 | email String @unique
|
---|
106 | address Json
|
---|
107 | bankAccounts Json? @map("bank_accounts")
|
---|
108 | logoUrl String? @map("logo_url")
|
---|
109 | phoneNumber String? @map("phone_number")
|
---|
110 | vatNumber String? @map("vat_number")
|
---|
111 | companyNumber String? @map("company_number")
|
---|
112 | representative String
|
---|
113 | lastInvoiceNumber String @default("0") @map("last_invoice_number")
|
---|
114 | createdAt DateTime @default(now()) @map("created_at")
|
---|
115 | updatedAt DateTime @updatedAt @map("updated_at")
|
---|
116 | invoicesSent Invoice[]
|
---|
117 | services Service[]
|
---|
118 | employees Employee[]
|
---|
119 | clients Client[]
|
---|
120 | users User[]
|
---|
121 | }
|
---|
122 |
|
---|
123 | model Employee {
|
---|
124 | id String @id @default(uuid())
|
---|
125 | name String
|
---|
126 | email String @unique
|
---|
127 | status EmployeeStatus @default(active)
|
---|
128 | iban String?
|
---|
129 | cv String?
|
---|
130 | photo String?
|
---|
131 | project String?
|
---|
132 | tenantId String @map("tenant_id")
|
---|
133 | tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
|
---|
134 | createdAt DateTime @default(now()) @map("created_at")
|
---|
135 | updatedAt DateTime @updatedAt @map("updated_at")
|
---|
136 | }
|
---|
137 |
|
---|
138 | model User {
|
---|
139 | id String @id @default(uuid())
|
---|
140 | uid String @unique // Firebase UID
|
---|
141 | email String @unique
|
---|
142 | displayName String @map("display_name")
|
---|
143 | role UserRole
|
---|
144 | tenantId String @map("tenant_id")
|
---|
145 | tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
|
---|
146 | createdAt DateTime @default(now()) @map("created_at")
|
---|
147 | updatedAt DateTime @updatedAt @map("updated_at")
|
---|
148 | }
|
---|
149 |
|
---|
150 | enum UserRole {
|
---|
151 | ADMIN @map("ADMIN")
|
---|
152 | MANAGER @map("MANAGER")
|
---|
153 | USER @map("USER")
|
---|
154 | }
|
---|
155 |
|
---|
156 | enum ClientStatus {
|
---|
157 | active @map("ACTIVE")
|
---|
158 | banned @map("BANNED")
|
---|
159 | inactive @map("INACTIVE")
|
---|
160 | }
|
---|
161 |
|
---|
162 | enum InvoiceStatus {
|
---|
163 | draft @map("DRAFT")
|
---|
164 | processing @map("PROCESSING")
|
---|
165 | pending @map("PENDING")
|
---|
166 | overdue @map("OVERDUE")
|
---|
167 | paid @map("PAID")
|
---|
168 | }
|
---|
169 |
|
---|
170 | enum Currency {
|
---|
171 | EUR @map("EUR")
|
---|
172 | USD @map("USD")
|
---|
173 | }
|
---|
174 |
|
---|
175 | enum QuantityType {
|
---|
176 | Unit @map("UNIT")
|
---|
177 | Hour @map("HOUR")
|
---|
178 | Sprint @map("SPRINT")
|
---|
179 | Month @map("MONTH")
|
---|
180 | }
|
---|
181 |
|
---|
182 | enum Month {
|
---|
183 | January @map("JANUARY")
|
---|
184 | February @map("FEBRUARY")
|
---|
185 | March @map("MARCH")
|
---|
186 | April @map("APRIL")
|
---|
187 | May @map("MAY")
|
---|
188 | June @map("JUNE")
|
---|
189 | July @map("JULY")
|
---|
190 | August @map("AUGUST")
|
---|
191 | September @map("SEPTEMBER")
|
---|
192 | October @map("OCTOBER")
|
---|
193 | November @map("NOVEMBER")
|
---|
194 | December @map("DECEMBER")
|
---|
195 | }
|
---|
196 |
|
---|
197 | enum EmployeeStatus {
|
---|
198 | active @map("ACTIVE")
|
---|
199 | inactive @map("INACTIVE")
|
---|
200 | }
|
---|