265 lines
9.0 KiB
Plaintext
265 lines
9.0 KiB
Plaintext
// R-P6 (P6 CRM core) — typed CRM entities that replace the generic Entity-Builder
|
|
// for the CRM domain (AD-2). Source of truth: ENTITY-MODEL.md Domäne 5 (LOCKED).
|
|
//
|
|
// AD-21: one self-contained schema file per module. `tenantId` /
|
|
// `createdByUserId` / `zustaendigerUserId` are intentionally BARE String scalars
|
|
// (no `@relation` to Tenant/User) so this file has zero merge-conflict surface
|
|
// against `core.prisma`/`accounts.prisma` (no back-relation fields on Tenant/User
|
|
// needed). Referential integrity to Tenant/User is carried at the application
|
|
// layer (crmService + crmAccess) plus the RLS policies added in the merge
|
|
// migration (see prisma/rls/crm-isolation.sql — deferred activation, like
|
|
// appsEngine/activity).
|
|
//
|
|
// Activity stream is NOT defined here: it lives in `activity.prisma` (model
|
|
// Activity, polymorphic subjectType/subjectId) and is appended via `logActivity()`
|
|
// from `@hivegrid/activity` (AGENTS DoD #14).
|
|
//
|
|
// Soft-delete via `archivedAt`. Demo-seed rows carry `isDemoData = true` and are
|
|
// bulk-removed on the production switch-over (per-module deleteMany).
|
|
//
|
|
// Lead → Projekt extension point (DoD 5, AD-20): Lead and Projekt are built in
|
|
// P7/P8. The polymorphic `CustomFieldEntityType` enum and the `TagAssignment`
|
|
// /`Activity` entityType strings already carry `lead`/`projekt`/`rechnung`, so
|
|
// custom fields, tags and activities attach to those entities without a schema
|
|
// change. A Kontakt is referenced from a future Lead/Projekt by bare id only.
|
|
|
|
/// Kontakt discriminator: a Kontakt is either a private person or an organisation.
|
|
enum KontaktTyp {
|
|
privatperson
|
|
organisation
|
|
}
|
|
|
|
/// Lifecycle status of a Kontakt (lead-independent commercial relationship state).
|
|
enum KontaktStatus {
|
|
kontakt
|
|
kunde
|
|
bestandskunde
|
|
}
|
|
|
|
/// Optional legal-form / classification marker for organisation contacts.
|
|
enum OrgMerkmal {
|
|
gmbh
|
|
kg
|
|
oeffentliche_einrichtung
|
|
sonstige
|
|
}
|
|
|
|
/// Role of a KontaktRelation (Kontaktart) — "this contact is the X of that one".
|
|
enum KontaktArt {
|
|
hauptkontakt
|
|
sekundaerkontakt
|
|
mitarbeiter
|
|
sonstige
|
|
}
|
|
|
|
/// Polymorphic target type for tenant custom fields. Carries `lead`/`projekt`/
|
|
/// `rechnung` already so P7/P8 attach without a migration (Lead → Projekt
|
|
/// extension point, DoD 5).
|
|
enum CustomFieldEntityType {
|
|
projekt
|
|
kontakt
|
|
lead
|
|
rechnung
|
|
}
|
|
|
|
/// Data type of a tenant custom field — drives the value renderer and validation.
|
|
enum CustomFieldDatentyp {
|
|
text
|
|
textarea
|
|
zahl
|
|
waehrung
|
|
datum
|
|
datum_range
|
|
select
|
|
multiselect
|
|
boolean
|
|
email
|
|
telefon
|
|
datei
|
|
}
|
|
|
|
/// A contact (Kontakt), the CRM core entity. Oberbegriff for both private persons
|
|
/// (kontaktTyp = privatperson → vorname/nachname) and organisations
|
|
/// (kontaktTyp = organisation → orgName/orgMerkmal). Scoped per tenant, soft-delete
|
|
/// via `archivedAt`. `zustaendigerUserId` is the owner for the crm.read_own filter.
|
|
model Kontakt {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
tenantId String
|
|
createdByUserId String
|
|
kontaktTyp KontaktTyp
|
|
kontaktStatus KontaktStatus @default(kontakt)
|
|
// Person fields (kontaktTyp = privatperson).
|
|
vorname String?
|
|
nachname String?
|
|
// Organisation fields (kontaktTyp = organisation).
|
|
orgName String?
|
|
orgMerkmal OrgMerkmal?
|
|
// Denormalised primary channels, kept in sync from KontaktEmail/KontaktTelefon
|
|
// by the service so list/dup-check queries stay single-table.
|
|
primaerEmail String?
|
|
primaerTelefon String?
|
|
// Single reusable address slot (Projekt has 3 slots, Kontakt has 1).
|
|
adresseId String?
|
|
// Owner of record — drives the crm.read_own owner filter (DoD 8).
|
|
zustaendigerUserId String?
|
|
// Preferred outbound language (i18n field per ENTITY-MODEL, AK-P3 DoD 6).
|
|
// Null = tenant default; outbound usage lands in v2 — no UI editor in v1.
|
|
language String?
|
|
isDemoData Boolean @default(false)
|
|
archivedAt DateTime?
|
|
|
|
adresse Adresse? @relation(fields: [adresseId], references: [id])
|
|
emails KontaktEmail[]
|
|
telefone KontaktTelefon[]
|
|
relationenVon KontaktRelation[] @relation("KontaktRelationVon")
|
|
relationenZu KontaktRelation[] @relation("KontaktRelationZu")
|
|
|
|
@@index([tenantId, archivedAt])
|
|
@@index([tenantId, kontaktTyp, archivedAt])
|
|
@@index([tenantId, primaerEmail])
|
|
@@index([tenantId, primaerTelefon])
|
|
@@index([tenantId, zustaendigerUserId])
|
|
@@index([tenantId, adresseId])
|
|
}
|
|
|
|
/// Self-referential M:N relation between contacts (e.g. organisation ↔ primary
|
|
/// contact person). The `rolle` (Kontaktart) is the role of the relation itself.
|
|
model KontaktRelation {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
tenantId String
|
|
vonKontaktId String
|
|
zuKontaktId String
|
|
rolle KontaktArt
|
|
kontext String? // optional free-text describing the relationship
|
|
|
|
vonKontakt Kontakt @relation("KontaktRelationVon", fields: [vonKontaktId], references: [id], onDelete: Cascade)
|
|
zuKontakt Kontakt @relation("KontaktRelationZu", fields: [zuKontaktId], references: [id], onDelete: Cascade)
|
|
|
|
@@unique([vonKontaktId, zuKontaktId, rolle])
|
|
@@index([tenantId, vonKontaktId])
|
|
@@index([tenantId, zuKontaktId])
|
|
}
|
|
|
|
/// One of a contact's e-mail addresses. Exactly one carries `primaer = true`
|
|
/// (enforced in the service), mirrored to `Kontakt.primaerEmail`.
|
|
model KontaktEmail {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
tenantId String
|
|
kontaktId String
|
|
email String
|
|
primaer Boolean @default(false)
|
|
|
|
kontakt Kontakt @relation(fields: [kontaktId], references: [id], onDelete: Cascade)
|
|
|
|
@@index([tenantId, kontaktId])
|
|
@@index([tenantId, email])
|
|
}
|
|
|
|
/// One of a contact's phone numbers. Exactly one carries `primaer = true`
|
|
/// (enforced in the service), mirrored to `Kontakt.primaerTelefon`.
|
|
model KontaktTelefon {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
tenantId String
|
|
kontaktId String
|
|
telefon String
|
|
primaer Boolean @default(false)
|
|
|
|
kontakt Kontakt @relation(fields: [kontaktId], references: [id], onDelete: Cascade)
|
|
|
|
@@index([tenantId, kontaktId])
|
|
@@index([tenantId, telefon])
|
|
}
|
|
|
|
/// A reusable postal address. v1 is DE-only (`land` defaults to "DE").
|
|
model Adresse {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
tenantId String
|
|
strasse String?
|
|
hausnummer String?
|
|
plz String?
|
|
ort String?
|
|
land String @default("DE")
|
|
|
|
kontakte Kontakt[]
|
|
|
|
@@index([tenantId])
|
|
}
|
|
|
|
/// Tenant-defined custom field for a polymorphic entity type. The actual values
|
|
/// live in CustomFieldValue. `archiviert` hides the field but keeps its values.
|
|
model CustomFieldDefinition {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
tenantId String
|
|
entityType CustomFieldEntityType
|
|
key String // machine name, unique per (tenant, entityType)
|
|
label String
|
|
datentyp CustomFieldDatentyp
|
|
optionen Json? // e.g. { "select": ["A", "B"] } for select/multiselect
|
|
archiviert Boolean @default(false)
|
|
isDemoData Boolean @default(false)
|
|
|
|
values CustomFieldValue[]
|
|
|
|
@@unique([tenantId, entityType, key])
|
|
@@index([tenantId, entityType])
|
|
}
|
|
|
|
/// Value of a custom field for a concrete entity. Polymorphic: `entityId` carries
|
|
/// NO DB foreign key — the service validates entityType match + entity existence.
|
|
model CustomFieldValue {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
updatedAt DateTime @updatedAt
|
|
tenantId String
|
|
definitionId String
|
|
entityType CustomFieldEntityType
|
|
entityId String
|
|
wert Json? // type-variable value (string, number, array for multiselect…)
|
|
|
|
definition CustomFieldDefinition @relation(fields: [definitionId], references: [id], onDelete: Cascade)
|
|
|
|
@@unique([definitionId, entityId])
|
|
@@index([tenantId, entityType, entityId])
|
|
@@index([definitionId])
|
|
}
|
|
|
|
/// A coloured tag. Cross-cutting and polymorphic via TagAssignment.
|
|
model Tag {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
tenantId String
|
|
name String
|
|
farbe String // hex code (#RRGGBB) or palette name
|
|
|
|
assignments TagAssignment[]
|
|
|
|
@@unique([tenantId, name])
|
|
@@index([tenantId])
|
|
}
|
|
|
|
/// Polymorphic M:N link Tag ↔ any entity. `entityType` is a String (extensible,
|
|
/// mirrors Activity.subjectType): kontakt | projekt | lead | rechnung | termin | …
|
|
/// `entityId` carries NO DB foreign key — the service validates existence.
|
|
model TagAssignment {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now())
|
|
tenantId String
|
|
tagId String
|
|
entityType String
|
|
entityId String
|
|
|
|
tag Tag @relation(fields: [tagId], references: [id], onDelete: Cascade)
|
|
|
|
@@unique([tagId, entityId, entityType])
|
|
@@index([tenantId, entityType, entityId])
|
|
@@index([tenantId, tagId])
|
|
}
|