-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.dbml
More file actions
601 lines (539 loc) · 14.7 KB
/
Copy pathdatabase.dbml
File metadata and controls
601 lines (539 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// ClientFlow MVP relational model
// Source of truth for SQLAlchemy models and migrations.
Enum membership_role {
owner
admin
manager
agent
technician
}
Enum lead_status {
new
contacted
qualified
won
lost
}
Enum job_status {
draft
scheduled
in_progress
review
completed
cancelled
}
Enum appointment_status {
scheduled
confirmed
completed
cancelled
no_show
}
Enum conversation_status {
open
waiting
resolved
}
Enum message_direction {
inbound
outbound
}
Enum next_action_status {
pending
in_progress
completed
cancelled
}
Enum job_stage_status {
pending
in_progress
completed
}
Enum material_status {
required
ordered
available
used
}
Enum channel_type {
web
email
whatsapp
instagram
}
Enum subscription_status {
trialing
active
past_due
cancelled
expired
}
Enum billing_interval {
monthly
yearly
}
Table plans {
id bigint [pk, increment]
code varchar(60) [not null, unique]
name varchar(100) [not null]
description text
price_eur decimal(10,2) [not null]
billing_interval billing_interval [not null, default: 'monthly']
trial_days int [not null, default: 3]
limits_json json
is_active boolean [not null, default: true]
created_at timestamp [not null]
updated_at timestamp [not null]
}
Table companies {
id bigint [pk, increment]
name varchar(160) [not null]
slug varchar(160) [not null, unique]
email varchar(255)
phone varchar(40)
timezone varchar(60) [not null, default: 'Europe/London']
default_language varchar(5) [not null, default: 'en']
theme varchar(10) [not null, default: 'system']
primary_colour varchar(20)
is_active boolean [not null, default: true]
created_at timestamp [not null]
updated_at timestamp [not null]
}
Table subscriptions {
id bigint [pk, increment]
company_id bigint [not null]
plan_id bigint [not null]
status subscription_status [not null, default: 'trialing']
trial_started_at timestamp
trial_ends_at timestamp
current_period_started_at timestamp
current_period_ends_at timestamp
cancelled_at timestamp
external_customer_id varchar(255)
external_subscription_id varchar(255)
created_at timestamp [not null]
updated_at timestamp [not null]
Indexes {
(company_id, status)
external_subscription_id [unique]
}
}
Table users {
id bigint [pk, increment]
email varchar(255) [not null, unique]
password_hash varchar(255) [not null]
first_name varchar(100) [not null]
last_name varchar(100) [not null]
preferred_language varchar(5) [not null, default: 'en']
is_active boolean [not null, default: true]
last_login_at timestamp
created_at timestamp [not null]
updated_at timestamp [not null]
}
Table company_memberships {
id bigint [pk, increment]
company_id bigint [not null]
user_id bigint [not null]
role membership_role [not null, default: 'agent']
colour varchar(20)
created_at timestamp [not null]
Indexes {
(company_id, user_id) [unique]
}
}
Table member_invitations {
id bigint [pk, increment]
company_id bigint [not null]
email varchar(255)
token_hash varchar(255) [not null, unique]
role membership_role [not null, default: 'agent']
expires_at timestamp [not null]
used_at timestamp
created_by_membership_id bigint [not null]
created_at timestamp [not null]
}
Table password_reset_tokens {
id bigint [pk, increment]
user_id bigint [not null]
token_hash varchar(255) [not null, unique]
expires_at timestamp [not null]
used_at timestamp
created_at timestamp [not null]
}
Table service_zones {
id bigint [pk, increment]
company_id bigint [not null]
name varchar(120) [not null]
postcode_pattern varchar(30)
is_active boolean [not null, default: true]
}
Table service_types {
id bigint [pk, increment]
company_id bigint [not null]
name varchar(120) [not null]
colour varchar(20) [not null]
default_duration_minutes int
is_active boolean [not null, default: true]
}
Table leads {
id bigint [pk, increment]
company_id bigint [not null]
assigned_membership_id bigint
service_type_id bigint
converted_client_id bigint
first_name varchar(100) [not null]
last_name varchar(100)
email varchar(255)
phone varchar(40)
source varchar(80)
status lead_status [not null, default: 'new']
notes text
converted_at timestamp
created_at timestamp [not null]
updated_at timestamp [not null]
Indexes {
(company_id, status)
(company_id, email)
}
}
Table clients {
id bigint [pk, increment]
company_id bigint [not null]
first_name varchar(100) [not null]
last_name varchar(100)
email varchar(255)
phone varchar(40)
notes text
is_active boolean [not null, default: true]
created_at timestamp [not null]
updated_at timestamp [not null]
Indexes {
(company_id, email)
}
}
Table client_addresses {
id bigint [pk, increment]
client_id bigint [not null]
label varchar(60)
line_1 varchar(160) [not null]
line_2 varchar(160)
city varchar(100) [not null]
postcode varchar(20) [not null]
is_primary boolean [not null, default: false]
}
Table jobs {
id bigint [pk, increment]
company_id bigint [not null]
client_id bigint [not null]
service_type_id bigint
service_zone_id bigint
assigned_membership_id bigint
title varchar(180) [not null]
description text
status job_status [not null, default: 'draft']
priority varchar(20) [not null, default: 'normal']
quoted_amount decimal(12,2)
scheduled_start timestamp
scheduled_end timestamp
completed_at timestamp
created_at timestamp [not null]
updated_at timestamp [not null]
Indexes {
(company_id, status)
(client_id, created_at)
}
}
Table job_assignments {
job_id bigint [not null]
membership_id bigint [not null]
role varchar(60)
assigned_at timestamp [not null]
Indexes {
(job_id, membership_id) [pk]
}
}
Table job_stages {
id bigint [pk, increment]
job_id bigint [not null]
title varchar(160) [not null]
description text
position int [not null]
status job_stage_status [not null, default: 'pending']
due_at timestamp
completed_at timestamp
created_at timestamp [not null]
updated_at timestamp [not null]
Indexes {
(job_id, position) [unique]
}
}
Table job_materials {
id bigint [pk, increment]
job_id bigint [not null]
name varchar(160) [not null]
quantity decimal(12,3) [not null]
unit varchar(30) [not null]
unit_cost decimal(12,2)
status material_status [not null, default: 'required']
notes text
created_at timestamp [not null]
updated_at timestamp [not null]
}
Table appointments {
id bigint [pk, increment]
company_id bigint [not null]
job_id bigint
client_id bigint [not null]
assigned_membership_id bigint [not null]
service_type_id bigint
title varchar(180) [not null]
status appointment_status [not null, default: 'scheduled']
starts_at timestamp [not null]
ends_at timestamp [not null]
address_text varchar(300)
notes text
created_at timestamp [not null]
updated_at timestamp [not null]
Indexes {
(company_id, starts_at)
(assigned_membership_id, starts_at)
}
}
Table next_actions {
id bigint [pk, increment]
company_id bigint [not null]
assigned_membership_id bigint [not null]
created_by_membership_id bigint [not null]
lead_id bigint
client_id bigint
job_id bigint
title varchar(180) [not null]
description text
due_at timestamp [not null]
status next_action_status [not null, default: 'pending']
completed_at timestamp
created_at timestamp [not null]
updated_at timestamp [not null]
Note: 'Exactly one of lead_id, client_id or job_id must be set.'
Indexes {
(company_id, due_at)
(assigned_membership_id, status)
}
}
Table activities {
id bigint [pk, increment]
company_id bigint [not null]
actor_membership_id bigint
lead_id bigint
client_id bigint
job_id bigint
conversation_id bigint
event_type varchar(80) [not null]
description text
metadata_json json
created_at timestamp [not null]
Note: 'Exactly one of lead_id, client_id, job_id or conversation_id must be set.'
Indexes {
(company_id, created_at)
}
}
Table attachments {
id bigint [pk, increment]
company_id bigint [not null]
uploaded_by_membership_id bigint [not null]
lead_id bigint
client_id bigint
job_id bigint
message_id bigint
filename varchar(255) [not null]
storage_key varchar(500) [not null]
content_type varchar(120) [not null]
size_bytes int [not null]
category varchar(40) [not null, default: 'document']
created_at timestamp [not null]
Note: 'Exactly one of lead_id, client_id, job_id or message_id must be set.'
Indexes {
(company_id, created_at)
}
}
Table conversations {
id bigint [pk, increment]
company_id bigint [not null]
lead_id bigint
client_id bigint
assigned_membership_id bigint
ai_agent_id bigint
channel channel_type [not null]
external_id varchar(255)
subject varchar(255)
status conversation_status [not null, default: 'open']
last_message_at timestamp
created_at timestamp [not null]
updated_at timestamp [not null]
Indexes {
(company_id, status)
(company_id, channel, external_id) [unique]
}
}
Table conversation_participants {
id bigint [pk, increment]
conversation_id bigint [not null]
membership_id bigint
lead_id bigint
client_id bigint
external_identifier varchar(255)
display_name varchar(160)
participant_type varchar(30) [not null]
joined_at timestamp [not null]
}
Table messages {
id bigint [pk, increment]
conversation_id bigint [not null]
sender_participant_id bigint
direction message_direction [not null]
external_id varchar(255)
content text [not null]
content_type varchar(30) [not null, default: 'text']
sent_by_ai boolean [not null, default: false]
delivery_status varchar(30)
created_at timestamp [not null]
Indexes {
(conversation_id, created_at)
}
}
Table ai_agents {
id bigint [pk, increment]
company_id bigint [not null]
name varchar(120) [not null]
purpose varchar(120) [not null]
model_name varchar(80) [not null]
main_instruction text [not null]
is_active boolean [not null, default: true]
requires_human_approval boolean [not null, default: true]
created_at timestamp [not null]
updated_at timestamp [not null]
}
Table knowledge_documents {
id bigint [pk, increment]
company_id bigint [not null]
title varchar(200) [not null]
source_type varchar(30) [not null]
storage_key varchar(500)
checksum varchar(128)
ingestion_status varchar(30) [not null, default: 'pending']
uploaded_by_membership_id bigint [not null]
created_at timestamp [not null]
updated_at timestamp [not null]
}
Table knowledge_chunks {
id bigint [pk, increment]
document_id bigint [not null]
chunk_index int [not null]
content text [not null]
embedding_reference varchar(255)
token_count int
created_at timestamp [not null]
Indexes {
(document_id, chunk_index) [unique]
}
}
Table agent_documents {
agent_id bigint [not null]
document_id bigint [not null]
Indexes {
(agent_id, document_id) [pk]
}
}
Table integrations {
id bigint [pk, increment]
company_id bigint [not null]
provider varchar(60) [not null]
status varchar(30) [not null, default: 'disconnected']
external_account_id varchar(255)
credentials_reference varchar(500)
settings_json json
created_at timestamp [not null]
updated_at timestamp [not null]
Indexes {
(company_id, provider) [unique]
}
}
Ref: companies.id < company_memberships.company_id
Ref: users.id < company_memberships.user_id
Ref: companies.id < subscriptions.company_id
Ref: plans.id < subscriptions.plan_id
Ref: companies.id < member_invitations.company_id
Ref: company_memberships.id < member_invitations.created_by_membership_id
Ref: users.id < password_reset_tokens.user_id
Ref: companies.id < service_zones.company_id
Ref: companies.id < service_types.company_id
Ref: companies.id < leads.company_id
Ref: company_memberships.id < leads.assigned_membership_id
Ref: service_types.id < leads.service_type_id
Ref: clients.id - leads.converted_client_id
Ref: companies.id < clients.company_id
Ref: clients.id < client_addresses.client_id
Ref: companies.id < jobs.company_id
Ref: clients.id < jobs.client_id
Ref: service_types.id < jobs.service_type_id
Ref: service_zones.id < jobs.service_zone_id
Ref: company_memberships.id < jobs.assigned_membership_id
Ref: jobs.id < job_assignments.job_id
Ref: company_memberships.id < job_assignments.membership_id
Ref: jobs.id < job_stages.job_id
Ref: jobs.id < job_materials.job_id
Ref: companies.id < appointments.company_id
Ref: jobs.id < appointments.job_id
Ref: clients.id < appointments.client_id
Ref: company_memberships.id < appointments.assigned_membership_id
Ref: service_types.id < appointments.service_type_id
Ref: companies.id < next_actions.company_id
Ref: company_memberships.id < next_actions.assigned_membership_id
Ref: company_memberships.id < next_actions.created_by_membership_id
Ref: leads.id < next_actions.lead_id
Ref: clients.id < next_actions.client_id
Ref: jobs.id < next_actions.job_id
Ref: companies.id < activities.company_id
Ref: company_memberships.id < activities.actor_membership_id
Ref: leads.id < activities.lead_id
Ref: clients.id < activities.client_id
Ref: jobs.id < activities.job_id
Ref: conversations.id < activities.conversation_id
Ref: companies.id < attachments.company_id
Ref: company_memberships.id < attachments.uploaded_by_membership_id
Ref: leads.id < attachments.lead_id
Ref: clients.id < attachments.client_id
Ref: jobs.id < attachments.job_id
Ref: messages.id < attachments.message_id
Ref: companies.id < conversations.company_id
Ref: leads.id < conversations.lead_id
Ref: clients.id < conversations.client_id
Ref: company_memberships.id < conversations.assigned_membership_id
Ref: ai_agents.id < conversations.ai_agent_id
Ref: conversations.id < conversation_participants.conversation_id
Ref: company_memberships.id < conversation_participants.membership_id
Ref: leads.id < conversation_participants.lead_id
Ref: clients.id < conversation_participants.client_id
Ref: conversations.id < messages.conversation_id
Ref: conversation_participants.id < messages.sender_participant_id
Ref: companies.id < ai_agents.company_id
Ref: companies.id < knowledge_documents.company_id
Ref: company_memberships.id < knowledge_documents.uploaded_by_membership_id
Ref: knowledge_documents.id < knowledge_chunks.document_id
Ref: ai_agents.id < agent_documents.agent_id
Ref: knowledge_documents.id < agent_documents.document_id
Ref: companies.id < integrations.company_id
// AUTH-API-01 (#22): integrar migraciones posteriormente en #43.
Table auth_sessions {
id varchar(64) [pk]
user_id integer [not null, ref: > users.id]
expires_at timestamptz [not null]
revoked_at timestamptz
indexes { user_id }
}
Table auth_rate_limits {
key varchar(64) [pk]
count integer [not null]
expires_at integer [not null]
indexes { expires_at }
}