Compare commits
48 Commits
v1.1.0
..
8534be22d0
| Author | SHA1 | Date | |
|---|---|---|---|
| 8534be22d0 | |||
| f0c97cd46d | |||
| 8a5ffbb563 | |||
| 6af1a4bbd4 | |||
| 92d2e62e79 | |||
| 08310ac302 | |||
| 72f7a9dbdb | |||
| c5dc271759 | |||
| 1451e362ff | |||
| 8188d17c87 | |||
| c4e62f0f50 | |||
| 9830ac29a5 | |||
| 0943f11999 | |||
| e2cd26a29e | |||
| f6df97226d | |||
| 185b38dc55 | |||
| 51eb12b414 | |||
| c2ebc7cf1e | |||
| b4be3cebfb | |||
| 083913cadb | |||
| 4c0cc90734 | |||
| 70e97d3ece | |||
| 8dff0310a6 | |||
| ab971618d5 | |||
| 4407bbfbb8 | |||
| 365c7994d5 | |||
| 2c7a87ccd3 | |||
| 45f63d1c48 | |||
| 2d3ca28691 | |||
| 4201a90fd0 | |||
| 3fb1925a98 | |||
| 63ebf3e75f | |||
| 27a0fdbc45 | |||
| 6f293211a4 | |||
| 70e5190594 | |||
| 7d07d52774 | |||
| 75c1f9a7bb | |||
| 62010b05d5 | |||
| e401c11e40 | |||
| d206b360a6 | |||
| 096aa63c6f | |||
| 77602bb4ac | |||
| e763952a84 | |||
| 3823f8aa50 | |||
| 0671565433 | |||
| e145edaa90 | |||
| 3b4a680326 | |||
| 389b878dbd |
@@ -46,6 +46,15 @@ backups
|
||||
backend/uploads
|
||||
backend/backups
|
||||
|
||||
# Daten-Verzeichnis (Bind-Mounts zur Laufzeit, nicht im Build-Context)
|
||||
data/
|
||||
|
||||
# Plesk-Test (nicht für Container)
|
||||
plesktest/
|
||||
|
||||
# Backup-Klone des Repos
|
||||
opencrm-backup-*/
|
||||
|
||||
# Prisma migrations (included, but not dev db)
|
||||
*.db
|
||||
*.db-journal
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
# OpenCRM – zentrale Konfiguration
|
||||
# ==================================
|
||||
# Kopiere diese Datei zu .env und passe die Werte an.
|
||||
# Diese .env wird sowohl vom Backend (npm run dev) als auch von Docker
|
||||
# Compose verwendet.
|
||||
|
||||
# ============== PORTS (extern erreichbar auf dem Host) ==============
|
||||
OPENCRM_PORT=3010 # Backend + Frontend (alles unter einer URL)
|
||||
ADMINER_PORT=8090 # Adminer (Datenbank-UI). 8081 ist häufig schon belegt.
|
||||
DB_PORT=3306 # MariaDB extern (für lokale Tools/Dev). 0 = nicht freigeben.
|
||||
|
||||
# ============== DATEN-PFADE (Bind-Mounts) ==============
|
||||
# Relativ zum Projektverzeichnis. Werden zur Laufzeit angelegt.
|
||||
DATA_DIR=./data
|
||||
DB_DATA_DIR=./data/db
|
||||
UPLOADS_DIR=./data/uploads
|
||||
FACTORY_DEFAULTS_DIR=./data/factory-defaults
|
||||
BACKUPS_DIR=./data/backups
|
||||
|
||||
# ============== DATENBANK ==============
|
||||
# Der App-User (DB_USER) wird beim ersten Start automatisch von MariaDB
|
||||
# angelegt (über MARIADB_USER/MARIADB_PASSWORD im docker-compose) – mit
|
||||
# GRANT ALL PRIVILEGES auf ${DB_NAME}.*. Damit nutzt das Backend NICHT root.
|
||||
# DB_ROOT_PASSWORD ist nur für Adminer / Notfall-Wartung.
|
||||
DB_HOST=localhost # Im Container überschreibt docker-compose das auf "db"
|
||||
DB_NAME=opencrm
|
||||
DB_USER=opencrm
|
||||
DB_PASSWORD=change-this-password
|
||||
DB_ROOT_PASSWORD=change-this-root-password
|
||||
|
||||
# Connection-String wird aus den DB_*-Komponenten zusammengebaut (dotenv-expand).
|
||||
# Manuell überschreiben nur wenn Sonderfälle (z.B. extra Query-Parameter).
|
||||
# Hinweis: für lokales Dev mit MariaDB im Container nutze DB_HOST=localhost,
|
||||
# weil docker-compose den DB-Port auf 127.0.0.1:DB_PORT mappt.
|
||||
DATABASE_URL=mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
|
||||
|
||||
# ============== SECURITY ==============
|
||||
# JWT-Secret: min. 32 Zeichen. Generieren: openssl rand -hex 64
|
||||
# Wird sowohl für Access- als auch Refresh-Token verwendet.
|
||||
JWT_SECRET=change-this-to-a-very-long-random-secret-please-rotate-before-production
|
||||
|
||||
# Access-/Refresh-Token-Lifetimes
|
||||
# - Access-Token: kurzlebig, lebt nur im Browser-Memory (XSS-Schutz)
|
||||
# - Refresh-Token: lang, im httpOnly-Cookie (JS-unzugänglich)
|
||||
# Wenn der Access abläuft, holt das Frontend transparent einen neuen über
|
||||
# /api/auth/refresh – User merkt nichts. Logout invalidiert beide sofort.
|
||||
JWT_EXPIRES_IN=15m
|
||||
JWT_REFRESH_EXPIRES_IN=7d
|
||||
|
||||
# Encryption-Key für Portal-Credentials: GENAU 64 Hex-Zeichen.
|
||||
# Generieren: openssl rand -hex 32
|
||||
ENCRYPTION_KEY=change-this-to-64-hex-characters-please-rotate-before-production-xx
|
||||
|
||||
# Server
|
||||
NODE_ENV=development
|
||||
PORT=3001 # Backend-internal Port (Dev: localhost:3001)
|
||||
LISTEN_ADDR=0.0.0.0 # In Docker = 0.0.0.0, in Bare-Metal-Production = 127.0.0.1
|
||||
|
||||
# CORS – nur in Production setzen, wenn Frontend auf separater Domain läuft.
|
||||
# Beispiel: CORS_ORIGINS=https://crm.deine-domain.de
|
||||
# CORS_ORIGINS=
|
||||
|
||||
# HTTPS-only-Header (HSTS + upgrade-insecure-requests) – NUR aktivieren, wenn
|
||||
# wirklich ein TLS-Proxy (Caddy/Traefik/Nginx) vor OpenCRM steht. Sonst sperrt
|
||||
# sich der Browser bei direktem http://ip:port-Zugriff selbst aus
|
||||
# (ERR_SSL_PROTOCOL_ERROR auf den Assets).
|
||||
HTTPS_ENABLED=false
|
||||
|
||||
# ============== ADMINER (DB-UI) ==============
|
||||
# Theme-Auswahl. Verfügbare Designs im offiziellen adminer:latest Image:
|
||||
# adminer-dark, brade, bueltge, dracula, esterka, flat, galkaev,
|
||||
# haeckel, hever, konya, lavender-light, lucas-sandery, mancave,
|
||||
# mvt, nette, ng9, nicu, pappu687, paranoiq, pepa-linha, pokorny,
|
||||
# price, rmsoft, rmsoft_blue, rmsoft_blue-dark, win98
|
||||
# Empfehlung: dracula (dark) oder adminer-dark – beide modern.
|
||||
ADMINER_DESIGN=dracula
|
||||
|
||||
# ============== SEED ==============
|
||||
# Bei leerer DB seedet der Container automatisch (legt admin@admin.com / admin
|
||||
# + Stammdaten an) – nichts zu konfigurieren.
|
||||
# Nur wenn man eine NICHT-leere DB nochmal forciert seeden will (z.B. nach
|
||||
# Reset / Stammdaten-Update), kurz auf 'true' setzen, neu starten, dann
|
||||
# wieder zurück.
|
||||
RUN_SEED=false
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
# Root-Gitignore: gemeinsame Patterns für Repo-Root + nested Verzeichnisse
|
||||
# (backend/, frontend/, docker/ haben zusätzlich eigene .gitignore-Files)
|
||||
|
||||
# Environment – echte Secrets blocken, .env.example weiter mittracken
|
||||
.env
|
||||
.env.local
|
||||
.env.*.local
|
||||
!.env.example
|
||||
|
||||
# OS
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
|
||||
# IDE
|
||||
.idea/
|
||||
.vscode/
|
||||
*.swp
|
||||
*.swo
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
|
||||
# Temp
|
||||
tmp/
|
||||
*.tmp
|
||||
*.bak
|
||||
|
||||
# Docker-Bind-Mounts: Inhalt nicht tracken, Verzeichnisstruktur via .gitkeep behalten
|
||||
data/db/*
|
||||
!data/db/.gitkeep
|
||||
data/uploads/*
|
||||
!data/uploads/.gitkeep
|
||||
data/factory-defaults/*
|
||||
!data/factory-defaults/.gitkeep
|
||||
data/backups/*
|
||||
!data/backups/.gitkeep
|
||||
|
||||
# Factory-Defaults-Drop-Box (Export-ZIPs zwischen dev/prod hin und her)
|
||||
factory-exports/*
|
||||
!factory-exports/.gitkeep
|
||||
@@ -41,38 +41,70 @@ Web-basiertes CRM-System für Kundenverwaltung mit Verträgen (Energie, Telekomm
|
||||
- **Backend**: Node.js, Express 4.x, TypeScript
|
||||
- **Datenbank**: MariaDB
|
||||
- **ORM**: Prisma
|
||||
- **Auth**: JWT mit Rollen-basierter Zugriffskontrolle
|
||||
- **Auth**: JWT-Access-Token (Memory, 15 min) + Refresh-Token im httpOnly-Cookie
|
||||
(7 Tage). Rollen-basierte Zugriffskontrolle. XSS klaut maximal einen
|
||||
15-min-Access-Token, der Refresh-Cookie ist JS-unzugänglich.
|
||||
|
||||
> **Hinweis zu Express 5:** Das Projekt verwendet bewusst Express 4.x (nicht 5.x). Express 5 ist seit Jahren in der Beta-Phase und noch nicht offiziell stable. Bei der Installation darauf achten, dass `@types/express` zur Express-Version passt:
|
||||
> - Express 4.x → `@types/express@^4.17.x`
|
||||
> - Express 5.x → `@types/express@^5.x` (erst bei offiziellem Release empfohlen)
|
||||
|
||||
## Quick-Start mit Docker (empfohlen)
|
||||
|
||||
Komplettes Setup mit MariaDB + OpenCRM + Adminer (DB-UI) in 3 Befehlen:
|
||||
|
||||
```bash
|
||||
git clone <repository-url>
|
||||
cd opencrm
|
||||
cp .env.example .env # Werte anpassen, Secrets rotieren!
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
Browser:
|
||||
- **CRM**: http://localhost:3010 (Login: `admin@admin.com` / `admin`)
|
||||
- **Datenbank-UI** (Adminer): http://localhost:8081 (Server: `db`, User: `root`, DB: `opencrm`)
|
||||
|
||||
Alle persistenten Daten liegen in `./data/`:
|
||||
|
||||
| Pfad | Inhalt |
|
||||
|------|--------|
|
||||
| `./data/db/` | MariaDB-Datafiles |
|
||||
| `./data/uploads/` | User-Uploads (PDFs, Bilder) |
|
||||
| `./data/factory-defaults/` | Stammdaten-Kataloge |
|
||||
| `./data/backups/` | DB-Backups (`npm run db:backup`) |
|
||||
|
||||
Ports + Pfade konfigurierst du in `./.env` (Default-Werte siehe `.env.example`).
|
||||
|
||||
> **Erste Inbetriebnahme:** In der `.env` einmalig `RUN_SEED=true` setzen,
|
||||
> `docker compose up -d` ausführen, dann wieder auf `false`. Danach existiert
|
||||
> der initiale Admin-User `admin@admin.com` / `admin`.
|
||||
|
||||
## Voraussetzungen
|
||||
|
||||
- Node.js 18+ (empfohlen: 20+)
|
||||
- Docker & Docker Compose
|
||||
- npm
|
||||
- Docker & Docker Compose v2
|
||||
- Für Backend-Entwicklung außerhalb von Docker: Node.js 20+ und npm
|
||||
|
||||
## Installation
|
||||
## Installation für Entwicklung (ohne Container)
|
||||
|
||||
### 1. Repository klonen
|
||||
|
||||
```bash
|
||||
git clone <repository-url>
|
||||
cd opencrm
|
||||
cp .env.example .env # Konfiguration anpassen
|
||||
```
|
||||
|
||||
### 2. MariaDB-Datenbank starten
|
||||
### 2. MariaDB-Container starten
|
||||
|
||||
```bash
|
||||
docker-compose up -d
|
||||
docker compose up -d db
|
||||
```
|
||||
|
||||
Dies startet einen MariaDB-Container mit:
|
||||
- **Port:** 3306
|
||||
- **Datenbank:** opencrm
|
||||
- **Root-Passwort:** rootpassword
|
||||
- **Benutzer:** opencrm / opencrm123
|
||||
Das startet nur die Datenbank (mit Daten in `./data/db/`).
|
||||
Konfiguration kommt aus `./.env`:
|
||||
|
||||
- **Port:** wie in `DB_PORT` (Standard: 3306, intern auf 127.0.0.1)
|
||||
- **Datenbank/User/Passwort:** wie in `DB_*`-Variablen
|
||||
|
||||
Warte ca. 10 Sekunden bis die Datenbank bereit ist.
|
||||
|
||||
@@ -94,9 +126,14 @@ Die `.env`-Datei sollte folgende Werte enthalten:
|
||||
# Database
|
||||
DATABASE_URL="mysql://root:rootpassword@localhost:3306/opencrm"
|
||||
|
||||
# JWT
|
||||
# JWT – Access-/Refresh-Token-Pattern (SPA-Standard)
|
||||
# Access-Token (Bearer-Header, nur im Browser-Memory, kurzlebig)
|
||||
# Refresh-Token (httpOnly-Cookie, lang)
|
||||
# Beide werden mit JWT_SECRET signiert; Refresh wird nur am
|
||||
# /api/auth/refresh-Endpoint akzeptiert (type-Claim).
|
||||
JWT_SECRET="change-this-to-a-very-long-random-secret-in-production"
|
||||
JWT_EXPIRES_IN="7d"
|
||||
JWT_EXPIRES_IN="15m" # Access-Token-Lifetime (Default: 15m)
|
||||
JWT_REFRESH_EXPIRES_IN="7d" # Refresh-Token-Lifetime (Default: 7d)
|
||||
|
||||
# Encryption (for portal credentials) - generate with: openssl rand -hex 32
|
||||
ENCRYPTION_KEY="0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
@@ -177,6 +214,13 @@ Plus:
|
||||
|
||||
- **Reverse-Proxy** (Nginx/Plesk) so konfigurieren, dass `X-Forwarded-For` hart auf
|
||||
die echte Client-IP gesetzt wird (nicht nur angefügt) – sonst Rate-Limit-Bypass möglich.
|
||||
- **Frontend + API müssen über dieselbe Origin laufen.** Die Auth nutzt einen
|
||||
httpOnly-Refresh-Cookie mit `SameSite=Strict; Path=/api/auth` – wenn Frontend
|
||||
und API auf getrennten Origins liegen (z.B. `crm.example.de` vs.
|
||||
`api.example.de`), schickt der Browser das Cookie cross-site nicht mit
|
||||
und der `/auth/refresh`-Endpoint kann den User nicht mehr nachladen
|
||||
(= alle 15 min Re-Login). Beim NPM-Setup landen Frontend und API automatisch
|
||||
auf derselben Domain via Proxy-Path.
|
||||
- **Default-Admin-Passwort ändern** (admin@admin.com / admin).
|
||||
- **Manuelle Test-Checkliste** aus [docs/TESTING.md](docs/TESTING.md) einmal komplett
|
||||
durchklicken.
|
||||
@@ -185,6 +229,138 @@ Plus:
|
||||
- Vollständige Hardening-Story + restliche Trade-offs:
|
||||
[docs/SECURITY-HARDENING.md](docs/SECURITY-HARDENING.md)
|
||||
|
||||
### ⚠️ Wichtig: gzip für `/api/*` am Reverse-Proxy deaktivieren (BREACH-Schutz)
|
||||
|
||||
Wenn ein TLS-Reverse-Proxy (Nginx Proxy Manager, Caddy, eigener Nginx, …) HTTPS
|
||||
terminiert und Antworten gzip-komprimiert, ist die **BREACH-Attacke** (CVE-2013-3587)
|
||||
theoretisch möglich: aus der gzip-komprimierten Response-Größe könnten unter
|
||||
ungünstigen Umständen Secrets erraten werden. Auch wenn unsere JWT-basierte SPA
|
||||
das Risiko praktisch klein hält (keine reflektierten Secrets im Response-Body),
|
||||
geht ein Penetration-Test mit testssl trotzdem auf „medium – Ausnutzbar: Ja".
|
||||
|
||||
**Lösung:** gzip-Komprimierung nur für statische Frontend-Assets erlauben, für
|
||||
`/api/*` deaktivieren. Statische Bundles bleiben damit performant ausgeliefert,
|
||||
JSON-API-Responses werden ohne Kompression gesendet → BREACH ist dort kein
|
||||
Einfallstor mehr.
|
||||
|
||||
**Nginx Proxy Manager (NPM):**
|
||||
1. Proxy-Hosts → den CRM-Host → **Edit**
|
||||
2. Tab **Custom Locations** → **„Add location"**
|
||||
3. **Define location:** `/api/`
|
||||
4. **Scheme:** `http`, **Forward Hostname/IP:** wie im Haupt-Host
|
||||
(z.B. `172.0.2.39`), **Forward Port:** `3010`
|
||||
5. Zahnrad rechts an der Location → erweiterte Config eintragen:
|
||||
```nginx
|
||||
gzip off;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
# Information-Disclosure-Header weg (Pentest-Hygiene):
|
||||
more_clear_headers Server X-Served-By;
|
||||
```
|
||||
6. **Save** (Location), **Save** (Proxy-Host)
|
||||
|
||||
> Der `more_clear_headers`-Befehl kommt aus dem `headers-more`-Modul, das
|
||||
> bei NPM standardmäßig dabei ist. Damit verschwinden die Banner
|
||||
> `Server: openresty` und `x-served-by: …` aus den Responses – Pentest-
|
||||
> Tools können den eingesetzten Webserver nicht mehr direkt aus dem Header
|
||||
> ablesen. Wer das auch auf der Hauptlocation will, kann denselben Eintrag
|
||||
> zusätzlich im **Advanced**-Tab des Proxy-Hosts setzen.
|
||||
|
||||
**Plain Nginx** (falls eigener Nginx statt NPM):
|
||||
```nginx
|
||||
location /api/ {
|
||||
gzip off;
|
||||
proxy_pass http://backend:3010;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
more_clear_headers Server X-Served-By; # braucht headers-more-Modul
|
||||
}
|
||||
# Optional global im server { … }-Block:
|
||||
server_tokens off;
|
||||
```
|
||||
|
||||
**Verifikation:**
|
||||
```bash
|
||||
# 1) gzip ist für /api/ deaktiviert (sollte leer sein)
|
||||
curl -sI -H 'Accept-Encoding: gzip' https://kundencenter.deine-domain.de/api/health \
|
||||
| grep -i content-encoding
|
||||
|
||||
# 2) Server-/x-served-by-Banner sind weg (sollte leer sein)
|
||||
curl -sI https://kundencenter.deine-domain.de/api/health \
|
||||
| grep -iE '^(server|x-served-by):'
|
||||
```
|
||||
|
||||
#### Was mit gzip auf `/` (SPA-HTML) ist
|
||||
|
||||
Pentest-Tools wie `testssl` melden BREACH **trotzdem weiter** für die
|
||||
Root-URL `/`, weil die SPA-`index.html` bewusst weiter gzip-komprimiert
|
||||
ausgeliefert wird (Performance: 50 KB → ~10 KB). Bei OpenCRM ist der
|
||||
Angriff dort nicht ausnutzbar:
|
||||
|
||||
- Die `/`-Response ist die statische `index.html` aus dem Vite-Build
|
||||
- Sie reflektiert **keinen user-controlled Input**
|
||||
- Sie enthält **keine Secrets** (JWT-Access ist im `Authorization`-Header,
|
||||
Refresh-Token im httpOnly-Cookie – beides nicht im HTML-Body)
|
||||
|
||||
Ohne Secret-im-Body und ohne Input-Reflektion hat BREACH keinen Hebel.
|
||||
|
||||
##### Wer den Audit-Marker trotzdem weg haben will
|
||||
|
||||
Wichtig: nicht einfach eine Custom-Location für `/` mit `gzip off`
|
||||
anlegen – das wäre ein **prefix-Match** und würde **alle** Pfade
|
||||
außer `/api/*` betreffen, also auch `/assets/*.{js,css}`. Das JS-Bundle
|
||||
käme dann unkomprimiert (~500 KB statt ~150 KB) → spürbarer
|
||||
Performance-Verlust für nichts.
|
||||
|
||||
Sauber ist eine **exact-Match-Location** (`location = /`) – die fängt
|
||||
nur die Root-URL ohne weitere Pfad-Komponente:
|
||||
|
||||
**Variante A** – Custom Location im NPM-UI (falls `= /` im
|
||||
„Define location"-Feld akzeptiert wird):
|
||||
|
||||
| Feld | Wert |
|
||||
|---|---|
|
||||
| Define location | `= /` |
|
||||
| Scheme | `http` |
|
||||
| Forward Hostname/IP | wie im Haupt-Host |
|
||||
| Forward Port | `3010` |
|
||||
|
||||
Im Zahnrad-Edit der Location:
|
||||
```nginx
|
||||
gzip off;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
# Information-Disclosure-Header weg (Pentest-Hygiene):
|
||||
more_clear_headers Server X-Served-By;
|
||||
```
|
||||
|
||||
**Variante B** – wenn das NPM-UI das `=` nicht akzeptiert, dieselbe
|
||||
Logik im **Advanced**-Tab des Proxy-Hosts:
|
||||
```nginx
|
||||
location = / {
|
||||
gzip off;
|
||||
proxy_pass $forward_scheme://$server:$port;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
more_clear_headers Server X-Served-By;
|
||||
}
|
||||
```
|
||||
|
||||
Verifikation – `/` ohne gzip, `/assets/*` aber weiter mit:
|
||||
```bash
|
||||
# Root: kein Content-Encoding mehr
|
||||
curl -sI -H 'Accept-Encoding: gzip' https://kundencenter.deine-domain.de/ \
|
||||
| grep -i content-encoding
|
||||
|
||||
# /assets/<file>.js: weiterhin gzip (Performance bleibt erhalten)
|
||||
JS=$(curl -s https://kundencenter.deine-domain.de/ | grep -oE 'assets/index-[A-Za-z0-9_-]+\.js' | head -1)
|
||||
curl -sI -H 'Accept-Encoding: gzip' "https://kundencenter.deine-domain.de/$JS" \
|
||||
| grep -i content-encoding
|
||||
```
|
||||
|
||||
Kostet 40 KB extra pro Tab-Reload – aber dafür ist auch der letzte
|
||||
BREACH-Marker weg und Pentest-Reports landen auf 0×MEDIUM.
|
||||
|
||||
## Developer-Tools aktivieren
|
||||
|
||||
Die Developer-Tools (Datenbankstruktur, ER-Diagramm) sind standardmäßig für Admins verfügbar. Falls der Menüpunkt nicht erscheint:
|
||||
@@ -1036,8 +1212,9 @@ Folgende Felder werden in Audit-Logs gefiltert:
|
||||
## Factory-Defaults: Stammdaten-Kataloge teilen
|
||||
|
||||
Das **Factory-Defaults**-System erlaubt den Export und Import von
|
||||
Stammdaten-Katalogen (Anbieter, Tarife, PDF-Vorlagen usw.) zwischen verschiedenen
|
||||
OpenCRM-Installationen. Es ist bewusst **streng abgegrenzt** zu Datenbank-Backups:
|
||||
Stammdaten-Katalogen (Anbieter, Tarife, PDF-Auftragsvorlagen, HTML-Standardtexte)
|
||||
zwischen verschiedenen OpenCRM-Installationen. Es ist bewusst **streng abgegrenzt**
|
||||
zu Datenbank-Backups:
|
||||
|
||||
### Abgrenzung
|
||||
|
||||
@@ -1045,64 +1222,117 @@ OpenCRM-Installationen. Es ist bewusst **streng abgegrenzt** zu Datenbank-Backup
|
||||
|---|---|---|
|
||||
| Anbieter, Tarife, Kündigungsfristen, Laufzeiten, Kategorien | ✅ | ✅ |
|
||||
| PDF-Auftragsvorlagen (inkl. Dateien + Feldzuordnungen) | ✅ | ✅ |
|
||||
| HTML-Standardtexte: Datenschutzerklärung, Impressum, Vollmacht-Vorlage, Website-Datenschutz | ✅ | ✅ |
|
||||
| **Kundendaten, Verträge, Dokumente** | ❌ | ✅ |
|
||||
| **Emails, SMTP-/IMAP-Zugangsdaten** | ❌ | ✅ |
|
||||
| **System-Einstellungen, Datenschutzerklärungen, Impressum** | ❌ | ✅ |
|
||||
| **Secrets, JWT, Encryption-Keys, User-Accounts** | ❌ | ✅ |
|
||||
| Zwischen verschiedenen Installationen teilbar | ✅ | ❌ (zu firmen-spezifisch) |
|
||||
|
||||
> **Kurz:** Factory-Defaults = reine Kataloge, Backup = alles.
|
||||
> **Kurz:** Factory-Defaults = generische Stammdaten + rechtliche Standardtexte,
|
||||
> Backup = die komplette Instanz.
|
||||
|
||||
### Export (Installation A → ZIP)
|
||||
### Drei Wege, eine ZIP zu transportieren
|
||||
|
||||
Es gibt drei Pfade, je nachdem wo die ZIP gerade liegen soll:
|
||||
|
||||
| Wo | Pfad | Wann |
|
||||
|---|---|---|
|
||||
| **Laufende DB einer Instanz** | UI-Upload oder `./factory-import.sh` | Bestehende Live-Instanz updaten |
|
||||
| **Drop-Box im Repo** (`factory-exports/`) | `./factory-export.sh` legt ab, `./factory-import.sh` liest | Transfer zwischen dev und prod via `scp` |
|
||||
| **Werkseinstellung im Image** (`backend/factory-defaults/`) | `./factory-import.sh --save-as-builtin` oder manuell entpacken | Neue VMs sollen die Defaults beim allerersten Start mitbringen |
|
||||
|
||||
Alle drei sind unabhängig, **alle drei zusammen** decken den typischen Workflow ab.
|
||||
|
||||
### Export
|
||||
|
||||
**Variante A – UI:**
|
||||
1. **Einstellungen** → **Factory-Defaults** öffnen
|
||||
2. Übersicht prüfen (Anzahl pro Kategorie)
|
||||
3. Button **„Factory-Defaults exportieren"** klicken
|
||||
4. ZIP wird als `factory-defaults-YYYY-MM-DD.zip` heruntergeladen
|
||||
2. Button **„Factory-Defaults exportieren"** klicken
|
||||
3. ZIP wird als `factory-defaults-YYYY-MM-DD.zip` heruntergeladen
|
||||
|
||||
**Variante B – CLI (für scp-Transfers):**
|
||||
```bash
|
||||
./factory-export.sh # → factory-exports/factory-defaults-…zip
|
||||
OPENCRM_URL=https://crm.prod.example.de \
|
||||
OPENCRM_EMAIL=admin@example.de ./factory-export.sh # gegen Prod-Instanz
|
||||
```
|
||||
Ohne `OPENCRM_PASSWORD` wird das Passwort interaktiv abgefragt. Der Zielordner
|
||||
`factory-exports/` ist gitignored – die ZIPs landen also nicht ins Repo.
|
||||
|
||||
**ZIP-Struktur:**
|
||||
```
|
||||
factory-defaults-2026-04-23.zip
|
||||
factory-defaults-2026-05-07-1949.zip
|
||||
├── manifest.json # Version + Datum + Counts
|
||||
├── providers/
|
||||
│ └── providers.json # Anbieter inkl. zugehöriger Tarife
|
||||
├── providers/providers.json
|
||||
├── contract-meta/
|
||||
│ ├── cancellation-periods.json # Kündigungsfristen (Code + Beschreibung)
|
||||
│ ├── contract-durations.json # Laufzeiten (Code + Beschreibung)
|
||||
│ └── contract-categories.json # Kategorien (Strom, Gas, DSL, ...)
|
||||
└── pdf-templates/
|
||||
├── pdf-templates.json # Vorlagen-Metadaten + Feldzuordnungen
|
||||
└── *.pdf # Die eigentlichen PDF-Dateien
|
||||
│ ├── cancellation-periods.json
|
||||
│ ├── contract-durations.json
|
||||
│ └── contract-categories.json
|
||||
├── pdf-templates/
|
||||
│ ├── pdf-templates.json
|
||||
│ └── *.pdf # Die eigentlichen PDF-Dateien
|
||||
└── app-settings/
|
||||
└── app-settings.json # HTML-Templates (Whitelist-only)
|
||||
```
|
||||
|
||||
Die ZIP kann an andere Installationen weitergegeben werden
|
||||
(Partner, Test-System, neue Installation).
|
||||
### Import
|
||||
|
||||
### Import (ZIP → Installation B)
|
||||
**Variante A – UI:**
|
||||
1. **Einstellungen** → **Factory-Defaults**
|
||||
2. Bereich **Import** → **„ZIP hochladen"** → Datei wählen
|
||||
3. Erfolgs-Box zeigt Counts pro Kategorie
|
||||
|
||||
1. ZIP herunterladen bzw. erhalten
|
||||
2. Inhalt nach `backend/factory-defaults/` entpacken (Unterordnerstruktur beibehalten)
|
||||
3. Im Backend-Verzeichnis ausführen:
|
||||
```bash
|
||||
npm run seed:defaults
|
||||
```
|
||||
**Variante B – CLI:**
|
||||
```bash
|
||||
./factory-import.sh # nimmt jüngste ZIP aus factory-exports/
|
||||
./factory-import.sh ./factory-exports/foo.zip # explizite ZIP
|
||||
./factory-import.sh --save-as-builtin # zusätzlich ins Image-Default
|
||||
./factory-import.sh --save-as-builtin ./foo.zip # entpacken (siehe unten)
|
||||
```
|
||||
|
||||
Konfigurierbar per ENV: `OPENCRM_URL`, `OPENCRM_EMAIL`, `OPENCRM_PASSWORD`.
|
||||
|
||||
**Variante C – Container-Bare-Metal (für Migration / mehrere ZIPs zusammenführen):**
|
||||
```bash
|
||||
# Inhalt der ZIP nach backend/factory-defaults/ entpacken (Unterordner beibehalten)
|
||||
cd backend && npm run seed:defaults
|
||||
```
|
||||
|
||||
**Beispiel-Output:**
|
||||
```
|
||||
📦 Factory-Defaults werden eingespielt...
|
||||
|
||||
✓ Anbieter: 7, Tarife: 12
|
||||
✓ Kündigungsfristen: 5
|
||||
✓ Laufzeiten: 4
|
||||
✓ Vertragskategorien: 8
|
||||
✓ PDF-Vorlagen: 3
|
||||
|
||||
✅ Factory-Defaults erfolgreich eingespielt.
|
||||
✓ Anbieter: 10
|
||||
✓ Tarife: 4
|
||||
✓ Kündigungsfristen: 18
|
||||
✓ Laufzeiten: 18
|
||||
✓ Vertragskategorien: 8
|
||||
✓ PDF-Vorlagen: 2
|
||||
✓ HTML-Templates: 2
|
||||
```
|
||||
|
||||
### Mehrere ZIPs kombinieren
|
||||
### `--save-as-builtin`: ZIP zur Werkseinstellung machen
|
||||
|
||||
Du kannst mehrere Exporte in `backend/factory-defaults/` übereinanderlegen –
|
||||
JSON-Dateien werden automatisch gemerged:
|
||||
Mit `--save-as-builtin` entpackt `factory-import.sh` die ZIP nach **erfolgreichem
|
||||
DB-Import** zusätzlich in `backend/factory-defaults/`. Beim nächsten
|
||||
`docker-compose up --build` landen die Defaults im Image. Frisch hochgezogene
|
||||
VMs bringen sie dann beim ersten Start automatisch mit (Auto-Seed-Pfad im
|
||||
Container-Entrypoint).
|
||||
|
||||
```bash
|
||||
# typischer Sync prod → dev → Image-Default
|
||||
ssh prod './factory-export.sh'
|
||||
scp prod:opencrm/factory-exports/factory-defaults-*.zip factory-exports/
|
||||
./factory-import.sh --save-as-builtin
|
||||
docker-compose up -d --build # neuer Build, neue VMs starten direkt mit Defaults
|
||||
```
|
||||
|
||||
Der Inhalt von `backend/factory-defaults/` wird beim `--save-as-builtin` vorher
|
||||
geleert (außer `README.md` und `.gitkeep`), damit nichts Veraltetes liegen
|
||||
bleibt.
|
||||
|
||||
### Mehrere ZIPs kombinieren (CLI-only, Variante C)
|
||||
|
||||
`backend/factory-defaults/` darf mehrere `*.json` pro Unterordner haben –
|
||||
`npm run seed:defaults` merged sie automatisch:
|
||||
|
||||
```
|
||||
backend/factory-defaults/
|
||||
@@ -1112,40 +1342,73 @@ backend/factory-defaults/
|
||||
eigene.json # 5 eigene Anbieter
|
||||
```
|
||||
|
||||
Das Import-Script liest **alle** `*.json` im jeweiligen Unterordner und merged per
|
||||
unique Key (letzter Eintrag gewinnt). Duplikate sind also unproblematisch.
|
||||
Bei gleichem Unique-Key gewinnt der zuletzt gelesene Eintrag. Der UI-/HTTP-Import
|
||||
nimmt nur eine ZIP entgegen – für Merges nutze `npm run seed:defaults`.
|
||||
|
||||
### Idempotenz
|
||||
|
||||
Das Script nutzt ausschließlich Prisma `upsert`:
|
||||
Alle Pfade nutzen Prisma `upsert`:
|
||||
- **Neue Einträge** werden angelegt
|
||||
- **Bestehende Einträge** (per unique Key: `name`, `code`) werden aktualisiert
|
||||
- **Bestehende Einträge** (per unique Key: `name` / `code` / `key`) werden aktualisiert
|
||||
- Nichts wird gelöscht
|
||||
|
||||
Du kannst `npm run seed:defaults` also beliebig oft ausführen, ohne Datenverlust
|
||||
Du kannst Imports also beliebig oft hintereinander ausführen, ohne Datenverlust
|
||||
oder Duplikate.
|
||||
|
||||
### PDF-Dateien beim Import
|
||||
### PDF-Dateien
|
||||
|
||||
Beim Import werden PDF-Vorlagen aus `factory-defaults/pdf-templates/*.pdf` nach
|
||||
`uploads/pdf-templates/` kopiert und die Pfade in der DB entsprechend gesetzt.
|
||||
Beim Re-Import wird die alte Datei in `uploads/` entsorgt und durch die neue
|
||||
ersetzt.
|
||||
Beim Import werden PDF-Vorlagen aus dem ZIP nach `uploads/pdf-templates/`
|
||||
kopiert (mit eindeutigem Suffix) und die `templatePath`-Spalte entsprechend
|
||||
gesetzt. Beim Re-Import wird die alte Datei in `uploads/` entsorgt und durch
|
||||
die neue ersetzt.
|
||||
|
||||
### AppSettings-Whitelist
|
||||
|
||||
Beim Import werden nur die Keys mit AppSetting-Schreibzugriff gewährt, die auch
|
||||
exportiert werden – aktuell:
|
||||
|
||||
- `privacyPolicyHtml`
|
||||
- `imprintHtml`
|
||||
- `authorizationTemplateHtml`
|
||||
- `websitePrivacyPolicyHtml`
|
||||
|
||||
Andere Keys (SMTP, JWT, etc.) werden mit einer Warnung ignoriert. Whitelist ist
|
||||
in [`backend/src/services/factoryDefaults.service.ts`](backend/src/services/factoryDefaults.service.ts)
|
||||
zentral gepflegt.
|
||||
|
||||
### Auto-Seed beim Erst-Deploy
|
||||
|
||||
Bei einer **frischen** Installation (leere DB) spielt der Container-Entrypoint
|
||||
nach dem Prisma-Seed automatisch das Built-in-Verzeichnis ein:
|
||||
|
||||
```
|
||||
[entrypoint] DB ist leer (User-Count=0) – Auto-Seed wird ausgeführt
|
||||
[entrypoint] Spiele eingebaute Factory-Defaults ein…
|
||||
✓ Anbieter: 10, Tarife: 4
|
||||
…
|
||||
```
|
||||
|
||||
Bei bestehenden Installs passiert das **nicht** – nur frische DBs.
|
||||
|
||||
### Berechtigungen
|
||||
|
||||
| Aktion | Berechtigung |
|
||||
|--------|--------------|
|
||||
| Factory-Defaults Vorschau | `settings:read` |
|
||||
| Factory-Defaults Export | `settings:update` |
|
||||
| Factory-Defaults Import (CLI) | Server-Zugang (SSH/Shell) |
|
||||
| Factory-Defaults Export (UI/CLI) | `settings:update` |
|
||||
| Factory-Defaults Import (UI/CLI) | `settings:update` |
|
||||
| Werkseinstellungen ändern (`--save-as-builtin` / `npm run seed:defaults`) | Server-Zugang (SSH/Shell) |
|
||||
|
||||
### Typischer Einsatzzweck
|
||||
### Typische Einsatzzwecke
|
||||
|
||||
- **Neue Installation aufsetzen**: Eine Kollegen-ZIP importieren und sofort mit
|
||||
gepflegtem Anbieter- und Vorlagenkatalog loslegen
|
||||
- **Neue VM aufsetzen**: ZIP einmalig nach `backend/factory-defaults/` entpacken
|
||||
(oder per `--save-as-builtin`), dann `docker-compose up --build` – die
|
||||
Werkseinstellungen sind beim ersten Start automatisch drin.
|
||||
- **Prod-Stand zurück nach dev synchronisieren**: `./factory-export.sh` auf prod,
|
||||
`scp` ins dev, `./factory-import.sh --save-as-builtin` lokal – damit ist
|
||||
sowohl die dev-DB aktuell als auch der nächste Image-Build.
|
||||
- **Vorlagen-Paket teilen**: Eine ZIP mit nur PDF-Vorlagen weitergeben
|
||||
(die anderen Ordner einfach aus der ZIP entfernen vor dem Entpacken)
|
||||
(andere Ordner aus der ZIP entfernen vor dem Entpacken).
|
||||
- **Anbieter-Paket teilen**: ZIP mit nur `providers/` weitergeben
|
||||
- **Versionskontrolle**: Die entpackten JSON-Dateien unter Versionskontrolle
|
||||
stellen (außerhalb von `backend/factory-defaults/`, da der Ordner gitignored ist)
|
||||
|
||||
@@ -1,3 +1,9 @@
|
||||
# Backend nutzt seit v1.1 die zentrale Root-.env im Projektverzeichnis.
|
||||
# → siehe ../.env.example für alle Variablen
|
||||
#
|
||||
# Diese Datei bleibt als Legacy-Fallback: wenn /.env nicht existiert,
|
||||
# liest das Backend backend/.env (z.B. für isolierte Backend-Tests).
|
||||
|
||||
# Database
|
||||
DATABASE_URL="mysql://user:password@localhost:3306/opencrm"
|
||||
|
||||
|
||||
+2
-1
@@ -4,10 +4,11 @@ node_modules/
|
||||
# Build
|
||||
dist/
|
||||
|
||||
# Environment
|
||||
# Environment – echte Secrets blocken, .env.example weiter mittracken
|
||||
.env
|
||||
.env.local
|
||||
.env.*.local
|
||||
!.env.example
|
||||
|
||||
# Database Backups (can be large, keep folder structure)
|
||||
prisma/backups/*
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
# Multi-Stage Build: Frontend bauen, dann Backend bauen, dann schlankes Runtime-Image
|
||||
# ---------------------------------------------------------------------------------
|
||||
|
||||
# Alle Stages auf node:20-slim (Debian-basiert) – dann passt die Prisma-Query-
|
||||
# Engine (glibc + openssl) zur Runtime.
|
||||
|
||||
# ============== STAGE 1: Frontend bauen ==============
|
||||
FROM node:20-slim AS frontend-builder
|
||||
WORKDIR /build/frontend
|
||||
COPY frontend/package.json frontend/package-lock.json ./
|
||||
RUN npm ci --no-audit --no-fund --prefer-offline
|
||||
COPY frontend/ ./
|
||||
RUN npm run build
|
||||
# Output: /build/frontend/dist/
|
||||
|
||||
# ============== STAGE 2: Backend bauen (TS → JS) ==============
|
||||
FROM node:20-slim AS backend-builder
|
||||
WORKDIR /build/backend
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends openssl \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
COPY backend/package.json backend/package-lock.json ./
|
||||
RUN npm ci --no-audit --no-fund --prefer-offline
|
||||
COPY backend/prisma ./prisma
|
||||
RUN npx prisma generate
|
||||
COPY backend/tsconfig.json ./
|
||||
COPY backend/src ./src
|
||||
RUN npx tsc
|
||||
# Output: /build/backend/dist/
|
||||
|
||||
# ============== STAGE 3: Runtime ==============
|
||||
# Debian-slim statt Alpine: Prisma-Engines erwarten libssl 1.1, das in Alpine 3.19+
|
||||
# nicht mehr verfügbar ist. Slim hat openssl 3 ABI-kompatibel + native binaries.
|
||||
FROM node:20-slim
|
||||
WORKDIR /app
|
||||
|
||||
# OpenSSL für Prisma-Query-Engine + wget für Healthcheck
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends openssl wget \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Nur Production-Dependencies + Prisma-Client
|
||||
COPY backend/package.json backend/package-lock.json ./
|
||||
RUN npm ci --omit=dev --no-audit --no-fund --prefer-offline && npm cache clean --force
|
||||
|
||||
# Build-Artefakte aus Stage 2
|
||||
COPY --from=backend-builder /build/backend/dist ./dist
|
||||
COPY --from=backend-builder /build/backend/node_modules/.prisma ./node_modules/.prisma
|
||||
COPY --from=backend-builder /build/backend/node_modules/@prisma ./node_modules/@prisma
|
||||
COPY backend/prisma ./prisma
|
||||
|
||||
# Frontend-Build ins public/-Verzeichnis (wird in production-Mode statisch ausgeliefert)
|
||||
COPY --from=frontend-builder /build/frontend/dist ./public
|
||||
|
||||
# Eingebaute Werkseinstellungen ins Image: bei Erstinstallation (leerer DB) zieht
|
||||
# der Entrypoint sie via tsx scripts/seed-factory-defaults.ts ein. Liegt in einem
|
||||
# eigenen Pfad – `factory-defaults/` selbst kann über Bind-Mount überlagert werden.
|
||||
COPY backend/factory-defaults /app/factory-defaults-builtin
|
||||
COPY backend/scripts /app/scripts
|
||||
|
||||
# Daten-Verzeichnisse (werden via Bind-Mount überlagert; hier nur als Fallback)
|
||||
RUN mkdir -p uploads factory-defaults prisma/backups
|
||||
|
||||
# Healthcheck
|
||||
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
|
||||
CMD wget --quiet --tries=1 --spider "http://localhost:${PORT:-3001}/api/health" || exit 1
|
||||
|
||||
# Beim Start: prisma db push (idempotent), dann node
|
||||
COPY backend/docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
|
||||
RUN chmod +x /usr/local/bin/docker-entrypoint.sh
|
||||
|
||||
ENTRYPOINT ["docker-entrypoint.sh"]
|
||||
CMD ["node", "dist/index.js"]
|
||||
Executable
+126
@@ -0,0 +1,126 @@
|
||||
#!/bin/sh
|
||||
# Container-Start:
|
||||
# 1) Auf DB warten
|
||||
# 2) Auto-Baseline für bestehende DBs (db-push-Ära ohne _prisma_migrations)
|
||||
# 3) `prisma migrate deploy` (idempotent, datenerhaltend)
|
||||
# 4) Auto-Seed bei leerer User-Tabelle (oder RUN_SEED=true)
|
||||
# Neue Schema-Änderung anlegen (lokal, im Dev): npm run schema:sync
|
||||
set -e
|
||||
|
||||
# DATABASE_URL aus DB_*-Komponenten bauen, falls nicht explizit gesetzt.
|
||||
# Wichtig: encodeURIComponent für DB_USER + DB_PASSWORD, damit Sonderzeichen
|
||||
# wie $, !, #, @, :, / etc. nicht die URL-Authority-Syntax brechen.
|
||||
# Wir nutzen node-eval (ist eh installiert), kein extra-Tool wie jq nötig.
|
||||
if [ -z "$DATABASE_URL" ] && [ -n "$DB_USER" ] && [ -n "$DB_PASSWORD" ] && [ -n "$DB_NAME" ]; then
|
||||
DATABASE_URL=$(node -e "
|
||||
const u = encodeURIComponent(process.env.DB_USER);
|
||||
const p = encodeURIComponent(process.env.DB_PASSWORD);
|
||||
const h = process.env.DB_HOST || 'db';
|
||||
const port = process.env.DB_PORT || '3306';
|
||||
const n = process.env.DB_NAME;
|
||||
process.stdout.write(\`mysql://\${u}:\${p}@\${h}:\${port}/\${n}\`);
|
||||
")
|
||||
export DATABASE_URL
|
||||
echo "[entrypoint] DATABASE_URL aus DB_*-Komponenten gebaut (host=${DB_HOST:-db})"
|
||||
fi
|
||||
|
||||
echo "[entrypoint] Warte auf Datenbank…"
|
||||
# Erst auf DB-Verfügbarkeit warten via einfachem Connect-Check.
|
||||
# Wir nutzen Prisma's interne Engine, kein extra mysql-client nötig.
|
||||
TRIES=30
|
||||
until node -e "
|
||||
const { PrismaClient } = require('@prisma/client');
|
||||
const p = new PrismaClient();
|
||||
p.\$queryRaw\`SELECT 1\`
|
||||
.then(() => p.\$disconnect().then(() => process.exit(0)))
|
||||
.catch(() => process.exit(1));
|
||||
" 2>/dev/null; do
|
||||
TRIES=$((TRIES - 1))
|
||||
if [ "$TRIES" -le 0 ]; then
|
||||
echo "[entrypoint] DB nicht erreichbar – Abbruch"
|
||||
exit 1
|
||||
fi
|
||||
echo "[entrypoint] DB noch nicht bereit – retry in 2s ($TRIES Versuche übrig)"
|
||||
sleep 2
|
||||
done
|
||||
echo "[entrypoint] DB erreichbar"
|
||||
|
||||
# Auto-Baseline: Wenn die DB Anwendungs-Tabellen enthält (z.B. User), aber noch
|
||||
# keine _prisma_migrations-Tabelle, dann ist es eine "alte" DB, die früher mit
|
||||
# `prisma db push` synced wurde. Wir markieren 0_init als bereits angewendet,
|
||||
# damit `migrate deploy` nicht versucht, alle Tabellen nochmal anzulegen.
|
||||
NEEDS_BASELINE=$(node -e "
|
||||
const { PrismaClient } = require('@prisma/client');
|
||||
const p = new PrismaClient();
|
||||
(async () => {
|
||||
try {
|
||||
const dbName = process.env.DB_NAME;
|
||||
const tables = await p.\$queryRawUnsafe(
|
||||
\`SELECT TABLE_NAME FROM information_schema.TABLES WHERE TABLE_SCHEMA = ?\`,
|
||||
dbName
|
||||
);
|
||||
const names = tables.map(t => t.TABLE_NAME);
|
||||
const hasMigrations = names.includes('_prisma_migrations');
|
||||
const hasUserTable = names.includes('User');
|
||||
// Existing DB (User da) ohne Migrations-Tracking => Baseline nötig
|
||||
if (hasUserTable && !hasMigrations) process.stdout.write('yes');
|
||||
else process.stdout.write('no');
|
||||
} catch (e) {
|
||||
process.stdout.write('no');
|
||||
} finally {
|
||||
await p.\$disconnect();
|
||||
}
|
||||
})();
|
||||
" 2>/dev/null)
|
||||
|
||||
if [ "$NEEDS_BASELINE" = "yes" ]; then
|
||||
echo "[entrypoint] Bestehende DB ohne Migrations-Tracking erkannt – markiere 0_init als angewendet (Baseline)"
|
||||
npx prisma migrate resolve --applied 0_init || echo "[entrypoint] Baseline fehlgeschlagen – fahre trotzdem fort"
|
||||
fi
|
||||
|
||||
# Migrations anwenden (idempotent: bereits angewendete werden übersprungen).
|
||||
# Im Gegensatz zu `db push` löscht `migrate deploy` keine Daten — Schema-
|
||||
# Änderungen werden über versionierte Migrations-Files unter prisma/migrations/
|
||||
# eingespielt. Neue Migration anlegen mit: npm run schema:sync (lokal, dev).
|
||||
echo "[entrypoint] Wende Migrations an…"
|
||||
if ! npx prisma migrate deploy; then
|
||||
echo "[entrypoint] migrate deploy fehlgeschlagen – Abbruch"
|
||||
exit 1
|
||||
fi
|
||||
echo "[entrypoint] DB-Schema aktuell"
|
||||
|
||||
# Auto-Seed: wenn die User-Tabelle leer ist (= Erstinstallation), automatisch seeden.
|
||||
# RUN_SEED=true erzwingt Seed auch bei nicht-leerer DB (z.B. nach Reset).
|
||||
USER_COUNT=$(node -e "
|
||||
const { PrismaClient } = require('@prisma/client');
|
||||
const p = new PrismaClient();
|
||||
p.user.count()
|
||||
.then((n) => { process.stdout.write(String(n)); process.exit(0); })
|
||||
.catch(() => { process.stdout.write('-1'); process.exit(0); });
|
||||
" 2>/dev/null)
|
||||
|
||||
RAN_SEED=false
|
||||
if [ "${RUN_SEED:-false}" = "true" ]; then
|
||||
echo "[entrypoint] RUN_SEED=true – seede DB (Force)"
|
||||
if npx prisma db seed; then RAN_SEED=true; else echo "[entrypoint] Seed fehlgeschlagen oder schon gelaufen – ignoriert"; fi
|
||||
elif [ "$USER_COUNT" = "0" ]; then
|
||||
echo "[entrypoint] DB ist leer (User-Count=0) – Auto-Seed wird ausgeführt"
|
||||
if npx prisma db seed; then RAN_SEED=true; else echo "[entrypoint] Auto-Seed fehlgeschlagen – ignoriert"; fi
|
||||
else
|
||||
echo "[entrypoint] DB enthält $USER_COUNT User – kein Seed nötig"
|
||||
fi
|
||||
|
||||
# Eingebaute Factory-Defaults nach Erstinstallation einspielen.
|
||||
# Das ist die Werkseinstellung für neue VMs: PDF-Vorlagen, Anbieter, Tarife,
|
||||
# HTML-Templates – alles aus /app/factory-defaults-builtin/. Erfolgt nur wenn
|
||||
# der Auto-Seed gerade lief (= frische DB), sonst werden Updates auf
|
||||
# bestehenden Installationen nicht ungewollt überschrieben.
|
||||
if [ "$RAN_SEED" = "true" ] && [ -d /app/factory-defaults-builtin ] \
|
||||
&& [ -n "$(ls -A /app/factory-defaults-builtin 2>/dev/null | grep -v -E '^(README\.md|\.gitkeep)$')" ]; then
|
||||
echo "[entrypoint] Spiele eingebaute Factory-Defaults ein…"
|
||||
FACTORY_DEFAULTS_DIR=/app/factory-defaults-builtin npx tsx scripts/seed-factory-defaults.ts \
|
||||
|| echo "[entrypoint] Factory-Defaults-Seed fehlgeschlagen – ignoriert"
|
||||
fi
|
||||
|
||||
echo "[entrypoint] Starte Backend…"
|
||||
exec "$@"
|
||||
@@ -18,15 +18,21 @@ backend/factory-defaults/
|
||||
│ ├── cancellation-periods.json # Kündigungsfristen
|
||||
│ ├── contract-durations.json # Vertragslaufzeiten
|
||||
│ └── contract-categories.json # Vertragskategorien (Strom/Gas/DSL/...)
|
||||
└── pdf-templates/
|
||||
├── pdf-templates.json # Metadaten + Feldzuordnungen
|
||||
└── *.pdf # PDF-Vorlagen-Dateien
|
||||
├── pdf-templates/
|
||||
│ ├── pdf-templates.json # Metadaten + Feldzuordnungen
|
||||
│ └── *.pdf # PDF-Vorlagen-Dateien
|
||||
└── app-settings/
|
||||
└── app-settings.json # HTML-Templates: Datenschutz / Impressum /
|
||||
# Vollmacht / Website-Datenschutz
|
||||
```
|
||||
|
||||
**Was NICHT enthalten ist:** Kundendaten, Verträge, Dokumente, E-Mails, SMTP-Einstellungen,
|
||||
Datenschutzerklärungen oder andere AppSettings. Dafür gibt es den separaten
|
||||
Secrets oder benutzerspezifische AppSettings. Dafür gibt es den separaten
|
||||
**Datenbank-Backup-Export** (Einstellungen → Datenbank & Zurücksetzen).
|
||||
|
||||
Bei den AppSettings ist nur eine **Whitelist** vorgesehen (HTML-Texte für rechtliche
|
||||
Standardpflichten) – andere Keys werden beim Import ignoriert.
|
||||
|
||||
---
|
||||
|
||||
## Export (aus einer bestehenden Installation)
|
||||
@@ -46,7 +52,8 @@ factory-defaults-2026-04-23.zip
|
||||
├── contract-meta/contract-durations.json
|
||||
├── contract-meta/contract-categories.json
|
||||
├── pdf-templates/pdf-templates.json
|
||||
└── pdf-templates/*.pdf
|
||||
├── pdf-templates/*.pdf
|
||||
└── app-settings/app-settings.json
|
||||
```
|
||||
|
||||
Die ZIP kann an andere Installationen weitergegeben werden – z.B. für Test-Systeme,
|
||||
@@ -56,7 +63,15 @@ neue Installationen oder Partner-Setups.
|
||||
|
||||
## Import (in eine andere Installation)
|
||||
|
||||
### Schritt-für-Schritt
|
||||
### Variante A: Über die UI (empfohlen)
|
||||
|
||||
1. Im Ziel-CRM als Admin einloggen
|
||||
2. **Einstellungen → Factory-Defaults**
|
||||
3. Im Bereich **Import** auf **„ZIP hochladen"** klicken
|
||||
4. Die exportierte ZIP wählen – der Import läuft direkt
|
||||
5. Erfolgsmeldung zeigt Counts pro Kategorie an
|
||||
|
||||
### Variante B: Über die CLI (für Bare-Metal / Migration / mehrere ZIPs zusammenführen)
|
||||
|
||||
1. **ZIP herunterladen** (aus einer Export-Installation oder von einer Vorlage)
|
||||
2. **Inhalt entpacken** in diesen Ordner (`backend/factory-defaults/`),
|
||||
@@ -234,6 +249,24 @@ Array von Providern, jeweils inkl. zugehöriger Tarife:
|
||||
**Unique Key:** `name`
|
||||
**Wichtig:** Die `pdfFilename` muss zu einer PDF-Datei im selben Ordner passen.
|
||||
|
||||
### `app-settings/app-settings.json`
|
||||
|
||||
HTML-Standardtexte als Werkseinstellung. Es ist eine **Whitelist** aktiv – andere Keys
|
||||
werden beim Import ignoriert (Schutz vor versehentlichem Überschreiben von Secrets).
|
||||
|
||||
```json
|
||||
[
|
||||
{ "key": "privacyPolicyHtml", "value": "<h1>Datenschutzerklärung</h1>..." },
|
||||
{ "key": "imprintHtml", "value": "<h1>Impressum</h1>..." },
|
||||
{ "key": "authorizationTemplateHtml","value": "<h1>Vollmacht</h1>..." },
|
||||
{ "key": "websitePrivacyPolicyHtml", "value": "<h1>Website-Datenschutz</h1>..." }
|
||||
]
|
||||
```
|
||||
|
||||
**Unique Key:** `key`
|
||||
**Erlaubte Keys:** `privacyPolicyHtml`, `imprintHtml`, `authorizationTemplateHtml`,
|
||||
`websitePrivacyPolicyHtml`.
|
||||
|
||||
---
|
||||
|
||||
## Berechtigungen
|
||||
@@ -242,6 +275,7 @@ Array von Providern, jeweils inkl. zugehöriger Tarife:
|
||||
|--------|--------------|
|
||||
| Factory-Defaults Vorschau | `settings:read` |
|
||||
| Factory-Defaults Export (UI) | `settings:update` |
|
||||
| Factory-Defaults Import (UI) | `settings:update` |
|
||||
| Factory-Defaults Import (CLI) | Server-Zugang (SSH/Shell) |
|
||||
|
||||
---
|
||||
|
||||
Generated
+66
-52
@@ -1,19 +1,22 @@
|
||||
{
|
||||
"name": "opencrm-backend",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "opencrm-backend",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"dependencies": {
|
||||
"@prisma/client": "^5.22.0",
|
||||
"@types/cookie-parser": "^1.4.10",
|
||||
"adm-zip": "^0.5.16",
|
||||
"archiver": "^7.0.1",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"cookie-parser": "^1.4.7",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.5",
|
||||
"dotenv-expand": "^13.0.0",
|
||||
"express": "^4.21.1",
|
||||
"express-rate-limit": "^8.4.0",
|
||||
"express-validator": "^7.2.0",
|
||||
@@ -26,6 +29,7 @@
|
||||
"nodemailer": "^7.0.13",
|
||||
"pdf-lib": "^1.17.1",
|
||||
"pdfkit": "^0.17.2",
|
||||
"tsx": "^4.19.2",
|
||||
"undici": "^6.23.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -42,7 +46,6 @@
|
||||
"@types/nodemailer": "^7.0.9",
|
||||
"@types/pdfkit": "^0.17.4",
|
||||
"prisma": "^5.22.0",
|
||||
"tsx": "^4.19.2",
|
||||
"typescript": "^5.6.3"
|
||||
}
|
||||
},
|
||||
@@ -53,7 +56,6 @@
|
||||
"cpu": [
|
||||
"ppc64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"aix"
|
||||
@@ -69,7 +71,6 @@
|
||||
"cpu": [
|
||||
"arm"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"android"
|
||||
@@ -85,7 +86,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"android"
|
||||
@@ -101,7 +101,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"android"
|
||||
@@ -117,7 +116,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
@@ -133,7 +131,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"darwin"
|
||||
@@ -149,7 +146,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"freebsd"
|
||||
@@ -165,7 +161,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"freebsd"
|
||||
@@ -181,7 +176,6 @@
|
||||
"cpu": [
|
||||
"arm"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
@@ -197,7 +191,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
@@ -213,7 +206,6 @@
|
||||
"cpu": [
|
||||
"ia32"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
@@ -229,7 +221,6 @@
|
||||
"cpu": [
|
||||
"loong64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
@@ -245,7 +236,6 @@
|
||||
"cpu": [
|
||||
"mips64el"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
@@ -261,7 +251,6 @@
|
||||
"cpu": [
|
||||
"ppc64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
@@ -277,7 +266,6 @@
|
||||
"cpu": [
|
||||
"riscv64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
@@ -293,7 +281,6 @@
|
||||
"cpu": [
|
||||
"s390x"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
@@ -309,7 +296,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"linux"
|
||||
@@ -325,7 +311,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"netbsd"
|
||||
@@ -341,7 +326,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"netbsd"
|
||||
@@ -357,7 +341,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"openbsd"
|
||||
@@ -373,7 +356,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"openbsd"
|
||||
@@ -389,7 +371,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"openharmony"
|
||||
@@ -405,7 +386,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"sunos"
|
||||
@@ -421,7 +401,6 @@
|
||||
"cpu": [
|
||||
"arm64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"win32"
|
||||
@@ -437,7 +416,6 @@
|
||||
"cpu": [
|
||||
"ia32"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"win32"
|
||||
@@ -453,7 +431,6 @@
|
||||
"cpu": [
|
||||
"x64"
|
||||
],
|
||||
"dev": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
"win32"
|
||||
@@ -633,7 +610,6 @@
|
||||
"version": "1.19.6",
|
||||
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.6.tgz",
|
||||
"integrity": "sha512-HLFeCYgz89uk22N5Qg3dvGvsv46B8GLvKKo1zKG4NybA8U2DiEO3w9lqGg29t/tfLRJpJ6iQxnVw4OnB7MoM9g==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/connect": "*",
|
||||
"@types/node": "*"
|
||||
@@ -643,11 +619,19 @@
|
||||
"version": "3.4.38",
|
||||
"resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.38.tgz",
|
||||
"integrity": "sha512-K6uROf1LD88uDQqJCktA4yzL1YYAK6NgfsI0v/mTgyPKWsX1CnJ0XPSDhViejru1GcRkLWb8RlzFYJRqGUbaug==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/cookie-parser": {
|
||||
"version": "1.4.10",
|
||||
"resolved": "https://registry.npmjs.org/@types/cookie-parser/-/cookie-parser-1.4.10.tgz",
|
||||
"integrity": "sha512-B4xqkqfZ8Wek+rCOeRxsjMS9OgvzebEzzLYw7NHYuvzb7IdxOkI0ZHGgeEBX4PUM7QGVvNSK60T3OvWj3YfBRg==",
|
||||
"license": "MIT",
|
||||
"peerDependencies": {
|
||||
"@types/express": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/cors": {
|
||||
"version": "2.8.19",
|
||||
"resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.19.tgz",
|
||||
@@ -661,7 +645,6 @@
|
||||
"version": "4.17.25",
|
||||
"resolved": "https://registry.npmjs.org/@types/express/-/express-4.17.25.tgz",
|
||||
"integrity": "sha512-dVd04UKsfpINUnK0yBoYHDF3xu7xVH4BuDotC/xGuycx4CgbP48X/KF/586bcObxT0HENHXEU8Nqtu6NR+eKhw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/body-parser": "*",
|
||||
"@types/express-serve-static-core": "^4.17.33",
|
||||
@@ -673,7 +656,6 @@
|
||||
"version": "4.19.8",
|
||||
"resolved": "https://registry.npmjs.org/@types/express-serve-static-core/-/express-serve-static-core-4.19.8.tgz",
|
||||
"integrity": "sha512-02S5fmqeoKzVZCHPZid4b8JH2eM5HzQLZWN2FohQEy/0eXTq8VXZfSN6Pcr3F6N9R/vNrj7cpgbhjie6m/1tCA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/node": "*",
|
||||
"@types/qs": "*",
|
||||
@@ -684,8 +666,7 @@
|
||||
"node_modules/@types/http-errors": {
|
||||
"version": "2.0.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/http-errors/-/http-errors-2.0.5.tgz",
|
||||
"integrity": "sha512-r8Tayk8HJnX0FztbZN7oVqGccWgw98T/0neJphO91KkmOzug1KkofZURD4UaD5uH8AqcFLfdPErnBod0u71/qg==",
|
||||
"dev": true
|
||||
"integrity": "sha512-r8Tayk8HJnX0FztbZN7oVqGccWgw98T/0neJphO91KkmOzug1KkofZURD4UaD5uH8AqcFLfdPErnBod0u71/qg=="
|
||||
},
|
||||
"node_modules/@types/jsonwebtoken": {
|
||||
"version": "9.0.10",
|
||||
@@ -722,8 +703,7 @@
|
||||
"node_modules/@types/mime": {
|
||||
"version": "1.3.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.5.tgz",
|
||||
"integrity": "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w==",
|
||||
"dev": true
|
||||
"integrity": "sha512-/pyBZWSLD2n0dcHE3hq8s8ZvcETHtEuF+3E7XVt0Ig2nvsVQXdghHVcEkIWjy9A0wKfTn97a/PSDYohKIlnP/w=="
|
||||
},
|
||||
"node_modules/@types/ms": {
|
||||
"version": "2.1.0",
|
||||
@@ -744,7 +724,6 @@
|
||||
"version": "22.19.7",
|
||||
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.19.7.tgz",
|
||||
"integrity": "sha512-MciR4AKGHWl7xwxkBa6xUGxQJ4VBOmPTF7sL+iGzuahOFaO0jHCsuEfS80pan1ef4gWId1oWOweIhrDEYLuaOw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"undici-types": "~6.21.0"
|
||||
}
|
||||
@@ -777,14 +756,12 @@
|
||||
"node_modules/@types/qs": {
|
||||
"version": "6.14.0",
|
||||
"resolved": "https://registry.npmjs.org/@types/qs/-/qs-6.14.0.tgz",
|
||||
"integrity": "sha512-eOunJqu0K1923aExK6y8p6fsihYEn/BYuQ4g0CxAAgFc4b/ZLN4CrsRZ55srTdqoiLzU2B2evC+apEIxprEzkQ==",
|
||||
"dev": true
|
||||
"integrity": "sha512-eOunJqu0K1923aExK6y8p6fsihYEn/BYuQ4g0CxAAgFc4b/ZLN4CrsRZ55srTdqoiLzU2B2evC+apEIxprEzkQ=="
|
||||
},
|
||||
"node_modules/@types/range-parser": {
|
||||
"version": "1.2.7",
|
||||
"resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.7.tgz",
|
||||
"integrity": "sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ==",
|
||||
"dev": true
|
||||
"integrity": "sha512-hKormJbkJqzQGhziax5PItDUTMAM9uE2XXQmM37dyd4hVM+5aVl7oVxMVUiVQn2oCQFN/LKCZdvSM0pFRqbSmQ=="
|
||||
},
|
||||
"node_modules/@types/readdir-glob": {
|
||||
"version": "1.1.5",
|
||||
@@ -799,7 +776,6 @@
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/@types/send/-/send-1.2.1.tgz",
|
||||
"integrity": "sha512-arsCikDvlU99zl1g69TcAB3mzZPpxgw0UQnaHeC1Nwb015xp8bknZv5rIfri9xTOcMuaVgvabfIRA7PSZVuZIQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
@@ -808,7 +784,6 @@
|
||||
"version": "1.15.10",
|
||||
"resolved": "https://registry.npmjs.org/@types/serve-static/-/serve-static-1.15.10.tgz",
|
||||
"integrity": "sha512-tRs1dB+g8Itk72rlSI2ZrW6vZg0YrLI81iQSTkMmOqnqCaNr/8Ek4VwWcN5vZgCYWbg/JJSGBlUaYGAOP73qBw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/http-errors": "*",
|
||||
"@types/node": "*",
|
||||
@@ -819,7 +794,6 @@
|
||||
"version": "0.17.6",
|
||||
"resolved": "https://registry.npmjs.org/@types/send/-/send-0.17.6.tgz",
|
||||
"integrity": "sha512-Uqt8rPBE8SY0RK8JB1EzVOIZ32uqy8HwdxCnoCOsYrvnswqmFZ/k+9Ikidlk/ImhsdvBsloHbAlewb2IEBV/Og==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@types/mime": "^1",
|
||||
"@types/node": "*"
|
||||
@@ -1277,6 +1251,25 @@
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/cookie-parser": {
|
||||
"version": "1.4.7",
|
||||
"resolved": "https://registry.npmjs.org/cookie-parser/-/cookie-parser-1.4.7.tgz",
|
||||
"integrity": "sha512-nGUvgXnotP3BsjiLX2ypbQnWoGUPIIfHQNZkkC668ntrzGWEZVW70HDEB1qnNGMicPje6EttlIgzo51YSwNQGw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"cookie": "0.7.2",
|
||||
"cookie-signature": "1.0.6"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.8.0"
|
||||
}
|
||||
},
|
||||
"node_modules/cookie-parser/node_modules/cookie-signature": {
|
||||
"version": "1.0.6",
|
||||
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz",
|
||||
"integrity": "sha512-QADzlaHc8icV8I7vbaJXJwod9HWYp8uCqf1xa4OfNu1T7JVxQIrUgOWtHdNDtPiywmFbiS12VjotIXLrKM3orQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/cookie-signature": {
|
||||
"version": "1.0.7",
|
||||
"resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.7.tgz",
|
||||
@@ -1463,6 +1456,33 @@
|
||||
"url": "https://dotenvx.com"
|
||||
}
|
||||
},
|
||||
"node_modules/dotenv-expand": {
|
||||
"version": "13.0.0",
|
||||
"resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-13.0.0.tgz",
|
||||
"integrity": "sha512-aBfBS8eYIeXmpHI9ThIlA7/WLq+SLt18iXUZhb52rW89QLKQFoIpPG1bPeewoPZsTyjSSO3T7234FBVUM1V2rA==",
|
||||
"license": "BSD-2-Clause",
|
||||
"dependencies": {
|
||||
"dotenv": "^17.4.2"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://dotenvx.com"
|
||||
}
|
||||
},
|
||||
"node_modules/dotenv-expand/node_modules/dotenv": {
|
||||
"version": "17.4.2",
|
||||
"resolved": "https://registry.npmjs.org/dotenv/-/dotenv-17.4.2.tgz",
|
||||
"integrity": "sha512-nI4U3TottKAcAD9LLud4Cb7b2QztQMUEfHbvhTH09bqXTxnSie8WnjPALV/WMCrJZ6UV/qHJ6L03OqO3LcdYZw==",
|
||||
"license": "BSD-2-Clause",
|
||||
"engines": {
|
||||
"node": ">=12"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://dotenvx.com"
|
||||
}
|
||||
},
|
||||
"node_modules/dunder-proto": {
|
||||
"version": "1.0.1",
|
||||
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
|
||||
@@ -1557,7 +1577,6 @@
|
||||
"version": "0.27.2",
|
||||
"resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.2.tgz",
|
||||
"integrity": "sha512-HyNQImnsOC7X9PMNaCIeAm4ISCQXs5a5YasTXVliKv4uuBo1dKrG0A+uQS8M5eXjVMnLg3WgXaKvprHlFJQffw==",
|
||||
"dev": true,
|
||||
"hasInstallScript": true,
|
||||
"bin": {
|
||||
"esbuild": "bin/esbuild"
|
||||
@@ -1784,7 +1803,6 @@
|
||||
"version": "2.3.3",
|
||||
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
|
||||
"integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==",
|
||||
"dev": true,
|
||||
"hasInstallScript": true,
|
||||
"optional": true,
|
||||
"os": [
|
||||
@@ -1841,7 +1859,6 @@
|
||||
"version": "4.13.0",
|
||||
"resolved": "https://registry.npmjs.org/get-tsconfig/-/get-tsconfig-4.13.0.tgz",
|
||||
"integrity": "sha512-1VKTZJCwBrvbd+Wn3AOgQP/2Av+TfTCOlE4AcRJE72W1ksZXbAx8PPBR9RzgTeSPzlPMHrbANMH3LbltH73wxQ==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"resolve-pkg-maps": "^1.0.0"
|
||||
},
|
||||
@@ -2867,7 +2884,6 @@
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz",
|
||||
"integrity": "sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw==",
|
||||
"dev": true,
|
||||
"funding": {
|
||||
"url": "https://github.com/privatenumber/resolve-pkg-maps?sponsor=1"
|
||||
}
|
||||
@@ -3315,7 +3331,6 @@
|
||||
"version": "4.21.0",
|
||||
"resolved": "https://registry.npmjs.org/tsx/-/tsx-4.21.0.tgz",
|
||||
"integrity": "sha512-5C1sg4USs1lfG0GFb2RLXsdpXqBSEhAaA/0kPL01wxzpMqLILNxIxIOKiILz+cdg/pLnOUxFYOR5yhHU666wbw==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"esbuild": "~0.27.0",
|
||||
"get-tsconfig": "^4.7.5"
|
||||
@@ -3377,8 +3392,7 @@
|
||||
"node_modules/undici-types": {
|
||||
"version": "6.21.0",
|
||||
"resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz",
|
||||
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==",
|
||||
"dev": true
|
||||
"integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ=="
|
||||
},
|
||||
"node_modules/unicode-properties": {
|
||||
"version": "1.4.1",
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
"description": "OpenCRM Backend API",
|
||||
"main": "dist/index.js",
|
||||
"prisma": {
|
||||
"seed": "tsx prisma/seed.ts"
|
||||
"seed": "npx tsx prisma/seed.ts"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "tsx watch src/index.ts",
|
||||
@@ -12,6 +12,7 @@
|
||||
"start": "node dist/index.js",
|
||||
"db:migrate": "prisma migrate dev",
|
||||
"db:push": "prisma db push",
|
||||
"schema:sync": "prisma migrate dev --name auto_$(date +%Y%m%d_%H%M%S)",
|
||||
"db:seed": "tsx prisma/seed.ts",
|
||||
"db:studio": "prisma studio",
|
||||
"db:backup": "tsx prisma/backup-data.ts",
|
||||
@@ -20,11 +21,14 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@prisma/client": "^5.22.0",
|
||||
"@types/cookie-parser": "^1.4.10",
|
||||
"adm-zip": "^0.5.16",
|
||||
"archiver": "^7.0.1",
|
||||
"bcryptjs": "^2.4.3",
|
||||
"cookie-parser": "^1.4.7",
|
||||
"cors": "^2.8.5",
|
||||
"dotenv": "^16.4.5",
|
||||
"dotenv-expand": "^13.0.0",
|
||||
"express": "^4.21.1",
|
||||
"express-rate-limit": "^8.4.0",
|
||||
"express-validator": "^7.2.0",
|
||||
@@ -37,6 +41,7 @@
|
||||
"nodemailer": "^7.0.13",
|
||||
"pdf-lib": "^1.17.1",
|
||||
"pdfkit": "^0.17.2",
|
||||
"tsx": "^4.19.2",
|
||||
"undici": "^6.23.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -53,7 +58,6 @@
|
||||
"@types/nodemailer": "^7.0.9",
|
||||
"@types/pdfkit": "^0.17.4",
|
||||
"prisma": "^5.22.0",
|
||||
"tsx": "^4.19.2",
|
||||
"typescript": "^5.6.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,989 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `PdfTemplate` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`description` VARCHAR(191) NULL,
|
||||
`providerName` VARCHAR(191) NULL,
|
||||
`templatePath` VARCHAR(191) NOT NULL,
|
||||
`originalName` VARCHAR(191) NOT NULL,
|
||||
`fieldMapping` LONGTEXT NOT NULL,
|
||||
`phoneFieldPrefix` VARCHAR(191) NULL,
|
||||
`maxPhoneFields` INTEGER NULL DEFAULT 8,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `PdfTemplate_name_key`(`name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `EmailLog` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`fromAddress` VARCHAR(191) NOT NULL,
|
||||
`toAddress` VARCHAR(191) NOT NULL,
|
||||
`subject` VARCHAR(191) NOT NULL,
|
||||
`context` VARCHAR(191) NOT NULL,
|
||||
`customerId` INTEGER NULL,
|
||||
`triggeredBy` VARCHAR(191) NULL,
|
||||
`smtpServer` VARCHAR(191) NOT NULL,
|
||||
`smtpPort` INTEGER NOT NULL,
|
||||
`smtpEncryption` VARCHAR(191) NOT NULL,
|
||||
`smtpUser` VARCHAR(191) NOT NULL,
|
||||
`success` BOOLEAN NOT NULL,
|
||||
`messageId` VARCHAR(191) NULL,
|
||||
`errorMessage` TEXT NULL,
|
||||
`smtpResponse` TEXT NULL,
|
||||
`sentAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
|
||||
INDEX `EmailLog_sentAt_idx`(`sentAt`),
|
||||
INDEX `EmailLog_customerId_idx`(`customerId`),
|
||||
INDEX `EmailLog_success_idx`(`success`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `AppSetting` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`key` VARCHAR(191) NOT NULL,
|
||||
`value` TEXT NOT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `AppSetting_key_key`(`key`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `User` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`email` VARCHAR(191) NOT NULL,
|
||||
`password` VARCHAR(191) NOT NULL,
|
||||
`firstName` VARCHAR(191) NOT NULL,
|
||||
`lastName` VARCHAR(191) NOT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`tokenInvalidatedAt` DATETIME(3) NULL,
|
||||
`passwordResetToken` VARCHAR(191) NULL,
|
||||
`passwordResetExpiresAt` DATETIME(3) NULL,
|
||||
`whatsappNumber` VARCHAR(191) NULL,
|
||||
`telegramUsername` VARCHAR(191) NULL,
|
||||
`signalNumber` VARCHAR(191) NULL,
|
||||
`customerId` INTEGER NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `User_email_key`(`email`),
|
||||
UNIQUE INDEX `User_passwordResetToken_key`(`passwordResetToken`),
|
||||
UNIQUE INDEX `User_customerId_key`(`customerId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Role` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`description` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Role_name_key`(`name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Permission` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`resource` VARCHAR(191) NOT NULL,
|
||||
`action` VARCHAR(191) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Permission_resource_action_key`(`resource`, `action`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `RolePermission` (
|
||||
`roleId` INTEGER NOT NULL,
|
||||
`permissionId` INTEGER NOT NULL,
|
||||
|
||||
PRIMARY KEY (`roleId`, `permissionId`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `UserRole` (
|
||||
`userId` INTEGER NOT NULL,
|
||||
`roleId` INTEGER NOT NULL,
|
||||
|
||||
PRIMARY KEY (`userId`, `roleId`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Customer` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerNumber` VARCHAR(191) NOT NULL,
|
||||
`type` ENUM('PRIVATE', 'BUSINESS') NOT NULL DEFAULT 'PRIVATE',
|
||||
`salutation` VARCHAR(191) NULL,
|
||||
`firstName` VARCHAR(191) NOT NULL,
|
||||
`lastName` VARCHAR(191) NOT NULL,
|
||||
`companyName` VARCHAR(191) NULL,
|
||||
`foundingDate` DATETIME(3) NULL,
|
||||
`birthDate` DATETIME(3) NULL,
|
||||
`birthPlace` VARCHAR(191) NULL,
|
||||
`email` VARCHAR(191) NULL,
|
||||
`phone` VARCHAR(191) NULL,
|
||||
`mobile` VARCHAR(191) NULL,
|
||||
`taxNumber` VARCHAR(191) NULL,
|
||||
`businessRegistrationPath` VARCHAR(191) NULL,
|
||||
`commercialRegisterPath` VARCHAR(191) NULL,
|
||||
`commercialRegisterNumber` VARCHAR(191) NULL,
|
||||
`privacyPolicyPath` VARCHAR(191) NULL,
|
||||
`consentHash` VARCHAR(191) NULL,
|
||||
`notes` TEXT NULL,
|
||||
`portalEnabled` BOOLEAN NOT NULL DEFAULT false,
|
||||
`portalEmail` VARCHAR(191) NULL,
|
||||
`portalPasswordHash` VARCHAR(191) NULL,
|
||||
`portalPasswordEncrypted` VARCHAR(191) NULL,
|
||||
`portalLastLogin` DATETIME(3) NULL,
|
||||
`portalPasswordResetToken` VARCHAR(191) NULL,
|
||||
`portalPasswordResetExpiresAt` DATETIME(3) NULL,
|
||||
`portalTokenInvalidatedAt` DATETIME(3) NULL,
|
||||
`lastBirthdayGreetingYear` INTEGER NULL,
|
||||
`useInformalAddress` BOOLEAN NOT NULL DEFAULT false,
|
||||
`autoBirthdayGreeting` BOOLEAN NOT NULL DEFAULT false,
|
||||
`autoBirthdayChannel` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Customer_customerNumber_key`(`customerNumber`),
|
||||
UNIQUE INDEX `Customer_consentHash_key`(`consentHash`),
|
||||
UNIQUE INDEX `Customer_portalEmail_key`(`portalEmail`),
|
||||
UNIQUE INDEX `Customer_portalPasswordResetToken_key`(`portalPasswordResetToken`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `CustomerRepresentative` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`representativeId` INTEGER NOT NULL,
|
||||
`notes` VARCHAR(191) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `CustomerRepresentative_customerId_representativeId_key`(`customerId`, `representativeId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `RepresentativeAuthorization` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`representativeId` INTEGER NOT NULL,
|
||||
`isGranted` BOOLEAN NOT NULL DEFAULT false,
|
||||
`grantedAt` DATETIME(3) NULL,
|
||||
`withdrawnAt` DATETIME(3) NULL,
|
||||
`source` VARCHAR(191) NULL,
|
||||
`documentPath` VARCHAR(191) NULL,
|
||||
`notes` TEXT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `RepresentativeAuthorization_customerId_representativeId_key`(`customerId`, `representativeId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Address` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`type` ENUM('DELIVERY_RESIDENCE', 'BILLING') NOT NULL DEFAULT 'DELIVERY_RESIDENCE',
|
||||
`street` VARCHAR(191) NOT NULL,
|
||||
`houseNumber` VARCHAR(191) NOT NULL,
|
||||
`postalCode` VARCHAR(191) NOT NULL,
|
||||
`city` VARCHAR(191) NOT NULL,
|
||||
`country` VARCHAR(191) NOT NULL DEFAULT 'Deutschland',
|
||||
`isDefault` BOOLEAN NOT NULL DEFAULT false,
|
||||
`ownerCompany` VARCHAR(191) NULL,
|
||||
`ownerFirstName` VARCHAR(191) NULL,
|
||||
`ownerLastName` VARCHAR(191) NULL,
|
||||
`ownerStreet` VARCHAR(191) NULL,
|
||||
`ownerHouseNumber` VARCHAR(191) NULL,
|
||||
`ownerPostalCode` VARCHAR(191) NULL,
|
||||
`ownerCity` VARCHAR(191) NULL,
|
||||
`ownerPhone` VARCHAR(191) NULL,
|
||||
`ownerMobile` VARCHAR(191) NULL,
|
||||
`ownerEmail` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `BankCard` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`accountHolder` VARCHAR(191) NOT NULL,
|
||||
`iban` VARCHAR(191) NOT NULL,
|
||||
`bic` VARCHAR(191) NULL,
|
||||
`bankName` VARCHAR(191) NULL,
|
||||
`expiryDate` DATETIME(3) NULL,
|
||||
`documentPath` VARCHAR(191) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `IdentityDocument` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`type` ENUM('ID_CARD', 'PASSPORT', 'DRIVERS_LICENSE', 'OTHER') NOT NULL DEFAULT 'ID_CARD',
|
||||
`documentNumber` VARCHAR(191) NOT NULL,
|
||||
`issuingAuthority` VARCHAR(191) NULL,
|
||||
`issueDate` DATETIME(3) NULL,
|
||||
`expiryDate` DATETIME(3) NULL,
|
||||
`documentPath` VARCHAR(191) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`licenseClasses` VARCHAR(191) NULL,
|
||||
`licenseIssueDate` DATETIME(3) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `EmailProviderConfig` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`type` ENUM('PLESK', 'CPANEL', 'DIRECTADMIN') NOT NULL,
|
||||
`apiUrl` VARCHAR(191) NOT NULL,
|
||||
`apiKey` VARCHAR(191) NULL,
|
||||
`username` VARCHAR(191) NULL,
|
||||
`passwordEncrypted` VARCHAR(191) NULL,
|
||||
`domain` VARCHAR(191) NOT NULL,
|
||||
`defaultForwardEmail` VARCHAR(191) NULL,
|
||||
`imapServer` VARCHAR(191) NULL,
|
||||
`imapPort` INTEGER NULL DEFAULT 993,
|
||||
`smtpServer` VARCHAR(191) NULL,
|
||||
`smtpPort` INTEGER NULL DEFAULT 465,
|
||||
`imapEncryption` ENUM('SSL', 'STARTTLS', 'NONE') NOT NULL DEFAULT 'SSL',
|
||||
`smtpEncryption` ENUM('SSL', 'STARTTLS', 'NONE') NOT NULL DEFAULT 'SSL',
|
||||
`allowSelfSignedCerts` BOOLEAN NOT NULL DEFAULT false,
|
||||
`systemEmailAddress` VARCHAR(191) NULL,
|
||||
`systemEmailPasswordEncrypted` VARCHAR(191) NULL,
|
||||
`customerEmailLabel` VARCHAR(191) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`isDefault` BOOLEAN NOT NULL DEFAULT false,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `EmailProviderConfig_name_key`(`name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `StressfreiEmail` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`email` VARCHAR(191) NOT NULL,
|
||||
`platform` VARCHAR(191) NULL,
|
||||
`notes` TEXT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`isProvisioned` BOOLEAN NOT NULL DEFAULT false,
|
||||
`provisionedAt` DATETIME(3) NULL,
|
||||
`provisionError` TEXT NULL,
|
||||
`hasMailbox` BOOLEAN NOT NULL DEFAULT false,
|
||||
`emailPasswordEncrypted` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `CachedEmail` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`stressfreiEmailId` INTEGER NOT NULL,
|
||||
`folder` ENUM('INBOX', 'SENT') NOT NULL DEFAULT 'INBOX',
|
||||
`messageId` VARCHAR(191) NOT NULL,
|
||||
`uid` INTEGER NOT NULL,
|
||||
`subject` VARCHAR(191) NULL,
|
||||
`fromAddress` VARCHAR(191) NOT NULL,
|
||||
`fromName` VARCHAR(191) NULL,
|
||||
`toAddresses` TEXT NOT NULL,
|
||||
`ccAddresses` TEXT NULL,
|
||||
`receivedAt` DATETIME(3) NOT NULL,
|
||||
`textBody` LONGTEXT NULL,
|
||||
`htmlBody` LONGTEXT NULL,
|
||||
`hasAttachments` BOOLEAN NOT NULL DEFAULT false,
|
||||
`attachmentNames` TEXT NULL,
|
||||
`contractId` INTEGER NULL,
|
||||
`assignedAt` DATETIME(3) NULL,
|
||||
`assignedBy` INTEGER NULL,
|
||||
`isAutoAssigned` BOOLEAN NOT NULL DEFAULT false,
|
||||
`isRead` BOOLEAN NOT NULL DEFAULT false,
|
||||
`isStarred` BOOLEAN NOT NULL DEFAULT false,
|
||||
`isDeleted` BOOLEAN NOT NULL DEFAULT false,
|
||||
`deletedAt` DATETIME(3) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
INDEX `CachedEmail_contractId_idx`(`contractId`),
|
||||
INDEX `CachedEmail_stressfreiEmailId_folder_receivedAt_idx`(`stressfreiEmailId`, `folder`, `receivedAt`),
|
||||
INDEX `CachedEmail_stressfreiEmailId_isDeleted_idx`(`stressfreiEmailId`, `isDeleted`),
|
||||
UNIQUE INDEX `CachedEmail_stressfreiEmailId_messageId_folder_key`(`stressfreiEmailId`, `messageId`, `folder`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Meter` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`meterNumber` VARCHAR(191) NOT NULL,
|
||||
`type` ENUM('ELECTRICITY', 'GAS') NOT NULL,
|
||||
`tariffModel` ENUM('SINGLE', 'DUAL') NOT NULL DEFAULT 'SINGLE',
|
||||
`location` VARCHAR(191) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `MeterReading` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`meterId` INTEGER NOT NULL,
|
||||
`readingDate` DATETIME(3) NOT NULL,
|
||||
`value` DOUBLE NOT NULL,
|
||||
`valueNt` DOUBLE NULL,
|
||||
`unit` VARCHAR(191) NOT NULL DEFAULT 'kWh',
|
||||
`notes` VARCHAR(191) NULL,
|
||||
`reportedBy` VARCHAR(191) NULL,
|
||||
`status` ENUM('RECORDED', 'REPORTED', 'TRANSFERRED') NOT NULL DEFAULT 'RECORDED',
|
||||
`transferredAt` DATETIME(3) NULL,
|
||||
`transferredBy` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `SalesPlatform` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`contactInfo` TEXT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `SalesPlatform_name_key`(`name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `CancellationPeriod` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`code` VARCHAR(191) NOT NULL,
|
||||
`description` VARCHAR(191) NOT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `CancellationPeriod_code_key`(`code`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractDuration` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`code` VARCHAR(191) NOT NULL,
|
||||
`description` VARCHAR(191) NOT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `ContractDuration_code_key`(`code`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Provider` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`portalUrl` VARCHAR(191) NULL,
|
||||
`usernameFieldName` VARCHAR(191) NULL,
|
||||
`passwordFieldName` VARCHAR(191) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Provider_name_key`(`name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Tariff` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`providerId` INTEGER NOT NULL,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Tariff_providerId_name_key`(`providerId`, `name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractCategory` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`code` VARCHAR(191) NOT NULL,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`icon` VARCHAR(191) NULL,
|
||||
`color` VARCHAR(191) NULL,
|
||||
`sortOrder` INTEGER NOT NULL DEFAULT 0,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `ContractCategory_code_key`(`code`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Contract` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractNumber` VARCHAR(191) NOT NULL,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`type` ENUM('ELECTRICITY', 'GAS', 'DSL', 'CABLE', 'FIBER', 'MOBILE', 'TV', 'CAR_INSURANCE') NOT NULL,
|
||||
`status` ENUM('DRAFT', 'PENDING', 'ACTIVE', 'CANCELLED', 'EXPIRED', 'DEACTIVATED') NOT NULL DEFAULT 'DRAFT',
|
||||
`contractCategoryId` INTEGER NULL,
|
||||
`addressId` INTEGER NULL,
|
||||
`billingAddressId` INTEGER NULL,
|
||||
`bankCardId` INTEGER NULL,
|
||||
`identityDocumentId` INTEGER NULL,
|
||||
`salesPlatformId` INTEGER NULL,
|
||||
`cancellationPeriodId` INTEGER NULL,
|
||||
`contractDurationId` INTEGER NULL,
|
||||
`previousContractId` INTEGER NULL,
|
||||
`previousProviderId` INTEGER NULL,
|
||||
`previousCustomerNumber` VARCHAR(191) NULL,
|
||||
`previousContractNumber` VARCHAR(191) NULL,
|
||||
`providerId` INTEGER NULL,
|
||||
`tariffId` INTEGER NULL,
|
||||
`providerName` VARCHAR(191) NULL,
|
||||
`tariffName` VARCHAR(191) NULL,
|
||||
`customerNumberAtProvider` VARCHAR(191) NULL,
|
||||
`contractNumberAtProvider` VARCHAR(191) NULL,
|
||||
`priceFirst12Months` VARCHAR(191) NULL,
|
||||
`priceFrom13Months` VARCHAR(191) NULL,
|
||||
`priceAfter24Months` VARCHAR(191) NULL,
|
||||
`startDate` DATETIME(3) NULL,
|
||||
`endDate` DATETIME(3) NULL,
|
||||
`commission` DOUBLE NULL,
|
||||
`cancellationLetterPath` VARCHAR(191) NULL,
|
||||
`cancellationConfirmationPath` VARCHAR(191) NULL,
|
||||
`cancellationLetterOptionsPath` VARCHAR(191) NULL,
|
||||
`cancellationConfirmationOptionsPath` VARCHAR(191) NULL,
|
||||
`cancellationConfirmationDate` DATETIME(3) NULL,
|
||||
`cancellationConfirmationOptionsDate` DATETIME(3) NULL,
|
||||
`wasSpecialCancellation` BOOLEAN NOT NULL DEFAULT false,
|
||||
`portalUsername` VARCHAR(191) NULL,
|
||||
`portalPasswordEncrypted` VARCHAR(191) NULL,
|
||||
`stressfreiEmailId` INTEGER NULL,
|
||||
`nextReviewDate` DATETIME(3) NULL,
|
||||
`notes` TEXT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Contract_contractNumber_key`(`contractNumber`),
|
||||
UNIQUE INDEX `Contract_previousContractId_key`(`previousContractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractDocument` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`documentType` VARCHAR(191) NOT NULL,
|
||||
`documentPath` VARCHAR(191) NOT NULL,
|
||||
`originalName` VARCHAR(191) NOT NULL,
|
||||
`notes` TEXT NULL,
|
||||
`uploadedBy` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
|
||||
INDEX `ContractDocument_contractId_idx`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractHistoryEntry` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`title` VARCHAR(191) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`isAutomatic` BOOLEAN NOT NULL DEFAULT false,
|
||||
`createdBy` VARCHAR(191) NOT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractTask` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`title` VARCHAR(191) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`status` ENUM('OPEN', 'COMPLETED') NOT NULL DEFAULT 'OPEN',
|
||||
`visibleInPortal` BOOLEAN NOT NULL DEFAULT false,
|
||||
`createdBy` VARCHAR(191) NULL,
|
||||
`completedAt` DATETIME(3) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractTaskSubtask` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`taskId` INTEGER NOT NULL,
|
||||
`title` VARCHAR(191) NOT NULL,
|
||||
`status` ENUM('OPEN', 'COMPLETED') NOT NULL DEFAULT 'OPEN',
|
||||
`createdBy` VARCHAR(191) NULL,
|
||||
`completedAt` DATETIME(3) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `EnergyContractDetails` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`meterId` INTEGER NULL,
|
||||
`maloId` VARCHAR(191) NULL,
|
||||
`annualConsumption` DOUBLE NULL,
|
||||
`annualConsumptionKwh` DOUBLE NULL,
|
||||
`basePrice` DOUBLE NULL,
|
||||
`unitPrice` DOUBLE NULL,
|
||||
`unitPriceNt` DOUBLE NULL,
|
||||
`bonus` DOUBLE NULL,
|
||||
`previousProviderName` VARCHAR(191) NULL,
|
||||
`previousCustomerNumber` VARCHAR(191) NULL,
|
||||
|
||||
UNIQUE INDEX `EnergyContractDetails_contractId_key`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractMeter` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`energyContractDetailsId` INTEGER NOT NULL,
|
||||
`meterId` INTEGER NOT NULL,
|
||||
`position` INTEGER NOT NULL DEFAULT 0,
|
||||
`installedAt` DATETIME(3) NULL,
|
||||
`removedAt` DATETIME(3) NULL,
|
||||
`finalReading` DOUBLE NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
|
||||
INDEX `ContractMeter_energyContractDetailsId_idx`(`energyContractDetailsId`),
|
||||
UNIQUE INDEX `ContractMeter_energyContractDetailsId_meterId_key`(`energyContractDetailsId`, `meterId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Invoice` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`energyContractDetailsId` INTEGER NULL,
|
||||
`contractId` INTEGER NULL,
|
||||
`invoiceDate` DATETIME(3) NOT NULL,
|
||||
`invoiceType` ENUM('INTERIM', 'FINAL', 'NOT_AVAILABLE') NOT NULL,
|
||||
`documentPath` VARCHAR(191) NULL,
|
||||
`notes` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
INDEX `Invoice_energyContractDetailsId_idx`(`energyContractDetailsId`),
|
||||
INDEX `Invoice_contractId_idx`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `InternetContractDetails` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`downloadSpeed` INTEGER NULL,
|
||||
`uploadSpeed` INTEGER NULL,
|
||||
`routerModel` VARCHAR(191) NULL,
|
||||
`routerSerialNumber` VARCHAR(191) NULL,
|
||||
`installationDate` DATETIME(3) NULL,
|
||||
`internetUsername` VARCHAR(191) NULL,
|
||||
`internetPasswordEncrypted` VARCHAR(191) NULL,
|
||||
`propertyType` VARCHAR(191) NULL,
|
||||
`propertyLocation` VARCHAR(191) NULL,
|
||||
`connectionLocation` VARCHAR(191) NULL,
|
||||
`homeId` VARCHAR(191) NULL,
|
||||
`activationCode` VARCHAR(191) NULL,
|
||||
|
||||
UNIQUE INDEX `InternetContractDetails_contractId_key`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `PhoneNumber` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`internetContractDetailsId` INTEGER NOT NULL,
|
||||
`phoneNumber` VARCHAR(191) NOT NULL,
|
||||
`isMain` BOOLEAN NOT NULL DEFAULT false,
|
||||
`sipUsername` VARCHAR(191) NULL,
|
||||
`sipPasswordEncrypted` VARCHAR(191) NULL,
|
||||
`sipServer` VARCHAR(191) NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `MobileContractDetails` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`requiresMultisim` BOOLEAN NOT NULL DEFAULT false,
|
||||
`dataVolume` DOUBLE NULL,
|
||||
`includedMinutes` INTEGER NULL,
|
||||
`includedSMS` INTEGER NULL,
|
||||
`deviceModel` VARCHAR(191) NULL,
|
||||
`deviceImei` VARCHAR(191) NULL,
|
||||
`phoneNumber` VARCHAR(191) NULL,
|
||||
`simCardNumber` VARCHAR(191) NULL,
|
||||
|
||||
UNIQUE INDEX `MobileContractDetails_contractId_key`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `SimCard` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`mobileDetailsId` INTEGER NOT NULL,
|
||||
`phoneNumber` VARCHAR(191) NULL,
|
||||
`simCardNumber` VARCHAR(191) NULL,
|
||||
`pin` VARCHAR(191) NULL,
|
||||
`puk` VARCHAR(191) NULL,
|
||||
`isMultisim` BOOLEAN NOT NULL DEFAULT false,
|
||||
`isMain` BOOLEAN NOT NULL DEFAULT false,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `TvContractDetails` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`receiverModel` VARCHAR(191) NULL,
|
||||
`smartcardNumber` VARCHAR(191) NULL,
|
||||
`package` VARCHAR(191) NULL,
|
||||
|
||||
UNIQUE INDEX `TvContractDetails_contractId_key`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `CarInsuranceDetails` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`licensePlate` VARCHAR(191) NULL,
|
||||
`hsn` VARCHAR(191) NULL,
|
||||
`tsn` VARCHAR(191) NULL,
|
||||
`vin` VARCHAR(191) NULL,
|
||||
`vehicleType` VARCHAR(191) NULL,
|
||||
`firstRegistration` DATETIME(3) NULL,
|
||||
`noClaimsClass` VARCHAR(191) NULL,
|
||||
`insuranceType` ENUM('LIABILITY', 'PARTIAL', 'FULL') NOT NULL DEFAULT 'LIABILITY',
|
||||
`deductiblePartial` DOUBLE NULL,
|
||||
`deductibleFull` DOUBLE NULL,
|
||||
`policyNumber` VARCHAR(191) NULL,
|
||||
`previousInsurer` VARCHAR(191) NULL,
|
||||
|
||||
UNIQUE INDEX `CarInsuranceDetails_contractId_key`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `AuditLog` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`userId` INTEGER NULL,
|
||||
`userEmail` VARCHAR(191) NOT NULL,
|
||||
`userRole` TEXT NULL,
|
||||
`customerId` INTEGER NULL,
|
||||
`isCustomerPortal` BOOLEAN NOT NULL DEFAULT false,
|
||||
`action` ENUM('CREATE', 'READ', 'UPDATE', 'DELETE', 'EXPORT', 'ANONYMIZE', 'LOGIN', 'LOGOUT', 'LOGIN_FAILED') NOT NULL,
|
||||
`sensitivity` ENUM('LOW', 'MEDIUM', 'HIGH', 'CRITICAL') NOT NULL DEFAULT 'MEDIUM',
|
||||
`resourceType` VARCHAR(191) NOT NULL,
|
||||
`resourceId` VARCHAR(191) NULL,
|
||||
`resourceLabel` VARCHAR(191) NULL,
|
||||
`endpoint` VARCHAR(191) NOT NULL,
|
||||
`httpMethod` VARCHAR(191) NOT NULL,
|
||||
`ipAddress` VARCHAR(191) NOT NULL,
|
||||
`userAgent` TEXT NULL,
|
||||
`changesBefore` LONGTEXT NULL,
|
||||
`changesAfter` LONGTEXT NULL,
|
||||
`changesEncrypted` BOOLEAN NOT NULL DEFAULT false,
|
||||
`dataSubjectId` INTEGER NULL,
|
||||
`legalBasis` VARCHAR(191) NULL,
|
||||
`success` BOOLEAN NOT NULL DEFAULT true,
|
||||
`errorMessage` TEXT NULL,
|
||||
`durationMs` INTEGER NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`hash` VARCHAR(191) NULL,
|
||||
`previousHash` VARCHAR(191) NULL,
|
||||
|
||||
INDEX `AuditLog_userId_idx`(`userId`),
|
||||
INDEX `AuditLog_customerId_idx`(`customerId`),
|
||||
INDEX `AuditLog_resourceType_resourceId_idx`(`resourceType`, `resourceId`),
|
||||
INDEX `AuditLog_dataSubjectId_idx`(`dataSubjectId`),
|
||||
INDEX `AuditLog_action_idx`(`action`),
|
||||
INDEX `AuditLog_createdAt_idx`(`createdAt`),
|
||||
INDEX `AuditLog_sensitivity_idx`(`sensitivity`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `CustomerConsent` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`consentType` ENUM('DATA_PROCESSING', 'MARKETING_EMAIL', 'MARKETING_PHONE', 'DATA_SHARING_PARTNER') NOT NULL,
|
||||
`status` ENUM('GRANTED', 'WITHDRAWN', 'PENDING') NOT NULL DEFAULT 'PENDING',
|
||||
`grantedAt` DATETIME(3) NULL,
|
||||
`withdrawnAt` DATETIME(3) NULL,
|
||||
`source` VARCHAR(191) NULL,
|
||||
`documentPath` VARCHAR(191) NULL,
|
||||
`version` VARCHAR(191) NULL,
|
||||
`ipAddress` VARCHAR(191) NULL,
|
||||
`createdBy` VARCHAR(191) NOT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
INDEX `CustomerConsent_customerId_idx`(`customerId`),
|
||||
INDEX `CustomerConsent_consentType_idx`(`consentType`),
|
||||
INDEX `CustomerConsent_status_idx`(`status`),
|
||||
UNIQUE INDEX `CustomerConsent_customerId_consentType_key`(`customerId`, `consentType`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `DataDeletionRequest` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`status` ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'PARTIALLY_COMPLETED', 'REJECTED') NOT NULL DEFAULT 'PENDING',
|
||||
`requestedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`requestSource` VARCHAR(191) NOT NULL,
|
||||
`requestedBy` VARCHAR(191) NOT NULL,
|
||||
`processedAt` DATETIME(3) NULL,
|
||||
`processedBy` VARCHAR(191) NULL,
|
||||
`deletedData` LONGTEXT NULL,
|
||||
`retainedData` LONGTEXT NULL,
|
||||
`retentionReason` TEXT NULL,
|
||||
`proofDocument` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
INDEX `DataDeletionRequest_customerId_idx`(`customerId`),
|
||||
INDEX `DataDeletionRequest_status_idx`(`status`),
|
||||
INDEX `DataDeletionRequest_requestedAt_idx`(`requestedAt`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `AuditRetentionPolicy` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`resourceType` VARCHAR(191) NOT NULL,
|
||||
`sensitivity` ENUM('LOW', 'MEDIUM', 'HIGH', 'CRITICAL') NULL,
|
||||
`retentionDays` INTEGER NOT NULL,
|
||||
`description` VARCHAR(191) NULL,
|
||||
`legalBasis` VARCHAR(191) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `AuditRetentionPolicy_resourceType_sensitivity_key`(`resourceType`, `sensitivity`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `SecurityEvent` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`type` ENUM('LOGIN_FAILED', 'LOGIN_SUCCESS', 'RATE_LIMIT_HIT', 'ACCESS_DENIED', 'SSRF_BLOCKED', 'PASSWORD_RESET_REQUEST', 'PASSWORD_RESET_CONFIRM', 'LOGOUT', 'TOKEN_REJECTED', 'PERMISSION_CHANGED', 'SUSPICIOUS') NOT NULL,
|
||||
`severity` ENUM('INFO', 'LOW', 'MEDIUM', 'HIGH', 'CRITICAL') NOT NULL,
|
||||
`message` TEXT NOT NULL,
|
||||
`ipAddress` VARCHAR(191) NULL,
|
||||
`userId` INTEGER NULL,
|
||||
`customerId` INTEGER NULL,
|
||||
`userEmail` VARCHAR(191) NULL,
|
||||
`endpoint` VARCHAR(191) NULL,
|
||||
`details` JSON NULL,
|
||||
`alerted` BOOLEAN NOT NULL DEFAULT false,
|
||||
`alertedAt` DATETIME(3) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
|
||||
INDEX `SecurityEvent_type_createdAt_idx`(`type`, `createdAt`),
|
||||
INDEX `SecurityEvent_severity_createdAt_idx`(`severity`, `createdAt`),
|
||||
INDEX `SecurityEvent_ipAddress_createdAt_idx`(`ipAddress`, `createdAt`),
|
||||
INDEX `SecurityEvent_alerted_severity_idx`(`alerted`, `severity`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `User` ADD CONSTRAINT `User_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `RolePermission` ADD CONSTRAINT `RolePermission_roleId_fkey` FOREIGN KEY (`roleId`) REFERENCES `Role`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `RolePermission` ADD CONSTRAINT `RolePermission_permissionId_fkey` FOREIGN KEY (`permissionId`) REFERENCES `Permission`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `UserRole` ADD CONSTRAINT `UserRole_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `UserRole` ADD CONSTRAINT `UserRole_roleId_fkey` FOREIGN KEY (`roleId`) REFERENCES `Role`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CustomerRepresentative` ADD CONSTRAINT `CustomerRepresentative_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CustomerRepresentative` ADD CONSTRAINT `CustomerRepresentative_representativeId_fkey` FOREIGN KEY (`representativeId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `RepresentativeAuthorization` ADD CONSTRAINT `RepresentativeAuthorization_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `RepresentativeAuthorization` ADD CONSTRAINT `RepresentativeAuthorization_representativeId_fkey` FOREIGN KEY (`representativeId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Address` ADD CONSTRAINT `Address_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `BankCard` ADD CONSTRAINT `BankCard_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `IdentityDocument` ADD CONSTRAINT `IdentityDocument_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `StressfreiEmail` ADD CONSTRAINT `StressfreiEmail_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CachedEmail` ADD CONSTRAINT `CachedEmail_stressfreiEmailId_fkey` FOREIGN KEY (`stressfreiEmailId`) REFERENCES `StressfreiEmail`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CachedEmail` ADD CONSTRAINT `CachedEmail_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Meter` ADD CONSTRAINT `Meter_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `MeterReading` ADD CONSTRAINT `MeterReading_meterId_fkey` FOREIGN KEY (`meterId`) REFERENCES `Meter`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Tariff` ADD CONSTRAINT `Tariff_providerId_fkey` FOREIGN KEY (`providerId`) REFERENCES `Provider`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_contractCategoryId_fkey` FOREIGN KEY (`contractCategoryId`) REFERENCES `ContractCategory`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_addressId_fkey` FOREIGN KEY (`addressId`) REFERENCES `Address`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_billingAddressId_fkey` FOREIGN KEY (`billingAddressId`) REFERENCES `Address`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_bankCardId_fkey` FOREIGN KEY (`bankCardId`) REFERENCES `BankCard`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_identityDocumentId_fkey` FOREIGN KEY (`identityDocumentId`) REFERENCES `IdentityDocument`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_salesPlatformId_fkey` FOREIGN KEY (`salesPlatformId`) REFERENCES `SalesPlatform`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_cancellationPeriodId_fkey` FOREIGN KEY (`cancellationPeriodId`) REFERENCES `CancellationPeriod`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_contractDurationId_fkey` FOREIGN KEY (`contractDurationId`) REFERENCES `ContractDuration`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_previousContractId_fkey` FOREIGN KEY (`previousContractId`) REFERENCES `Contract`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_previousProviderId_fkey` FOREIGN KEY (`previousProviderId`) REFERENCES `Provider`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_providerId_fkey` FOREIGN KEY (`providerId`) REFERENCES `Provider`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_tariffId_fkey` FOREIGN KEY (`tariffId`) REFERENCES `Tariff`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_stressfreiEmailId_fkey` FOREIGN KEY (`stressfreiEmailId`) REFERENCES `StressfreiEmail`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `ContractDocument` ADD CONSTRAINT `ContractDocument_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `ContractHistoryEntry` ADD CONSTRAINT `ContractHistoryEntry_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `ContractTask` ADD CONSTRAINT `ContractTask_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `ContractTaskSubtask` ADD CONSTRAINT `ContractTaskSubtask_taskId_fkey` FOREIGN KEY (`taskId`) REFERENCES `ContractTask`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `EnergyContractDetails` ADD CONSTRAINT `EnergyContractDetails_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `EnergyContractDetails` ADD CONSTRAINT `EnergyContractDetails_meterId_fkey` FOREIGN KEY (`meterId`) REFERENCES `Meter`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `ContractMeter` ADD CONSTRAINT `ContractMeter_energyContractDetailsId_fkey` FOREIGN KEY (`energyContractDetailsId`) REFERENCES `EnergyContractDetails`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `ContractMeter` ADD CONSTRAINT `ContractMeter_meterId_fkey` FOREIGN KEY (`meterId`) REFERENCES `Meter`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Invoice` ADD CONSTRAINT `Invoice_energyContractDetailsId_fkey` FOREIGN KEY (`energyContractDetailsId`) REFERENCES `EnergyContractDetails`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Invoice` ADD CONSTRAINT `Invoice_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `InternetContractDetails` ADD CONSTRAINT `InternetContractDetails_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `PhoneNumber` ADD CONSTRAINT `PhoneNumber_internetContractDetailsId_fkey` FOREIGN KEY (`internetContractDetailsId`) REFERENCES `InternetContractDetails`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `MobileContractDetails` ADD CONSTRAINT `MobileContractDetails_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `SimCard` ADD CONSTRAINT `SimCard_mobileDetailsId_fkey` FOREIGN KEY (`mobileDetailsId`) REFERENCES `MobileContractDetails`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `TvContractDetails` ADD CONSTRAINT `TvContractDetails_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CarInsuranceDetails` ADD CONSTRAINT `CarInsuranceDetails_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CustomerConsent` ADD CONSTRAINT `CustomerConsent_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
@@ -1,354 +0,0 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `User` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`email` VARCHAR(191) NOT NULL,
|
||||
`password` VARCHAR(191) NOT NULL,
|
||||
`firstName` VARCHAR(191) NOT NULL,
|
||||
`lastName` VARCHAR(191) NOT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`customerId` INTEGER NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `User_email_key`(`email`),
|
||||
UNIQUE INDEX `User_customerId_key`(`customerId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Role` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`description` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Role_name_key`(`name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Permission` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`resource` VARCHAR(191) NOT NULL,
|
||||
`action` VARCHAR(191) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Permission_resource_action_key`(`resource`, `action`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `RolePermission` (
|
||||
`roleId` INTEGER NOT NULL,
|
||||
`permissionId` INTEGER NOT NULL,
|
||||
|
||||
PRIMARY KEY (`roleId`, `permissionId`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `UserRole` (
|
||||
`userId` INTEGER NOT NULL,
|
||||
`roleId` INTEGER NOT NULL,
|
||||
|
||||
PRIMARY KEY (`userId`, `roleId`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Customer` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerNumber` VARCHAR(191) NOT NULL,
|
||||
`type` ENUM('PRIVATE', 'BUSINESS') NOT NULL DEFAULT 'PRIVATE',
|
||||
`salutation` VARCHAR(191) NULL,
|
||||
`firstName` VARCHAR(191) NOT NULL,
|
||||
`lastName` VARCHAR(191) NOT NULL,
|
||||
`companyName` VARCHAR(191) NULL,
|
||||
`email` VARCHAR(191) NULL,
|
||||
`phone` VARCHAR(191) NULL,
|
||||
`mobile` VARCHAR(191) NULL,
|
||||
`taxNumber` VARCHAR(191) NULL,
|
||||
`businessRegistration` TEXT NULL,
|
||||
`commercialRegister` VARCHAR(191) NULL,
|
||||
`notes` TEXT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Customer_customerNumber_key`(`customerNumber`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Address` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`type` ENUM('DELIVERY_RESIDENCE', 'BILLING') NOT NULL DEFAULT 'DELIVERY_RESIDENCE',
|
||||
`street` VARCHAR(191) NOT NULL,
|
||||
`houseNumber` VARCHAR(191) NOT NULL,
|
||||
`postalCode` VARCHAR(191) NOT NULL,
|
||||
`city` VARCHAR(191) NOT NULL,
|
||||
`country` VARCHAR(191) NOT NULL DEFAULT 'Deutschland',
|
||||
`isDefault` BOOLEAN NOT NULL DEFAULT false,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `BankCard` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`accountHolder` VARCHAR(191) NOT NULL,
|
||||
`iban` VARCHAR(191) NOT NULL,
|
||||
`bic` VARCHAR(191) NULL,
|
||||
`bankName` VARCHAR(191) NULL,
|
||||
`expiryDate` DATETIME(3) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `IdentityDocument` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`type` ENUM('ID_CARD', 'PASSPORT', 'DRIVERS_LICENSE', 'OTHER') NOT NULL DEFAULT 'ID_CARD',
|
||||
`documentNumber` VARCHAR(191) NOT NULL,
|
||||
`issuingAuthority` VARCHAR(191) NULL,
|
||||
`issueDate` DATETIME(3) NULL,
|
||||
`expiryDate` DATETIME(3) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Meter` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`meterNumber` VARCHAR(191) NOT NULL,
|
||||
`type` ENUM('ELECTRICITY', 'GAS') NOT NULL,
|
||||
`location` VARCHAR(191) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `MeterReading` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`meterId` INTEGER NOT NULL,
|
||||
`readingDate` DATETIME(3) NOT NULL,
|
||||
`value` DOUBLE NOT NULL,
|
||||
`unit` VARCHAR(191) NOT NULL DEFAULT 'kWh',
|
||||
`notes` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `SalesPlatform` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`contactInfo` TEXT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `SalesPlatform_name_key`(`name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Contract` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractNumber` VARCHAR(191) NOT NULL,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`type` ENUM('ELECTRICITY', 'GAS', 'DSL', 'FIBER', 'MOBILE', 'TV', 'CAR_INSURANCE') NOT NULL,
|
||||
`status` ENUM('DRAFT', 'PENDING', 'ACTIVE', 'CANCELLED', 'EXPIRED') NOT NULL DEFAULT 'DRAFT',
|
||||
`addressId` INTEGER NULL,
|
||||
`bankCardId` INTEGER NULL,
|
||||
`identityDocumentId` INTEGER NULL,
|
||||
`salesPlatformId` INTEGER NULL,
|
||||
`previousContractId` INTEGER NULL,
|
||||
`providerName` VARCHAR(191) NULL,
|
||||
`tariffName` VARCHAR(191) NULL,
|
||||
`customerNumberAtProvider` VARCHAR(191) NULL,
|
||||
`startDate` DATETIME(3) NULL,
|
||||
`endDate` DATETIME(3) NULL,
|
||||
`cancellationPeriod` INTEGER NULL,
|
||||
`commission` DOUBLE NULL,
|
||||
`portalUsername` VARCHAR(191) NULL,
|
||||
`portalPasswordEncrypted` VARCHAR(191) NULL,
|
||||
`notes` TEXT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Contract_contractNumber_key`(`contractNumber`),
|
||||
UNIQUE INDEX `Contract_previousContractId_key`(`previousContractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `EnergyContractDetails` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`meterId` INTEGER NULL,
|
||||
`annualConsumption` DOUBLE NULL,
|
||||
`basePrice` DOUBLE NULL,
|
||||
`unitPrice` DOUBLE NULL,
|
||||
`bonus` DOUBLE NULL,
|
||||
`previousProviderName` VARCHAR(191) NULL,
|
||||
`previousCustomerNumber` VARCHAR(191) NULL,
|
||||
|
||||
UNIQUE INDEX `EnergyContractDetails_contractId_key`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `InternetContractDetails` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`downloadSpeed` INTEGER NULL,
|
||||
`uploadSpeed` INTEGER NULL,
|
||||
`routerModel` VARCHAR(191) NULL,
|
||||
`routerSerialNumber` VARCHAR(191) NULL,
|
||||
`installationDate` DATETIME(3) NULL,
|
||||
|
||||
UNIQUE INDEX `InternetContractDetails_contractId_key`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `PhoneNumber` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`internetContractDetailsId` INTEGER NOT NULL,
|
||||
`phoneNumber` VARCHAR(191) NOT NULL,
|
||||
`isMain` BOOLEAN NOT NULL DEFAULT false,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `MobileContractDetails` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`phoneNumber` VARCHAR(191) NULL,
|
||||
`simCardNumber` VARCHAR(191) NULL,
|
||||
`dataVolume` DOUBLE NULL,
|
||||
`includedMinutes` INTEGER NULL,
|
||||
`includedSMS` INTEGER NULL,
|
||||
`deviceModel` VARCHAR(191) NULL,
|
||||
`deviceImei` VARCHAR(191) NULL,
|
||||
|
||||
UNIQUE INDEX `MobileContractDetails_contractId_key`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `TvContractDetails` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`receiverModel` VARCHAR(191) NULL,
|
||||
`smartcardNumber` VARCHAR(191) NULL,
|
||||
`package` VARCHAR(191) NULL,
|
||||
|
||||
UNIQUE INDEX `TvContractDetails_contractId_key`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `CarInsuranceDetails` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`licensePlate` VARCHAR(191) NULL,
|
||||
`hsn` VARCHAR(191) NULL,
|
||||
`tsn` VARCHAR(191) NULL,
|
||||
`vin` VARCHAR(191) NULL,
|
||||
`vehicleType` VARCHAR(191) NULL,
|
||||
`firstRegistration` DATETIME(3) NULL,
|
||||
`noClaimsClass` VARCHAR(191) NULL,
|
||||
`insuranceType` ENUM('LIABILITY', 'PARTIAL', 'FULL') NOT NULL DEFAULT 'LIABILITY',
|
||||
`deductiblePartial` DOUBLE NULL,
|
||||
`deductibleFull` DOUBLE NULL,
|
||||
`policyNumber` VARCHAR(191) NULL,
|
||||
`previousInsurer` VARCHAR(191) NULL,
|
||||
|
||||
UNIQUE INDEX `CarInsuranceDetails_contractId_key`(`contractId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `User` ADD CONSTRAINT `User_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `RolePermission` ADD CONSTRAINT `RolePermission_roleId_fkey` FOREIGN KEY (`roleId`) REFERENCES `Role`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `RolePermission` ADD CONSTRAINT `RolePermission_permissionId_fkey` FOREIGN KEY (`permissionId`) REFERENCES `Permission`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `UserRole` ADD CONSTRAINT `UserRole_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `UserRole` ADD CONSTRAINT `UserRole_roleId_fkey` FOREIGN KEY (`roleId`) REFERENCES `Role`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Address` ADD CONSTRAINT `Address_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `BankCard` ADD CONSTRAINT `BankCard_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `IdentityDocument` ADD CONSTRAINT `IdentityDocument_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Meter` ADD CONSTRAINT `Meter_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `MeterReading` ADD CONSTRAINT `MeterReading_meterId_fkey` FOREIGN KEY (`meterId`) REFERENCES `Meter`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_addressId_fkey` FOREIGN KEY (`addressId`) REFERENCES `Address`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_bankCardId_fkey` FOREIGN KEY (`bankCardId`) REFERENCES `BankCard`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_identityDocumentId_fkey` FOREIGN KEY (`identityDocumentId`) REFERENCES `IdentityDocument`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_salesPlatformId_fkey` FOREIGN KEY (`salesPlatformId`) REFERENCES `SalesPlatform`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_previousContractId_fkey` FOREIGN KEY (`previousContractId`) REFERENCES `Contract`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `EnergyContractDetails` ADD CONSTRAINT `EnergyContractDetails_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `EnergyContractDetails` ADD CONSTRAINT `EnergyContractDetails_meterId_fkey` FOREIGN KEY (`meterId`) REFERENCES `Meter`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `InternetContractDetails` ADD CONSTRAINT `InternetContractDetails_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `PhoneNumber` ADD CONSTRAINT `PhoneNumber_internetContractDetailsId_fkey` FOREIGN KEY (`internetContractDetailsId`) REFERENCES `InternetContractDetails`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `MobileContractDetails` ADD CONSTRAINT `MobileContractDetails_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `TvContractDetails` ADD CONSTRAINT `TvContractDetails_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CarInsuranceDetails` ADD CONSTRAINT `CarInsuranceDetails_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -1,5 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `BankCard` ADD COLUMN `documentPath` VARCHAR(191) NULL;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE `IdentityDocument` ADD COLUMN `documentPath` VARCHAR(191) NULL;
|
||||
@@ -1,3 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Customer` ADD COLUMN `birthDate` DATETIME(3) NULL,
|
||||
ADD COLUMN `birthPlace` VARCHAR(191) NULL;
|
||||
@@ -1,3 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `IdentityDocument` ADD COLUMN `licenseClasses` VARCHAR(191) NULL,
|
||||
ADD COLUMN `licenseIssueDate` DATETIME(3) NULL;
|
||||
@@ -1,14 +0,0 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the column `businessRegistration` on the `Customer` table. All the data in the column will be lost.
|
||||
- You are about to drop the column `commercialRegister` on the `Customer` table. All the data in the column will be lost.
|
||||
|
||||
*/
|
||||
-- AlterTable
|
||||
ALTER TABLE `Customer` DROP COLUMN `businessRegistration`,
|
||||
DROP COLUMN `commercialRegister`,
|
||||
ADD COLUMN `businessRegistrationPath` VARCHAR(191) NULL,
|
||||
ADD COLUMN `commercialRegisterNumber` VARCHAR(191) NULL,
|
||||
ADD COLUMN `commercialRegisterPath` VARCHAR(191) NULL,
|
||||
ADD COLUMN `foundingDate` DATETIME(3) NULL;
|
||||
-31
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- You are about to drop the column `cancellationPeriod` on the `Contract` table. All the data in the column will be lost.
|
||||
|
||||
*/
|
||||
-- AlterTable
|
||||
ALTER TABLE `Contract` DROP COLUMN `cancellationPeriod`,
|
||||
ADD COLUMN `cancellationPeriodId` INTEGER NULL,
|
||||
ADD COLUMN `priceAfter24Months` VARCHAR(191) NULL,
|
||||
ADD COLUMN `priceFirst12Months` VARCHAR(191) NULL,
|
||||
ADD COLUMN `priceFrom13Months` VARCHAR(191) NULL;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE `Customer` ADD COLUMN `privacyPolicyPath` VARCHAR(191) NULL;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `CancellationPeriod` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`code` VARCHAR(191) NOT NULL,
|
||||
`description` VARCHAR(191) NOT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `CancellationPeriod_code_key`(`code`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_cancellationPeriodId_fkey` FOREIGN KEY (`cancellationPeriodId`) REFERENCES `CancellationPeriod`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -1,18 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Contract` ADD COLUMN `contractDurationId` INTEGER NULL;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractDuration` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`code` VARCHAR(191) NOT NULL,
|
||||
`description` VARCHAR(191) NOT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `ContractDuration_code_key`(`code`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_contractDurationId_fkey` FOREIGN KEY (`contractDurationId`) REFERENCES `ContractDuration`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
-8
@@ -1,8 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Contract` ADD COLUMN `cancellationConfirmationDate` DATETIME(3) NULL,
|
||||
ADD COLUMN `cancellationConfirmationOptionsDate` DATETIME(3) NULL,
|
||||
ADD COLUMN `cancellationConfirmationOptionsPath` VARCHAR(191) NULL,
|
||||
ADD COLUMN `cancellationConfirmationPath` VARCHAR(191) NULL,
|
||||
ADD COLUMN `cancellationLetterOptionsPath` VARCHAR(191) NULL,
|
||||
ADD COLUMN `cancellationLetterPath` VARCHAR(191) NULL,
|
||||
ADD COLUMN `wasSpecialCancellation` BOOLEAN NOT NULL DEFAULT false;
|
||||
@@ -1,40 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Contract` ADD COLUMN `providerId` INTEGER NULL,
|
||||
ADD COLUMN `tariffId` INTEGER NULL;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Provider` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`portalUrl` VARCHAR(191) NULL,
|
||||
`usernameFieldName` VARCHAR(191) NULL,
|
||||
`passwordFieldName` VARCHAR(191) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Provider_name_key`(`name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `Tariff` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`providerId` INTEGER NOT NULL,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `Tariff_providerId_name_key`(`providerId`, `name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Tariff` ADD CONSTRAINT `Tariff_providerId_fkey` FOREIGN KEY (`providerId`) REFERENCES `Provider`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_providerId_fkey` FOREIGN KEY (`providerId`) REFERENCES `Provider`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_tariffId_fkey` FOREIGN KEY (`tariffId`) REFERENCES `Tariff`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -1,21 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `MobileContractDetails` ADD COLUMN `requiresMultisim` BOOLEAN NOT NULL DEFAULT false;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `SimCard` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`mobileDetailsId` INTEGER NOT NULL,
|
||||
`phoneNumber` VARCHAR(191) NULL,
|
||||
`simCardNumber` VARCHAR(191) NULL,
|
||||
`pin` VARCHAR(191) NULL,
|
||||
`puk` VARCHAR(191) NULL,
|
||||
`isMultisim` BOOLEAN NOT NULL DEFAULT false,
|
||||
`isMain` BOOLEAN NOT NULL DEFAULT false,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `SimCard` ADD CONSTRAINT `SimCard_mobileDetailsId_fkey` FOREIGN KEY (`mobileDetailsId`) REFERENCES `MobileContractDetails`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -1,21 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Contract` ADD COLUMN `contractCategoryId` INTEGER NULL;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractCategory` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`code` VARCHAR(191) NOT NULL,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`icon` VARCHAR(191) NULL,
|
||||
`color` VARCHAR(191) NULL,
|
||||
`sortOrder` INTEGER NOT NULL DEFAULT 0,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `ContractCategory_code_key`(`code`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_contractCategoryId_fkey` FOREIGN KEY (`contractCategoryId`) REFERENCES `ContractCategory`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -1,13 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Contract` MODIFY `type` ENUM('ELECTRICITY', 'GAS', 'DSL', 'CABLE', 'FIBER', 'MOBILE', 'TV', 'CAR_INSURANCE') NOT NULL;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE `InternetContractDetails` ADD COLUMN `activationCode` VARCHAR(191) NULL,
|
||||
ADD COLUMN `homeId` VARCHAR(191) NULL,
|
||||
ADD COLUMN `internetPasswordEncrypted` VARCHAR(191) NULL,
|
||||
ADD COLUMN `internetUsername` VARCHAR(191) NULL;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE `PhoneNumber` ADD COLUMN `sipPasswordEncrypted` VARCHAR(191) NULL,
|
||||
ADD COLUMN `sipServer` VARCHAR(191) NULL,
|
||||
ADD COLUMN `sipUsername` VARCHAR(191) NULL;
|
||||
@@ -1,180 +0,0 @@
|
||||
/*
|
||||
Warnings:
|
||||
|
||||
- A unique constraint covering the columns `[portalEmail]` on the table `Customer` will be added. If there are existing duplicate values, this will fail.
|
||||
|
||||
*/
|
||||
-- AlterTable
|
||||
ALTER TABLE `Contract` ADD COLUMN `stressfreiEmailId` INTEGER NULL,
|
||||
MODIFY `status` ENUM('DRAFT', 'PENDING', 'ACTIVE', 'CANCELLED', 'EXPIRED', 'DEACTIVATED') NOT NULL DEFAULT 'DRAFT';
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE `Customer` ADD COLUMN `portalEmail` VARCHAR(191) NULL,
|
||||
ADD COLUMN `portalEnabled` BOOLEAN NOT NULL DEFAULT false,
|
||||
ADD COLUMN `portalLastLogin` DATETIME(3) NULL,
|
||||
ADD COLUMN `portalPasswordEncrypted` VARCHAR(191) NULL,
|
||||
ADD COLUMN `portalPasswordHash` VARCHAR(191) NULL;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `AppSetting` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`key` VARCHAR(191) NOT NULL,
|
||||
`value` TEXT NOT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `AppSetting_key_key`(`key`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `CustomerRepresentative` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`representativeId` INTEGER NOT NULL,
|
||||
`notes` VARCHAR(191) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `CustomerRepresentative_customerId_representativeId_key`(`customerId`, `representativeId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `EmailProviderConfig` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`name` VARCHAR(191) NOT NULL,
|
||||
`type` ENUM('PLESK', 'CPANEL', 'DIRECTADMIN') NOT NULL,
|
||||
`apiUrl` VARCHAR(191) NOT NULL,
|
||||
`apiKey` VARCHAR(191) NULL,
|
||||
`username` VARCHAR(191) NULL,
|
||||
`passwordEncrypted` VARCHAR(191) NULL,
|
||||
`domain` VARCHAR(191) NOT NULL,
|
||||
`defaultForwardEmail` VARCHAR(191) NULL,
|
||||
`imapServer` VARCHAR(191) NULL,
|
||||
`imapPort` INTEGER NULL DEFAULT 993,
|
||||
`smtpServer` VARCHAR(191) NULL,
|
||||
`smtpPort` INTEGER NULL DEFAULT 465,
|
||||
`imapEncryption` ENUM('SSL', 'STARTTLS', 'NONE') NOT NULL DEFAULT 'SSL',
|
||||
`smtpEncryption` ENUM('SSL', 'STARTTLS', 'NONE') NOT NULL DEFAULT 'SSL',
|
||||
`allowSelfSignedCerts` BOOLEAN NOT NULL DEFAULT false,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`isDefault` BOOLEAN NOT NULL DEFAULT false,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `EmailProviderConfig_name_key`(`name`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `StressfreiEmail` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`email` VARCHAR(191) NOT NULL,
|
||||
`platform` VARCHAR(191) NULL,
|
||||
`notes` TEXT NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`isProvisioned` BOOLEAN NOT NULL DEFAULT false,
|
||||
`provisionedAt` DATETIME(3) NULL,
|
||||
`provisionError` TEXT NULL,
|
||||
`hasMailbox` BOOLEAN NOT NULL DEFAULT false,
|
||||
`emailPasswordEncrypted` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `CachedEmail` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`stressfreiEmailId` INTEGER NOT NULL,
|
||||
`folder` ENUM('INBOX', 'SENT') NOT NULL DEFAULT 'INBOX',
|
||||
`messageId` VARCHAR(191) NOT NULL,
|
||||
`uid` INTEGER NOT NULL,
|
||||
`subject` VARCHAR(191) NULL,
|
||||
`fromAddress` VARCHAR(191) NOT NULL,
|
||||
`fromName` VARCHAR(191) NULL,
|
||||
`toAddresses` TEXT NOT NULL,
|
||||
`ccAddresses` TEXT NULL,
|
||||
`receivedAt` DATETIME(3) NOT NULL,
|
||||
`textBody` LONGTEXT NULL,
|
||||
`htmlBody` LONGTEXT NULL,
|
||||
`hasAttachments` BOOLEAN NOT NULL DEFAULT false,
|
||||
`attachmentNames` TEXT NULL,
|
||||
`contractId` INTEGER NULL,
|
||||
`assignedAt` DATETIME(3) NULL,
|
||||
`assignedBy` INTEGER NULL,
|
||||
`isAutoAssigned` BOOLEAN NOT NULL DEFAULT false,
|
||||
`isRead` BOOLEAN NOT NULL DEFAULT false,
|
||||
`isStarred` BOOLEAN NOT NULL DEFAULT false,
|
||||
`isDeleted` BOOLEAN NOT NULL DEFAULT false,
|
||||
`deletedAt` DATETIME(3) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
INDEX `CachedEmail_contractId_idx`(`contractId`),
|
||||
INDEX `CachedEmail_stressfreiEmailId_folder_receivedAt_idx`(`stressfreiEmailId`, `folder`, `receivedAt`),
|
||||
INDEX `CachedEmail_stressfreiEmailId_isDeleted_idx`(`stressfreiEmailId`, `isDeleted`),
|
||||
UNIQUE INDEX `CachedEmail_stressfreiEmailId_messageId_folder_key`(`stressfreiEmailId`, `messageId`, `folder`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractTask` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`title` VARCHAR(191) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`status` ENUM('OPEN', 'COMPLETED') NOT NULL DEFAULT 'OPEN',
|
||||
`visibleInPortal` BOOLEAN NOT NULL DEFAULT false,
|
||||
`createdBy` VARCHAR(191) NULL,
|
||||
`completedAt` DATETIME(3) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractTaskSubtask` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`taskId` INTEGER NOT NULL,
|
||||
`title` VARCHAR(191) NOT NULL,
|
||||
`status` ENUM('OPEN', 'COMPLETED') NOT NULL DEFAULT 'OPEN',
|
||||
`createdBy` VARCHAR(191) NULL,
|
||||
`completedAt` DATETIME(3) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX `Customer_portalEmail_key` ON `Customer`(`portalEmail`);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CustomerRepresentative` ADD CONSTRAINT `CustomerRepresentative_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CustomerRepresentative` ADD CONSTRAINT `CustomerRepresentative_representativeId_fkey` FOREIGN KEY (`representativeId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `StressfreiEmail` ADD CONSTRAINT `StressfreiEmail_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CachedEmail` ADD CONSTRAINT `CachedEmail_stressfreiEmailId_fkey` FOREIGN KEY (`stressfreiEmailId`) REFERENCES `StressfreiEmail`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CachedEmail` ADD CONSTRAINT `CachedEmail_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_stressfreiEmailId_fkey` FOREIGN KEY (`stressfreiEmailId`) REFERENCES `StressfreiEmail`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `ContractTask` ADD CONSTRAINT `ContractTask_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `ContractTaskSubtask` ADD CONSTRAINT `ContractTaskSubtask_taskId_fkey` FOREIGN KEY (`taskId`) REFERENCES `ContractTask`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -1,2 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `User` ADD COLUMN `tokenInvalidatedAt` DATETIME(3) NULL;
|
||||
@@ -1,5 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Contract` ADD COLUMN `billingAddressId` INTEGER NULL;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_billingAddressId_fkey` FOREIGN KEY (`billingAddressId`) REFERENCES `Address`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -1,2 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `EnergyContractDetails` ADD COLUMN `annualConsumptionKwh` DOUBLE NULL;
|
||||
@@ -1,2 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `EnergyContractDetails` ADD COLUMN `maloId` VARCHAR(191) NULL;
|
||||
@@ -1,17 +0,0 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `Invoice` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`energyContractDetailsId` INTEGER NOT NULL,
|
||||
`invoiceDate` DATETIME(3) NOT NULL,
|
||||
`invoiceType` ENUM('INTERIM', 'FINAL', 'NOT_AVAILABLE') NOT NULL,
|
||||
`documentPath` VARCHAR(191) NULL,
|
||||
`notes` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
INDEX `Invoice_energyContractDetailsId_idx`(`energyContractDetailsId`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Invoice` ADD CONSTRAINT `Invoice_energyContractDetailsId_fkey` FOREIGN KEY (`energyContractDetailsId`) REFERENCES `EnergyContractDetails`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -1,2 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Contract` ADD COLUMN `nextReviewDate` DATETIME(3) NULL;
|
||||
@@ -1,2 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Contract` ADD COLUMN `contractNumberAtProvider` VARCHAR(191) NULL;
|
||||
@@ -1,7 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Contract` ADD COLUMN `previousContractNumber` VARCHAR(191) NULL,
|
||||
ADD COLUMN `previousCustomerNumber` VARCHAR(191) NULL,
|
||||
ADD COLUMN `previousProviderId` INTEGER NULL;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `Contract` ADD CONSTRAINT `Contract_previousProviderId_fkey` FOREIGN KEY (`previousProviderId`) REFERENCES `Provider`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
|
||||
@@ -1,15 +0,0 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `ContractHistoryEntry` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`contractId` INTEGER NOT NULL,
|
||||
`title` VARCHAR(191) NOT NULL,
|
||||
`description` TEXT NULL,
|
||||
`isAutomatic` BOOLEAN NOT NULL DEFAULT false,
|
||||
`createdBy` VARCHAR(191) NOT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `ContractHistoryEntry` ADD CONSTRAINT `ContractHistoryEntry_contractId_fkey` FOREIGN KEY (`contractId`) REFERENCES `Contract`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -1,103 +0,0 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE `AuditLog` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`userId` INTEGER NULL,
|
||||
`userEmail` VARCHAR(191) NOT NULL,
|
||||
`userRole` VARCHAR(191) NULL,
|
||||
`customerId` INTEGER NULL,
|
||||
`isCustomerPortal` BOOLEAN NOT NULL DEFAULT false,
|
||||
`action` ENUM('CREATE', 'READ', 'UPDATE', 'DELETE', 'EXPORT', 'ANONYMIZE', 'LOGIN', 'LOGOUT', 'LOGIN_FAILED') NOT NULL,
|
||||
`sensitivity` ENUM('LOW', 'MEDIUM', 'HIGH', 'CRITICAL') NOT NULL DEFAULT 'MEDIUM',
|
||||
`resourceType` VARCHAR(191) NOT NULL,
|
||||
`resourceId` VARCHAR(191) NULL,
|
||||
`resourceLabel` VARCHAR(191) NULL,
|
||||
`endpoint` VARCHAR(191) NOT NULL,
|
||||
`httpMethod` VARCHAR(191) NOT NULL,
|
||||
`ipAddress` VARCHAR(191) NOT NULL,
|
||||
`userAgent` TEXT NULL,
|
||||
`changesBefore` LONGTEXT NULL,
|
||||
`changesAfter` LONGTEXT NULL,
|
||||
`changesEncrypted` BOOLEAN NOT NULL DEFAULT false,
|
||||
`dataSubjectId` INTEGER NULL,
|
||||
`legalBasis` VARCHAR(191) NULL,
|
||||
`success` BOOLEAN NOT NULL DEFAULT true,
|
||||
`errorMessage` TEXT NULL,
|
||||
`durationMs` INTEGER NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`hash` VARCHAR(191) NULL,
|
||||
`previousHash` VARCHAR(191) NULL,
|
||||
|
||||
INDEX `AuditLog_userId_idx`(`userId`),
|
||||
INDEX `AuditLog_customerId_idx`(`customerId`),
|
||||
INDEX `AuditLog_resourceType_resourceId_idx`(`resourceType`, `resourceId`),
|
||||
INDEX `AuditLog_dataSubjectId_idx`(`dataSubjectId`),
|
||||
INDEX `AuditLog_action_idx`(`action`),
|
||||
INDEX `AuditLog_createdAt_idx`(`createdAt`),
|
||||
INDEX `AuditLog_sensitivity_idx`(`sensitivity`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `CustomerConsent` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`consentType` ENUM('DATA_PROCESSING', 'MARKETING_EMAIL', 'MARKETING_PHONE', 'DATA_SHARING_PARTNER') NOT NULL,
|
||||
`status` ENUM('GRANTED', 'WITHDRAWN', 'PENDING') NOT NULL DEFAULT 'PENDING',
|
||||
`grantedAt` DATETIME(3) NULL,
|
||||
`withdrawnAt` DATETIME(3) NULL,
|
||||
`source` VARCHAR(191) NULL,
|
||||
`documentPath` VARCHAR(191) NULL,
|
||||
`version` VARCHAR(191) NULL,
|
||||
`ipAddress` VARCHAR(191) NULL,
|
||||
`createdBy` VARCHAR(191) NOT NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
INDEX `CustomerConsent_customerId_idx`(`customerId`),
|
||||
INDEX `CustomerConsent_consentType_idx`(`consentType`),
|
||||
INDEX `CustomerConsent_status_idx`(`status`),
|
||||
UNIQUE INDEX `CustomerConsent_customerId_consentType_key`(`customerId`, `consentType`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `DataDeletionRequest` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`customerId` INTEGER NOT NULL,
|
||||
`status` ENUM('PENDING', 'IN_PROGRESS', 'COMPLETED', 'PARTIALLY_COMPLETED', 'REJECTED') NOT NULL DEFAULT 'PENDING',
|
||||
`requestedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`requestSource` VARCHAR(191) NOT NULL,
|
||||
`requestedBy` VARCHAR(191) NOT NULL,
|
||||
`processedAt` DATETIME(3) NULL,
|
||||
`processedBy` VARCHAR(191) NULL,
|
||||
`deletedData` LONGTEXT NULL,
|
||||
`retainedData` LONGTEXT NULL,
|
||||
`retentionReason` TEXT NULL,
|
||||
`proofDocument` VARCHAR(191) NULL,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
INDEX `DataDeletionRequest_customerId_idx`(`customerId`),
|
||||
INDEX `DataDeletionRequest_status_idx`(`status`),
|
||||
INDEX `DataDeletionRequest_requestedAt_idx`(`requestedAt`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE `AuditRetentionPolicy` (
|
||||
`id` INTEGER NOT NULL AUTO_INCREMENT,
|
||||
`resourceType` VARCHAR(191) NOT NULL,
|
||||
`sensitivity` ENUM('LOW', 'MEDIUM', 'HIGH', 'CRITICAL') NULL,
|
||||
`retentionDays` INTEGER NOT NULL,
|
||||
`description` VARCHAR(191) NULL,
|
||||
`legalBasis` VARCHAR(191) NULL,
|
||||
`isActive` BOOLEAN NOT NULL DEFAULT true,
|
||||
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
`updatedAt` DATETIME(3) NOT NULL,
|
||||
|
||||
UNIQUE INDEX `AuditRetentionPolicy_resourceType_sensitivity_key`(`resourceType`, `sensitivity`),
|
||||
PRIMARY KEY (`id`)
|
||||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE `CustomerConsent` ADD CONSTRAINT `CustomerConsent_customerId_fkey` FOREIGN KEY (`customerId`) REFERENCES `Customer`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
|
||||
@@ -1,10 +0,0 @@
|
||||
-- AlterTable
|
||||
ALTER TABLE `Customer` ADD COLUMN `consentHash` VARCHAR(191) NULL;
|
||||
|
||||
-- AlterTable
|
||||
ALTER TABLE `User` ADD COLUMN `whatsappNumber` VARCHAR(191) NULL,
|
||||
ADD COLUMN `telegramUsername` VARCHAR(191) NULL,
|
||||
ADD COLUMN `signalNumber` VARCHAR(191) NULL;
|
||||
|
||||
-- CreateIndex
|
||||
CREATE UNIQUE INDEX `Customer_consentHash_key` ON `Customer`(`consentHash`);
|
||||
@@ -1,3 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "mysql"
|
||||
provider = "mysql"
|
||||
|
||||
@@ -172,6 +172,10 @@ model Customer {
|
||||
portalPasswordResetExpiresAt DateTime?
|
||||
// Portal Session-Invalidation (nach Passwort-Reset / Rechte-Änderung)
|
||||
portalTokenInvalidatedAt DateTime?
|
||||
// Einmalpasswort: gesetzt durch "Zugangsdaten versenden"-Button. Beim ersten
|
||||
// erfolgreichen Login wird der Hash sofort gelöscht (OTP verbraucht) und
|
||||
// Frontend in Force-Change-Password-Flow geleitet.
|
||||
portalPasswordMustChange Boolean @default(false)
|
||||
|
||||
// Geburtstagsmodal: Jahr in dem dem Kunden der Geburtstagsgruß gezeigt wurde (vermeidet mehrfaches Anzeigen)
|
||||
lastBirthdayGreetingYear Int?
|
||||
|
||||
@@ -15,7 +15,11 @@ import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
const ROOT = path.join(process.cwd(), 'factory-defaults');
|
||||
// ROOT kann via FACTORY_DEFAULTS_DIR überschrieben werden (Container-Bootstrap
|
||||
// mit eingebauten Defaults aus dem Image).
|
||||
const ROOT = process.env.FACTORY_DEFAULTS_DIR
|
||||
? path.resolve(process.env.FACTORY_DEFAULTS_DIR)
|
||||
: path.join(process.cwd(), 'factory-defaults');
|
||||
const UPLOADS_ROOT = path.join(process.cwd(), 'uploads');
|
||||
const PDF_UPLOAD_DIR = path.join(UPLOADS_ROOT, 'pdf-templates');
|
||||
|
||||
@@ -61,6 +65,19 @@ interface PdfTemplateDef {
|
||||
pdfFilename: string; // Dateiname im pdf-templates/-Ordner
|
||||
}
|
||||
|
||||
interface AppSettingDef {
|
||||
key: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
// Whitelist – muss synchron zu factoryDefaults.service.ts sein.
|
||||
const FACTORY_DEFAULT_APP_SETTING_KEYS = new Set([
|
||||
'privacyPolicyHtml',
|
||||
'authorizationTemplateHtml',
|
||||
'imprintHtml',
|
||||
'websitePrivacyPolicyHtml',
|
||||
]);
|
||||
|
||||
/**
|
||||
* Liest alle *.json Dateien aus einem Ordner und gibt die zusammengeführten Arrays zurück.
|
||||
*/
|
||||
@@ -299,6 +316,31 @@ async function seedPdfTemplates() {
|
||||
console.log(` ✓ PDF-Vorlagen: ${count}${skipped > 0 ? ` (${skipped} übersprungen)` : ''}`);
|
||||
}
|
||||
|
||||
async function seedAppSettings() {
|
||||
const items = readJsonArrays<AppSettingDef>(path.join(ROOT, 'app-settings'));
|
||||
if (items.length === 0) {
|
||||
console.log(' app-settings – keine Einträge');
|
||||
return;
|
||||
}
|
||||
let count = 0;
|
||||
let skipped = 0;
|
||||
for (const s of items) {
|
||||
if (!s.key || typeof s.value !== 'string') continue;
|
||||
if (!FACTORY_DEFAULT_APP_SETTING_KEYS.has(s.key)) {
|
||||
console.warn(` ⚠ AppSetting-Key '${s.key}' nicht auf Whitelist – übersprungen`);
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
await prisma.appSetting.upsert({
|
||||
where: { key: s.key },
|
||||
update: { value: s.value },
|
||||
create: { key: s.key, value: s.value },
|
||||
});
|
||||
count++;
|
||||
}
|
||||
console.log(` ✓ HTML-Templates: ${count}${skipped > 0 ? ` (${skipped} übersprungen)` : ''}`);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log('\n📦 Factory-Defaults werden eingespielt...\n');
|
||||
|
||||
@@ -313,6 +355,7 @@ async function main() {
|
||||
await seedContractDurations();
|
||||
await seedContractCategories();
|
||||
await seedPdfTemplates();
|
||||
await seedAppSettings();
|
||||
|
||||
console.log('\n✅ Factory-Defaults erfolgreich eingespielt.\n');
|
||||
}
|
||||
|
||||
@@ -1,8 +1,31 @@
|
||||
import { Request, Response } from 'express';
|
||||
import { Request, Response, CookieOptions } from 'express';
|
||||
import * as authService from '../services/auth.service.js';
|
||||
import { AuthRequest, ApiResponse } from '../types/index.js';
|
||||
import prisma from '../lib/prisma.js';
|
||||
import { emit as emitSecurityEvent, contextFromRequest } from '../services/securityMonitor.service.js';
|
||||
import { validatePasswordComplexity } from '../utils/passwordGenerator.js';
|
||||
|
||||
// Refresh-Token-Cookie-Konfiguration. Der Cookie:
|
||||
// - httpOnly → kein JavaScript-Zugriff → bei XSS nicht klaubar
|
||||
// - secure → nur über HTTPS (in Prod via HTTPS_ENABLED, in Dev egal)
|
||||
// - sameSite 'strict' → CSRF-Schutz; Cross-Site-Requests senden den Cookie nicht
|
||||
// - path '/api/auth' → wird nur an Auth-Endpoints mitgeschickt
|
||||
const REFRESH_COOKIE_NAME = 'refresh_token';
|
||||
function getRefreshCookieOptions(): CookieOptions {
|
||||
return {
|
||||
httpOnly: true,
|
||||
secure: process.env.HTTPS_ENABLED === 'true',
|
||||
sameSite: 'strict',
|
||||
path: '/api/auth',
|
||||
maxAge: 7 * 24 * 60 * 60 * 1000, // 7 Tage, gleicht Refresh-JWT-Lifetime
|
||||
};
|
||||
}
|
||||
function setRefreshCookie(res: Response, token: string): void {
|
||||
res.cookie(REFRESH_COOKIE_NAME, token, getRefreshCookieOptions());
|
||||
}
|
||||
function clearRefreshCookie(res: Response): void {
|
||||
res.clearCookie(REFRESH_COOKIE_NAME, { path: '/api/auth' });
|
||||
}
|
||||
|
||||
// Mitarbeiter-Login
|
||||
export async function login(req: Request, res: Response): Promise<void> {
|
||||
@@ -18,6 +41,9 @@ export async function login(req: Request, res: Response): Promise<void> {
|
||||
}
|
||||
|
||||
const result = await authService.login(email, password);
|
||||
// Refresh-Token in httpOnly-Cookie, Access-Token im Body (Frontend hält
|
||||
// ihn nur in memory). `token`-Feld bleibt aus Kompatibilität bestehen.
|
||||
setRefreshCookie(res, result.refreshToken);
|
||||
emitSecurityEvent({
|
||||
type: 'LOGIN_SUCCESS',
|
||||
severity: 'INFO',
|
||||
@@ -27,7 +53,10 @@ export async function login(req: Request, res: Response): Promise<void> {
|
||||
userEmail: email,
|
||||
endpoint: ctx.endpoint,
|
||||
});
|
||||
res.json({ success: true, data: result } as ApiResponse);
|
||||
res.json({
|
||||
success: true,
|
||||
data: { token: result.accessToken, user: result.user },
|
||||
} as ApiResponse);
|
||||
} catch (error) {
|
||||
emitSecurityEvent({
|
||||
type: 'LOGIN_FAILED',
|
||||
@@ -58,6 +87,7 @@ export async function customerLogin(req: Request, res: Response): Promise<void>
|
||||
}
|
||||
|
||||
const result = await authService.customerLogin(email, password);
|
||||
setRefreshCookie(res, result.refreshToken);
|
||||
emitSecurityEvent({
|
||||
type: 'LOGIN_SUCCESS',
|
||||
severity: 'INFO',
|
||||
@@ -67,7 +97,10 @@ export async function customerLogin(req: Request, res: Response): Promise<void>
|
||||
userEmail: email,
|
||||
endpoint: ctx.endpoint,
|
||||
});
|
||||
res.json({ success: true, data: result } as ApiResponse);
|
||||
res.json({
|
||||
success: true,
|
||||
data: { token: result.accessToken, user: result.user },
|
||||
} as ApiResponse);
|
||||
} catch (error) {
|
||||
emitSecurityEvent({
|
||||
type: 'LOGIN_FAILED',
|
||||
@@ -191,10 +224,11 @@ export async function confirmPasswordReset(req: Request, res: Response): Promise
|
||||
return;
|
||||
}
|
||||
|
||||
if (password.length < 6) {
|
||||
const complexity = validatePasswordComplexity(password);
|
||||
if (!complexity.ok) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Das Passwort muss mindestens 6 Zeichen lang sein',
|
||||
error: 'Passwort erfüllt Mindestanforderungen nicht: ' + complexity.errors.join(', '),
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
@@ -257,6 +291,10 @@ export async function logout(req: AuthRequest, res: Response): Promise<void> {
|
||||
data: { tokenInvalidatedAt: new Date() },
|
||||
});
|
||||
}
|
||||
// Refresh-Cookie löschen, sonst könnte der Browser einen abgemeldeten User
|
||||
// direkt wieder einloggen (server-seitige Invalidation oben fängt das ab,
|
||||
// aber UI würde sich verirren).
|
||||
clearRefreshCookie(res);
|
||||
const ctx = contextFromRequest(req);
|
||||
emitSecurityEvent({
|
||||
type: 'LOGOUT',
|
||||
@@ -277,6 +315,36 @@ export async function logout(req: AuthRequest, res: Response): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
// Neuen Access-Token aus dem httpOnly-Refresh-Cookie holen. Wird vom Frontend
|
||||
// (axios-Interceptor) bei 401 oder beim App-Start aufgerufen.
|
||||
export async function refresh(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const cookies = (req as any).cookies || {};
|
||||
const refreshToken = cookies[REFRESH_COOKIE_NAME];
|
||||
if (!refreshToken) {
|
||||
res.status(401).json({ success: false, error: 'Kein Refresh-Token vorhanden' } as ApiResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
const result = await authService.refreshAccessToken(refreshToken);
|
||||
// Refresh-Cookie rotieren – verhindert Replay eines geklauten Refresh-Tokens
|
||||
// bis zur vollen Lifetime.
|
||||
setRefreshCookie(res, result.refreshToken);
|
||||
res.json({
|
||||
success: true,
|
||||
data: { token: result.accessToken, user: result.user },
|
||||
} as ApiResponse);
|
||||
} catch (error) {
|
||||
// Refresh fehlgeschlagen: Cookie wegputzen, damit der Browser nicht
|
||||
// weiter mit einem invaliden Token weiterhin den Endpoint klopft.
|
||||
clearRefreshCookie(res);
|
||||
res.status(401).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Refresh fehlgeschlagen',
|
||||
} as ApiResponse);
|
||||
}
|
||||
}
|
||||
|
||||
export async function register(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { email, password, firstName, lastName, roleIds } = req.body;
|
||||
@@ -289,6 +357,15 @@ export async function register(req: Request, res: Response): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
const complexity = validatePasswordComplexity(password);
|
||||
if (!complexity.ok) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Passwort erfüllt Mindestanforderungen nicht: ' + complexity.errors.join(', '),
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
const user = await authService.createUser({
|
||||
email,
|
||||
password,
|
||||
@@ -308,3 +385,42 @@ export async function register(req: Request, res: Response): Promise<void> {
|
||||
} as ApiResponse);
|
||||
}
|
||||
}
|
||||
|
||||
// Vom Endkunden selbst nach Einmalpasswort-Login aufgerufen, um sein eigenes
|
||||
// Passwort zu vergeben. Server invalidiert die laufende Session, Frontend
|
||||
// loggt aus und schickt zurück zum Login.
|
||||
export async function changeInitialPortalPassword(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
if (!req.user?.isCustomerPortal || !req.user?.customerId) {
|
||||
res.status(403).json({
|
||||
success: false,
|
||||
error: 'Nur für Kundenportal-Login',
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
const { newPassword } = req.body || {};
|
||||
if (!newPassword || typeof newPassword !== 'string') {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Neues Passwort erforderlich',
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
const complexity = validatePasswordComplexity(newPassword);
|
||||
if (!complexity.ok) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Passwort erfüllt Mindestanforderungen nicht: ' + complexity.errors.join(', '),
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
await authService.changeInitialPortalPassword(req.user.customerId, newPassword);
|
||||
clearRefreshCookie(res);
|
||||
res.json({ success: true, message: 'Passwort geändert' } as ApiResponse);
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Passwort konnte nicht geändert werden',
|
||||
} as ApiResponse);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import { sendEmail, SmtpCredentials, SendEmailParams, EmailAttachment } from '..
|
||||
import { fetchAttachment, appendToSent, ImapCredentials } from '../services/imapService.js';
|
||||
import { getImapSmtpSettings } from '../services/emailProvider/emailProviderService.js';
|
||||
import { decrypt } from '../utils/encryption.js';
|
||||
import { logChange } from '../services/audit.service.js';
|
||||
import { ApiResponse } from '../types/index.js';
|
||||
import { getCustomerTargets, getContractTargets, getIdentityDocumentTargets, getBankCardTargets, documentTargets } from '../config/documentTargets.config.js';
|
||||
import { generateEmailPdf } from '../services/pdfService.js';
|
||||
@@ -21,10 +22,24 @@ import {
|
||||
canAccessCustomer,
|
||||
canAccessContract,
|
||||
canAccessCachedEmail,
|
||||
canAccessStressfreiEmail,
|
||||
} from '../utils/accessControl.js';
|
||||
|
||||
// ==================== E-MAIL LIST ====================
|
||||
|
||||
// Hilfsfunktion: Query-Param zu boolean parsen ('true' / 'false' / fehlt).
|
||||
function parseBoolParam(v: unknown): boolean | undefined {
|
||||
if (v === 'true') return true;
|
||||
if (v === 'false') return false;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function parseDateParam(v: unknown): Date | undefined {
|
||||
if (typeof v !== 'string' || !v.trim()) return undefined;
|
||||
const d = new Date(v);
|
||||
return isNaN(d.getTime()) ? undefined : d;
|
||||
}
|
||||
|
||||
// E-Mails für einen Kunden abrufen
|
||||
export async function getEmailsForCustomer(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
@@ -42,6 +57,17 @@ export async function getEmailsForCustomer(req: AuthRequest, res: Response): Pro
|
||||
limit,
|
||||
offset,
|
||||
includeBody: false,
|
||||
search: typeof req.query.search === 'string' ? req.query.search : undefined,
|
||||
fromFilter: typeof req.query.fromFilter === 'string' ? req.query.fromFilter : undefined,
|
||||
toFilter: typeof req.query.toFilter === 'string' ? req.query.toFilter : undefined,
|
||||
subjectFilter: typeof req.query.subjectFilter === 'string' ? req.query.subjectFilter : undefined,
|
||||
bodyFilter: typeof req.query.bodyFilter === 'string' ? req.query.bodyFilter : undefined,
|
||||
attachmentNameFilter: typeof req.query.attachmentNameFilter === 'string' ? req.query.attachmentNameFilter : undefined,
|
||||
hasAttachments: parseBoolParam(req.query.hasAttachments),
|
||||
isRead: parseBoolParam(req.query.isRead),
|
||||
isStarred: parseBoolParam(req.query.isStarred),
|
||||
receivedFrom: parseDateParam(req.query.receivedFrom),
|
||||
receivedTo: parseDateParam(req.query.receivedTo),
|
||||
});
|
||||
|
||||
res.json({ success: true, data: emails } as ApiResponse);
|
||||
@@ -189,9 +215,10 @@ export async function unassignFromContract(req: Request, res: Response): Promise
|
||||
}
|
||||
|
||||
// E-Mail-Anzahl pro Ordner für ein Konto
|
||||
export async function getFolderCounts(req: Request, res: Response): Promise<void> {
|
||||
export async function getFolderCounts(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const stressfreiEmailId = parseInt(req.params.id);
|
||||
if (!(await canAccessStressfreiEmail(req, res, stressfreiEmailId))) return;
|
||||
|
||||
const counts = await cachedEmailService.getFolderCountsForAccount(stressfreiEmailId);
|
||||
|
||||
@@ -225,9 +252,10 @@ export async function getContractFolderCounts(req: Request, res: Response): Prom
|
||||
// ==================== SYNC & SEND ====================
|
||||
|
||||
// E-Mails für ein Konto synchronisieren (INBOX + SENT)
|
||||
export async function syncAccount(req: Request, res: Response): Promise<void> {
|
||||
export async function syncAccount(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const stressfreiEmailId = parseInt(req.params.id);
|
||||
if (!(await canAccessStressfreiEmail(req, res, stressfreiEmailId))) return;
|
||||
const fullSync = req.query.full === 'true';
|
||||
|
||||
// Synchronisiert sowohl INBOX als auch SENT
|
||||
@@ -267,9 +295,10 @@ function hasCRLF(value: unknown): boolean {
|
||||
}
|
||||
|
||||
// E-Mail senden
|
||||
export async function sendEmailFromAccount(req: Request, res: Response): Promise<void> {
|
||||
export async function sendEmailFromAccount(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const stressfreiEmailId = parseInt(req.params.id);
|
||||
if (!(await canAccessStressfreiEmail(req, res, stressfreiEmailId))) return;
|
||||
const { to, cc, subject, text, html, inReplyTo, references, attachments, contractId } = req.body;
|
||||
|
||||
// Header-Injection (CRLF) in Empfänger/Betreff ablehnen
|
||||
@@ -599,9 +628,10 @@ export async function getMailboxAccounts(req: Request, res: Response): Promise<v
|
||||
}
|
||||
|
||||
// Mailbox nachträglich aktivieren
|
||||
export async function enableMailbox(req: Request, res: Response): Promise<void> {
|
||||
export async function enableMailbox(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const id = parseInt(req.params.id);
|
||||
if (!(await canAccessStressfreiEmail(req, res, id))) return;
|
||||
|
||||
const result = await stressfreiEmailService.enableMailbox(id);
|
||||
|
||||
@@ -624,9 +654,10 @@ export async function enableMailbox(req: Request, res: Response): Promise<void>
|
||||
}
|
||||
|
||||
// Mailbox-Status mit Provider synchronisieren
|
||||
export async function syncMailboxStatus(req: Request, res: Response): Promise<void> {
|
||||
export async function syncMailboxStatus(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const id = parseInt(req.params.id);
|
||||
if (!(await canAccessStressfreiEmail(req, res, id))) return;
|
||||
|
||||
const result = await stressfreiEmailService.syncMailboxStatus(id);
|
||||
|
||||
@@ -672,9 +703,13 @@ export async function getThread(req: Request, res: Response): Promise<void> {
|
||||
}
|
||||
|
||||
// Mailbox-Zugangsdaten abrufen (IMAP/SMTP)
|
||||
export async function getMailboxCredentials(req: Request, res: Response): Promise<void> {
|
||||
export async function getMailboxCredentials(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const id = parseInt(req.params.id);
|
||||
// Ownership-Check: ohne diesen Check konnte ein Portal-Kunde mit
|
||||
// bekannter Stressfrei-Email-ID die kompletten IMAP/SMTP-Credentials
|
||||
// eines anderen Kunden abrufen (IDOR). Pentest-Finding 2026-05-XX.
|
||||
if (!(await canAccessStressfreiEmail(req, res, id))) return;
|
||||
|
||||
// StressfreiEmail laden
|
||||
const stressfreiEmail = await stressfreiEmailService.getEmailWithMailboxById(id);
|
||||
@@ -709,6 +744,15 @@ export async function getMailboxCredentials(req: Request, res: Response): Promis
|
||||
// IMAP/SMTP-Einstellungen laden
|
||||
const settings = await getImapSmtpSettings();
|
||||
|
||||
// Klartext-Mailbox-Passwort-Read auditieren (CRITICAL)
|
||||
await logChange({
|
||||
req,
|
||||
action: 'READ',
|
||||
resourceType: 'MailboxCredentials',
|
||||
resourceId: id.toString(),
|
||||
label: `Klartext-Mailbox-Zugangsdaten von ${stressfreiEmail.email} entschlüsselt`,
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
|
||||
@@ -256,6 +256,58 @@ export async function createFollowUp(req: AuthRequest, res: Response): Promise<v
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* VVL = Vertragsverlängerung beim selben Anbieter.
|
||||
* Erstellt einen neuen Vertrag mit allen Daten des Vorgängers (außer
|
||||
* Auftragsdokument), Startdatum = altes Start + Vertragslaufzeit.
|
||||
*/
|
||||
export async function createRenewal(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const previousContractId = parseInt(req.params.id);
|
||||
|
||||
const previousContract = await prisma.contract.findUnique({
|
||||
where: { id: previousContractId },
|
||||
select: { contractNumber: true },
|
||||
});
|
||||
if (!previousContract) {
|
||||
res.status(404).json({ success: false, error: 'Vorgängervertrag nicht gefunden' } as ApiResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
const contract = await contractService.createRenewalContract(previousContractId);
|
||||
if (!contract) {
|
||||
res.status(500).json({ success: false, error: 'VVL konnte nicht erstellt werden' } as ApiResponse);
|
||||
return;
|
||||
}
|
||||
const createdBy = req.user?.email || 'unbekannt';
|
||||
|
||||
await contractHistoryService.createRenewalHistoryEntry(
|
||||
previousContractId,
|
||||
contract.contractNumber,
|
||||
createdBy,
|
||||
);
|
||||
await contractHistoryService.createNewRenewalFromPredecessorEntry(
|
||||
contract.id,
|
||||
previousContract.contractNumber,
|
||||
createdBy,
|
||||
);
|
||||
|
||||
await logChange({
|
||||
req, action: 'CREATE', resourceType: 'Contract',
|
||||
resourceId: contract.id.toString(),
|
||||
label: `VVL erstellt für ${previousContract.contractNumber}`,
|
||||
customerId: contract.customerId,
|
||||
});
|
||||
|
||||
res.status(201).json({ success: true, data: contract } as ApiResponse);
|
||||
} catch (error) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Erstellen der VVL',
|
||||
} as ApiResponse);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getContractPassword(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const contractId = parseInt(req.params.id);
|
||||
@@ -269,6 +321,14 @@ export async function getContractPassword(req: AuthRequest, res: Response): Prom
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
// Klartext-Passwort-Read auditieren (CRITICAL)
|
||||
await logChange({
|
||||
req,
|
||||
action: 'READ',
|
||||
resourceType: 'ContractPassword',
|
||||
resourceId: contractId.toString(),
|
||||
label: `Klartext-Anbieter-Passwort von Vertrag #${contractId} entschlüsselt`,
|
||||
});
|
||||
res.json({ success: true, data: { password } } as ApiResponse);
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
@@ -293,6 +353,14 @@ export async function getSimCardCredentials(req: AuthRequest, res: Response): Pr
|
||||
if (!(await canAccessContract(req, res, sim.mobileDetails.contractId))) return;
|
||||
|
||||
const credentials = await contractService.getSimCardCredentials(simCardId);
|
||||
// Klartext-Read (PIN/PUK) auditieren (CRITICAL)
|
||||
await logChange({
|
||||
req,
|
||||
action: 'READ',
|
||||
resourceType: 'SimCardCredentials',
|
||||
resourceId: simCardId.toString(),
|
||||
label: `Klartext-SIM-Karten-PIN/PUK von SIM #${simCardId} (Vertrag #${sim.mobileDetails.contractId}) entschlüsselt`,
|
||||
});
|
||||
res.json({ success: true, data: credentials } as ApiResponse);
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
@@ -308,6 +376,14 @@ export async function getInternetCredentials(req: AuthRequest, res: Response): P
|
||||
if (!(await canAccessContract(req, res, contractId))) return;
|
||||
|
||||
const credentials = await contractService.getInternetCredentials(contractId);
|
||||
// Klartext-DSL/Internet-Login auditieren (CRITICAL)
|
||||
await logChange({
|
||||
req,
|
||||
action: 'READ',
|
||||
resourceType: 'InternetCredentials',
|
||||
resourceId: contractId.toString(),
|
||||
label: `Klartext-Internet-Zugangsdaten von Vertrag #${contractId} entschlüsselt`,
|
||||
});
|
||||
res.json({ success: true, data: credentials } as ApiResponse);
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
@@ -332,6 +408,14 @@ export async function getSipCredentials(req: AuthRequest, res: Response): Promis
|
||||
if (!(await canAccessContract(req, res, phone.internetDetails.contractId))) return;
|
||||
|
||||
const credentials = await contractService.getSipCredentials(phoneNumberId);
|
||||
// Klartext-SIP/Telefon-Login auditieren (CRITICAL)
|
||||
await logChange({
|
||||
req,
|
||||
action: 'READ',
|
||||
resourceType: 'SipCredentials',
|
||||
resourceId: phoneNumberId.toString(),
|
||||
label: `Klartext-SIP-Zugangsdaten von Rufnummer #${phoneNumberId} (Vertrag #${phone.internetDetails.contractId}) entschlüsselt`,
|
||||
});
|
||||
res.json({ success: true, data: credentials } as ApiResponse);
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
|
||||
@@ -3,6 +3,7 @@ import prisma from '../lib/prisma.js';
|
||||
import * as customerService from '../services/customer.service.js';
|
||||
import * as authService from '../services/auth.service.js';
|
||||
import { logChange } from '../services/audit.service.js';
|
||||
import { validatePasswordComplexity, generateSecurePassword } from '../utils/passwordGenerator.js';
|
||||
import { ApiResponse, AuthRequest } from '../types/index.js';
|
||||
import {
|
||||
sanitizeCustomer,
|
||||
@@ -957,13 +958,115 @@ export async function updatePortalSettings(req: Request, res: Response): Promise
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generiert ein zufälliges, komplexes Passwort (16 Zeichen, gemischt).
|
||||
* Setzt es NICHT direkt — wird im Frontend in den Setzen-Button-Flow gefüttert.
|
||||
* Damit hat der Admin Wahlfreiheit (Generieren → ggf. anpassen → speichern).
|
||||
*/
|
||||
export async function generatePortalPassword(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const password = generateSecurePassword({ length: 16 });
|
||||
res.json({ success: true, data: { password } } as ApiResponse);
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Generieren des Passworts',
|
||||
} as ApiResponse);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verschickt die Portal-Zugangsdaten per E-Mail an die hinterlegte
|
||||
* `email` (bevorzugt) oder fallback auf `portalEmail` des Kunden. Das
|
||||
* Passwort wird aus dem `portalPasswordEncrypted`-Feld entschlüsselt
|
||||
* (= das aktuell aktive Klartext-Passwort, das auch in der UI angezeigt wird).
|
||||
*/
|
||||
export async function sendPortalCredentials(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const customerId = parseInt(req.params.customerId);
|
||||
const customer = await prisma.customer.findUnique({
|
||||
where: { id: customerId },
|
||||
select: {
|
||||
id: true, firstName: true, lastName: true, salutation: true, companyName: true,
|
||||
email: true, portalEmail: true, portalEnabled: true,
|
||||
portalPasswordEncrypted: true, portalPasswordHash: true,
|
||||
},
|
||||
});
|
||||
if (!customer) {
|
||||
res.status(404).json({ success: false, error: 'Kunde nicht gefunden' } as ApiResponse);
|
||||
return;
|
||||
}
|
||||
if (!customer.portalEnabled) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Portal ist für diesen Kunden nicht aktiviert',
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
if (!customer.portalPasswordHash) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Es ist noch kein Portal-Passwort gesetzt',
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
const targetEmail = customer.email || customer.portalEmail;
|
||||
if (!targetEmail) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Kunde hat keine E-Mail-Adresse hinterlegt',
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
const loginEmail = customer.portalEmail || customer.email!;
|
||||
const plaintextPassword = await authService.getCustomerPortalPassword(customerId);
|
||||
if (!plaintextPassword) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Klartext-Passwort nicht verfügbar (alte Anlage ohne Encrypted-Feld – bitte neu setzen)',
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
await authService.sendPortalCredentialsEmail({
|
||||
to: targetEmail,
|
||||
customer,
|
||||
loginEmail,
|
||||
password: plaintextPassword,
|
||||
});
|
||||
|
||||
// Versendetes Passwort ist ein Einmalpasswort → beim ersten Login muss
|
||||
// der Kunde sich ein eigenes setzen.
|
||||
await authService.markPortalPasswordForChange(customerId);
|
||||
|
||||
await logChange({
|
||||
req,
|
||||
action: 'UPDATE',
|
||||
resourceType: 'PortalSettings',
|
||||
resourceId: customerId.toString(),
|
||||
label: `Portal-Zugangsdaten per E-Mail versendet an ${targetEmail} (Einmalpasswort)`,
|
||||
customerId,
|
||||
});
|
||||
|
||||
res.json({ success: true, message: `Zugangsdaten an ${targetEmail} versendet (Einmalpasswort)` } as ApiResponse);
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Versenden der Zugangsdaten',
|
||||
} as ApiResponse);
|
||||
}
|
||||
}
|
||||
|
||||
export async function setPortalPassword(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const { password } = req.body;
|
||||
if (!password || password.length < 6) {
|
||||
// Komplexität: 12 Zeichen, Groß/Klein/Zahl/Sonderzeichen (zentrale Regel)
|
||||
const complexity = validatePasswordComplexity(password);
|
||||
if (!complexity.ok) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Passwort muss mindestens 6 Zeichen lang sein',
|
||||
error: 'Passwort erfüllt Mindestanforderungen nicht: ' + complexity.errors.join(', '),
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
@@ -986,7 +1089,19 @@ export async function setPortalPassword(req: Request, res: Response): Promise<vo
|
||||
|
||||
export async function getPortalPassword(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
const password = await authService.getCustomerPortalPassword(parseInt(req.params.customerId));
|
||||
const customerId = parseInt(req.params.customerId);
|
||||
const password = await authService.getCustomerPortalPassword(customerId);
|
||||
// Klartext-Passwort-Read auditieren (CRITICAL): wer hat wann das Portal-
|
||||
// Passwort eines Kunden entschlüsselt? Wichtig für DSGVO-Nachvollziehbarkeit
|
||||
// + Insider-Threat-Erkennung.
|
||||
await logChange({
|
||||
req,
|
||||
action: 'READ',
|
||||
resourceType: 'PortalPassword',
|
||||
resourceId: customerId.toString(),
|
||||
label: `Klartext-Portal-Passwort von Kunde #${customerId} entschlüsselt`,
|
||||
customerId,
|
||||
});
|
||||
res.json({ success: true, data: { password } } as ApiResponse);
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
|
||||
@@ -54,6 +54,7 @@ export async function previewFactoryDefaults(req: AuthRequest, res: Response) {
|
||||
contractDurations: data.contractDurations.length,
|
||||
contractCategories: data.contractCategories.length,
|
||||
pdfTemplates: data.pdfTemplates.length,
|
||||
appSettings: data.appSettings.length,
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -62,3 +63,39 @@ export async function previewFactoryDefaults(req: AuthRequest, res: Response) {
|
||||
res.status(500).json({ success: false, error: 'Fehler beim Laden' });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory-Defaults aus ZIP importieren (Upload via multipart/form-data, Feld 'zip').
|
||||
* Idempotent: bestehende Einträge werden per unique-Key aktualisiert, nichts wird gelöscht.
|
||||
*/
|
||||
export async function importFactoryDefaults(req: AuthRequest, res: Response) {
|
||||
try {
|
||||
const file = (req as any).file as Express.Multer.File | undefined;
|
||||
if (!file || !file.buffer) {
|
||||
return res.status(400).json({ success: false, error: 'Keine ZIP-Datei hochgeladen' });
|
||||
}
|
||||
|
||||
const result = await factoryDefaultsService.importFactoryDefaults(file.buffer);
|
||||
|
||||
await createAuditLog({
|
||||
userId: req.user?.userId,
|
||||
userEmail: req.user?.email || 'unknown',
|
||||
// 'UPDATE' weil Factory-Defaults DB-Records upserted; das Label nennt
|
||||
// den Vorgang explizit als Import.
|
||||
action: 'UPDATE',
|
||||
resourceType: 'FactoryDefaults',
|
||||
resourceLabel: `Factory-Defaults importiert: ${result.providers} Anbieter, ${result.tariffs} Tarife, ${result.pdfTemplates} PDF-Vorlagen, ${result.appSettings} HTML-Templates`,
|
||||
endpoint: req.path,
|
||||
httpMethod: req.method,
|
||||
ipAddress: req.socket.remoteAddress || 'unknown',
|
||||
});
|
||||
|
||||
res.json({ success: true, data: result });
|
||||
} catch (error) {
|
||||
console.error('Fehler beim Factory-Defaults-Import:', error);
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Import',
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,9 +68,11 @@ export async function createEmail(req: Request, res: Response): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateEmail(req: Request, res: Response): Promise<void> {
|
||||
export async function updateEmail(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const email = await stressfreiEmailService.updateEmail(parseInt(req.params.id), req.body);
|
||||
const emailId = parseInt(req.params.id);
|
||||
if (!(await canAccessStressfreiEmail(req, res, emailId))) return;
|
||||
const email = await stressfreiEmailService.updateEmail(emailId, req.body);
|
||||
await logChange({
|
||||
req, action: 'UPDATE', resourceType: 'StressfreiEmail',
|
||||
resourceId: email.id.toString(),
|
||||
@@ -85,9 +87,10 @@ export async function updateEmail(req: Request, res: Response): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteEmail(req: Request, res: Response): Promise<void> {
|
||||
export async function deleteEmail(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const emailId = parseInt(req.params.id);
|
||||
if (!(await canAccessStressfreiEmail(req, res, emailId))) return;
|
||||
await stressfreiEmailService.deleteEmail(emailId);
|
||||
await logChange({
|
||||
req, action: 'DELETE', resourceType: 'StressfreiEmail',
|
||||
@@ -103,9 +106,50 @@ export async function deleteEmail(req: Request, res: Response): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
export async function resetPassword(req: Request, res: Response): Promise<void> {
|
||||
export async function syncForwarding(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const result = await stressfreiEmailService.resetMailboxPassword(parseInt(req.params.id));
|
||||
const emailId = parseInt(req.params.id);
|
||||
if (!(await canAccessStressfreiEmail(req, res, emailId))) return;
|
||||
|
||||
const result = await stressfreiEmailService.syncForwardingForEmail(emailId);
|
||||
if (!result.success) {
|
||||
res.status(400).json({ success: false, error: result.error } as ApiResponse);
|
||||
return;
|
||||
}
|
||||
|
||||
const labelParts = [`Weiterleitungen: ${(result.forwardTargets || []).join(', ')}`];
|
||||
if (result.passwordReset) labelParts.push('Mailbox-Passwort am Provider neu gesetzt');
|
||||
|
||||
await logChange({
|
||||
req,
|
||||
action: 'UPDATE',
|
||||
resourceType: 'StressfreiEmail',
|
||||
resourceId: emailId.toString(),
|
||||
label: `Stressfrei-Sync: ${labelParts.join(' | ')}`,
|
||||
});
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
forwardTargets: result.forwardTargets,
|
||||
customerEmail: result.customerEmail,
|
||||
passwordReset: result.passwordReset,
|
||||
},
|
||||
message: 'Weiterleitungen aktualisiert',
|
||||
} as ApiResponse);
|
||||
} catch (error) {
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : 'Fehler beim Synchronisieren der Weiterleitungen',
|
||||
} as ApiResponse);
|
||||
}
|
||||
}
|
||||
|
||||
export async function resetPassword(req: AuthRequest, res: Response): Promise<void> {
|
||||
try {
|
||||
const emailId = parseInt(req.params.id);
|
||||
if (!(await canAccessStressfreiEmail(req, res, emailId))) return;
|
||||
const result = await stressfreiEmailService.resetMailboxPassword(emailId);
|
||||
if (!result.success) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
|
||||
@@ -4,6 +4,7 @@ import * as userService from '../services/user.service.js';
|
||||
import { logChange } from '../services/audit.service.js';
|
||||
import { ApiResponse } from '../types/index.js';
|
||||
import { pickUserCreate, pickUserUpdate } from '../utils/sanitize.js';
|
||||
import { validatePasswordComplexity } from '../utils/passwordGenerator.js';
|
||||
|
||||
// Users
|
||||
export async function getUsers(req: Request, res: Response): Promise<void> {
|
||||
@@ -51,7 +52,18 @@ export async function getUser(req: Request, res: Response): Promise<void> {
|
||||
export async function createUser(req: Request, res: Response): Promise<void> {
|
||||
try {
|
||||
// Whitelist: nur erlaubte Felder aus req.body übernehmen (Mass-Assignment-Schutz)
|
||||
const user = await userService.createUser(pickUserCreate(req.body) as any);
|
||||
const data = pickUserCreate(req.body) as any;
|
||||
if (data?.password) {
|
||||
const c = validatePasswordComplexity(data.password);
|
||||
if (!c.ok) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Passwort erfüllt Mindestanforderungen nicht: ' + c.errors.join(', '),
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
}
|
||||
const user = await userService.createUser(data);
|
||||
await logChange({
|
||||
req, action: 'CREATE', resourceType: 'User',
|
||||
resourceId: user.id.toString(),
|
||||
@@ -71,9 +83,30 @@ export async function updateUser(req: Request, res: Response): Promise<void> {
|
||||
const userId = parseInt(req.params.id);
|
||||
// Whitelist: nur erlaubte Felder aus req.body übernehmen (Mass-Assignment-Schutz)
|
||||
const data = pickUserUpdate(req.body);
|
||||
if ((data as any)?.password) {
|
||||
const c = validatePasswordComplexity((data as any).password);
|
||||
if (!c.ok) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: 'Passwort erfüllt Mindestanforderungen nicht: ' + c.errors.join(', '),
|
||||
} as ApiResponse);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Vorherigen Stand laden für Audit
|
||||
const before = await prisma.user.findUnique({ where: { id: userId } });
|
||||
// Vorherigen Stand laden für Audit – inkl. Rollen, damit hasGdprAccess /
|
||||
// hasDeveloperAccess (versteckte Rollen) korrekt verglichen werden.
|
||||
const beforeUser = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
include: { roles: { include: { role: true } } },
|
||||
});
|
||||
const before = beforeUser
|
||||
? {
|
||||
...beforeUser,
|
||||
hasGdprAccess: beforeUser.roles.some((ur) => ur.role.name === 'DSGVO'),
|
||||
hasDeveloperAccess: beforeUser.roles.some((ur) => ur.role.name === 'Developer'),
|
||||
}
|
||||
: null;
|
||||
|
||||
const user = await userService.updateUser(userId, data as any);
|
||||
if (user) {
|
||||
@@ -82,6 +115,7 @@ export async function updateUser(req: Request, res: Response): Promise<void> {
|
||||
const changes: Record<string, { von: unknown; nach: unknown }> = {};
|
||||
const fieldLabels: Record<string, string> = {
|
||||
email: 'E-Mail', firstName: 'Vorname', lastName: 'Nachname', isActive: 'Aktiv',
|
||||
hasGdprAccess: 'DSGVO-Zugriff', hasDeveloperAccess: 'Entwicklerzugriff',
|
||||
};
|
||||
for (const [key, newVal] of Object.entries(data)) {
|
||||
if (['id', 'createdAt', 'updatedAt'].includes(key)) continue;
|
||||
|
||||
+176
-17
@@ -1,8 +1,34 @@
|
||||
import express from 'express';
|
||||
import cookieParser from 'cookie-parser';
|
||||
import cors from 'cors';
|
||||
import helmet from 'helmet';
|
||||
import path from 'path';
|
||||
import dotenv from 'dotenv';
|
||||
import dotenvExpand from 'dotenv-expand';
|
||||
|
||||
// .env-Dateien laden – Root-.env hat Priorität (zentrale Konfiguration für
|
||||
// Dev + Docker), backend/.env als Legacy-Fallback. Im Container sind
|
||||
// Variablen schon via env_file/environment gesetzt – dotenv überschreibt
|
||||
// existierende process.env-Werte nicht.
|
||||
// __dirname zeigt auf src/ (dev via tsx) oder dist/ (build). In beiden Fällen
|
||||
// liegt Root /.env zwei Ebenen darüber.
|
||||
//
|
||||
// dotenvExpand löst ${VAR}-Substitution auf, sodass z.B.
|
||||
// DATABASE_URL=mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
|
||||
// dynamisch aus den Komponenten zusammengebaut wird (kein Doppel-Pflegen).
|
||||
dotenvExpand.expand(dotenv.config({ path: path.resolve(__dirname, '../../.env') }));
|
||||
dotenvExpand.expand(dotenv.config({ path: path.resolve(__dirname, '../.env') }));
|
||||
dotenvExpand.expand(dotenv.config());
|
||||
|
||||
// Fallback: wenn DATABASE_URL nicht direkt gesetzt ist (oder Substitution
|
||||
// nicht funktioniert hat), aus den DB_*-Komponenten zusammenbauen.
|
||||
if (!process.env.DATABASE_URL && process.env.DB_USER && process.env.DB_PASSWORD && process.env.DB_NAME) {
|
||||
const u = encodeURIComponent(process.env.DB_USER);
|
||||
const p = encodeURIComponent(process.env.DB_PASSWORD);
|
||||
const h = process.env.DB_HOST || 'localhost';
|
||||
const port = process.env.DB_PORT || '3306';
|
||||
process.env.DATABASE_URL = `mysql://${u}:${p}@${h}:${port}/${process.env.DB_NAME}`;
|
||||
}
|
||||
|
||||
import authRoutes from './routes/auth.routes.js';
|
||||
import customerRoutes from './routes/customer.routes.js';
|
||||
@@ -43,8 +69,6 @@ import { auditContextMiddleware } from './middleware/auditContext.js';
|
||||
import { auditMiddleware } from './middleware/audit.js';
|
||||
import { authenticate } from './middleware/auth.js';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
// ==================== SECURITY: Pflicht-Umgebungsvariablen prüfen ====================
|
||||
if (!process.env.JWT_SECRET || process.env.JWT_SECRET.length < 32) {
|
||||
console.error('❌ JWT_SECRET ist nicht gesetzt oder zu kurz (min. 32 Zeichen)');
|
||||
@@ -60,25 +84,124 @@ if (!process.env.ENCRYPTION_KEY || process.env.ENCRYPTION_KEY.length !== 64) {
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3001;
|
||||
|
||||
// Hinter einem Reverse-Proxy (Nginx/Plesk) läuft der Server typisch auf localhost.
|
||||
// `trust proxy = 'loopback'` vertraut nur Connections von 127.0.0.1 / ::1
|
||||
// (= lokaler Reverse-Proxy). Damit kann ein Angreifer mit DIREKTEM Zugriff
|
||||
// auf das Backend nicht via X-Forwarded-For den Rate-Limiter umgehen,
|
||||
// während gleichzeitig der lokale Reverse-Proxy die echte Client-IP liefern darf.
|
||||
// Trust-Proxy-Konfiguration für `req.ip` und `X-Forwarded-For`.
|
||||
//
|
||||
// WICHTIG für Production: Backend nur auf 127.0.0.1 lauschen lassen
|
||||
// (LISTEN_ADDR=127.0.0.1) – sonst kann ein direkter Connect von außen
|
||||
// trotzdem als loopback gelten, falls das Routing das so durchstellt.
|
||||
app.set('trust proxy', 'loopback');
|
||||
// Zwei Szenarien:
|
||||
// 1) **HTTPS_ENABLED=true** (Produktion mit vorgelagertem TLS-Proxy auf
|
||||
// EIGENER Box, z.B. Nginx Proxy Manager): `trust proxy = 1` vertraut
|
||||
// genau einem Hop → req.ip = echter Client (nicht der Proxy).
|
||||
// Voraussetzung: Backend ist NICHT direkt aus dem Internet erreichbar,
|
||||
// sonst könnte ein Direkt-Connect X-Forwarded-For faken und den
|
||||
// Rate-Limiter / Security-Monitor umgehen. Bei NPM-Setup ist das
|
||||
// durch das Docker-Network + nicht-veröffentlichten Backend-Port
|
||||
// gewährleistet.
|
||||
// 2) **HTTPS_ENABLED=false** (lokales Dev oder direkter http://ip:port-
|
||||
// Zugriff): `loopback` reicht – kein vertrauenswürdiger Hop davor.
|
||||
//
|
||||
// Vor dem Fix stand das auf `'loopback'` was im Produktiv-NPM-Setup
|
||||
// IMMER die Proxy-IP statt der Client-IP lieferte → Rate-Limit und
|
||||
// IDOR-Threshold-Detection sahen alle Angriffe als von „einem" Client.
|
||||
const trustProxyValue = process.env.HTTPS_ENABLED === 'true' ? 1 : 'loopback';
|
||||
app.set('trust proxy', trustProxyValue);
|
||||
|
||||
// ==================== SECURITY MIDDLEWARE ====================
|
||||
|
||||
// HTTP Security Headers (X-Frame-Options, X-Content-Type-Options, HSTS, etc.)
|
||||
// HTTP Security Headers (X-Frame-Options, X-Content-Type-Options, HSTS, CSP, ...)
|
||||
//
|
||||
// CSP ist konservativ aber SPA-tauglich:
|
||||
// - script-src 'self' → keine externen Skripte, keine inline-Scripts
|
||||
// (Vite baut Module-Skripte zu separaten Files,
|
||||
// die sind 'self')
|
||||
// - style-src 'self' 'unsafe-inline' → Tailwind/inline-Styles brauchen das
|
||||
// (sicheres Trade-off; XSS via CSS ist
|
||||
// marginal vs Lock-Out gegen die UI)
|
||||
// - img-src self/data/blob → base64-Avatare + blob-URLs für PDFs/Downloads
|
||||
// - font-src self/data → eingebettete Fonts
|
||||
// - connect-src 'self' → API + WebSocket nur zur eigenen Origin
|
||||
// - frame-ancestors 'none' → Clickjacking-Schutz (ersetzt X-Frame-Options)
|
||||
// - object-src 'none' → keine Flash/<object>/<embed>-Embeds
|
||||
// - base-uri 'self' → keine <base>-Hijacking-Tricks
|
||||
// - form-action 'self' → POST-Targets nur auf eigene Origin
|
||||
// Permissions-Policy: schaltet Browser-APIs aus, die wir nicht brauchen.
|
||||
// Verhindert, dass eingeschleustes JS Zugriff auf Kamera/Mikro/GPS/Payment etc.
|
||||
// bekommt. clipboard-write ist 'self' für die CopyButton-Komponenten,
|
||||
// fullscreen 'self' falls jemand mal eine Vorschau in Vollbild öffnet.
|
||||
app.use((_req, res, next) => {
|
||||
res.setHeader(
|
||||
'Permissions-Policy',
|
||||
[
|
||||
'accelerometer=()',
|
||||
'ambient-light-sensor=()',
|
||||
'autoplay=()',
|
||||
'battery=()',
|
||||
'camera=()',
|
||||
'clipboard-read=()',
|
||||
'clipboard-write=(self)',
|
||||
'cross-origin-isolated=()',
|
||||
'display-capture=()',
|
||||
'encrypted-media=()',
|
||||
'fullscreen=(self)',
|
||||
'geolocation=()',
|
||||
'gyroscope=()',
|
||||
'hid=()',
|
||||
'idle-detection=()',
|
||||
'magnetometer=()',
|
||||
'microphone=()',
|
||||
'midi=()',
|
||||
'payment=()',
|
||||
'picture-in-picture=()',
|
||||
'publickey-credentials-get=()',
|
||||
'screen-wake-lock=()',
|
||||
'sync-xhr=()',
|
||||
'usb=()',
|
||||
'web-share=()',
|
||||
'xr-spatial-tracking=()',
|
||||
].join(', '),
|
||||
);
|
||||
next();
|
||||
});
|
||||
|
||||
// HTTPS-only-Header (HSTS + upgrade-insecure-requests) nur setzen, wenn
|
||||
// wirklich TLS davor läuft – sonst sperrt sich die App auf direkt-via-IP-
|
||||
// Deployments (Browser versucht /assets/* via https zu laden → SSL-Error).
|
||||
// Aktivieren mit HTTPS_ENABLED=true in der .env, sobald ein TLS-Proxy
|
||||
// (Caddy/Traefik/Nginx) vor OpenCRM steht.
|
||||
const httpsEnabled = process.env.HTTPS_ENABLED === 'true';
|
||||
|
||||
app.use(
|
||||
helmet({
|
||||
// CSP ausschalten – wird bei SPA schwierig, frontend setzt eigene CSP via meta
|
||||
contentSecurityPolicy: false,
|
||||
// Cross-Origin-Resource-Policy: "same-site" für SPA mit gleicher Origin
|
||||
contentSecurityPolicy: {
|
||||
useDefaults: true,
|
||||
directives: {
|
||||
'default-src': ["'self'"],
|
||||
'script-src': ["'self'"],
|
||||
'style-src': ["'self'", "'unsafe-inline'"],
|
||||
'img-src': ["'self'", 'data:', 'blob:'],
|
||||
'font-src': ["'self'", 'data:'],
|
||||
'connect-src': ["'self'"],
|
||||
// Explizit gesetzt obwohl Fallback auf default-src/script-src greift –
|
||||
// ZAP markiert sonst "No-Fallback-Direktiven" als CSP-Lücke.
|
||||
'worker-src': ["'self'"],
|
||||
'manifest-src': ["'self'"],
|
||||
'media-src': ["'self'"],
|
||||
// 'self': eigene App darf eigene Resourcen in iframes embeden (z.B. die
|
||||
// annotierte PDF-Vorschau in der Auftragsvorlagen-Konfiguration).
|
||||
// 'none' würde sogar same-origin blocken und damit die UI brechen.
|
||||
// Externe Sites bleiben weiterhin gesperrt.
|
||||
'frame-ancestors': ["'self'"],
|
||||
'object-src': ["'none'"],
|
||||
'base-uri': ["'self'"],
|
||||
'form-action': ["'self'"],
|
||||
// useDefaults bringt 'upgrade-insecure-requests' selbst mit – explizit
|
||||
// auf null setzen entfernt es aus dem Header (helmet-API).
|
||||
'upgrade-insecure-requests': httpsEnabled ? [] : null,
|
||||
},
|
||||
},
|
||||
// HSTS NIE in Helmet senden – der vorgelagerte TLS-Reverse-Proxy
|
||||
// (Nginx Proxy Manager) macht das bereits. Doppelter Header verletzt
|
||||
// RFC 6797 (Multiple Header Entries) und wird von ZAP angemahnt.
|
||||
// HTTPS_ENABLED-Flag bleibt für upgrade-insecure-requests (CSP) relevant.
|
||||
strictTransportSecurity: false,
|
||||
crossOriginResourcePolicy: { policy: 'same-site' },
|
||||
}),
|
||||
);
|
||||
@@ -99,6 +222,9 @@ app.use(
|
||||
|
||||
// JSON-Body-Limit: 5 MB (Uploads laufen über multer, brauchen kein json())
|
||||
app.use(express.json({ limit: '5mb' }));
|
||||
// Cookie-Parser: wird für den httpOnly-Refresh-Token-Cookie gebraucht
|
||||
// (POST /api/auth/refresh liest ihn aus req.cookies).
|
||||
app.use(cookieParser());
|
||||
|
||||
// Audit-Logging Middleware (DSGVO-konform)
|
||||
app.use(auditContextMiddleware);
|
||||
@@ -127,6 +253,15 @@ app.get('/api/uploads/*', authenticate as any, (req, res, next) => {
|
||||
return (downloadFile as any)(req, res, next);
|
||||
});
|
||||
|
||||
// Cache-Control für alle API-Responses: `no-store` verhindert, dass Shared
|
||||
// Caches (CDN/Reverse-Proxy/Browser-History) JSON mit sensiblen Daten
|
||||
// vorhalten. Statische Frontend-Assets unter /assets/* sind weiter cacheable
|
||||
// (siehe express.static mit immutable weiter unten).
|
||||
app.use('/api', (_req, res, next) => {
|
||||
res.setHeader('Cache-Control', 'no-store');
|
||||
next();
|
||||
});
|
||||
|
||||
// Öffentliche Routes (OHNE Authentifizierung)
|
||||
app.use('/api/public/consent', consentPublicRoutes);
|
||||
|
||||
@@ -171,8 +306,29 @@ app.get('/api/health', (req, res) => {
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
const publicPath = path.join(process.cwd(), 'public');
|
||||
|
||||
// Serve static files
|
||||
app.use(express.static(publicPath));
|
||||
// Vite-Build-Assets (z.B. /assets/index-abc123.js) haben einen Content-Hash
|
||||
// im Dateinamen – das Image ist also versioniert. Daher kann der Browser
|
||||
// sie für ein Jahr aggressiv cachen und muss nicht revalidieren.
|
||||
app.use(
|
||||
'/assets',
|
||||
express.static(path.join(publicPath, 'assets'), {
|
||||
maxAge: '1y',
|
||||
immutable: true,
|
||||
}),
|
||||
);
|
||||
|
||||
// Rest des Frontends (index.html selbst, vite.svg, robots.txt, sitemap.xml).
|
||||
// express.static findet index.html bei GET /, deshalb MUSS hier das gleiche
|
||||
// no-store-Verhalten greifen wie im SPA-Fallback weiter unten – sonst
|
||||
// serviert der erste Static-Handler / mit dem express-Default `max-age=0`,
|
||||
// bevor der Fallback überhaupt greift, und der Browser cached die alte SPA.
|
||||
app.use(
|
||||
express.static(publicPath, {
|
||||
setHeaders: (res) => {
|
||||
res.setHeader('Cache-Control', 'no-store, must-revalidate');
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
// SPA fallback: serve index.html for all non-API routes
|
||||
app.get('*', (req, res, next) => {
|
||||
@@ -180,6 +336,9 @@ if (process.env.NODE_ENV === 'production') {
|
||||
if (req.path.startsWith('/api')) {
|
||||
return next();
|
||||
}
|
||||
// SPA-Wurzel darf NIE gecached werden – sonst sieht der Browser nach einem
|
||||
// Deploy weiterhin die alte index.html mit alten Asset-Hashes.
|
||||
res.setHeader('Cache-Control', 'no-store, must-revalidate');
|
||||
res.sendFile(path.join(publicPath, 'index.html'));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -31,7 +31,17 @@ export async function authenticate(
|
||||
// Algorithmus explizit auf HS256 festlegen (Defense-in-Depth gegen alg-confusion).
|
||||
const decoded = jwt.verify(token, process.env.JWT_SECRET as string, {
|
||||
algorithms: ['HS256'],
|
||||
}) as JwtPayload;
|
||||
}) as JwtPayload & { type?: string };
|
||||
|
||||
// Defense-in-Depth: Refresh-Tokens haben `type: 'refresh'` und dürfen
|
||||
// NICHT für normale API-Calls verwendet werden – nur am /api/auth/refresh-
|
||||
// Endpoint. Legacy-Tokens (vor der Refresh-Token-Einführung) haben kein
|
||||
// `type` und werden als Access akzeptiert, damit bestehende Sessions nicht
|
||||
// zwangsabgemeldet werden.
|
||||
if (decoded.type && decoded.type !== 'access') {
|
||||
res.status(401).json({ success: false, error: 'Falscher Token-Typ' });
|
||||
return;
|
||||
}
|
||||
|
||||
// Prüfen ob Token durch Rechteänderung/Passwort-Reset invalidiert wurde
|
||||
if (decoded.userId && decoded.iat) {
|
||||
|
||||
@@ -7,6 +7,7 @@ const router = Router();
|
||||
|
||||
router.post('/login', loginRateLimiter, authController.login);
|
||||
router.post('/customer-login', loginRateLimiter, authController.customerLogin);
|
||||
router.post('/refresh', authController.refresh);
|
||||
router.get('/me', authenticate, authController.me);
|
||||
router.post('/logout', authenticate, authController.logout);
|
||||
router.post('/register', authenticate, requirePermission('users:create'), authController.register);
|
||||
@@ -15,4 +16,7 @@ router.post('/register', authenticate, requirePermission('users:create'), authCo
|
||||
router.post('/password-reset/request', passwordResetRateLimiter, authController.requestPasswordReset);
|
||||
router.post('/password-reset/confirm', passwordResetRateLimiter, authController.confirmPasswordReset);
|
||||
|
||||
// Force-Change-Password nach Einmalpasswort-Login (Kundenportal)
|
||||
router.post('/change-initial-portal-password', authenticate, authController.changeInitialPortalPassword);
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -42,6 +42,9 @@ router.delete('/:id', authenticate, requirePermission('contracts:delete'), contr
|
||||
// Follow-up contract
|
||||
router.post('/:id/follow-up', authenticate, requirePermission('contracts:create'), contractController.createFollowUp);
|
||||
|
||||
// VVL (Vertragsverlängerung beim selben Anbieter, vollständige Kopie + Datums-Berechnung)
|
||||
router.post('/:id/renewal', authenticate, requirePermission('contracts:create'), contractController.createRenewal);
|
||||
|
||||
// Snooze (Vertrag zurückstellen)
|
||||
router.patch('/:id/snooze', authenticate, requirePermission('contracts:update'), contractController.snoozeContract);
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@ router.get('/:customerId/portal', authenticate, requirePermission('customers:upd
|
||||
router.put('/:customerId/portal', authenticate, requirePermission('customers:update'), customerController.updatePortalSettings);
|
||||
router.post('/:customerId/portal/password', authenticate, requirePermission('customers:update'), customerController.setPortalPassword);
|
||||
router.get('/:customerId/portal/password', authenticate, requirePermission('customers:update'), customerController.getPortalPassword);
|
||||
router.post('/:customerId/portal/password/generate', authenticate, requirePermission('customers:update'), customerController.generatePortalPassword);
|
||||
router.post('/:customerId/portal/send-credentials', authenticate, requirePermission('customers:update'), customerController.sendPortalCredentials);
|
||||
|
||||
// Representatives (Vertreter)
|
||||
router.get('/:customerId/representatives', authenticate, requirePermission('customers:read'), customerController.getRepresentatives);
|
||||
|
||||
@@ -1,9 +1,25 @@
|
||||
import { Router } from 'express';
|
||||
import multer from 'multer';
|
||||
import * as factoryDefaultsController from '../controllers/factoryDefaults.controller.js';
|
||||
import { authenticate, requirePermission } from '../middleware/auth.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
// In-Memory-Upload für die ZIP – wird direkt verarbeitet, keine temporäre Datei.
|
||||
const upload = multer({
|
||||
storage: multer.memoryStorage(),
|
||||
fileFilter: (_req, file, cb) => {
|
||||
const ok =
|
||||
file.mimetype === 'application/zip' ||
|
||||
file.mimetype === 'application/x-zip-compressed' ||
|
||||
file.mimetype === 'application/octet-stream' || // manche Browser senden das für .zip
|
||||
file.originalname.toLowerCase().endsWith('.zip');
|
||||
if (ok) cb(null, true);
|
||||
else cb(new Error('Nur ZIP-Dateien sind erlaubt'));
|
||||
},
|
||||
limits: { fileSize: 50 * 1024 * 1024 },
|
||||
});
|
||||
|
||||
// Preview (was wäre im Export drin?)
|
||||
router.get(
|
||||
'/preview',
|
||||
@@ -20,4 +36,13 @@ router.get(
|
||||
factoryDefaultsController.exportFactoryDefaults,
|
||||
);
|
||||
|
||||
// Import aus ZIP (multipart, Feld 'zip')
|
||||
router.post(
|
||||
'/import',
|
||||
authenticate,
|
||||
requirePermission('settings:update'),
|
||||
upload.single('zip'),
|
||||
factoryDefaultsController.importFactoryDefaults,
|
||||
);
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -12,4 +12,7 @@ router.delete('/:id', authenticate, requirePermission('customers:delete'), stres
|
||||
// Passwort zurücksetzen (generiert neues Passwort und setzt es beim Provider)
|
||||
router.post('/:id/reset-password', authenticate, requirePermission('customers:update'), stressfreiEmailController.resetPassword);
|
||||
|
||||
// Weiterleitungen neu setzen (z.B. nach Änderung der Kunden-Stamm-E-Mail)
|
||||
router.post('/:id/sync-forwarding', authenticate, requirePermission('customers:update'), stressfreiEmailController.syncForwarding);
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -112,6 +112,13 @@ function determineSensitivity(resourceType: string): AuditSensitivity {
|
||||
Authentication: 'CRITICAL',
|
||||
BankCard: 'CRITICAL',
|
||||
IdentityDocument: 'CRITICAL',
|
||||
// Klartext-Passwort-Reads – jeder Decrypt-Vorgang muss nachvollziehbar sein
|
||||
PortalPassword: 'CRITICAL',
|
||||
ContractPassword: 'CRITICAL',
|
||||
SimCardCredentials: 'CRITICAL',
|
||||
InternetCredentials: 'CRITICAL',
|
||||
SipCredentials: 'CRITICAL',
|
||||
MailboxCredentials: 'CRITICAL',
|
||||
// HIGH
|
||||
Customer: 'HIGH',
|
||||
User: 'HIGH',
|
||||
|
||||
@@ -7,6 +7,26 @@ import { encrypt, decrypt } from '../utils/encryption.js';
|
||||
import { sendEmail, SmtpCredentials } from './smtpService.js';
|
||||
import { getSystemEmailCredentials } from './emailProvider/emailProviderService.js';
|
||||
|
||||
// Token-Lifetimes
|
||||
// - Access-Token: kurzlebig, nur im Browser-Memory → XSS klaut max. 15 min
|
||||
// - Refresh-Token: lang, im httpOnly-Cookie → kein JS-Zugriff
|
||||
const ACCESS_TOKEN_EXPIRES_IN = (process.env.JWT_EXPIRES_IN || '15m') as jwt.SignOptions['expiresIn'];
|
||||
const REFRESH_TOKEN_EXPIRES_IN = (process.env.JWT_REFRESH_EXPIRES_IN || '7d') as jwt.SignOptions['expiresIn'];
|
||||
|
||||
// Helper: signiert ein Access- bzw. Refresh-JWT mit dem `type`-Claim als
|
||||
// Unterscheidung. Der Refresh-Token landet im httpOnly-Cookie und wird beim
|
||||
// /auth/refresh-Endpoint geprüft, der dann einen neuen Access ausgibt.
|
||||
export function signAccessToken(payload: JwtPayload): string {
|
||||
return jwt.sign({ ...payload, type: 'access' }, process.env.JWT_SECRET as string, {
|
||||
expiresIn: ACCESS_TOKEN_EXPIRES_IN,
|
||||
});
|
||||
}
|
||||
export function signRefreshToken(payload: JwtPayload): string {
|
||||
return jwt.sign({ ...payload, type: 'refresh' }, process.env.JWT_SECRET as string, {
|
||||
expiresIn: REFRESH_TOKEN_EXPIRES_IN,
|
||||
});
|
||||
}
|
||||
|
||||
// Bcrypt-Cost-Faktor: 12 = OWASP-empfohlen (Stand 2026), ca. 250 ms pro Hash.
|
||||
// Bestehende Hashes mit Faktor 10 bleiben gültig (bcrypt kodiert den Faktor im Hash).
|
||||
const BCRYPT_COST = 12;
|
||||
@@ -100,12 +120,12 @@ export async function login(email: string, password: string) {
|
||||
isCustomerPortal: false,
|
||||
};
|
||||
|
||||
const token = jwt.sign(payload, process.env.JWT_SECRET as string, {
|
||||
expiresIn: (process.env.JWT_EXPIRES_IN || '7d') as jwt.SignOptions['expiresIn'],
|
||||
});
|
||||
const accessToken = signAccessToken(payload);
|
||||
const refreshToken = signRefreshToken(payload);
|
||||
|
||||
return {
|
||||
token,
|
||||
accessToken,
|
||||
refreshToken,
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
@@ -160,14 +180,30 @@ export async function customerLogin(email: string, password: string) {
|
||||
throw new Error('Ungültige Anmeldedaten');
|
||||
}
|
||||
|
||||
// Lazy-Upgrade analog zu Mitarbeiter-Login
|
||||
maybeUpgradePasswordHash('customer', customer.id, password, customer.portalPasswordHash).catch(() => {});
|
||||
// Einmalpasswort-Check: wurde es per "Zugangsdaten versenden" verschickt?
|
||||
// Falls ja, jetzt sofort verbrauchen – Hash + Encrypted nullen, damit
|
||||
// weder Re-Login noch Klartext-Abruf möglich ist. Customer landet im
|
||||
// Force-Change-Password-Flow.
|
||||
const mustChangePassword = customer.portalPasswordMustChange === true;
|
||||
if (mustChangePassword) {
|
||||
await prisma.customer.update({
|
||||
where: { id: customer.id },
|
||||
data: {
|
||||
portalPasswordHash: null,
|
||||
portalPasswordEncrypted: null,
|
||||
portalLastLogin: new Date(),
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// Lazy-Upgrade analog zu Mitarbeiter-Login
|
||||
maybeUpgradePasswordHash('customer', customer.id, password, customer.portalPasswordHash).catch(() => {});
|
||||
|
||||
// Letzte Anmeldung aktualisieren
|
||||
await prisma.customer.update({
|
||||
where: { id: customer.id },
|
||||
data: { portalLastLogin: new Date() },
|
||||
});
|
||||
// Letzte Anmeldung aktualisieren
|
||||
await prisma.customer.update({
|
||||
where: { id: customer.id },
|
||||
data: { portalLastLogin: new Date() },
|
||||
});
|
||||
}
|
||||
|
||||
// IDs der Kunden sammeln, die dieser Kunde vertreten kann
|
||||
const representedCustomerIds = customer.representingFor.map(
|
||||
@@ -188,12 +224,13 @@ export async function customerLogin(email: string, password: string) {
|
||||
representedCustomerIds,
|
||||
};
|
||||
|
||||
const token = jwt.sign(payload, process.env.JWT_SECRET as string, {
|
||||
expiresIn: (process.env.JWT_EXPIRES_IN || '7d') as jwt.SignOptions['expiresIn'],
|
||||
});
|
||||
const accessToken = signAccessToken(payload);
|
||||
const refreshToken = signRefreshToken(payload);
|
||||
|
||||
return {
|
||||
token,
|
||||
accessToken,
|
||||
refreshToken,
|
||||
mustChangePassword,
|
||||
user: {
|
||||
id: customer.id,
|
||||
email: customer.portalEmail,
|
||||
@@ -202,6 +239,7 @@ export async function customerLogin(email: string, password: string) {
|
||||
permissions: customerPermissions,
|
||||
customerId: customer.id,
|
||||
isCustomerPortal: true,
|
||||
mustChangePassword,
|
||||
representedCustomers: customer.representingFor.map((rep) => ({
|
||||
id: rep.customer.id,
|
||||
customerNumber: rep.customer.customerNumber,
|
||||
@@ -214,6 +252,94 @@ export async function customerLogin(email: string, password: string) {
|
||||
};
|
||||
}
|
||||
|
||||
// Refresh-Token verifizieren und neuen Access-Token ausstellen. Wirft bei
|
||||
// ungültigem/abgelaufenem/invalidiertem Token. Greift auch tokenInvalidatedAt
|
||||
// vom User/Customer ab → bei Rolle-Ändern oder Logout sind alle Tokens (auch
|
||||
// das Refresh) sofort tot.
|
||||
export async function refreshAccessToken(refreshToken: string): Promise<{
|
||||
accessToken: string;
|
||||
refreshToken: string;
|
||||
user: any;
|
||||
}> {
|
||||
let decoded: any;
|
||||
try {
|
||||
decoded = jwt.verify(refreshToken, process.env.JWT_SECRET as string, {
|
||||
algorithms: ['HS256'],
|
||||
});
|
||||
} catch {
|
||||
throw new Error('Refresh-Token ungültig oder abgelaufen');
|
||||
}
|
||||
if (decoded.type !== 'refresh') {
|
||||
throw new Error('Falscher Token-Typ');
|
||||
}
|
||||
const issuedAt = decoded.iat ? decoded.iat * 1000 : 0;
|
||||
|
||||
// Mitarbeiter
|
||||
if (!decoded.isCustomerPortal && decoded.userId) {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: decoded.userId },
|
||||
include: {
|
||||
roles: { include: { role: { include: { permissions: { include: { permission: true } } } } } },
|
||||
},
|
||||
});
|
||||
if (!user || !user.isActive) throw new Error('Benutzer nicht aktiv');
|
||||
if (user.tokenInvalidatedAt && issuedAt < user.tokenInvalidatedAt.getTime()) {
|
||||
throw new Error('Refresh-Token wurde invalidiert (Logout/Rechteänderung)');
|
||||
}
|
||||
const permissions = new Set<string>();
|
||||
for (const ur of user.roles) {
|
||||
for (const rp of ur.role.permissions) {
|
||||
permissions.add(`${rp.permission.resource}:${rp.permission.action}`);
|
||||
}
|
||||
}
|
||||
const payload: JwtPayload = {
|
||||
userId: user.id,
|
||||
email: user.email,
|
||||
permissions: Array.from(permissions),
|
||||
customerId: user.customerId ?? undefined,
|
||||
isCustomerPortal: false,
|
||||
};
|
||||
return {
|
||||
accessToken: signAccessToken(payload),
|
||||
refreshToken: signRefreshToken(payload),
|
||||
user: {
|
||||
id: user.id,
|
||||
email: user.email,
|
||||
firstName: user.firstName,
|
||||
lastName: user.lastName,
|
||||
permissions: Array.from(permissions),
|
||||
customerId: user.customerId,
|
||||
isCustomerPortal: false,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Customer-Portal
|
||||
if (decoded.isCustomerPortal && decoded.customerId) {
|
||||
const customer = await prisma.customer.findUnique({ where: { id: decoded.customerId } });
|
||||
if (!customer || !customer.portalEmail) throw new Error('Portal-Konto nicht gefunden');
|
||||
if (customer.portalTokenInvalidatedAt && issuedAt < customer.portalTokenInvalidatedAt.getTime()) {
|
||||
throw new Error('Refresh-Token wurde invalidiert');
|
||||
}
|
||||
const portalUser = await getCustomerPortalUser(customer.id);
|
||||
if (!portalUser) throw new Error('Portal-Konto nicht gefunden');
|
||||
const payload: JwtPayload = {
|
||||
email: customer.portalEmail,
|
||||
permissions: portalUser.permissions,
|
||||
customerId: customer.id,
|
||||
isCustomerPortal: true,
|
||||
representedCustomerIds: portalUser.representedCustomers?.map((c: any) => c.id),
|
||||
};
|
||||
return {
|
||||
accessToken: signAccessToken(payload),
|
||||
refreshToken: signRefreshToken(payload),
|
||||
user: portalUser,
|
||||
};
|
||||
}
|
||||
|
||||
throw new Error('Refresh-Token konnte nicht interpretiert werden');
|
||||
}
|
||||
|
||||
// Kundenportal-Passwort setzen/ändern
|
||||
export async function setCustomerPortalPassword(customerId: number, password: string) {
|
||||
console.log('[SetPortalPassword] Setze Passwort für Kunde:', customerId);
|
||||
@@ -223,17 +349,45 @@ export async function setCustomerPortalPassword(customerId: number, password: st
|
||||
|
||||
console.log('[SetPortalPassword] Hash erstellt, Länge:', hashedPassword.length);
|
||||
|
||||
// Manuelles Setzen ist KEIN Einmalpasswort → Flag immer zurücksetzen,
|
||||
// falls vorher ein OTP gesetzt war.
|
||||
await prisma.customer.update({
|
||||
where: { id: customerId },
|
||||
data: {
|
||||
portalPasswordHash: hashedPassword,
|
||||
portalPasswordEncrypted: encryptedPassword,
|
||||
portalPasswordMustChange: false,
|
||||
},
|
||||
});
|
||||
|
||||
console.log('[SetPortalPassword] Passwort gespeichert');
|
||||
}
|
||||
|
||||
// Vom Endkunden selbst gesetztes Initial-Passwort nach OTP-Login.
|
||||
// Speichert neuen Hash, löscht das verbrauchte Encrypted-Feld (Klartext-
|
||||
// Speicherung soll bei OFF self-service nicht zurückkommen) und invalidiert
|
||||
// sofort alle bestehenden Sessions, damit Login mit dem neuen Passwort
|
||||
// gefordert wird.
|
||||
export async function changeInitialPortalPassword(customerId: number, newPassword: string) {
|
||||
const hashedPassword = await bcrypt.hash(newPassword, BCRYPT_COST);
|
||||
await prisma.customer.update({
|
||||
where: { id: customerId },
|
||||
data: {
|
||||
portalPasswordHash: hashedPassword,
|
||||
portalPasswordEncrypted: null,
|
||||
portalPasswordMustChange: false,
|
||||
portalTokenInvalidatedAt: new Date(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function markPortalPasswordForChange(customerId: number) {
|
||||
await prisma.customer.update({
|
||||
where: { id: customerId },
|
||||
data: { portalPasswordMustChange: true },
|
||||
});
|
||||
}
|
||||
|
||||
// Kundenportal-Passwort im Klartext abrufen
|
||||
export async function getCustomerPortalPassword(customerId: number): Promise<string | null> {
|
||||
const customer = await prisma.customer.findUnique({
|
||||
@@ -405,6 +559,86 @@ function getPublicUrl(): string {
|
||||
return process.env.PUBLIC_URL || 'http://localhost:5173';
|
||||
}
|
||||
|
||||
/**
|
||||
* Portal-Zugangsdaten per E-Mail an den Kunden versenden. Nur durch Admin-
|
||||
* UI ausgelöst – nie automatisch –, weil das Klartext-Passwort im Mail-
|
||||
* Body steht. Login-URL zeigt auf das `/portal/login`-Frontend-Route.
|
||||
*/
|
||||
export async function sendPortalCredentialsEmail(params: {
|
||||
to: string;
|
||||
customer: { firstName: string | null; lastName: string | null; salutation: string | null; companyName: string | null };
|
||||
loginEmail: string;
|
||||
password: string;
|
||||
}): Promise<void> {
|
||||
const systemEmail = await getSystemEmailCredentials();
|
||||
if (!systemEmail) {
|
||||
throw new Error('Kein System-E-Mail-Konto konfiguriert (Einstellungen → E-Mail-Provider)');
|
||||
}
|
||||
|
||||
const credentials: SmtpCredentials = {
|
||||
host: systemEmail.smtpServer,
|
||||
port: systemEmail.smtpPort,
|
||||
user: systemEmail.emailAddress,
|
||||
password: systemEmail.password,
|
||||
encryption: systemEmail.smtpEncryption,
|
||||
allowSelfSignedCerts: systemEmail.allowSelfSignedCerts,
|
||||
};
|
||||
|
||||
const loginUrl = `${getPublicUrl()}/portal/login`;
|
||||
const name = params.customer.companyName?.trim()
|
||||
|| `${params.customer.firstName || ''} ${params.customer.lastName || ''}`.trim()
|
||||
|| 'Kunde';
|
||||
|
||||
// HTML-Escape – Customer-Namen können theoretisch Sonderzeichen enthalten,
|
||||
// die wir nicht ungefiltert in die Mail rendern wollen.
|
||||
const esc = (s: string) =>
|
||||
s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
||||
|
||||
const html = `
|
||||
<div style="font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto;">
|
||||
<h2 style="color: #1e40af;">Ihre Zugangsdaten zum Kundenportal</h2>
|
||||
<p>Hallo ${esc(name)},</p>
|
||||
<p>anbei Ihre Zugangsdaten zum Kundenportal:</p>
|
||||
<table style="border-collapse: collapse; margin: 16px 0;">
|
||||
<tr><td style="padding: 6px 12px; color: #6b7280;">Login-URL:</td>
|
||||
<td style="padding: 6px 12px;"><a href="${loginUrl}">${esc(loginUrl)}</a></td></tr>
|
||||
<tr><td style="padding: 6px 12px; color: #6b7280;">E-Mail:</td>
|
||||
<td style="padding: 6px 12px; font-family: monospace;">${esc(params.loginEmail)}</td></tr>
|
||||
<tr><td style="padding: 6px 12px; color: #6b7280;">Passwort:</td>
|
||||
<td style="padding: 6px 12px; font-family: monospace;">${esc(params.password)}</td></tr>
|
||||
</table>
|
||||
<p style="color: #b91c1c; font-size: 14px; font-weight: 600;">
|
||||
⚠️ Dieses Passwort ist ein <u>Einmalpasswort</u>.
|
||||
</p>
|
||||
<p style="color: #6b7280; font-size: 14px;">
|
||||
Beim ersten Login werden Sie aufgefordert, ein eigenes Passwort zu vergeben.
|
||||
Danach ist dieses Passwort hier <strong>nicht mehr gültig</strong> – falls Sie den
|
||||
Vorgang abbrechen, fordern Sie bitte neue Zugangsdaten an oder nutzen die
|
||||
Passwort-vergessen-Funktion.
|
||||
</p>
|
||||
<hr style="border: none; border-top: 1px solid #e5e7eb; margin: 24px 0;">
|
||||
<p style="color: #9ca3af; font-size: 12px;">
|
||||
Diese Nachricht enthält sensible Zugangsdaten – bitte sicher verwahren oder nach
|
||||
dem Login löschen.
|
||||
</p>
|
||||
</div>
|
||||
`;
|
||||
|
||||
await sendEmail(
|
||||
credentials,
|
||||
systemEmail.emailAddress,
|
||||
{
|
||||
to: params.to,
|
||||
subject: 'Ihre Zugangsdaten zum Kundenportal',
|
||||
html,
|
||||
},
|
||||
{
|
||||
context: 'portal-credentials',
|
||||
triggeredBy: 'admin-action',
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Passwort-Reset-Link per Email senden.
|
||||
* Findet User/Customer per Email. Wirft keinen Error wenn nicht gefunden
|
||||
|
||||
@@ -249,6 +249,7 @@ export async function createBackup(): Promise<BackupResult> {
|
||||
{ name: 'EmailLog', query: () => prisma.emailLog.findMany() },
|
||||
{ name: 'AuditRetentionPolicy', query: () => prisma.auditRetentionPolicy.findMany() },
|
||||
{ name: 'AuditLog', query: () => prisma.auditLog.findMany() },
|
||||
{ name: 'SecurityEvent', query: () => prisma.securityEvent.findMany() },
|
||||
];
|
||||
|
||||
let totalRecords = 0;
|
||||
@@ -310,6 +311,7 @@ export async function restoreBackup(backupName: string): Promise<RestoreResult>
|
||||
// Logs & Audit zuerst (hängen an allem)
|
||||
await prisma.auditLog.deleteMany({});
|
||||
await prisma.emailLog.deleteMany({});
|
||||
await prisma.securityEvent.deleteMany({});
|
||||
|
||||
// Detail-Tabellen
|
||||
await prisma.carInsuranceDetails.deleteMany({});
|
||||
@@ -887,6 +889,18 @@ export async function restoreBackup(backupName: string): Promise<RestoreResult>
|
||||
}
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'SecurityEvent',
|
||||
restore: async (data: any[]) => {
|
||||
for (const item of data) {
|
||||
await prisma.securityEvent.upsert({
|
||||
where: { id: item.id },
|
||||
update: convertDates(item),
|
||||
create: convertDates(item),
|
||||
});
|
||||
}
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
let totalRestored = 0;
|
||||
|
||||
@@ -49,6 +49,18 @@ export interface EmailListOptions {
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
includeBody?: boolean;
|
||||
// Suche / Filter (alle AND-verknüpft)
|
||||
search?: string; // Volltextsuche über subject + from + body
|
||||
fromFilter?: string; // Absender enthält
|
||||
toFilter?: string; // Empfänger enthält
|
||||
subjectFilter?: string; // Subject enthält
|
||||
bodyFilter?: string; // Body enthält (text/html)
|
||||
attachmentNameFilter?: string; // Anhang-Dateiname enthält
|
||||
hasAttachments?: boolean; // Nur mit/ohne Anhang
|
||||
isRead?: boolean; // Gelesen-Status
|
||||
isStarred?: boolean; // Markiert-Status
|
||||
receivedFrom?: Date; // Empfangen ab
|
||||
receivedTo?: Date; // Empfangen bis
|
||||
}
|
||||
|
||||
// ==================== SYNC FUNCTIONS ====================
|
||||
@@ -273,6 +285,59 @@ export async function getCachedEmails(
|
||||
where.folder = EmailFolder.INBOX;
|
||||
}
|
||||
|
||||
// ===== Such-/Filter-Parameter =====
|
||||
// Volltext-Quicksearch: durchsucht parallel Subject, From-Address/Name und
|
||||
// Body. MariaDB `contains` ist case-insensitive bei utf8mb4_unicode_ci.
|
||||
if (options.search && options.search.trim()) {
|
||||
const q = options.search.trim();
|
||||
where.OR = [
|
||||
{ subject: { contains: q } },
|
||||
{ fromAddress: { contains: q } },
|
||||
{ fromName: { contains: q } },
|
||||
{ textBody: { contains: q } },
|
||||
];
|
||||
}
|
||||
|
||||
// Feldspezifische Filter (alle AND-verknüpft mit dem Rest)
|
||||
if (options.fromFilter?.trim()) {
|
||||
const q = options.fromFilter.trim();
|
||||
// Treffer in fromAddress ODER fromName – für den Nutzer ist „Von" beides
|
||||
where.AND = [
|
||||
...(Array.isArray(where.AND) ? where.AND : where.AND ? [where.AND] : []),
|
||||
{ OR: [{ fromAddress: { contains: q } }, { fromName: { contains: q } }] },
|
||||
];
|
||||
}
|
||||
if (options.toFilter?.trim()) {
|
||||
where.toAddresses = { contains: options.toFilter.trim() };
|
||||
}
|
||||
if (options.subjectFilter?.trim()) {
|
||||
where.subject = { contains: options.subjectFilter.trim() };
|
||||
}
|
||||
if (options.bodyFilter?.trim()) {
|
||||
const q = options.bodyFilter.trim();
|
||||
where.AND = [
|
||||
...(Array.isArray(where.AND) ? where.AND : where.AND ? [where.AND] : []),
|
||||
{ OR: [{ textBody: { contains: q } }, { htmlBody: { contains: q } }] },
|
||||
];
|
||||
}
|
||||
if (options.attachmentNameFilter?.trim()) {
|
||||
where.attachmentNames = { contains: options.attachmentNameFilter.trim() };
|
||||
}
|
||||
if (typeof options.hasAttachments === 'boolean') {
|
||||
where.hasAttachments = options.hasAttachments;
|
||||
}
|
||||
if (typeof options.isRead === 'boolean') {
|
||||
where.isRead = options.isRead;
|
||||
}
|
||||
if (typeof options.isStarred === 'boolean') {
|
||||
where.isStarred = options.isStarred;
|
||||
}
|
||||
if (options.receivedFrom || options.receivedTo) {
|
||||
where.receivedAt = {};
|
||||
if (options.receivedFrom) (where.receivedAt as Prisma.DateTimeFilter).gte = options.receivedFrom;
|
||||
if (options.receivedTo) (where.receivedAt as Prisma.DateTimeFilter).lte = options.receivedTo;
|
||||
}
|
||||
|
||||
// Body-Felder nur wenn explizit angefordert (spart Bandbreite)
|
||||
const select: Prisma.CachedEmailSelect = {
|
||||
id: true,
|
||||
|
||||
@@ -2,6 +2,7 @@ import { ContractType, ContractStatus } from '@prisma/client';
|
||||
import prisma from '../lib/prisma.js';
|
||||
import { generateContractNumber, paginate, buildPaginationResponse } from '../utils/helpers.js';
|
||||
import { encrypt, decrypt } from '../utils/encryption.js';
|
||||
import { sanitizeCustomerStrict } from '../utils/sanitize.js';
|
||||
|
||||
export interface ContractFilters {
|
||||
customerId?: number;
|
||||
@@ -154,7 +155,18 @@ export async function getContractById(id: number, decryptPassword = false) {
|
||||
|
||||
if (!contract) return null;
|
||||
|
||||
// Decrypt password if requested and exists
|
||||
// SECURITY: Embedded Customer-Objekt sanitizen, sonst leaken
|
||||
// portalPasswordHash + portalPasswordEncrypted + Reset-Token in jede
|
||||
// contract.customer-Response. Der direkte `/customers/:id`-Endpoint hat
|
||||
// den Schutz schon; hier wäre er ohne Sanitize bypassbar.
|
||||
if (contract.customer) {
|
||||
(contract as Record<string, unknown>).customer = sanitizeCustomerStrict(
|
||||
contract.customer as Record<string, unknown>,
|
||||
);
|
||||
}
|
||||
|
||||
// Decrypt password if requested and exists (Contract-Anbieter-Passwort,
|
||||
// nicht zu verwechseln mit Customer-Portal-Passwort)
|
||||
if (decryptPassword && contract.portalPasswordEncrypted) {
|
||||
try {
|
||||
(contract as Record<string, unknown>).portalPasswordDecrypted = decrypt(
|
||||
@@ -385,6 +397,15 @@ export async function createContract(data: ContractCreateData) {
|
||||
},
|
||||
});
|
||||
|
||||
// Embedded Customer-Objekt sanitizen (siehe getContractById – derselbe
|
||||
// Schutz; createContract gibt den frisch erstellten Vertrag inkl. Customer
|
||||
// zurück, und der darf keine Passwort-Hashes/-Encryptions leaken).
|
||||
if (contract.customer) {
|
||||
(contract as Record<string, unknown>).customer = sanitizeCustomerStrict(
|
||||
contract.customer as Record<string, unknown>,
|
||||
);
|
||||
}
|
||||
|
||||
return contract;
|
||||
}
|
||||
|
||||
@@ -765,6 +786,251 @@ export async function createFollowUpContract(previousContractId: number) {
|
||||
return createContract(newContractData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hilfsfunktion: extrahiert die Anzahl Monate aus einer ContractDuration.
|
||||
* Code-Beispiele: "12M", "24M", "1J", "2J". Falls nichts erkannt wird, fällt
|
||||
* sie auf 12 Monate als sicheren Default zurück.
|
||||
*/
|
||||
function durationToMonths(code: string | null | undefined, description: string | null | undefined): number {
|
||||
const c = (code || '').trim();
|
||||
const d = (description || '').trim();
|
||||
let m = c.match(/^(\d+)\s*M$/i);
|
||||
if (m) return parseInt(m[1], 10);
|
||||
m = c.match(/^(\d+)\s*J$/i);
|
||||
if (m) return parseInt(m[1], 10) * 12;
|
||||
m = d.match(/(\d+)\s*Monat/i);
|
||||
if (m) return parseInt(m[1], 10);
|
||||
m = d.match(/(\d+)\s*Jahr/i);
|
||||
if (m) return parseInt(m[1], 10) * 12;
|
||||
return 12;
|
||||
}
|
||||
|
||||
/**
|
||||
* VVL = Vertragsverlängerung beim selben Anbieter.
|
||||
*
|
||||
* Im Gegensatz zu createFollowUpContract werden ALLE Daten 1:1 kopiert:
|
||||
* Provider, Tarif, Portal-Credentials, Preise, Notes, ContractDocuments.
|
||||
*
|
||||
* Berechnet wird das neue Startdatum: altes startDate + Vertragslaufzeit.
|
||||
* Stimmt das gefundene Datum nicht mit dem späteren Auftrag überein, kann
|
||||
* der User es im Vertrag manuell anpassen.
|
||||
*
|
||||
* NICHT mitkopiert wird:
|
||||
* - das Auftragsdokument (documentType "Auftragsformular") – das ist
|
||||
* schließlich die NEU zu unterschreibende VVL.
|
||||
* - Kündigungsschreiben/-bestätigung (das war der ALTE Cancel-Flow,
|
||||
* bei einer VVL nicht relevant)
|
||||
*/
|
||||
export async function createRenewalContract(previousContractId: number) {
|
||||
const previousContract = await getContractById(previousContractId, true);
|
||||
if (!previousContract) {
|
||||
throw new Error('Vorgängervertrag nicht gefunden');
|
||||
}
|
||||
|
||||
// Bereits ein Folge-/VVL-Vertrag vorhanden?
|
||||
const existing = await prisma.contract.findFirst({
|
||||
where: { previousContractId },
|
||||
select: { id: true, contractNumber: true },
|
||||
});
|
||||
if (existing) {
|
||||
throw new Error(`Es existiert bereits ein Folgevertrag: ${existing.contractNumber}`);
|
||||
}
|
||||
|
||||
// Neues Startdatum = altes Start + Laufzeit
|
||||
let newStartDate: Date | null = null;
|
||||
let newEndDate: Date | null = null;
|
||||
if (previousContract.startDate && previousContract.contractDuration) {
|
||||
const months = durationToMonths(
|
||||
previousContract.contractDuration.code,
|
||||
previousContract.contractDuration.description,
|
||||
);
|
||||
newStartDate = new Date(previousContract.startDate);
|
||||
newStartDate.setMonth(newStartDate.getMonth() + months);
|
||||
newEndDate = new Date(newStartDate);
|
||||
newEndDate.setMonth(newEndDate.getMonth() + months);
|
||||
}
|
||||
|
||||
// Vertrags-Daten 1:1 kopieren (außer id/contractNumber/Datums-/Cancellation-Felder)
|
||||
const contractNumber = generateContractNumber(previousContract.type);
|
||||
|
||||
const newContract = await prisma.contract.create({
|
||||
data: {
|
||||
contractNumber,
|
||||
customerId: previousContract.customerId,
|
||||
type: previousContract.type,
|
||||
status: 'DRAFT',
|
||||
contractCategoryId: previousContract.contractCategoryId,
|
||||
addressId: previousContract.addressId,
|
||||
billingAddressId: previousContract.billingAddressId,
|
||||
bankCardId: previousContract.bankCardId,
|
||||
identityDocumentId: previousContract.identityDocumentId,
|
||||
salesPlatformId: previousContract.salesPlatformId,
|
||||
cancellationPeriodId: previousContract.cancellationPeriodId,
|
||||
contractDurationId: previousContract.contractDurationId,
|
||||
previousContractId: previousContract.id,
|
||||
previousProviderId: previousContract.previousProviderId,
|
||||
providerId: previousContract.providerId,
|
||||
tariffId: previousContract.tariffId,
|
||||
providerName: previousContract.providerName,
|
||||
tariffName: previousContract.tariffName,
|
||||
customerNumberAtProvider: previousContract.customerNumberAtProvider,
|
||||
portalUsername: previousContract.portalUsername,
|
||||
portalPasswordEncrypted: previousContract.portalPasswordEncrypted,
|
||||
commission: previousContract.commission,
|
||||
notes: previousContract.notes,
|
||||
startDate: newStartDate,
|
||||
endDate: newEndDate,
|
||||
// Cancellation-Felder bewusst leer lassen – die VVL hat den alten
|
||||
// Cancel-Flow nicht geerbt.
|
||||
},
|
||||
});
|
||||
|
||||
// Detail-Tabellen 1:1 kopieren (id rausnehmen, contractId neu)
|
||||
if (previousContract.energyDetails) {
|
||||
const ed = previousContract.energyDetails;
|
||||
const newEnergy = await prisma.energyContractDetails.create({
|
||||
data: {
|
||||
contractId: newContract.id,
|
||||
meterId: ed.meterId,
|
||||
maloId: ed.maloId,
|
||||
annualConsumption: ed.annualConsumption,
|
||||
annualConsumptionKwh: ed.annualConsumptionKwh,
|
||||
basePrice: ed.basePrice,
|
||||
unitPrice: ed.unitPrice,
|
||||
unitPriceNt: ed.unitPriceNt,
|
||||
bonus: ed.bonus,
|
||||
previousProviderName: ed.previousProviderName,
|
||||
previousCustomerNumber: ed.previousCustomerNumber,
|
||||
},
|
||||
});
|
||||
// ContractMeter-Verknüpfungen mitkopieren
|
||||
for (const cm of ed.contractMeters || []) {
|
||||
await prisma.contractMeter.create({
|
||||
data: {
|
||||
energyContractDetailsId: newEnergy.id,
|
||||
meterId: cm.meterId,
|
||||
position: cm.position,
|
||||
installedAt: cm.installedAt,
|
||||
removedAt: cm.removedAt,
|
||||
finalReading: cm.finalReading,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
if (previousContract.internetDetails) {
|
||||
const id = previousContract.internetDetails;
|
||||
const newInet = await prisma.internetContractDetails.create({
|
||||
data: {
|
||||
contractId: newContract.id,
|
||||
downloadSpeed: id.downloadSpeed,
|
||||
uploadSpeed: id.uploadSpeed,
|
||||
routerModel: id.routerModel,
|
||||
routerSerialNumber: id.routerSerialNumber,
|
||||
installationDate: id.installationDate,
|
||||
internetUsername: id.internetUsername,
|
||||
internetPasswordEncrypted: id.internetPasswordEncrypted,
|
||||
propertyType: id.propertyType,
|
||||
propertyLocation: id.propertyLocation,
|
||||
connectionLocation: id.connectionLocation,
|
||||
homeId: id.homeId,
|
||||
activationCode: id.activationCode,
|
||||
},
|
||||
});
|
||||
for (const pn of id.phoneNumbers || []) {
|
||||
await prisma.phoneNumber.create({
|
||||
data: {
|
||||
internetContractDetailsId: newInet.id,
|
||||
phoneNumber: pn.phoneNumber,
|
||||
isMain: pn.isMain,
|
||||
sipUsername: pn.sipUsername,
|
||||
sipPasswordEncrypted: pn.sipPasswordEncrypted,
|
||||
sipServer: pn.sipServer,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
if (previousContract.mobileDetails) {
|
||||
const md = previousContract.mobileDetails;
|
||||
const newMob = await prisma.mobileContractDetails.create({
|
||||
data: {
|
||||
contractId: newContract.id,
|
||||
requiresMultisim: md.requiresMultisim,
|
||||
dataVolume: md.dataVolume,
|
||||
includedMinutes: md.includedMinutes,
|
||||
includedSMS: md.includedSMS,
|
||||
deviceModel: md.deviceModel,
|
||||
deviceImei: md.deviceImei,
|
||||
phoneNumber: md.phoneNumber,
|
||||
simCardNumber: md.simCardNumber,
|
||||
},
|
||||
});
|
||||
for (const sc of md.simCards || []) {
|
||||
await prisma.simCard.create({
|
||||
data: {
|
||||
mobileDetailsId: newMob.id,
|
||||
phoneNumber: sc.phoneNumber,
|
||||
simCardNumber: sc.simCardNumber,
|
||||
isMultisim: sc.isMultisim,
|
||||
isMain: sc.isMain,
|
||||
pin: sc.pin,
|
||||
puk: sc.puk,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
if (previousContract.tvDetails) {
|
||||
await prisma.tvContractDetails.create({
|
||||
data: {
|
||||
contractId: newContract.id,
|
||||
receiverModel: previousContract.tvDetails.receiverModel,
|
||||
smartcardNumber: previousContract.tvDetails.smartcardNumber,
|
||||
package: previousContract.tvDetails.package,
|
||||
},
|
||||
});
|
||||
}
|
||||
if (previousContract.carInsuranceDetails) {
|
||||
const ci = previousContract.carInsuranceDetails;
|
||||
await prisma.carInsuranceDetails.create({
|
||||
data: {
|
||||
contractId: newContract.id,
|
||||
licensePlate: ci.licensePlate,
|
||||
hsn: ci.hsn,
|
||||
tsn: ci.tsn,
|
||||
vin: ci.vin,
|
||||
vehicleType: ci.vehicleType,
|
||||
firstRegistration: ci.firstRegistration,
|
||||
noClaimsClass: ci.noClaimsClass,
|
||||
insuranceType: ci.insuranceType,
|
||||
deductiblePartial: ci.deductiblePartial,
|
||||
deductibleFull: ci.deductibleFull,
|
||||
previousInsurer: ci.previousInsurer,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// ContractDocuments mitkopieren – AUSSER "Auftragsformular" (das ist die
|
||||
// neue Unterschrift, die der User selbst hochlädt). Files werden NICHT
|
||||
// physisch dupliziert; beide Verträge zeigen auf dieselbe Datei.
|
||||
const docs = await prisma.contractDocument.findMany({
|
||||
where: { contractId: previousContract.id },
|
||||
});
|
||||
for (const d of docs) {
|
||||
if (d.documentType.toLowerCase().includes('auftragsformular')) continue;
|
||||
await prisma.contractDocument.create({
|
||||
data: {
|
||||
contractId: newContract.id,
|
||||
documentType: d.documentType,
|
||||
documentPath: d.documentPath,
|
||||
originalName: d.originalName,
|
||||
notes: d.notes,
|
||||
uploadedBy: d.uploadedBy,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return prisma.contract.findUnique({ where: { id: newContract.id } });
|
||||
}
|
||||
|
||||
// Decrypt password for viewing
|
||||
export async function getContractPassword(id: number): Promise<string | null> {
|
||||
const contract = await prisma.contract.findUnique({
|
||||
|
||||
@@ -129,3 +129,35 @@ export async function createNewContractFromPredecessorEntry(
|
||||
createdBy,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatischen Historie-Eintrag für VVL (Vertragsverlängerung) im Vorgängervertrag.
|
||||
*/
|
||||
export async function createRenewalHistoryEntry(
|
||||
previousContractId: number,
|
||||
newContractNumber: string,
|
||||
createdBy: string
|
||||
) {
|
||||
return createHistoryEntry(previousContractId, {
|
||||
title: `Vertragsverlängerung erstellt: ${newContractNumber}`,
|
||||
description: `Eine Vertragsverlängerung (VVL) als ${newContractNumber} wurde aus diesem Vertrag erstellt – alle Daten wurden 1:1 übernommen, das Auftragsdokument muss neu hochgeladen werden.`,
|
||||
isAutomatic: true,
|
||||
createdBy,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Automatischen Historie-Eintrag im neuen VVL-Vertrag.
|
||||
*/
|
||||
export async function createNewRenewalFromPredecessorEntry(
|
||||
newContractId: number,
|
||||
previousContractNumber: string,
|
||||
createdBy: string
|
||||
) {
|
||||
return createHistoryEntry(newContractId, {
|
||||
title: `VVL zu ${previousContractNumber}`,
|
||||
description: `Dieser Vertrag wurde als Vertragsverlängerung (VVL) zu ${previousContractNumber} erstellt.`,
|
||||
isAutomatic: true,
|
||||
createdBy,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -469,6 +469,22 @@ export async function deprovisionEmail(localPart: string): Promise<EmailOperatio
|
||||
}
|
||||
}
|
||||
|
||||
// Weiterleitungsziele ersetzen (set:, nicht add:) – nutzen wir, um nach einer
|
||||
// Kunden-Email-Änderung die Forwards einer Stressfrei-Adresse auf den neuen
|
||||
// Kunden-Inbox + unsere Service-Adresse zu setzen.
|
||||
export async function setEmailForwardTargets(
|
||||
localPart: string,
|
||||
targets: string[],
|
||||
): Promise<EmailOperationResult> {
|
||||
try {
|
||||
const provider = await getProviderInstance();
|
||||
return provider.updateForwardTargets(localPart, targets);
|
||||
} catch (error) {
|
||||
const errorMessage = error instanceof Error ? error.message : 'Unbekannter Fehler';
|
||||
return { success: false, error: errorMessage };
|
||||
}
|
||||
}
|
||||
|
||||
// E-Mail umbenennen
|
||||
export async function renameProvisionedEmail(
|
||||
oldLocalPart: string,
|
||||
|
||||
@@ -1,15 +1,32 @@
|
||||
/**
|
||||
* Factory-Defaults: Export + Import von Stammdaten-Katalogen.
|
||||
* Enthält KEINE Kundendaten, Verträge, Dokumente oder Einstellungen –
|
||||
* Enthält KEINE Kundendaten, Verträge, Dokumente oder E-Mails –
|
||||
* nur reine Kataloge: Anbieter, Tarife, Kündigungsfristen, Laufzeiten,
|
||||
* Vertragskategorien und PDF-Auftragsvorlagen.
|
||||
* Vertragskategorien, PDF-Auftragsvorlagen und ausgewählte
|
||||
* HTML-Templates (Datenschutz / Impressum / Vollmacht).
|
||||
*/
|
||||
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import archiver from 'archiver';
|
||||
import AdmZip from 'adm-zip';
|
||||
import prisma from '../lib/prisma.js';
|
||||
|
||||
// Whitelist der AppSetting-Keys, die ins Factory-Default-Bundle gehören.
|
||||
// Bewusst klein gehalten: nur HTML-Templates für rechtliche Standardtexte –
|
||||
// keine Secrets, keine SMTP-Konfiguration, keine User-spezifischen Settings.
|
||||
export const FACTORY_DEFAULT_APP_SETTING_KEYS = [
|
||||
'privacyPolicyHtml',
|
||||
'authorizationTemplateHtml',
|
||||
'imprintHtml',
|
||||
'websitePrivacyPolicyHtml',
|
||||
] as const;
|
||||
|
||||
export interface AppSettingExport {
|
||||
key: (typeof FACTORY_DEFAULT_APP_SETTING_KEYS)[number];
|
||||
value: string;
|
||||
}
|
||||
|
||||
export interface FactoryDefaultsManifest {
|
||||
version: 1;
|
||||
exportedAt: string;
|
||||
@@ -20,6 +37,7 @@ export interface FactoryDefaultsManifest {
|
||||
contractDurations: number;
|
||||
contractCategories: number;
|
||||
pdfTemplates: number;
|
||||
appSettings: number;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -49,7 +67,7 @@ export interface PdfTemplateExport {
|
||||
* Sammelt alle Katalog-Daten aus der DB.
|
||||
*/
|
||||
export async function collectFactoryDefaults() {
|
||||
const [providers, cancellationPeriods, contractDurations, contractCategories, pdfTemplates] =
|
||||
const [providers, cancellationPeriods, contractDurations, contractCategories, pdfTemplates, appSettings] =
|
||||
await Promise.all([
|
||||
prisma.provider.findMany({
|
||||
include: { tariffs: { select: { name: true, isActive: true } } },
|
||||
@@ -59,6 +77,11 @@ export async function collectFactoryDefaults() {
|
||||
prisma.contractDuration.findMany({ orderBy: { code: 'asc' } }),
|
||||
prisma.contractCategory.findMany({ orderBy: { sortOrder: 'asc' } }),
|
||||
prisma.pdfTemplate.findMany({ orderBy: { name: 'asc' } }),
|
||||
prisma.appSetting.findMany({
|
||||
where: { key: { in: [...FACTORY_DEFAULT_APP_SETTING_KEYS] } },
|
||||
select: { key: true, value: true },
|
||||
orderBy: { key: 'asc' },
|
||||
}),
|
||||
]);
|
||||
|
||||
return {
|
||||
@@ -108,6 +131,7 @@ export async function collectFactoryDefaults() {
|
||||
pdfFilename: sanitizeFilename(t.name) + path.extname(t.originalName || '.pdf'),
|
||||
};
|
||||
}),
|
||||
appSettings: appSettings as AppSettingExport[],
|
||||
};
|
||||
}
|
||||
|
||||
@@ -132,6 +156,7 @@ export async function exportFactoryDefaults(): Promise<Buffer> {
|
||||
contractDurations: data.contractDurations.length,
|
||||
contractCategories: data.contractCategories.length,
|
||||
pdfTemplates: data.pdfTemplates.length,
|
||||
appSettings: data.appSettings.length,
|
||||
},
|
||||
};
|
||||
|
||||
@@ -160,6 +185,9 @@ export async function exportFactoryDefaults(): Promise<Buffer> {
|
||||
archive.append(JSON.stringify(data.pdfTemplates, null, 2), {
|
||||
name: 'pdf-templates/pdf-templates.json',
|
||||
});
|
||||
archive.append(JSON.stringify(data.appSettings, null, 2), {
|
||||
name: 'app-settings/app-settings.json',
|
||||
});
|
||||
|
||||
// PDF-Dateien physisch hinzufügen (Pfade aus DB laden)
|
||||
const uploadsRoot = path.join(process.cwd(), 'uploads');
|
||||
@@ -192,3 +220,244 @@ export async function exportFactoryDefaults(): Promise<Buffer> {
|
||||
})();
|
||||
});
|
||||
}
|
||||
|
||||
// ============================================================
|
||||
// IMPORT
|
||||
// ============================================================
|
||||
|
||||
export interface FactoryDefaultsImportResult {
|
||||
providers: number;
|
||||
tariffs: number;
|
||||
cancellationPeriods: number;
|
||||
contractDurations: number;
|
||||
contractCategories: number;
|
||||
pdfTemplates: number;
|
||||
pdfTemplatesSkipped: number;
|
||||
appSettings: number;
|
||||
warnings: string[];
|
||||
}
|
||||
|
||||
function parseJsonEntry<T>(zip: AdmZip, name: string): T[] {
|
||||
const entry = zip.getEntry(name);
|
||||
if (!entry) return [];
|
||||
try {
|
||||
const parsed = JSON.parse(entry.getData().toString('utf-8'));
|
||||
return Array.isArray(parsed) ? parsed : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wendet ein Factory-Defaults-ZIP idempotent auf die DB an.
|
||||
* - upsert über unique-Keys: nichts wird gelöscht
|
||||
* - PDFs landen in `${uploads}/pdf-templates/` mit eindeutigem Suffix
|
||||
* - AppSettings nur Whitelist-Keys (FACTORY_DEFAULT_APP_SETTING_KEYS)
|
||||
*
|
||||
* Robust gegen Zip-Slip: wir greifen nur auf bekannte Entry-Namen zu
|
||||
* (`pdf-templates/<basename>`), niemals auf einen aus dem ZIP konstruierten
|
||||
* Pfad im Filesystem.
|
||||
*/
|
||||
export async function importFactoryDefaults(
|
||||
zipBuffer: Buffer,
|
||||
): Promise<FactoryDefaultsImportResult> {
|
||||
const zip = new AdmZip(zipBuffer);
|
||||
const result: FactoryDefaultsImportResult = {
|
||||
providers: 0,
|
||||
tariffs: 0,
|
||||
cancellationPeriods: 0,
|
||||
contractDurations: 0,
|
||||
contractCategories: 0,
|
||||
pdfTemplates: 0,
|
||||
pdfTemplatesSkipped: 0,
|
||||
appSettings: 0,
|
||||
warnings: [],
|
||||
};
|
||||
|
||||
// --- Providers + Tariffs
|
||||
const providers = parseJsonEntry<ProviderExport>(zip, 'providers/providers.json');
|
||||
for (const p of providers) {
|
||||
if (!p.name) continue;
|
||||
const provider = await prisma.provider.upsert({
|
||||
where: { name: p.name },
|
||||
update: {
|
||||
portalUrl: p.portalUrl ?? null,
|
||||
usernameFieldName: p.usernameFieldName ?? null,
|
||||
passwordFieldName: p.passwordFieldName ?? null,
|
||||
isActive: p.isActive ?? true,
|
||||
},
|
||||
create: {
|
||||
name: p.name,
|
||||
portalUrl: p.portalUrl ?? null,
|
||||
usernameFieldName: p.usernameFieldName ?? null,
|
||||
passwordFieldName: p.passwordFieldName ?? null,
|
||||
isActive: p.isActive ?? true,
|
||||
},
|
||||
});
|
||||
result.providers++;
|
||||
for (const t of p.tariffs ?? []) {
|
||||
if (!t.name) continue;
|
||||
await prisma.tariff.upsert({
|
||||
where: { providerId_name: { providerId: provider.id, name: t.name } },
|
||||
update: { isActive: t.isActive ?? true },
|
||||
create: { providerId: provider.id, name: t.name, isActive: t.isActive ?? true },
|
||||
});
|
||||
result.tariffs++;
|
||||
}
|
||||
}
|
||||
|
||||
// --- Contract-Meta
|
||||
const cancellationPeriods = parseJsonEntry<{ code: string; description: string; isActive?: boolean }>(
|
||||
zip,
|
||||
'contract-meta/cancellation-periods.json',
|
||||
);
|
||||
for (const c of cancellationPeriods) {
|
||||
if (!c.code || !c.description) continue;
|
||||
await prisma.cancellationPeriod.upsert({
|
||||
where: { code: c.code },
|
||||
update: { description: c.description, isActive: c.isActive ?? true },
|
||||
create: { code: c.code, description: c.description, isActive: c.isActive ?? true },
|
||||
});
|
||||
result.cancellationPeriods++;
|
||||
}
|
||||
|
||||
const contractDurations = parseJsonEntry<{ code: string; description: string; isActive?: boolean }>(
|
||||
zip,
|
||||
'contract-meta/contract-durations.json',
|
||||
);
|
||||
for (const d of contractDurations) {
|
||||
if (!d.code || !d.description) continue;
|
||||
await prisma.contractDuration.upsert({
|
||||
where: { code: d.code },
|
||||
update: { description: d.description, isActive: d.isActive ?? true },
|
||||
create: { code: d.code, description: d.description, isActive: d.isActive ?? true },
|
||||
});
|
||||
result.contractDurations++;
|
||||
}
|
||||
|
||||
const contractCategories = parseJsonEntry<{
|
||||
code: string;
|
||||
name: string;
|
||||
icon?: string | null;
|
||||
color?: string | null;
|
||||
sortOrder?: number;
|
||||
isActive?: boolean;
|
||||
}>(zip, 'contract-meta/contract-categories.json');
|
||||
for (const c of contractCategories) {
|
||||
if (!c.code || !c.name) continue;
|
||||
await prisma.contractCategory.upsert({
|
||||
where: { code: c.code },
|
||||
update: {
|
||||
name: c.name,
|
||||
icon: c.icon ?? null,
|
||||
color: c.color ?? null,
|
||||
sortOrder: c.sortOrder ?? 0,
|
||||
isActive: c.isActive ?? true,
|
||||
},
|
||||
create: {
|
||||
code: c.code,
|
||||
name: c.name,
|
||||
icon: c.icon ?? null,
|
||||
color: c.color ?? null,
|
||||
sortOrder: c.sortOrder ?? 0,
|
||||
isActive: c.isActive ?? true,
|
||||
},
|
||||
});
|
||||
result.contractCategories++;
|
||||
}
|
||||
|
||||
// --- PDF-Vorlagen (JSON + binär aus dem ZIP)
|
||||
const pdfTemplates = parseJsonEntry<PdfTemplateExport>(
|
||||
zip,
|
||||
'pdf-templates/pdf-templates.json',
|
||||
);
|
||||
if (pdfTemplates.length > 0) {
|
||||
const uploadsRoot = path.join(process.cwd(), 'uploads');
|
||||
const pdfDestDir = path.join(uploadsRoot, 'pdf-templates');
|
||||
if (!fs.existsSync(pdfDestDir)) {
|
||||
fs.mkdirSync(pdfDestDir, { recursive: true });
|
||||
}
|
||||
for (const t of pdfTemplates) {
|
||||
if (!t.name || !t.pdfFilename) continue;
|
||||
// Anti-Zip-Slip: nur basename verwenden, kein Pfad
|
||||
const basename = path.basename(t.pdfFilename);
|
||||
const entry = zip.getEntry(`pdf-templates/${basename}`);
|
||||
if (!entry) {
|
||||
result.pdfTemplatesSkipped++;
|
||||
result.warnings.push(`PDF fehlt im ZIP: ${basename} – Vorlage "${t.name}" übersprungen`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const ext = path.extname(t.originalName || basename) || '.pdf';
|
||||
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1e9);
|
||||
const safeName = t.name.replace(/[^a-zA-Z0-9]/g, '-');
|
||||
const destFilename = `seed-${safeName}-${uniqueSuffix}${ext}`;
|
||||
const destPdf = path.join(pdfDestDir, destFilename);
|
||||
const relativePath = `/uploads/pdf-templates/${destFilename}`;
|
||||
|
||||
fs.writeFileSync(destPdf, entry.getData());
|
||||
|
||||
// Bei existierender Vorlage die alte Datei aufräumen
|
||||
const existing = await prisma.pdfTemplate.findUnique({ where: { name: t.name } });
|
||||
if (existing?.templatePath) {
|
||||
const oldRel = existing.templatePath.startsWith('/uploads/')
|
||||
? existing.templatePath.substring('/uploads/'.length)
|
||||
: existing.templatePath;
|
||||
const oldAbs = path.join(uploadsRoot, oldRel);
|
||||
if (fs.existsSync(oldAbs)) {
|
||||
try {
|
||||
fs.unlinkSync(oldAbs);
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const fieldMappingJson = JSON.stringify(t.fieldMapping ?? {});
|
||||
await prisma.pdfTemplate.upsert({
|
||||
where: { name: t.name },
|
||||
update: {
|
||||
description: t.description ?? null,
|
||||
providerName: t.providerName ?? null,
|
||||
templatePath: relativePath,
|
||||
originalName: t.originalName,
|
||||
fieldMapping: fieldMappingJson,
|
||||
phoneFieldPrefix: t.phoneFieldPrefix ?? null,
|
||||
maxPhoneFields: t.maxPhoneFields ?? 8,
|
||||
isActive: t.isActive ?? true,
|
||||
},
|
||||
create: {
|
||||
name: t.name,
|
||||
description: t.description ?? null,
|
||||
providerName: t.providerName ?? null,
|
||||
templatePath: relativePath,
|
||||
originalName: t.originalName,
|
||||
fieldMapping: fieldMappingJson,
|
||||
phoneFieldPrefix: t.phoneFieldPrefix ?? null,
|
||||
maxPhoneFields: t.maxPhoneFields ?? 8,
|
||||
isActive: t.isActive ?? true,
|
||||
},
|
||||
});
|
||||
result.pdfTemplates++;
|
||||
}
|
||||
}
|
||||
|
||||
// --- AppSettings (HTML-Templates, Whitelist)
|
||||
const appSettings = parseJsonEntry<AppSettingExport>(zip, 'app-settings/app-settings.json');
|
||||
const allowedKeys = new Set<string>(FACTORY_DEFAULT_APP_SETTING_KEYS);
|
||||
for (const s of appSettings) {
|
||||
if (!s.key || typeof s.value !== 'string') continue;
|
||||
if (!allowedKeys.has(s.key)) {
|
||||
result.warnings.push(`AppSetting-Key '${s.key}' nicht auf Whitelist – ignoriert`);
|
||||
continue;
|
||||
}
|
||||
await prisma.appSetting.upsert({
|
||||
where: { key: s.key },
|
||||
update: { value: s.value },
|
||||
create: { key: s.key, value: s.value },
|
||||
});
|
||||
result.appSettings++;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -155,14 +155,16 @@ export async function detectThresholds(): Promise<void> {
|
||||
});
|
||||
for (const g of grouped) {
|
||||
if ((g._count as number) < b.threshold) continue;
|
||||
// Prüfen ob wir für diese (IP+Type+Stunde) schon einen CRITICAL emittiert haben
|
||||
const hourBucket = new Date(now.getTime() - (now.getTime() % (60 * 60 * 1000)));
|
||||
// Debounce: pro IP max. 1 SUSPICIOUS-Alert pro 60min (sliding window).
|
||||
// Vorher: floor(now, hour) → resettete bei Stundenwechsel und produzierte
|
||||
// doppelte Alerts (Bug aus Runde 10).
|
||||
const oneHourAgo = new Date(now.getTime() - 60 * 60 * 1000);
|
||||
const existing = await prisma.securityEvent.findFirst({
|
||||
where: {
|
||||
type: 'SUSPICIOUS',
|
||||
severity: 'CRITICAL',
|
||||
ipAddress: g.ipAddress,
|
||||
createdAt: { gte: hourBucket },
|
||||
createdAt: { gte: oneHourAgo },
|
||||
},
|
||||
});
|
||||
if (existing) continue;
|
||||
|
||||
@@ -7,6 +7,8 @@ import {
|
||||
checkEmailExists,
|
||||
getProviderDomain,
|
||||
updateMailboxPassword,
|
||||
setEmailForwardTargets,
|
||||
getActiveProviderConfig,
|
||||
} from './emailProvider/emailProviderService.js';
|
||||
import { generateSecurePassword } from '../utils/passwordGenerator.js';
|
||||
|
||||
@@ -113,6 +115,8 @@ export async function createEmail(data: CreateEmailData) {
|
||||
...emailData,
|
||||
isActive: true,
|
||||
hasMailbox: true,
|
||||
isProvisioned: true,
|
||||
provisionedAt: new Date(),
|
||||
emailPasswordEncrypted: passwordEncrypted,
|
||||
},
|
||||
});
|
||||
@@ -131,6 +135,11 @@ export async function createEmail(data: CreateEmailData) {
|
||||
...emailData,
|
||||
isActive: true,
|
||||
hasMailbox: createMailbox || false,
|
||||
// Provisioned-Flag nur setzen wenn Provider-Aufruf gerade lief (oder
|
||||
// die Mail bei Plesk schon existierte und der „existiert bereits"-Pfad
|
||||
// gegriffen hat).
|
||||
isProvisioned: !!provisionAtProvider,
|
||||
provisionedAt: provisionAtProvider ? new Date() : null,
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -201,7 +210,7 @@ export async function syncMailboxStatus(id: number): Promise<{
|
||||
}> {
|
||||
const stressfreiEmail = await prisma.stressfreiEmail.findUnique({
|
||||
where: { id },
|
||||
select: { email: true, hasMailbox: true },
|
||||
select: { email: true, hasMailbox: true, isProvisioned: true, provisionedAt: true },
|
||||
});
|
||||
|
||||
if (!stressfreiEmail) {
|
||||
@@ -213,19 +222,42 @@ export async function syncMailboxStatus(id: number): Promise<{
|
||||
// Provider-Status prüfen
|
||||
const providerStatus = await checkEmailExists(localPart);
|
||||
|
||||
// Self-Healing für `isProvisioned`: das Flag wurde in einer früheren Code-
|
||||
// Version beim Provisioning nie gesetzt → DB ist stellenweise inkonsistent
|
||||
// zum Provider. Wir reconciliieren bei jedem Status-Sync mit.
|
||||
const updates: Record<string, unknown> = {};
|
||||
|
||||
if (!providerStatus.exists) {
|
||||
// Beim Provider nicht (mehr) vorhanden → DB-Flag entsprechend
|
||||
if (stressfreiEmail.isProvisioned) {
|
||||
updates.isProvisioned = false;
|
||||
}
|
||||
if (stressfreiEmail.hasMailbox) {
|
||||
updates.hasMailbox = false;
|
||||
}
|
||||
if (Object.keys(updates).length > 0) {
|
||||
await prisma.stressfreiEmail.update({ where: { id }, data: updates });
|
||||
return { success: true, hasMailbox: false, wasUpdated: true };
|
||||
}
|
||||
return { success: true, hasMailbox: false, wasUpdated: false };
|
||||
}
|
||||
|
||||
const providerHasMailbox = providerStatus.hasMailbox === true;
|
||||
// Beim Provider vorhanden → isProvisioned auf true ziehen falls noch nicht
|
||||
if (!stressfreiEmail.isProvisioned) {
|
||||
updates.isProvisioned = true;
|
||||
if (!stressfreiEmail.provisionedAt) {
|
||||
updates.provisionedAt = new Date();
|
||||
}
|
||||
}
|
||||
|
||||
// DB aktualisieren wenn Status abweicht
|
||||
const providerHasMailbox = providerStatus.hasMailbox === true;
|
||||
if (stressfreiEmail.hasMailbox !== providerHasMailbox) {
|
||||
await prisma.stressfreiEmail.update({
|
||||
where: { id },
|
||||
data: { hasMailbox: providerHasMailbox },
|
||||
});
|
||||
console.log(`Mailbox-Status für ${stressfreiEmail.email} aktualisiert: ${stressfreiEmail.hasMailbox} -> ${providerHasMailbox}`);
|
||||
updates.hasMailbox = providerHasMailbox;
|
||||
}
|
||||
|
||||
if (Object.keys(updates).length > 0) {
|
||||
await prisma.stressfreiEmail.update({ where: { id }, data: updates });
|
||||
console.log(`Stressfrei-Status für ${stressfreiEmail.email} reconciled:`, updates);
|
||||
return { success: true, hasMailbox: providerHasMailbox, wasUpdated: true };
|
||||
}
|
||||
|
||||
@@ -251,6 +283,120 @@ export async function getDecryptedPassword(id: number): Promise<string | null> {
|
||||
}
|
||||
}
|
||||
|
||||
// Weiterleitungen einer Stressfrei-Adresse neu setzen (z.B. nach Änderung der
|
||||
// Stamm-E-Mail des Kunden). Ersetzt alle bestehenden Forwards durch
|
||||
// [aktuelle Kunden-E-Mail, defaultForwardEmail aus Provider-Config].
|
||||
//
|
||||
// Wenn die Adresse `hasMailbox` ist: setzt zusätzlich das im CRM verschlüsselt
|
||||
// hinterlegte Passwort am Provider neu (Use-Case: Plesk-Restore, manueller
|
||||
// Eingriff im Plesk-UI etc. – CRM und Provider können sich entkoppeln, sodass
|
||||
// IMAP/SMTP-Logins im CRM nicht mehr passen). Self-Healing.
|
||||
//
|
||||
// Idempotent: das Plesk-CLI `set:` überschreibt die Adressliste komplett, kein
|
||||
// Duplikat-Risiko bei Mehrfachaufruf. Wenn die Operation erfolgreich war wird
|
||||
// das `isProvisioned`-Flag automatisch auf `true` gezogen (historische
|
||||
// Einträge, bei denen das Flag nie gesetzt wurde, werden so geheilt).
|
||||
export async function syncForwardingForEmail(
|
||||
id: number,
|
||||
): Promise<{
|
||||
success: boolean;
|
||||
forwardTargets?: string[];
|
||||
customerEmail?: string;
|
||||
passwordReset?: boolean;
|
||||
error?: string;
|
||||
}> {
|
||||
const stressfreiEmail = await prisma.stressfreiEmail.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
email: true,
|
||||
customerId: true,
|
||||
isProvisioned: true,
|
||||
hasMailbox: true,
|
||||
emailPasswordEncrypted: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!stressfreiEmail) {
|
||||
return { success: false, error: 'StressfreiEmail nicht gefunden' };
|
||||
}
|
||||
|
||||
const customer = await prisma.customer.findUnique({
|
||||
where: { id: stressfreiEmail.customerId },
|
||||
select: { email: true },
|
||||
});
|
||||
if (!customer?.email) {
|
||||
return { success: false, error: 'Kunde hat keine Stamm-E-Mail-Adresse hinterlegt' };
|
||||
}
|
||||
|
||||
const config = await getActiveProviderConfig();
|
||||
const forwardTargets: string[] = [customer.email];
|
||||
if (config?.defaultForwardEmail) {
|
||||
forwardTargets.push(config.defaultForwardEmail);
|
||||
}
|
||||
|
||||
const localPart = stressfreiEmail.email.split('@')[0];
|
||||
|
||||
// 1) Forwards neu setzen.
|
||||
const forwardResult = await setEmailForwardTargets(localPart, forwardTargets);
|
||||
if (!forwardResult.success) {
|
||||
// Wenn Plesk meldet „nicht gefunden", liefern wir eine sprechende Meldung
|
||||
// statt der rohen Provider-Nachricht.
|
||||
const err = forwardResult.error || 'Provider-Update fehlgeschlagen';
|
||||
const friendly = /not\s*found|nicht\s*gefunden/i.test(err)
|
||||
? 'E-Mail-Adresse beim Provider nicht gefunden – wurde sie dort gelöscht?'
|
||||
: err;
|
||||
return { success: false, error: friendly };
|
||||
}
|
||||
|
||||
// 2) Wenn Mailbox: Passwort aus CRM-Speicher entschlüsseln und am Provider
|
||||
// neu setzen (Self-Healing nach Provider-seitigen Änderungen).
|
||||
let passwordReset = false;
|
||||
if (stressfreiEmail.hasMailbox && stressfreiEmail.emailPasswordEncrypted) {
|
||||
try {
|
||||
const password = decrypt(stressfreiEmail.emailPasswordEncrypted);
|
||||
const pwResult = await updateMailboxPassword(localPart, password);
|
||||
if (!pwResult.success) {
|
||||
// Forwards waren schon erfolgreich – wir geben Forward-Erfolg + Passwort-
|
||||
// Fehler kombiniert zurück, statt die ganze Operation rot zu machen.
|
||||
return {
|
||||
success: false,
|
||||
forwardTargets,
|
||||
customerEmail: customer.email,
|
||||
error:
|
||||
'Weiterleitungen aktualisiert, aber Passwort-Sync fehlgeschlagen: ' +
|
||||
(pwResult.error || 'unbekannt'),
|
||||
};
|
||||
}
|
||||
passwordReset = true;
|
||||
} catch (e) {
|
||||
return {
|
||||
success: false,
|
||||
forwardTargets,
|
||||
customerEmail: customer.email,
|
||||
error:
|
||||
'Weiterleitungen aktualisiert, aber Passwort konnte nicht entschlüsselt werden – ' +
|
||||
'evtl. wurde der ENCRYPTION_KEY rotiert',
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 3) Self-Healing: nach erfolgreichem Provider-Aufruf wissen wir definitiv,
|
||||
// dass die Adresse beim Provider existiert → Flag korrigieren.
|
||||
if (!stressfreiEmail.isProvisioned) {
|
||||
await prisma.stressfreiEmail.update({
|
||||
where: { id },
|
||||
data: { isProvisioned: true, provisionedAt: new Date() },
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
forwardTargets,
|
||||
customerEmail: customer.email,
|
||||
passwordReset,
|
||||
};
|
||||
}
|
||||
|
||||
// Passwort neu generieren und beim Provider setzen
|
||||
export async function resetMailboxPassword(id: number): Promise<{ success: boolean; password?: string; error?: string }> {
|
||||
const stressfreiEmail = await prisma.stressfreiEmail.findUnique({
|
||||
|
||||
@@ -88,6 +88,43 @@ export function generateSimplePassword(length = 12): string {
|
||||
});
|
||||
}
|
||||
|
||||
// ==================== PASSWORD COMPLEXITY VALIDATION ====================
|
||||
|
||||
/**
|
||||
* Mindestanforderungen für vom User vergebene Passwörter.
|
||||
* Generator-Output (generateSecurePassword) erfüllt diese standardmäßig.
|
||||
*/
|
||||
export interface PasswordComplexityResult {
|
||||
ok: boolean;
|
||||
errors: string[];
|
||||
}
|
||||
|
||||
export function validatePasswordComplexity(pw: unknown): PasswordComplexityResult {
|
||||
const errors: string[] = [];
|
||||
if (typeof pw !== 'string') {
|
||||
return { ok: false, errors: ['Passwort fehlt oder ist kein Text'] };
|
||||
}
|
||||
if (pw.length < 12) errors.push('mindestens 12 Zeichen');
|
||||
if (!/[a-z]/.test(pw)) errors.push('mindestens einen Kleinbuchstaben');
|
||||
if (!/[A-Z]/.test(pw)) errors.push('mindestens einen Großbuchstaben');
|
||||
if (!/[0-9]/.test(pw)) errors.push('mindestens eine Ziffer');
|
||||
// Sonderzeichen-Set bewusst breit – auch Leerzeichen + Unicode-Punktuation
|
||||
// zulassen, damit gängige Passwort-Manager-Outputs nicht abgelehnt werden.
|
||||
if (!/[^A-Za-z0-9]/.test(pw)) errors.push('mindestens ein Sonderzeichen');
|
||||
return { ok: errors.length === 0, errors };
|
||||
}
|
||||
|
||||
/**
|
||||
* Wirft mit sprechender Fehlermeldung, wenn das Passwort die Komplexität
|
||||
* nicht erfüllt. Für Aufruf direkt im Controller, der die Exception fängt.
|
||||
*/
|
||||
export function assertPasswordComplexity(pw: unknown): void {
|
||||
const r = validatePasswordComplexity(pw);
|
||||
if (!r.ok) {
|
||||
throw new Error('Passwort erfüllt Mindestanforderungen nicht: ' + r.errors.join(', '));
|
||||
}
|
||||
}
|
||||
|
||||
// Kryptografisch sichere Zufallszahl
|
||||
function getRandomInt(max: number): number {
|
||||
const bytes = randomBytes(4);
|
||||
|
||||
@@ -110,6 +110,12 @@ const USER_UPDATABLE_FIELDS = [
|
||||
'signalNumber',
|
||||
'roleIds',
|
||||
'password', // nur Admin, wird im Service gehashed
|
||||
// hasGdprAccess + hasDeveloperAccess sind keine User-Spalten – der Service
|
||||
// mappt sie auf die versteckten Rollen DSGVO/Developer (siehe
|
||||
// setUserGdprAccess / setUserDeveloperAccess). Müssen aber auf der Whitelist
|
||||
// stehen, damit pick() sie nicht aus dem Request entfernt.
|
||||
'hasGdprAccess',
|
||||
'hasDeveloperAccess',
|
||||
// Nicht: id, customerId, tokenInvalidatedAt, passwordResetToken, passwordResetExpiresAt
|
||||
] as const;
|
||||
|
||||
|
||||
+98
-11
@@ -1,4 +1,19 @@
|
||||
version: '3.8'
|
||||
# OpenCRM – komplettes Setup: MariaDB + Backend/Frontend + Adminer
|
||||
# Konfiguration über ./.env (siehe ./.env.example)
|
||||
#
|
||||
# Quick-Start (Compose v2):
|
||||
# cp .env.example .env # Werte anpassen (Secrets rotieren!)
|
||||
# docker compose up -d # erstes Mal: holt Images, baut Backend, startet alles
|
||||
# Quick-Start (Compose v1, Legacy):
|
||||
# docker-compose up -d
|
||||
#
|
||||
# Browser:
|
||||
# http://localhost:${OPENCRM_PORT} # CRM
|
||||
# http://localhost:${ADMINER_PORT} # DB-UI
|
||||
#
|
||||
# Daten liegen alle unter ./data/* – Bind-Mounts statt Volumes (auf Wunsch).
|
||||
|
||||
#version: '3.8'
|
||||
|
||||
services:
|
||||
db:
|
||||
@@ -6,20 +21,92 @@ services:
|
||||
container_name: opencrm-db
|
||||
restart: unless-stopped
|
||||
environment:
|
||||
MYSQL_ROOT_PASSWORD: rootpassword
|
||||
MYSQL_DATABASE: opencrm
|
||||
MYSQL_USER: opencrm
|
||||
MYSQL_PASSWORD: opencrm123
|
||||
MARIADB_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
|
||||
MARIADB_DATABASE: ${DB_NAME}
|
||||
MARIADB_USER: ${DB_USER}
|
||||
MARIADB_PASSWORD: ${DB_PASSWORD}
|
||||
ports:
|
||||
- "3306:3306"
|
||||
# Externe Erreichbarkeit für lokale DB-Tools (TablePlus etc.).
|
||||
# Auf 127.0.0.1 binden – kein public exposure.
|
||||
- "127.0.0.1:${DB_PORT:-3306}:3306"
|
||||
volumes:
|
||||
- mariadb_data:/var/lib/mysql
|
||||
- ${DB_DATA_DIR:-./data/db}:/var/lib/mysql
|
||||
healthcheck:
|
||||
test: ["CMD", "healthcheck.sh", "--connect", "--innodb_initialized"]
|
||||
start_period: 10s
|
||||
start_period: 20s
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
retries: 5
|
||||
|
||||
volumes:
|
||||
mariadb_data:
|
||||
opencrm:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: backend/Dockerfile
|
||||
container_name: opencrm-app
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
# DATABASE_URL wird vom entrypoint.sh aus den DB_*-Komponenten gebaut –
|
||||
# mit encodeURIComponent für Passwörter mit Sonderzeichen ($, !, #, @, :,
|
||||
# / etc.). KEIN root für die App, sondern der App-User ${DB_USER}, den
|
||||
# MariaDB beim ersten Start automatisch mit GRANT ALL PRIVILEGES auf
|
||||
# ${DB_NAME}.* anlegt (über MARIADB_USER/MARIADB_PASSWORD).
|
||||
DB_HOST: db
|
||||
DB_PORT: 3306
|
||||
DB_NAME: ${DB_NAME}
|
||||
DB_USER: ${DB_USER}
|
||||
DB_PASSWORD: ${DB_PASSWORD}
|
||||
JWT_SECRET: ${JWT_SECRET}
|
||||
JWT_EXPIRES_IN: ${JWT_EXPIRES_IN:-15m}
|
||||
JWT_REFRESH_EXPIRES_IN: ${JWT_REFRESH_EXPIRES_IN:-7d}
|
||||
ENCRYPTION_KEY: ${ENCRYPTION_KEY}
|
||||
NODE_ENV: production
|
||||
PORT: 3001
|
||||
LISTEN_ADDR: 0.0.0.0
|
||||
CORS_ORIGINS: ${CORS_ORIGINS:-}
|
||||
HTTPS_ENABLED: ${HTTPS_ENABLED:-false}
|
||||
RUN_SEED: ${RUN_SEED:-false}
|
||||
ports:
|
||||
- "${OPENCRM_PORT:-3010}:3001"
|
||||
volumes:
|
||||
# Bind-Mounts für persistente Daten unter ./data/
|
||||
- ${UPLOADS_DIR:-./data/uploads}:/app/uploads
|
||||
- ${FACTORY_DEFAULTS_DIR:-./data/factory-defaults}:/app/factory-defaults
|
||||
- ${BACKUPS_DIR:-./data/backups}:/app/prisma/backups
|
||||
|
||||
adminer:
|
||||
image: adminer:latest
|
||||
container_name: opencrm-adminer
|
||||
restart: unless-stopped
|
||||
depends_on:
|
||||
- db
|
||||
environment:
|
||||
ADMINER_DEFAULT_SERVER: db
|
||||
ADMINER_DESIGN: ${ADMINER_DESIGN:-pepa-linha}
|
||||
# Adminers offizieller entrypoint linkt nur Designs, deren CSS exakt
|
||||
# `adminer.css` heißt. Manche Designs (dracula, adminer-dark) haben aber
|
||||
# `adminer-dark.css`. Wir machen den Symlink generisch: erstes .css im
|
||||
# gewählten Design wird verlinkt. Danach übergeben wir an den originalen
|
||||
# entrypoint.sh.
|
||||
entrypoint:
|
||||
- /bin/sh
|
||||
- -c
|
||||
- >
|
||||
cd /var/www/html;
|
||||
if [ -n "$$ADMINER_DESIGN" ] && [ -d "designs/$$ADMINER_DESIGN" ]; then
|
||||
CSS=$$(ls designs/$$ADMINER_DESIGN/*.css 2>/dev/null | head -1);
|
||||
if [ -n "$$CSS" ]; then
|
||||
ln -sf "$$CSS" adminer.css;
|
||||
touch .adminer-init;
|
||||
echo "[adminer-bootstrap] Theme aktiv: $$ADMINER_DESIGN -> $$CSS";
|
||||
else
|
||||
echo "[adminer-bootstrap] Design '$$ADMINER_DESIGN' enthält kein CSS – nutze Default";
|
||||
fi;
|
||||
fi;
|
||||
exec entrypoint.sh docker-php-entrypoint "$$@"
|
||||
- --
|
||||
command: ["php", "-S", "[::]:8080", "-t", "/var/www/html"]
|
||||
ports:
|
||||
- "127.0.0.1:${ADMINER_PORT:-8090}:8080"
|
||||
|
||||
@@ -230,6 +230,146 @@ Nichts Kritisches mehr gefunden. Liefert noch:
|
||||
- **Concurrent Password-Reset Race**: Token wird nach erstem Confirm
|
||||
atomar gelöscht – zweiter Versuch findet keinen Token. ✅
|
||||
|
||||
### Runde 11 – Externer Pentest-Folge: Header-Hygiene + Klartext-Audit
|
||||
|
||||
Externer Pentest (testssl, ZAP, Nikto, Nuclei) gegen Prod-VM hat drei
|
||||
Klassen Defense-in-Depth-Findings rausgespült. Reale Ausnutzbarkeit jeweils
|
||||
gering, aber Audit-Bewertung fordert konsistente Header-Hygiene.
|
||||
|
||||
- **HSTS-Doppel-Header (18×)**: Nginx-Proxy-Manager (TLS-Terminierung) UND
|
||||
Helmet schickten beide `Strict-Transport-Security` → RFC-6797-Verletzung.
|
||||
Helmet's HSTS deaktiviert (`strictTransportSecurity: false`); der
|
||||
Reverse-Proxy übernimmt die Policy zentral am Edge.
|
||||
- **Cache-Control (~10×)**: `/api/*` → `no-store` (sensible JSON-Daten),
|
||||
SPA-HTML (`/`, `/sitemap.xml`, `/robots.txt`) → `no-store, must-revalidate`
|
||||
(sonst hängt der Browser nach Deploy an alter `index.html` fest),
|
||||
`/assets/*.{js,css}` → `public, max-age=31536000, immutable` (Vite-Bundles
|
||||
haben Content-Hash im Filename).
|
||||
- **CSP No-Fallback-Direktiven (2×)**: `worker-src`, `manifest-src`,
|
||||
`media-src` jetzt explizit auf `'self'`.
|
||||
- **CSP `frame-ancestors`**: war `'none'`, das blockt auch same-origin-iframes
|
||||
→ PDF-Vorschau im PDF-Template-Editor lädt nicht. Korrigiert auf
|
||||
`'self'` (eigene App darf eigene Resourcen embeden, externe Sites bleiben
|
||||
via `X-Frame-Options: SAMEORIGIN` weiter gesperrt).
|
||||
- **BREACH (CVE-2013-3587)**: testssl meldet "potentially VULNERABLE,
|
||||
gzip HTTP compression detected" – theoretischer Side-Channel-Angriff
|
||||
auf gzip-komprimierte HTTPS-Responses. Praktisch klein bei JWT-SPA (keine
|
||||
reflektierten Secrets im Response), Audit-Marker bleibt aber MEDIUM.
|
||||
Fix: gzip im Reverse-Proxy für `/api/*` deaktivieren (Custom-Location im
|
||||
NPM, Statische Assets bleiben weiter komprimiert). README dokumentiert
|
||||
Setup.
|
||||
- **`Server: openresty` + `x-served-by`-Banner**: am NPM via
|
||||
`more_clear_headers Server X-Served-By;` weg.
|
||||
- **Audit-Log für Klartext-Passwort-Reads**: Pentest fand "HOCH (post-auth):
|
||||
Klartext-Passwörter über API abrufbar" — reversible AES-256-GCM ist
|
||||
by-design für das Feature "Anbieter-Login anzeigen", aber **keiner** der
|
||||
sechs Endpoints (`PortalPassword`, `ContractPassword`, `SimCardCredentials`,
|
||||
`InternetCredentials`, `SipCredentials`, `MailboxCredentials`) schrieb
|
||||
bisher einen Audit-Log-Eintrag. Jetzt: `action: 'READ'` mit eigenem
|
||||
Resource-Type + `sensitivity: CRITICAL`, Label nennt explizit "Klartext
|
||||
… entschlüsselt" + Resource-ID. Damit ist im Audit-Log-Viewer jederzeit
|
||||
nachvollziehbar, wer wann welches Passwort eingesehen hat
|
||||
(DSGVO + Insider-Threat).
|
||||
|
||||
### Runde 13 – KRITISCH: IDOR auf Stressfrei-Email-Sub-Routes (Live-Pentest-Fund)
|
||||
|
||||
Externer Pentest hat einen echten Credential-Exfiltration-Angriff erfolgreich
|
||||
durchgespielt: **als Portal-User von Kunde A komplette IMAP/SMTP-Klartext-
|
||||
Credentials von Kunde B abgreifen können**.
|
||||
|
||||
**Angriffspfad:**
|
||||
1. Portal-Login als Kunde A
|
||||
2. `/api/stressfrei-emails/{id}` GET unterschied saubere Antworten:
|
||||
- „E-Mail-Konto nicht gefunden" (ID existiert nicht)
|
||||
- „Kein Zugriff auf diese Kundendaten" (ID existiert, gehört anderem)
|
||||
→ Information-Disclosure: Existenz von IDs durchprobierbar
|
||||
3. `/api/stressfrei-emails/{id}/credentials` GET ohne Ownership-Check →
|
||||
IMAP/SMTP-Server, Username und **Klartext-Passwort** der fremden Mailbox
|
||||
|
||||
**Root Cause:** der Haupt-Endpoint `GET /:id` hatte `canAccessStressfreiEmail`,
|
||||
die 8 Sub-Endpoints unter `:id/*` hatten **alle keinen** Ownership-Check —
|
||||
nur `authenticate + requirePermission('customers:read')`, was jeder Portal-User
|
||||
hat.
|
||||
|
||||
**Betroffene Endpoints (alle gefixt):**
|
||||
- `GET /:id/credentials` ← **der kritische** (Klartext-Passwort + IMAP/SMTP)
|
||||
- `GET /:id/folder-counts`
|
||||
- `POST /:id/sync`
|
||||
- `POST /:id/send`
|
||||
- `POST /:id/enable-mailbox`
|
||||
- `POST /:id/sync-mailbox-status`
|
||||
- `POST /:id/reset-password`
|
||||
- `PUT /:id` (updateEmail im stressfreiEmail.controller)
|
||||
- `DELETE /:id` (deleteEmail)
|
||||
|
||||
`canAccessStressfreiEmail(req, res, emailId)` als erste Zeile in jedem
|
||||
Controller. `canAccessResourceByCustomerId` emittiert bei Fehlversuch
|
||||
automatisch ein `ACCESS_DENIED MEDIUM`-Event ins Security-Monitoring → bei
|
||||
>5 Versuchen in 5 min wird ein `CRITICAL SUSPICIOUS`-Event erzeugt + Alert
|
||||
verschickt.
|
||||
|
||||
**Live-verifiziert (Portal-User Kunde A versucht Email-ID von Kunde B):**
|
||||
|
||||
| Endpoint | Vorher | Nachher |
|
||||
| --- | --- | --- |
|
||||
| `GET /:id/credentials` | 🚨 **200 mit Klartext-Passwort** | ✅ 403 |
|
||||
| `GET /:id/folder-counts` | 🚨 200 | ✅ 403 |
|
||||
| `POST /:id/sync` | 🚨 200 | ✅ 403 |
|
||||
| `POST /:id/send` | 🚨 fremde Mailbox zum Versand missbrauchbar | ✅ 403 |
|
||||
| `POST /:id/enable-mailbox` | 🚨 200 | ✅ 403 |
|
||||
| `POST /:id/sync-mailbox-status` | 🚨 200 | ✅ 403 |
|
||||
| `POST /:id/reset-password` | 🚨 fremdes Mailbox-Passwort zurücksetzbar | ✅ 403 |
|
||||
| `POST /:id/sync-forwarding` | (vorher schon gefixt) | ✅ 403 |
|
||||
| `PUT /:id` | 🚨 fremde Adresse änderbar | ✅ 403 |
|
||||
| `DELETE /:id` | 🚨 fremde Adresse löschbar | ✅ 403 |
|
||||
| Eigene Email-ID | (legitim) | ✅ 200/400 (durch) |
|
||||
| Security-Monitor | – | 8× `ACCESS_DENIED MEDIUM` geloggt ✅ |
|
||||
|
||||
**Lehre:** wenn ein Haupt-Endpoint `:id` einen Ownership-Check hat, müssen
|
||||
**alle** Sub-Endpoints unter `:id/*` denselben Check haben. Eine fehlende
|
||||
Zeile am Anfang eines Sub-Controllers reicht für komplette Credential-
|
||||
Exfiltration über das Customer-Portal.
|
||||
|
||||
### Runde 12 – JWT raus aus localStorage (XSS-Resistenz)
|
||||
|
||||
Externer Pentest: "JWT in `localStorage` (MITTEL)". Bei einer XSS-Lücke
|
||||
irgendwo in der App wäre der Token JS-erreichbar → Angreifer könnte alle
|
||||
Anbieter-Credentials abrufen. Aktuell gibt's keinen bekannten XSS-Vektor
|
||||
(CSP `script-src 'self'`, React-DOM-Escaping, keine `dangerouslySetInnerHTML`
|
||||
außer in Admin-befüllten HTML-Templates), aber das Defense-in-Depth-Pattern
|
||||
gehört auf den SPA-Branchenstandard:
|
||||
|
||||
- **Access-Token**: 15 min Lifetime, lebt **nur im JavaScript-Memory**
|
||||
(Modul-State in `api.ts` + `AuthContext`). Kein `localStorage` mehr.
|
||||
- **Refresh-Token**: 7 Tage, im **httpOnly-Cookie** (`Secure` bei
|
||||
`HTTPS_ENABLED`, `SameSite=Strict`, `Path=/api/auth`). JS hat keinen
|
||||
Zugriff → XSS klaut **maximal** einen 15-min-Access-Token.
|
||||
- **POST `/api/auth/refresh`**: liest Cookie, gibt neuen Access aus, rotiert
|
||||
Refresh-Cookie. Prüft `tokenInvalidatedAt` (Logout/Rollenänderung =
|
||||
sofortige Invalidation aller Tokens, auch des Refresh).
|
||||
- **Auth-Middleware**: lehnt Refresh-Tokens (`type: 'refresh'`) als Bearer
|
||||
ab → 401 `"Falscher Token-Typ"`. Defense-in-Depth gegen Token-Confusion.
|
||||
- **Axios-Interceptor**: bei 401 → Single-Flight-Refresh-Retry. Original-Request
|
||||
wird transparent wiederholt; concurrent 401s teilen sich denselben
|
||||
Refresh-Aufruf.
|
||||
- **App-Start**: ruft `/auth/refresh` auf; wenn Cookie gültig → User
|
||||
automatisch eingeloggt, kein Re-Login nach Tab-Reload trotz
|
||||
memory-only Access-Token.
|
||||
- **Logout**: löscht Cookie + setzt `tokenInvalidatedAt` → auch parallele
|
||||
Sessions auf anderen Geräten sind ungültig.
|
||||
|
||||
Live-Tests (alle ✅):
|
||||
|
||||
| Test | Resultat |
|
||||
| --- | --- |
|
||||
| Login | Cookie `HttpOnly; SameSite=Strict; Path=/api/auth` gesetzt, Access-Token im Body |
|
||||
| API-Call mit Bearer | 200 |
|
||||
| API-Call ohne Bearer | 401 |
|
||||
| `/auth/refresh` mit Cookie | 200, rotiertes Cookie, neuer Access |
|
||||
| `/auth/refresh` ohne Cookie | 401 |
|
||||
| Refresh-Token als Bearer benutzt | 401 „Falscher Token-Typ" |
|
||||
| Logout → `/auth/refresh` | 401 (Cookie weg, tokenInvalidatedAt gesetzt) |
|
||||
|
||||
---
|
||||
|
||||
## 🔧 Geprüft + sauber (kein Bug, aber explizit getestet)
|
||||
|
||||
+306
@@ -97,6 +97,312 @@ isolierte Instanz (keine Multi-Tenancy im Code), Provisioning + Abrechnung
|
||||
|
||||
## ✅ Erledigt
|
||||
|
||||
- [x] **🔐 Einmalpasswort-Flow für Portal-Credentials**
|
||||
- **Intention**: Wenn wir Zugangsdaten per E-Mail an den Kunden
|
||||
schicken, kennen wir das Passwort als Admin – das ist solange OK,
|
||||
bis er sich einmal eingeloggt hat. Danach soll er gezwungen sein,
|
||||
sich ein eigenes zu vergeben, und das per-Mail-Passwort ist tot.
|
||||
- **Datenmodell**: neues Feld `portalPasswordMustChange: Boolean
|
||||
@default(false)` am Customer.
|
||||
- **Flow**:
|
||||
1. Admin klickt **Zugangsdaten versenden** → Flag wird gesetzt,
|
||||
Mail-Template weist explizit auf „Einmalpasswort" hin.
|
||||
2. Kunde loggt sich mit dem OTP ein → Backend gibt
|
||||
`mustChangePassword: true` im Login-Response zurück UND
|
||||
**konsumiert das OTP sofort**: setzt `portalPasswordHash =
|
||||
null` und `portalPasswordEncrypted = null`. Ein zweiter
|
||||
Login mit demselben Passwort schlägt fehl (401).
|
||||
3. Frontend (`ProtectedRoute`) sieht `mustChangePassword=true`
|
||||
und leitet auf `/change-initial-password` um – egal welche
|
||||
Route der Kunde aufrufen will, er kommt nicht weiter.
|
||||
4. Auf der Seite gibt er ein neues, komplexes Passwort vor
|
||||
(Live-Hint mit ✓/○, dieselben Regeln wie Backend).
|
||||
5. `POST /api/auth/change-initial-portal-password` speichert
|
||||
neuen Hash, **löscht das Encrypted-Feld** (Admin kann das
|
||||
eigene Passwort des Kunden nicht mehr im Klartext lesen),
|
||||
setzt `portalTokenInvalidatedAt = now()` und
|
||||
`portalPasswordMustChange = false`.
|
||||
6. Frontend loggt aus, leitet zu `/login?changed=1`,
|
||||
Erfolgs-Banner: „Passwort wurde geändert. Bitte mit dem
|
||||
neuen Passwort anmelden."
|
||||
- **Edge case**: Tab geschlossen ohne Setzen → Kunde ist
|
||||
ausgesperrt (OTP weg, eigenes Passwort nicht gesetzt). Lösung
|
||||
aus seiner Sicht: Passwort-vergessen-Funktion oder Admin
|
||||
versendet neue Zugangsdaten.
|
||||
- **Edge case**: Admin macht zwischendurch nochmal manuelles
|
||||
„Setzen" → `mustChange` wird automatisch wieder `false`. So
|
||||
kann ein versehentlich versendetes OTP problemlos durch ein
|
||||
direkt-gesetztes Passwort ersetzt werden.
|
||||
- **Live-verifiziert (10 Schritte)**: Setzen → Send → Flag in
|
||||
DB=true → Login mit OTP gibt mustChange=true zurück + Hash
|
||||
in DB ist null → Re-Login mit OTP → 401 → Change-Endpoint
|
||||
schwach → 400 → komplex → 200 → Login mit neuem PW →
|
||||
mustChange=false + tokenInvalidatedAt gesetzt.
|
||||
|
||||
- [x] **🔐 Passwort-Komplexität + Portal-Credentials-UX**
|
||||
- **Problem**: Bisher reichten 6 Zeichen für gesetzte Passwörter
|
||||
(Portal-Login, User-Reset, Registrierung, User-Anlage). Das hat
|
||||
der Pentest bemängelt, und es entsprach auch nicht dem, was wir
|
||||
selbst von Endkunden erwarten würden.
|
||||
- **Lösung**:
|
||||
* `validatePasswordComplexity()` in `passwordGenerator.ts`:
|
||||
mind. 12 Zeichen + Großbuchstaben + Kleinbuchstaben + Ziffer
|
||||
+ Sonderzeichen, mit detaillierter Fehlerliste auf deutsch.
|
||||
* Erzwungen in **5 Endpoints**: `setPortalPassword`,
|
||||
`confirmPasswordReset`, `register`, `createUser`, `updateUser`.
|
||||
- **Neue UX im Kunden-Portal-Block (CustomerDetail)**:
|
||||
* **Generate-Button**: erzeugt 16-Zeichen-Zufallspasswort, das
|
||||
garantiert allen Komplexitätsregeln entspricht, und füllt
|
||||
das Eingabefeld direkt aus.
|
||||
* **Send-Credentials-Button**: schickt Login-URL + Username +
|
||||
Klartext-Passwort an die Kunden-E-Mail. Funktioniert nur,
|
||||
wenn "Portal aktiviert" tatsächlich aktiviert ist.
|
||||
* **Live-Komplexitäts-Hint** beim Tippen: ✓/○-Liste zeigt
|
||||
sofort, welche Regeln noch fehlen.
|
||||
* `alert()`-Boxen durch Toast-Notifications ersetzt.
|
||||
- **Live-verifiziert**: schwaches Passwort `hallo123` → HTTP 400
|
||||
mit Fehlerliste, komplexes Passwort `Hallo123!Test` → HTTP 200,
|
||||
Generator-Endpoint liefert 16-Zeichen-Passwort, Send-Credentials
|
||||
versendet Mail nur bei portalEnabled=true.
|
||||
|
||||
- [x] **🌐 Real-IP hinter Nginx-Proxy-Manager**
|
||||
- **Problem**: Rate-Limiter und Security-Monitor haben statt der
|
||||
echten Client-IP nur die NPM-IP (`172.0.2.12`) geloggt. Damit
|
||||
wären alle Threshold-basierten Blockings nutzlos – ein Brute-
|
||||
Force von 100 verschiedenen Clients wäre für uns 1 Quelle.
|
||||
- **Root Cause**: `app.set('trust proxy', 'loopback')` – das passt
|
||||
nur, wenn der Proxy auf 127.0.0.1 läuft. NPM läuft aber auf
|
||||
einem anderen Host, also wurde X-Forwarded-For ignoriert.
|
||||
- **Fix**: trust-proxy abhängig von `HTTPS_ENABLED`:
|
||||
`HTTPS_ENABLED=true` → `1` (genau 1 Hop, der NPM), sonst
|
||||
`loopback` (Direkt-Verbindungen lokal).
|
||||
- **Live-verifiziert**: req.ip zeigt jetzt die echte Browser-IP
|
||||
statt der NPM-IP, Threshold-Events triggern korrekt.
|
||||
|
||||
- [x] **🚨 KRITISCH: IDOR auf Stressfrei-Email-Sub-Routes (Pentest-Fund)**
|
||||
- **Realer Angriff erfolgreich durchgespielt**: Portal-User konnte über
|
||||
`/api/stressfrei-emails/{id}/credentials` die kompletten Klartext-
|
||||
IMAP/SMTP-Zugangsdaten der Mailbox eines anderen Kunden abrufen.
|
||||
- **Root Cause**: der Haupt-Endpoint `GET /:id` hatte
|
||||
`canAccessStressfreiEmail`-Check, die **8 Sub-Endpoints** unter
|
||||
`:id/*` hatten alle KEINEN Ownership-Check (nur `authenticate +
|
||||
requirePermission('customers:read')`, was Portal-User von Haus aus
|
||||
haben).
|
||||
- **Fix**: `canAccessStressfreiEmail(req, res, id)` als erste Zeile in
|
||||
allen 9 betroffenen Controllern: `getMailboxCredentials`,
|
||||
`getFolderCounts`, `syncAccount`, `sendEmailFromAccount`,
|
||||
`enableMailbox`, `syncMailboxStatus`, `resetPassword`, `updateEmail`,
|
||||
`deleteEmail`.
|
||||
- **Security-Monitor**: `canAccessResourceByCustomerId` emittiert
|
||||
bei jedem Fehlversuch automatisch ein `ACCESS_DENIED MEDIUM`-Event
|
||||
→ Threshold-Detection (>5 in 5 min) erzeugt `CRITICAL SUSPICIOUS` +
|
||||
Sofort-Alert.
|
||||
- **Live-verifiziert**: Portal-User Kunde A probiert Email-ID von
|
||||
Kunde B durch alle 8 Sub-Routes → **alle 8× HTTP 403**, eigene
|
||||
Email-ID kommt sauber durch (200/400), 8× `ACCESS_DENIED`-Events
|
||||
im Security-Monitor.
|
||||
|
||||
- [x] **🛡️ JWT-Tokens raus aus localStorage – Refresh-Cookie-Pattern**
|
||||
- Pentest-Finding „JWT in localStorage (MITTEL)": bei XSS könnte JS
|
||||
den Token klauen + alle Anbieter-Credentials abrufen. Lösung:
|
||||
Branchenstandard für SPAs.
|
||||
- **Access-Token**: kurzlebig (15 min), lebt nur im
|
||||
JavaScript-Memory (Modul-State + AuthContext). Kein localStorage
|
||||
mehr → XSS-Angriff klaut maximal einen 15-min-Token, mit dem er
|
||||
eh nicht weit kommt.
|
||||
- **Refresh-Token**: 7 Tage Lifetime, im **httpOnly-Cookie** (`Secure`
|
||||
bei HTTPS_ENABLED, `SameSite=Strict`, `Path=/api/auth`). JavaScript
|
||||
hat **keinen Zugriff** → XSS kann ihn nicht klauen.
|
||||
- Backend:
|
||||
* `signAccessToken/signRefreshToken` mit `type`-Claim als
|
||||
Unterscheidung; Auth-Middleware lässt nur `type=access` durch
|
||||
* Login + Customer-Login setzen Cookie + geben Access im Body
|
||||
* `POST /api/auth/refresh` liest Cookie, gibt neuen Access aus,
|
||||
rotiert Refresh-Cookie, prüft `tokenInvalidatedAt`
|
||||
(sofortige Invalidation bei Rolle-Ändern/Logout)
|
||||
* Logout löscht Cookie + setzt `tokenInvalidatedAt`
|
||||
* `cookie-parser` als neue dependency
|
||||
- Frontend:
|
||||
* `api.ts`: in-memory `tokenStore` + axios-Interceptor mit
|
||||
Auto-Refresh-Retry bei 401 (single-flight gegen
|
||||
Concurrent-Requests)
|
||||
* `AuthContext`: beim App-Start `/auth/refresh` aufrufen → wenn
|
||||
Cookie noch gültig, ist der User automatisch eingeloggt
|
||||
(kein Re-Login nach Tab-Reload trotz memory-only Access-Token)
|
||||
* 9 alte `localStorage.getItem('token')`-Stellen migriert auf
|
||||
`getAccessToken()` (PDF-Vorschau-iframe, Audit-Log-Export,
|
||||
Backup-Download, File-Download-URL, …)
|
||||
- Live verifiziert: Login setzt Cookie+Bearer, API-Calls mit
|
||||
Bearer→200, ohne→401, Refresh-Endpoint rotiert Cookie sauber,
|
||||
Refresh-Token wird als Bearer (Access) abgelehnt („Falscher
|
||||
Token-Typ"), Logout löscht Cookie + invalidiert Token.
|
||||
|
||||
- [x] **🔒 Audit-Log für alle Klartext-Passwort-Reads**
|
||||
- Pentest-Finding „Klartext-Passwörter über API abrufbar (HIGH,
|
||||
post-auth)" → reversible Verschlüsselung ist by-design (Feature
|
||||
„Anbieter-Login anzeigen" braucht es), aber jeder Decrypt-Vorgang
|
||||
sollte im Audit-Log auftauchen. Bisher: keiner der 6 Endpoints
|
||||
schrieb ein Log.
|
||||
- Audit-Logs jetzt für: `getPortalPassword`, `getContractPassword`,
|
||||
`getSimCardCredentials`, `getInternetCredentials`,
|
||||
`getSipCredentials`, `getMailboxCredentials`.
|
||||
- `action: 'READ'`, eigene Resource-Types (PortalPassword,
|
||||
ContractPassword, SimCardCredentials, InternetCredentials,
|
||||
SipCredentials, MailboxCredentials), alle mit `sensitivity:
|
||||
CRITICAL` über die Sensitivity-Map.
|
||||
- Label nennt explizit „Klartext … entschlüsselt" + Ressourcen-ID,
|
||||
damit im Audit-Log-Viewer auf einen Blick erkennbar ist, was
|
||||
passiert ist (DSGVO-Nachvollziehbarkeit + Insider-Threat-Erkennung).
|
||||
|
||||
- [x] **↗ E-Mail-Postfach: Weiterleiten + Erneut senden**
|
||||
- **Weiterleiten** (Compose-Modal-Erweiterung): neuer Button im
|
||||
EmailDetail öffnet das ComposeEmailModal im Forward-Modus –
|
||||
To-Feld leer (User trägt den neuen Empfänger ein), Betreff mit
|
||||
„Fwd:"-Prefix, Body mit zitierten Original-Headern (Von, An,
|
||||
Datum, Betreff) + Original-Text.
|
||||
- **Erneut senden** (One-Click): schickt die Mail noch einmal an
|
||||
die ursprüngliche Empfänger-Adresse (= die Stressfrei-Adresse
|
||||
selbst). Damit läuft sie durch die heute hinterlegten Forwards
|
||||
und landet beim aktuell konfigurierten Kunden-Postfach – Use-Case:
|
||||
Stressfrei-Adresse wurde nach Empfang umgestellt, Original ist nur
|
||||
in der alten Inbox. Confirm-Dialog mit Hinweis, dass Anhänge nicht
|
||||
erneut mit gesendet werden (Weiterleiten dafür nutzen). Toast für
|
||||
Erfolg/Fehler.
|
||||
|
||||
- [x] **🔍 E-Mail-Postfach: Suche + erweiterte Filter (Variante B)**
|
||||
- Suchleiste über der Email-Liste – durchsucht parallel Subject,
|
||||
From-Address/Name und Body.
|
||||
- Filter-Button mit Badge (Anzahl aktiver Filter) klappt eine Box mit
|
||||
Detail-Filtern auf: Von, An, Betreff, Inhalt, Datum von/bis,
|
||||
Anhang-Dateiname, Mit/Ohne Anhang, Gelesen-Status, Markiert-Status.
|
||||
Alle Filter werden im Backend mit UND verknüpft.
|
||||
- „Alle zurücksetzen"-Button räumt komplett auf.
|
||||
- Backend: `GET /api/customers/:id/emails` nimmt die Filter als
|
||||
Query-Parameter entgegen, `getCachedEmails` übersetzt sie in eine
|
||||
Prisma `where`-Klausel.
|
||||
- **Bewusst nicht gebaut**: voller AND/OR-Builder mit Plus-Button und
|
||||
Bool-Verschachtelung – Trade-off-Diskussion mit User: reale
|
||||
Use-Cases sind quasi immer AND, UI-Komplexität verschachtelter
|
||||
Bool-Builder bringt mehr Bedienprobleme als Mehrwert.
|
||||
|
||||
- [x] **🔁 Stressfrei-Adressen: Weiterleitungen + Passwort manuell synchronisieren**
|
||||
- Refresh-Icon-Button in der Action-Reihe jeder Stressfrei-Adresse
|
||||
(Tooltip erklärt: „ersetzt die Forwards am Provider durch
|
||||
Kunden-Stamm-E-Mail + Service-Adresse"). Use-Case: nach Änderung der
|
||||
Stamm-E-Mail eines Kunden, oder nach Wechsel der
|
||||
`defaultForwardEmail` in den Provider-Settings.
|
||||
- **Bei `hasMailbox: true`** wird zusätzlich das im CRM verschlüsselt
|
||||
hinterlegte Mailbox-Passwort am Provider neu gesetzt. Self-Healing
|
||||
für den Fall, dass jemand im Plesk-UI manuell ein anderes Passwort
|
||||
gesetzt hat und IMAP/SMTP im CRM nicht mehr passt.
|
||||
- Backend nutzt Plesk's `updateForwardTargets` (`set:email1,email2`
|
||||
→ ersetzt komplett, idempotent) + bei Mailbox auch
|
||||
`updateMailboxPassword` (Plesk-Passwort-Update).
|
||||
- Endpoint: `POST /api/stressfrei-emails/:id/sync-forwarding`,
|
||||
`customers:update`-Permission, Audit-Log mit Forward-Targets +
|
||||
Passwort-Reset-Marker.
|
||||
- Self-Healing: `isProvisioned`-Flag wird bei erfolgreichem
|
||||
Provider-Aufruf automatisch auf `true` korrigiert (historischer Bug:
|
||||
Flag wurde beim `createEmail` mit `provisionAtProvider: true` nie
|
||||
gesetzt – jetzt behoben + Backfill via Sync).
|
||||
- Erfolgs-/Fehler-Meldungen via `react-hot-toast` (statt `alert()`)
|
||||
mit Liste der gesetzten Forward-Targets + Hinweis ob Passwort-Reset
|
||||
durchgeführt wurde.
|
||||
- In der Kundenakte (Stammdaten → Kontakt → E-Mail) externes
|
||||
Link-Icon, das in neuem Tab direkt den Stressfrei-Tab des Kunden
|
||||
öffnet – sichtbar nur wenn Stressfrei-Adressen vorhanden sind.
|
||||
|
||||
- [x] **🛡️ Pentest-Hardening-Runde 11: Header-Hygiene**
|
||||
- **HSTS-Doppel-Header** (18× low im Audit): Helmet's
|
||||
`Strict-Transport-Security` komplett deaktiviert. Der Nginx Proxy Manager
|
||||
vor der CRM-VM setzt HSTS bereits, doppelter Header verletzte RFC 6797.
|
||||
- **Cache-Control** (≥10× info im Audit):
|
||||
`/api/*` bekommt `no-store` (sensible JSON-Daten),
|
||||
SPA-HTML (`/`, `/sitemap.xml`, `/robots.txt`, `/vite.svg`) bekommt
|
||||
`no-store, must-revalidate` (sonst hängt Browser an alter index.html
|
||||
fest nach Deploy),
|
||||
`/assets/*` (Vite-Build mit Content-Hash im Filename) bekommt
|
||||
`public, max-age=31536000, immutable`.
|
||||
- **CSP No-Fallback-Direktiven** (2× medium): `worker-src`, `manifest-src`,
|
||||
`media-src` explizit auf `'self'` – ZAP markiert sonst „Failure to
|
||||
Define Directive with No Fallback".
|
||||
- Bewusst NICHT angefasst: `style-src 'unsafe-inline'` (Tailwind/React-
|
||||
inline-styles, kompletter Refactor unverhältnismäßig).
|
||||
- Live verifiziert: Headers für `/`, `/api/*`, `/assets/*.js` und SPA-
|
||||
Fallback-Pfade alle wie erwartet.
|
||||
|
||||
- [x] **🐛 PDF-Vorschau im PDF-Template-Editor lädt nicht**
|
||||
- CSP-Direktive `frame-ancestors 'none'` blockte ALLE iframe-Embeddings
|
||||
der eigenen Resourcen, auch same-origin – Browser zeigte je nach
|
||||
Variante "Verbindung abgelehnt" oder CSP-Violation.
|
||||
- Fix: `frame-ancestors 'self'` (statt `'none'`). App darf eigene
|
||||
Resourcen embeden (z.B. die annotierte PDF-Vorschau), externe Sites
|
||||
bleiben weiterhin gesperrt.
|
||||
|
||||
- [x] **🔁 Factory-Defaults Sync-Scripts (dev ↔ prod ↔ Image)**
|
||||
- `./factory-export.sh` zieht eine ZIP per API in `factory-exports/`
|
||||
(gitignored Drop-Box).
|
||||
- `./factory-import.sh [zip]` lädt die ZIP per API in eine andere Instanz
|
||||
– ohne Argument wählt es die jüngste ZIP automatisch.
|
||||
- `./factory-import.sh --save-as-builtin` entpackt die ZIP zusätzlich nach
|
||||
`backend/factory-defaults/` (vorher aufgeräumt). Damit landet sie beim
|
||||
nächsten `docker-compose up --build` als Werkseinstellung im Image und
|
||||
seedet frische DBs automatisch.
|
||||
- Konfigurierbar per Env: `OPENCRM_URL`, `OPENCRM_EMAIL`,
|
||||
`OPENCRM_PASSWORD` (sonst interaktive Abfrage).
|
||||
- README-Abschnitt „Factory-Defaults: Stammdaten-Kataloge teilen"
|
||||
komplett überarbeitet (drei Transport-Pfade, Auto-Seed, Whitelist).
|
||||
|
||||
- [x] **🚀 Auto-Seed: Werkseinstellungen beim Erst-Deploy**
|
||||
- Inhalt von `backend/factory-defaults/` wird via Dockerfile als
|
||||
`/app/factory-defaults-builtin/` ins Image gebrannt.
|
||||
- Entrypoint spielt sie nach erfolgreichem Auto-Seed (frische DB) automatisch
|
||||
via `tsx scripts/seed-factory-defaults.ts` ein – steuerbar über
|
||||
`FACTORY_DEFAULTS_DIR`.
|
||||
- Damit bringen neue VMs sofort Anbieter, Tarife, PDF-Auftragsvorlagen +
|
||||
Datenschutzerklärung/Impressum mit, ohne manuelles UI-/CLI-Import.
|
||||
- Bestehende Installs werden NIE überschrieben (Trigger nur wenn der
|
||||
Auto-Seed im selben Start-Lauf gelaufen ist).
|
||||
|
||||
- [x] **📦 Factory-Defaults: HTML-Templates + Import via UI**
|
||||
- Datenschutzerklärung, Impressum, Vollmacht-Vorlage und Website-Datenschutz
|
||||
werden jetzt mit ins Factory-Defaults-ZIP gepackt (`app-settings/`-Ordner,
|
||||
Whitelist-geschützt – andere AppSetting-Keys werden ignoriert).
|
||||
- Import läuft jetzt auch über die UI (Einstellungen → Factory-Defaults →
|
||||
„ZIP hochladen"). Der CLI-Weg `npm run seed:defaults` bleibt erhalten und
|
||||
wurde gleichermaßen um die HTML-Templates erweitert.
|
||||
- Zwei-Wege-Roundtrip live verifiziert: Export → AppSetting löschen →
|
||||
Import → Wert wieder vollständig hergestellt; Counts in Audit-Log.
|
||||
|
||||
- [x] **🐛 Benutzer-Verwaltung: DSGVO- + Entwickler-Zugriff zuweisbar**
|
||||
- Mass-Assignment-Whitelist (`pickUserUpdate`) hat `hasGdprAccess` /
|
||||
`hasDeveloperAccess` rausgefiltert → Service erhielt sie nie → Rollen
|
||||
DSGVO/Developer waren in der UI nicht zuweisbar (Checkbox ohne Wirkung).
|
||||
- Beide Felder zur Whitelist hinzugefügt + Audit-Log liest die Pre-Werte
|
||||
jetzt aus den geladenen Rollen (kein False-Positive-Change mehr).
|
||||
|
||||
- [x] **🔒 HTTPS-only-Header per Flag (`HTTPS_ENABLED`)**
|
||||
- HSTS + `upgrade-insecure-requests` (CSP) sperrten den Browser bei
|
||||
direktem `http://ip:port`-Zugriff aus (`ERR_SSL_PROTOCOL_ERROR`).
|
||||
- Beide Header default OFF, kommen nur mit `HTTPS_ENABLED=true` (sobald
|
||||
TLS-Reverse-Proxy davor steht).
|
||||
|
||||
- [x] **🗃️ Prisma-Migrations-System (statt `db push`)**
|
||||
- Initial-Migration `0_init` aus aktuellem Schema generiert
|
||||
(`prisma migrate diff --from-empty --to-schema-datamodel`).
|
||||
- 24 alte gedriftete Migrations gelöscht – frischer Start.
|
||||
- `migration_lock.toml` für MySQL hinzugefügt.
|
||||
- Container-Entrypoint umgebaut:
|
||||
- Auto-Baseline-Detection: bestehende DB ohne `_prisma_migrations` →
|
||||
`migrate resolve --applied 0_init` läuft automatisch.
|
||||
- Statt `db push --accept-data-loss` jetzt `migrate deploy` (idempotent,
|
||||
datenerhaltend, keine stillen DROPs mehr).
|
||||
- Neuer npm-Script `schema:sync` (lokal/Dev): legt automatisch eine
|
||||
versionierte Migration mit Zeitstempel-Namen an
|
||||
(`prisma migrate dev --name auto_$(date +%Y%m%d_%H%M%S)`).
|
||||
- Workflow ab jetzt: schema.prisma ändern → `npm run schema:sync` →
|
||||
Migration committen → Push → Container-Restart wendet sie automatisch an.
|
||||
|
||||
- [x] **🔄 Automatische Vertrags-Status-Übergänge**
|
||||
- Nightly-Cron (02:00 + Catch-up 60s nach Start): alle Verträge mit
|
||||
`status=ACTIVE` und `endDate < heute` → `EXPIRED` (mit Audit-Log).
|
||||
|
||||
Executable
+64
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env bash
|
||||
# Factory-Defaults-Export – holt eine ZIP vom laufenden OpenCRM und legt sie
|
||||
# in ./factory-exports/ ab. Dieselbe ZIP, die du auch über die UI bekommst.
|
||||
#
|
||||
# Workflow:
|
||||
# ./factory-export.sh # default: localhost:3010, admin@admin.com
|
||||
# OPENCRM_URL=https://crm.example.de \
|
||||
# OPENCRM_EMAIL=admin@example.de \
|
||||
# ./factory-export.sh # gegen die Prod-Instanz
|
||||
#
|
||||
# Optional:
|
||||
# OPENCRM_PASSWORD=… (sonst wird interaktiv abgefragt)
|
||||
#
|
||||
# Die ZIP ist gitignored – du kannst sie via scp transferieren und mit
|
||||
# ./factory-import.sh auf der anderen Seite einspielen.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
URL="${OPENCRM_URL:-http://localhost:3010}"
|
||||
EMAIL="${OPENCRM_EMAIL:-admin@admin.com}"
|
||||
PASSWORD="${OPENCRM_PASSWORD:-}"
|
||||
|
||||
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
EXPORT_DIR="$REPO_ROOT/factory-exports"
|
||||
mkdir -p "$EXPORT_DIR"
|
||||
|
||||
if [ -z "$PASSWORD" ]; then
|
||||
read -r -s -p "Passwort für $EMAIL @ $URL: " PASSWORD
|
||||
echo
|
||||
fi
|
||||
|
||||
echo "→ Login als $EMAIL @ $URL"
|
||||
LOGIN_RESPONSE="$(curl -sS -X POST "$URL/api/auth/login" \
|
||||
-H 'Content-Type: application/json' \
|
||||
--data-raw "$(E="$EMAIL" P="$PASSWORD" python3 -c 'import json,os;print(json.dumps({"email":os.environ["E"],"password":os.environ["P"]}))')")"
|
||||
|
||||
TOKEN="$(printf '%s' "$LOGIN_RESPONSE" | python3 -c 'import json,sys;d=json.load(sys.stdin);print((d.get("data") or {}).get("token","") or d.get("token",""))')"
|
||||
|
||||
if [ -z "$TOKEN" ]; then
|
||||
echo "✗ Login fehlgeschlagen. Antwort:"
|
||||
echo "$LOGIN_RESPONSE" | head -c 500
|
||||
echo
|
||||
exit 1
|
||||
fi
|
||||
|
||||
TIMESTAMP="$(date +%Y-%m-%d-%H%M)"
|
||||
DEST="$EXPORT_DIR/factory-defaults-$TIMESTAMP.zip"
|
||||
|
||||
echo "→ Lade ZIP nach $DEST"
|
||||
HTTP_CODE="$(curl -sS -o "$DEST" -w '%{http_code}' \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
"$URL/api/factory-defaults/export")"
|
||||
|
||||
if [ "$HTTP_CODE" != "200" ]; then
|
||||
echo "✗ Export-Endpoint antwortete mit HTTP $HTTP_CODE"
|
||||
rm -f "$DEST"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
SIZE_KB="$(du -k "$DEST" | cut -f1)"
|
||||
echo "✓ Export erfolgreich: $DEST (${SIZE_KB} KB)"
|
||||
echo
|
||||
echo "Inhalt:"
|
||||
unzip -l "$DEST" | sed 's/^/ /'
|
||||
Executable
+140
@@ -0,0 +1,140 @@
|
||||
#!/usr/bin/env bash
|
||||
# Factory-Defaults-Import – pflegt eine ZIP in eine OpenCRM-Instanz ein.
|
||||
# Idempotent (upserts pro Kategorie, nichts wird gelöscht).
|
||||
#
|
||||
# Aufruf:
|
||||
# ./factory-import.sh # jüngste ZIP aus factory-exports/
|
||||
# ./factory-import.sh ./factory-exports/foo.zip # explizite ZIP
|
||||
# ./factory-import.sh --save-as-builtin # nach Import auch ins
|
||||
# ./factory-import.sh --save-as-builtin ./foo.zip # backend/factory-defaults/
|
||||
# # entpacken → nächster
|
||||
# # Image-Build hat sie
|
||||
# # als Werkseinstellung
|
||||
#
|
||||
# ENV (wie factory-export.sh):
|
||||
# OPENCRM_URL (default http://localhost:3010)
|
||||
# OPENCRM_EMAIL (default admin@admin.com)
|
||||
# OPENCRM_PASSWORD (sonst interaktiv)
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
URL="${OPENCRM_URL:-http://localhost:3010}"
|
||||
EMAIL="${OPENCRM_EMAIL:-admin@admin.com}"
|
||||
PASSWORD="${OPENCRM_PASSWORD:-}"
|
||||
|
||||
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
EXPORT_DIR="$REPO_ROOT/factory-exports"
|
||||
BUILTIN_DIR="$REPO_ROOT/backend/factory-defaults"
|
||||
|
||||
# Argumente parsen: erlaubt sind --save-as-builtin und 0/1 ZIP-Pfade in
|
||||
# beliebiger Reihenfolge.
|
||||
SAVE_AS_BUILTIN=false
|
||||
ZIP_PATH=""
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--save-as-builtin) SAVE_AS_BUILTIN=true ;;
|
||||
-h|--help)
|
||||
sed -n '2,16p' "$0" | sed 's/^# \?//'
|
||||
exit 0
|
||||
;;
|
||||
--*) echo "✗ Unbekanntes Flag: $arg"; exit 2 ;;
|
||||
*)
|
||||
if [ -n "$ZIP_PATH" ]; then
|
||||
echo "✗ Mehrere ZIP-Pfade angegeben (nur einer erlaubt)"; exit 2
|
||||
fi
|
||||
ZIP_PATH="$arg"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ -z "$ZIP_PATH" ]; then
|
||||
# Jüngste ZIP automatisch wählen
|
||||
ZIP_PATH="$(ls -1t "$EXPORT_DIR"/*.zip 2>/dev/null | head -1 || true)"
|
||||
if [ -z "$ZIP_PATH" ]; then
|
||||
echo "✗ Keine ZIP angegeben und keine in $EXPORT_DIR/ gefunden."
|
||||
echo " Aufruf: ./factory-import.sh <pfad/zur/factory-defaults.zip>"
|
||||
exit 1
|
||||
fi
|
||||
echo "→ Keine ZIP angegeben – nehme jüngste aus $EXPORT_DIR/:"
|
||||
echo " $(basename "$ZIP_PATH")"
|
||||
fi
|
||||
|
||||
if [ ! -f "$ZIP_PATH" ]; then
|
||||
echo "✗ Datei nicht gefunden: $ZIP_PATH"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ -z "$PASSWORD" ]; then
|
||||
read -r -s -p "Passwort für $EMAIL @ $URL: " PASSWORD
|
||||
echo
|
||||
fi
|
||||
|
||||
echo "→ Login als $EMAIL @ $URL"
|
||||
LOGIN_RESPONSE="$(curl -sS -X POST "$URL/api/auth/login" \
|
||||
-H 'Content-Type: application/json' \
|
||||
--data-raw "$(E="$EMAIL" P="$PASSWORD" python3 -c 'import json,os;print(json.dumps({"email":os.environ["E"],"password":os.environ["P"]}))')")"
|
||||
|
||||
TOKEN="$(printf '%s' "$LOGIN_RESPONSE" | python3 -c 'import json,sys;d=json.load(sys.stdin);print((d.get("data") or {}).get("token","") or d.get("token",""))')"
|
||||
|
||||
if [ -z "$TOKEN" ]; then
|
||||
echo "✗ Login fehlgeschlagen. Antwort:"
|
||||
echo "$LOGIN_RESPONSE" | head -c 500
|
||||
echo
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "→ Upload + Import: $(basename "$ZIP_PATH")"
|
||||
RESPONSE="$(curl -sS -X POST "$URL/api/factory-defaults/import" \
|
||||
-H "Authorization: Bearer $TOKEN" \
|
||||
-F "zip=@$ZIP_PATH")"
|
||||
|
||||
# Hübsch ausgeben + auf success prüfen
|
||||
if ! printf '%s' "$RESPONSE" | python3 -c '
|
||||
import json, sys
|
||||
r = json.load(sys.stdin)
|
||||
if not r.get("success"):
|
||||
print("✗ Import fehlgeschlagen:", r.get("error", "(unbekannt)"))
|
||||
sys.exit(1)
|
||||
d = r.get("data", {})
|
||||
print("✓ Import erfolgreich:")
|
||||
for label, key in [
|
||||
("Anbieter", "providers"),
|
||||
("Tarife", "tariffs"),
|
||||
("Kündigungsfristen", "cancellationPeriods"),
|
||||
("Laufzeiten", "contractDurations"),
|
||||
("Vertragskategorien","contractCategories"),
|
||||
("PDF-Vorlagen", "pdfTemplates"),
|
||||
("HTML-Templates", "appSettings"),
|
||||
]:
|
||||
print(f" {label}: {d.get(key, 0)}")
|
||||
skipped = d.get("pdfTemplatesSkipped", 0)
|
||||
if skipped:
|
||||
print(f" (PDF-Vorlagen übersprungen: {skipped})")
|
||||
warnings = d.get("warnings", []) or []
|
||||
if warnings:
|
||||
print("Hinweise:")
|
||||
for w in warnings:
|
||||
print(f" - {w}")
|
||||
'; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# --save-as-builtin: ZIP zusätzlich in backend/factory-defaults/ entpacken,
|
||||
# damit der nächste Image-Build sie als Werkseinstellung mitnimmt.
|
||||
# Vorher räumen wir auf (außer README.md + .gitkeep), damit nichts Veraltetes
|
||||
# liegen bleibt.
|
||||
if [ "$SAVE_AS_BUILTIN" = "true" ]; then
|
||||
echo
|
||||
echo "→ --save-as-builtin: aktualisiere $BUILTIN_DIR/"
|
||||
if [ ! -d "$BUILTIN_DIR" ]; then
|
||||
mkdir -p "$BUILTIN_DIR"
|
||||
fi
|
||||
# Aufräumen: alles außer README.md und .gitkeep löschen
|
||||
find "$BUILTIN_DIR" -mindepth 1 \
|
||||
\! -name 'README.md' \! -name '.gitkeep' \
|
||||
-delete
|
||||
# ZIP entpacken (manifest.json kommt mit, ist aber harmlos)
|
||||
unzip -q -o "$ZIP_PATH" -d "$BUILTIN_DIR"
|
||||
echo "✓ Werkseinstellungen aktualisiert. Beim nächsten 'docker-compose up"
|
||||
echo " --build' landen sie im Image und seeden frische DBs automatisch."
|
||||
fi
|
||||
+2
-1
@@ -5,10 +5,11 @@ node_modules/
|
||||
dist/
|
||||
dist-ssr/
|
||||
|
||||
# Environment
|
||||
# Environment – echte Secrets blocken, .env.example weiter mittracken
|
||||
.env
|
||||
.env.local
|
||||
.env.*.local
|
||||
!.env.example
|
||||
|
||||
# Logs
|
||||
*.log
|
||||
|
||||
+27
-1
@@ -8,6 +8,7 @@ import Layout from './components/layout/Layout';
|
||||
import Login from './pages/Login';
|
||||
import PasswordResetRequest from './pages/PasswordResetRequest';
|
||||
import PasswordResetConfirm from './pages/PasswordResetConfirm';
|
||||
import ChangeInitialPassword from './pages/ChangeInitialPassword';
|
||||
import Dashboard from './pages/Dashboard';
|
||||
import CustomerList from './pages/customers/CustomerList';
|
||||
import CustomerDetail from './pages/customers/CustomerDetail';
|
||||
@@ -49,7 +50,7 @@ import PortalProfile from './pages/portal/PortalProfile';
|
||||
import PortalMeters from './pages/portal/PortalMeters';
|
||||
|
||||
function ProtectedRoute({ children }: { children: React.ReactNode }) {
|
||||
const { isAuthenticated, isLoading } = useAuth();
|
||||
const { isAuthenticated, isLoading, user } = useAuth();
|
||||
|
||||
if (isLoading) {
|
||||
return (
|
||||
@@ -63,9 +64,31 @@ function ProtectedRoute({ children }: { children: React.ReactNode }) {
|
||||
return <Navigate to="/login" replace />;
|
||||
}
|
||||
|
||||
// Force-Change-Password-Flow: nach Einmalpasswort-Login muss der Kunde
|
||||
// zwingend ein eigenes Passwort vergeben, bevor er irgendwohin sonst
|
||||
// navigieren darf.
|
||||
if (user?.mustChangePassword) {
|
||||
return <Navigate to="/change-initial-password" replace />;
|
||||
}
|
||||
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
||||
function ChangeInitialPasswordGate() {
|
||||
const { isAuthenticated, isLoading, user } = useAuth();
|
||||
if (isLoading) {
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center">
|
||||
<div className="text-gray-500">Laden...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
if (!isAuthenticated) return <Navigate to="/login" replace />;
|
||||
// Wer nicht im Einmalpasswort-Flow ist, hat hier nichts zu suchen.
|
||||
if (!user?.mustChangePassword) return <Navigate to="/" replace />;
|
||||
return <ChangeInitialPassword />;
|
||||
}
|
||||
|
||||
function PortalConsentGate({ children }: { children: React.ReactNode }) {
|
||||
const { isCustomerPortal } = useAuth();
|
||||
|
||||
@@ -153,6 +176,9 @@ function App() {
|
||||
<Route path="/password-reset/request" element={<PasswordResetRequest />} />
|
||||
<Route path="/password-reset" element={<PasswordResetConfirm />} />
|
||||
|
||||
{/* Einmalpasswort → eigenes Passwort vergeben (eingeloggt, eigene Gate-Logik) */}
|
||||
<Route path="/change-initial-password" element={<ChangeInitialPasswordGate />} />
|
||||
|
||||
<Route
|
||||
path="/"
|
||||
element={
|
||||
|
||||
@@ -11,6 +11,7 @@ interface ComposeEmailModalProps {
|
||||
onClose: () => void;
|
||||
account: MailboxAccount;
|
||||
replyTo?: CachedEmail;
|
||||
forwardOf?: CachedEmail; // Weiterleiten: Body vorausgefüllt, To leer
|
||||
onSuccess?: () => void;
|
||||
contractId?: number; // Optional: Vertrag dem die gesendete E-Mail zugeordnet wird
|
||||
}
|
||||
@@ -20,6 +21,7 @@ export default function ComposeEmailModal({
|
||||
onClose,
|
||||
account,
|
||||
replyTo,
|
||||
forwardOf,
|
||||
onSuccess,
|
||||
contractId,
|
||||
}: ComposeEmailModalProps) {
|
||||
@@ -47,6 +49,30 @@ export default function ComposeEmailModal({
|
||||
? `\n\n--- Ursprüngliche Nachricht ---\nVon: ${replyTo.fromName || replyTo.fromAddress}\nAm: ${originalDate}\n\n${replyTo.textBody}`
|
||||
: '';
|
||||
setBody(quotedText);
|
||||
} else if (forwardOf) {
|
||||
// Weiterleiten: To leer (User trägt selbst ein), Betreff mit „Fwd:"
|
||||
setTo('');
|
||||
const existingSubject = forwardOf.subject || '';
|
||||
const hasFwdPrefix = /^(Fwd|Wg):\s*/i.test(existingSubject);
|
||||
setSubject(hasFwdPrefix ? existingSubject : `Fwd: ${existingSubject}`);
|
||||
// Original-Header + Body zitieren (mehr Felder als bei Reply, damit
|
||||
// der weitergeleitete Kontext erhalten bleibt)
|
||||
const originalDate = new Date(forwardOf.receivedAt).toLocaleString('de-DE');
|
||||
let toLine = '';
|
||||
try {
|
||||
const parsed = JSON.parse(forwardOf.toAddresses || '[]');
|
||||
if (Array.isArray(parsed) && parsed.length > 0) {
|
||||
toLine = `\nAn: ${parsed.join(', ')}`;
|
||||
}
|
||||
} catch {
|
||||
// toAddresses war kein JSON – ignorieren
|
||||
}
|
||||
const quotedText = forwardOf.textBody
|
||||
? `\n\n--- Weitergeleitete Nachricht ---\nVon: ${
|
||||
forwardOf.fromName || forwardOf.fromAddress
|
||||
}${toLine}\nDatum: ${originalDate}\nBetreff: ${existingSubject}\n\n${forwardOf.textBody}`
|
||||
: '';
|
||||
setBody(quotedText);
|
||||
} else {
|
||||
// Neue E-Mail: Felder leer
|
||||
setTo('');
|
||||
@@ -57,7 +83,7 @@ export default function ComposeEmailModal({
|
||||
setAttachments([]);
|
||||
setError(null);
|
||||
}
|
||||
}, [isOpen, replyTo]);
|
||||
}, [isOpen, replyTo, forwardOf]);
|
||||
|
||||
// Maximale Dateigröße: 10 MB
|
||||
const MAX_FILE_SIZE = 10 * 1024 * 1024;
|
||||
@@ -194,7 +220,7 @@ export default function ComposeEmailModal({
|
||||
<Modal
|
||||
isOpen={isOpen}
|
||||
onClose={handleClose}
|
||||
title={replyTo ? 'Antworten' : 'Neue E-Mail'}
|
||||
title={replyTo ? 'Antworten' : forwardOf ? 'Weiterleiten' : 'Neue E-Mail'}
|
||||
size="lg"
|
||||
>
|
||||
<div className="space-y-4">
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { RefreshCw, Plus, Mail, Inbox, Send, Trash2 } from 'lucide-react';
|
||||
import { useState, useEffect, useMemo } from 'react';
|
||||
import { RefreshCw, Plus, Mail, Inbox, Send, Trash2, Search, SlidersHorizontal, X } from 'lucide-react';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import toast from 'react-hot-toast';
|
||||
import { cachedEmailApi, stressfreiEmailApi, CachedEmail } from '../../services/api';
|
||||
import { cachedEmailApi, stressfreiEmailApi, CachedEmail, EmailFilterParams } from '../../services/api';
|
||||
import { useAuth } from '../../context/AuthContext';
|
||||
import { useProviderSettings } from '../../hooks/useProviderSettings';
|
||||
import Button from '../ui/Button';
|
||||
@@ -26,6 +26,68 @@ export default function EmailClientTab({ customerId }: EmailClientTabProps) {
|
||||
const [showCompose, setShowCompose] = useState(false);
|
||||
const [showAssign, setShowAssign] = useState(false);
|
||||
const [replyToEmail, setReplyToEmail] = useState<CachedEmail | null>(null);
|
||||
const [forwardEmail, setForwardEmail] = useState<CachedEmail | null>(null);
|
||||
|
||||
// Such- und Filterzustand. Alle Filter sind AND-verknüpft im Backend.
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
const [showFilters, setShowFilters] = useState(false);
|
||||
const [filterFrom, setFilterFrom] = useState('');
|
||||
const [filterTo, setFilterTo] = useState('');
|
||||
const [filterSubject, setFilterSubject] = useState('');
|
||||
const [filterBody, setFilterBody] = useState('');
|
||||
const [filterAttachmentName, setFilterAttachmentName] = useState('');
|
||||
const [filterHasAttachments, setFilterHasAttachments] = useState<'any' | 'yes' | 'no'>('any');
|
||||
const [filterReadStatus, setFilterReadStatus] = useState<'any' | 'unread' | 'read'>('any');
|
||||
const [filterStarred, setFilterStarred] = useState<'any' | 'starred'>('any');
|
||||
const [filterDateFrom, setFilterDateFrom] = useState('');
|
||||
const [filterDateTo, setFilterDateTo] = useState('');
|
||||
|
||||
// Filter-Parameter (memoized) – fließen in queryKey + queryFn.
|
||||
const filterParams: EmailFilterParams = useMemo(() => {
|
||||
const p: EmailFilterParams = {};
|
||||
if (searchQuery.trim()) p.search = searchQuery.trim();
|
||||
if (filterFrom.trim()) p.fromFilter = filterFrom.trim();
|
||||
if (filterTo.trim()) p.toFilter = filterTo.trim();
|
||||
if (filterSubject.trim()) p.subjectFilter = filterSubject.trim();
|
||||
if (filterBody.trim()) p.bodyFilter = filterBody.trim();
|
||||
if (filterAttachmentName.trim()) p.attachmentNameFilter = filterAttachmentName.trim();
|
||||
if (filterHasAttachments === 'yes') p.hasAttachments = true;
|
||||
if (filterHasAttachments === 'no') p.hasAttachments = false;
|
||||
if (filterReadStatus === 'read') p.isRead = true;
|
||||
if (filterReadStatus === 'unread') p.isRead = false;
|
||||
if (filterStarred === 'starred') p.isStarred = true;
|
||||
if (filterDateFrom) p.receivedFrom = new Date(filterDateFrom + 'T00:00:00').toISOString();
|
||||
if (filterDateTo) p.receivedTo = new Date(filterDateTo + 'T23:59:59').toISOString();
|
||||
return p;
|
||||
}, [
|
||||
searchQuery,
|
||||
filterFrom,
|
||||
filterTo,
|
||||
filterSubject,
|
||||
filterBody,
|
||||
filterAttachmentName,
|
||||
filterHasAttachments,
|
||||
filterReadStatus,
|
||||
filterStarred,
|
||||
filterDateFrom,
|
||||
filterDateTo,
|
||||
]);
|
||||
|
||||
const activeFilterCount = Object.keys(filterParams).length;
|
||||
|
||||
const clearAllFilters = () => {
|
||||
setSearchQuery('');
|
||||
setFilterFrom('');
|
||||
setFilterTo('');
|
||||
setFilterSubject('');
|
||||
setFilterBody('');
|
||||
setFilterAttachmentName('');
|
||||
setFilterHasAttachments('any');
|
||||
setFilterReadStatus('any');
|
||||
setFilterStarred('any');
|
||||
setFilterDateFrom('');
|
||||
setFilterDateTo('');
|
||||
};
|
||||
const queryClient = useQueryClient();
|
||||
const { hasPermission } = useAuth();
|
||||
|
||||
@@ -50,11 +112,12 @@ export default function EmailClientTab({ customerId }: EmailClientTabProps) {
|
||||
|
||||
// E-Mails laden (nur für INBOX und SENT)
|
||||
const { data: emailsData, isLoading: emailsLoading, refetch: refetchEmails } = useQuery({
|
||||
queryKey: ['emails', 'customer', customerId, selectedAccountId, selectedFolder],
|
||||
queryKey: ['emails', 'customer', customerId, selectedAccountId, selectedFolder, filterParams],
|
||||
queryFn: () =>
|
||||
cachedEmailApi.getForCustomer(customerId, {
|
||||
accountId: selectedAccountId || undefined,
|
||||
folder: selectedFolder as 'INBOX' | 'SENT',
|
||||
...filterParams,
|
||||
}),
|
||||
enabled: !!selectedAccountId && selectedFolder !== 'TRASH',
|
||||
});
|
||||
@@ -131,14 +194,74 @@ export default function EmailClientTab({ customerId }: EmailClientTabProps) {
|
||||
|
||||
const handleReply = () => {
|
||||
setReplyToEmail(emailDetail || null);
|
||||
setForwardEmail(null);
|
||||
setShowCompose(true);
|
||||
};
|
||||
|
||||
const handleForward = () => {
|
||||
setForwardEmail(emailDetail || null);
|
||||
setReplyToEmail(null);
|
||||
setShowCompose(true);
|
||||
};
|
||||
|
||||
const handleNewEmail = () => {
|
||||
setReplyToEmail(null);
|
||||
setForwardEmail(null);
|
||||
setShowCompose(true);
|
||||
};
|
||||
|
||||
// "Erneut senden": die E-Mail an die ursprüngliche Empfänger-Adresse
|
||||
// (= die Stressfrei-Adresse selbst) noch einmal schicken. Use-Case:
|
||||
// wenn die Forwards der Stressfrei-Adresse zwischenzeitlich auf eine
|
||||
// andere Kunden-E-Mail umgestellt wurden, kommt die alte Mail dort nicht
|
||||
// an – durch erneutes Senden ans Postfach läuft sie durch die jetzt
|
||||
// aktuellen Forwards und landet beim neuen Empfänger.
|
||||
const handleResend = async () => {
|
||||
if (!emailDetail || !selectedAccount) return;
|
||||
|
||||
let toAddresses: string[] = [];
|
||||
try {
|
||||
const parsed = JSON.parse(emailDetail.toAddresses || '[]');
|
||||
if (Array.isArray(parsed)) toAddresses = parsed;
|
||||
} catch {
|
||||
// Fallback: bekannte Mailbox-Adresse
|
||||
if (selectedAccount.email) toAddresses = [selectedAccount.email];
|
||||
}
|
||||
if (toAddresses.length === 0 && selectedAccount.email) {
|
||||
toAddresses = [selectedAccount.email];
|
||||
}
|
||||
|
||||
const hasAttachments = emailDetail.hasAttachments;
|
||||
const lines = [
|
||||
`Diese E-Mail erneut an ${toAddresses.join(', ')} senden?`,
|
||||
'',
|
||||
'Die Mail wird via SMTP wieder ans Postfach zugestellt und nimmt den',
|
||||
'Weg durch die AKTUELL hinterlegten Forwards – damit landet sie bei',
|
||||
'dem heute hinterlegten Empfänger (auch wenn er sich seit dem',
|
||||
'Original-Empfang geändert hat).',
|
||||
];
|
||||
if (hasAttachments) {
|
||||
lines.push('', '⚠ Hinweis: Anhänge werden NICHT erneut versendet. Wenn du Anhänge brauchst, nutze stattdessen "Weiterleiten".');
|
||||
}
|
||||
if (!confirm(lines.join('\n'))) return;
|
||||
|
||||
try {
|
||||
const subj = emailDetail.subject || '(Kein Betreff)';
|
||||
await stressfreiEmailApi.sendEmail(selectedAccount.id, {
|
||||
to: toAddresses,
|
||||
subject: subj,
|
||||
text: emailDetail.textBody || undefined,
|
||||
html: emailDetail.htmlBody || undefined,
|
||||
});
|
||||
toast.success('E-Mail wurde erneut an ' + toAddresses.join(', ') + ' gesendet.');
|
||||
// Gesendet-Ordner-Counts aktualisieren
|
||||
queryClient.invalidateQueries({ queryKey: ['folder-counts', selectedAccountId] });
|
||||
queryClient.invalidateQueries({ queryKey: ['emails'] });
|
||||
} catch (err: any) {
|
||||
toast.error(err?.response?.data?.error || err?.message || 'Fehler beim erneuten Senden');
|
||||
}
|
||||
};
|
||||
|
||||
const handleAssignContract = () => {
|
||||
setShowAssign(true);
|
||||
};
|
||||
@@ -302,7 +425,144 @@ export default function EmailClientTab({ customerId }: EmailClientTabProps) {
|
||||
{/* Content */}
|
||||
<div className="flex flex-1 overflow-hidden">
|
||||
{/* Email List */}
|
||||
<div className="w-1/3 border-r border-gray-200 overflow-auto">
|
||||
<div className="w-1/3 border-r border-gray-200 flex flex-col overflow-hidden">
|
||||
{selectedFolder !== 'TRASH' && selectedAccountId && (
|
||||
<div className="border-b border-gray-200 bg-gray-50 p-2 space-y-2">
|
||||
{/* Suchleiste */}
|
||||
<div className="flex items-center gap-1">
|
||||
<div className="relative flex-1">
|
||||
<Search className="absolute left-2 top-1/2 -translate-y-1/2 w-4 h-4 text-gray-400" />
|
||||
<input
|
||||
type="text"
|
||||
value={searchQuery}
|
||||
onChange={(e) => setSearchQuery(e.target.value)}
|
||||
placeholder="Suche in Betreff, Absender, Inhalt…"
|
||||
className="w-full pl-8 pr-7 py-1.5 text-sm border border-gray-300 rounded focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-blue-500"
|
||||
/>
|
||||
{searchQuery && (
|
||||
<button
|
||||
onClick={() => setSearchQuery('')}
|
||||
className="absolute right-1.5 top-1/2 -translate-y-1/2 text-gray-400 hover:text-gray-600"
|
||||
title="Suche zurücksetzen"
|
||||
>
|
||||
<X className="w-4 h-4" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
<button
|
||||
onClick={() => setShowFilters((v) => !v)}
|
||||
className={`flex items-center gap-1 px-2 py-1.5 text-sm border rounded ${
|
||||
showFilters || activeFilterCount > 0
|
||||
? 'bg-blue-50 border-blue-300 text-blue-700'
|
||||
: 'bg-white border-gray-300 text-gray-600 hover:bg-gray-50'
|
||||
}`}
|
||||
title="Erweiterte Filter ein-/ausblenden"
|
||||
>
|
||||
<SlidersHorizontal className="w-4 h-4" />
|
||||
{activeFilterCount > 0 && (
|
||||
<span className="text-xs font-semibold">{activeFilterCount}</span>
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Ausklappbare erweiterte Filter (alle AND-verknüpft) */}
|
||||
{showFilters && (
|
||||
<div className="space-y-2 pt-1 border-t border-gray-200">
|
||||
<div className="grid grid-cols-2 gap-2">
|
||||
<input
|
||||
type="text"
|
||||
value={filterFrom}
|
||||
onChange={(e) => setFilterFrom(e.target.value)}
|
||||
placeholder="Von (Adresse/Name)"
|
||||
className="px-2 py-1 text-xs border border-gray-300 rounded focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={filterTo}
|
||||
onChange={(e) => setFilterTo(e.target.value)}
|
||||
placeholder="An"
|
||||
className="px-2 py-1 text-xs border border-gray-300 rounded focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={filterSubject}
|
||||
onChange={(e) => setFilterSubject(e.target.value)}
|
||||
placeholder="Betreff enthält"
|
||||
className="px-2 py-1 text-xs border border-gray-300 rounded focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={filterBody}
|
||||
onChange={(e) => setFilterBody(e.target.value)}
|
||||
placeholder="Inhalt enthält"
|
||||
className="px-2 py-1 text-xs border border-gray-300 rounded focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
/>
|
||||
<input
|
||||
type="date"
|
||||
value={filterDateFrom}
|
||||
onChange={(e) => setFilterDateFrom(e.target.value)}
|
||||
title="Empfangen ab"
|
||||
className="px-2 py-1 text-xs border border-gray-300 rounded focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
/>
|
||||
<input
|
||||
type="date"
|
||||
value={filterDateTo}
|
||||
onChange={(e) => setFilterDateTo(e.target.value)}
|
||||
title="Empfangen bis"
|
||||
className="px-2 py-1 text-xs border border-gray-300 rounded focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
/>
|
||||
<input
|
||||
type="text"
|
||||
value={filterAttachmentName}
|
||||
onChange={(e) => setFilterAttachmentName(e.target.value)}
|
||||
placeholder="Anhang-Dateiname"
|
||||
className="col-span-2 px-2 py-1 text-xs border border-gray-300 rounded focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
/>
|
||||
</div>
|
||||
<div className="grid grid-cols-3 gap-2">
|
||||
<select
|
||||
value={filterHasAttachments}
|
||||
onChange={(e) => setFilterHasAttachments(e.target.value as 'any' | 'yes' | 'no')}
|
||||
className="px-2 py-1 text-xs border border-gray-300 rounded bg-white focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
>
|
||||
<option value="any">Anhang: egal</option>
|
||||
<option value="yes">Mit Anhang</option>
|
||||
<option value="no">Ohne Anhang</option>
|
||||
</select>
|
||||
<select
|
||||
value={filterReadStatus}
|
||||
onChange={(e) => setFilterReadStatus(e.target.value as 'any' | 'unread' | 'read')}
|
||||
className="px-2 py-1 text-xs border border-gray-300 rounded bg-white focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
>
|
||||
<option value="any">Status: egal</option>
|
||||
<option value="unread">Ungelesen</option>
|
||||
<option value="read">Gelesen</option>
|
||||
</select>
|
||||
<select
|
||||
value={filterStarred}
|
||||
onChange={(e) => setFilterStarred(e.target.value as 'any' | 'starred')}
|
||||
className="px-2 py-1 text-xs border border-gray-300 rounded bg-white focus:outline-none focus:ring-1 focus:ring-blue-500"
|
||||
>
|
||||
<option value="any">Stern: egal</option>
|
||||
<option value="starred">Nur markiert</option>
|
||||
</select>
|
||||
</div>
|
||||
<div className="flex items-center justify-between text-xs text-gray-500">
|
||||
<span>Filter werden mit UND verknüpft.</span>
|
||||
{activeFilterCount > 0 && (
|
||||
<button
|
||||
onClick={clearAllFilters}
|
||||
className="text-blue-600 hover:underline"
|
||||
>
|
||||
Alle zurücksetzen
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<div className="flex-1 overflow-auto">
|
||||
{selectedFolder === 'TRASH' ? (
|
||||
<TrashEmailList
|
||||
emails={trashEmails}
|
||||
@@ -343,6 +603,7 @@ export default function EmailClientTab({ customerId }: EmailClientTabProps) {
|
||||
accountId={selectedAccountId}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Email Detail */}
|
||||
@@ -351,6 +612,8 @@ export default function EmailClientTab({ customerId }: EmailClientTabProps) {
|
||||
<EmailDetail
|
||||
email={emailDetail}
|
||||
onReply={handleReply}
|
||||
onForward={handleForward}
|
||||
onResend={selectedFolder !== 'TRASH' ? handleResend : undefined}
|
||||
onAssignContract={handleAssignContract}
|
||||
onDeleted={() => {
|
||||
setSelectedEmail(null);
|
||||
@@ -382,9 +645,11 @@ export default function EmailClientTab({ customerId }: EmailClientTabProps) {
|
||||
onClose={() => {
|
||||
setShowCompose(false);
|
||||
setReplyToEmail(null);
|
||||
setForwardEmail(null);
|
||||
}}
|
||||
account={selectedAccount}
|
||||
replyTo={replyToEmail || undefined}
|
||||
forwardOf={forwardEmail || undefined}
|
||||
onSuccess={() => {
|
||||
// Gesendete E-Mails aktualisieren
|
||||
queryClient.invalidateQueries({ queryKey: ['emails', 'customer', customerId, selectedAccountId, 'SENT'] });
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Reply, Star, Paperclip, Link2, X, Download, ExternalLink, Trash2, Undo2, Save, FileDown } from 'lucide-react';
|
||||
import { Reply, Forward, RotateCcw, Star, Paperclip, Link2, X, Download, ExternalLink, Trash2, Undo2, Save, FileDown } from 'lucide-react';
|
||||
import DOMPurify from 'dompurify';
|
||||
import { CachedEmail, cachedEmailApi } from '../../services/api';
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
@@ -13,6 +13,8 @@ import SaveEmailAsPdfModal from './SaveEmailAsPdfModal';
|
||||
interface EmailDetailProps {
|
||||
email: CachedEmail;
|
||||
onReply: () => void;
|
||||
onForward?: () => void; // Weiterleiten (öffnet ComposeModal im Forward-Modus)
|
||||
onResend?: () => void; // Erneut an Empfänger senden (One-Click-Resend)
|
||||
onAssignContract: () => void;
|
||||
onDeleted?: () => void; // Callback nach Löschen
|
||||
isSentFolder?: boolean;
|
||||
@@ -25,6 +27,8 @@ interface EmailDetailProps {
|
||||
export default function EmailDetail({
|
||||
email,
|
||||
onReply,
|
||||
onForward,
|
||||
onResend,
|
||||
onAssignContract,
|
||||
onDeleted,
|
||||
isSentFolder: _isSentFolder = false,
|
||||
@@ -222,6 +226,28 @@ export default function EmailDetail({
|
||||
<Reply className="w-4 h-4 mr-1" />
|
||||
Antworten
|
||||
</Button>
|
||||
{onForward && (
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={onForward}
|
||||
title="Diese E-Mail als neue Nachricht weiterleiten (Empfänger kann beliebig eingegeben werden, Inhalt + Header werden zitiert)"
|
||||
>
|
||||
<Forward className="w-4 h-4 mr-1" />
|
||||
Weiterleiten
|
||||
</Button>
|
||||
)}
|
||||
{onResend && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
onClick={onResend}
|
||||
title="Erneut an die ursprüngliche Empfänger-Adresse senden. Nützlich wenn die Stressfrei-Weiterleitungsadresse umgezogen ist – die Mail kommt dann an den aktuell hinterlegten Forward-Empfänger."
|
||||
>
|
||||
<RotateCcw className="w-4 h-4 mr-1" />
|
||||
Erneut senden
|
||||
</Button>
|
||||
)}
|
||||
{/* E-Mail als PDF speichern */}
|
||||
<Button
|
||||
variant="ghost"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { createContext, useContext, useState, useEffect, ReactNode } from 'react';
|
||||
import { authApi } from '../services/api';
|
||||
import axios from 'axios';
|
||||
import { authApi, setAccessToken, getAccessToken } from '../services/api';
|
||||
import type { User } from '../types';
|
||||
|
||||
interface AuthContextType {
|
||||
@@ -7,8 +8,8 @@ interface AuthContextType {
|
||||
isLoading: boolean;
|
||||
isAuthenticated: boolean;
|
||||
login: (email: string, password: string) => Promise<void>;
|
||||
customerLogin: (email: string, password: string) => Promise<void>;
|
||||
logout: () => void;
|
||||
customerLogin: (email: string, password: string) => Promise<User>;
|
||||
logout: () => Promise<void>;
|
||||
hasPermission: (permission: string) => boolean;
|
||||
isCustomer: boolean;
|
||||
isCustomerPortal: boolean;
|
||||
@@ -40,66 +41,65 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
}
|
||||
}, [user, developerMode]);
|
||||
|
||||
// Beim App-Start versuchen, einen Access-Token via Refresh-Cookie zu holen.
|
||||
// Wenn das klappt → User ist eingeloggt. Wenn nicht → User muss sich neu
|
||||
// anmelden. Der Access-Token bleibt nur im memory (kein localStorage).
|
||||
useEffect(() => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
authApi.me()
|
||||
.then((res) => {
|
||||
if (res.success && res.data) {
|
||||
setUser(res.data);
|
||||
} else {
|
||||
localStorage.removeItem('token');
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
localStorage.removeItem('token');
|
||||
})
|
||||
.finally(() => {
|
||||
setIsLoading(false);
|
||||
});
|
||||
} else {
|
||||
setIsLoading(false);
|
||||
}
|
||||
(async () => {
|
||||
try {
|
||||
const res = await axios.post('/api/auth/refresh', {}, { withCredentials: true });
|
||||
if (res.data?.success && res.data?.data?.token) {
|
||||
setAccessToken(res.data.data.token);
|
||||
// Danach den vollen User aus /me laden (Permissions etc.)
|
||||
const me = await authApi.me();
|
||||
if (me.success && me.data) setUser(me.data);
|
||||
}
|
||||
} catch {
|
||||
// Kein gültiger Refresh-Cookie → User ist nicht eingeloggt
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
})();
|
||||
}, []);
|
||||
|
||||
const login = async (email: string, password: string) => {
|
||||
const res = await authApi.login(email, password);
|
||||
if (res.success && res.data) {
|
||||
localStorage.setItem('token', res.data.token);
|
||||
setAccessToken(res.data.token);
|
||||
setUser(res.data.user);
|
||||
} else {
|
||||
throw new Error(res.error || 'Login fehlgeschlagen');
|
||||
}
|
||||
};
|
||||
|
||||
const customerLogin = async (email: string, password: string) => {
|
||||
const customerLogin = async (email: string, password: string): Promise<User> => {
|
||||
const res = await authApi.customerLogin(email, password);
|
||||
if (res.success && res.data) {
|
||||
localStorage.setItem('token', res.data.token);
|
||||
setAccessToken(res.data.token);
|
||||
setUser(res.data.user);
|
||||
} else {
|
||||
throw new Error(res.error || 'Login fehlgeschlagen');
|
||||
return res.data.user;
|
||||
}
|
||||
throw new Error(res.error || 'Login fehlgeschlagen');
|
||||
};
|
||||
|
||||
const logout = () => {
|
||||
localStorage.removeItem('token');
|
||||
const logout = async () => {
|
||||
// Server-Logout: invalidiert Refresh-Token-Cookie + tokenInvalidatedAt
|
||||
try {
|
||||
await authApi.logout();
|
||||
} catch {
|
||||
// Selbst wenn der Server-Logout fehlschlägt: client-side clear
|
||||
}
|
||||
setAccessToken(null);
|
||||
setUser(null);
|
||||
};
|
||||
|
||||
const refreshUser = async () => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
try {
|
||||
const res = await authApi.me();
|
||||
console.log('refreshUser response:', res);
|
||||
console.log('permissions:', res.data?.permissions);
|
||||
if (res.success && res.data) {
|
||||
setUser(res.data);
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('refreshUser error:', err);
|
||||
}
|
||||
if (!getAccessToken()) return;
|
||||
try {
|
||||
const res = await authApi.me();
|
||||
if (res.success && res.data) setUser(res.data);
|
||||
} catch (err) {
|
||||
console.error('refreshUser error:', err);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
import { useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { useAuth } from '../context/AuthContext';
|
||||
import { authApi } from '../services/api';
|
||||
import Button from '../components/ui/Button';
|
||||
import Input from '../components/ui/Input';
|
||||
import Card from '../components/ui/Card';
|
||||
|
||||
const MIN_LENGTH = 12;
|
||||
|
||||
function checkComplexity(pw: string) {
|
||||
return {
|
||||
length: pw.length >= MIN_LENGTH,
|
||||
upper: /[A-Z]/.test(pw),
|
||||
lower: /[a-z]/.test(pw),
|
||||
digit: /[0-9]/.test(pw),
|
||||
special: /[^A-Za-z0-9]/.test(pw),
|
||||
};
|
||||
}
|
||||
|
||||
function ComplexityHint({ pw }: { pw: string }) {
|
||||
const c = checkComplexity(pw);
|
||||
const items: [boolean, string][] = [
|
||||
[c.length, `Mindestens ${MIN_LENGTH} Zeichen`],
|
||||
[c.upper, 'Großbuchstabe'],
|
||||
[c.lower, 'Kleinbuchstabe'],
|
||||
[c.digit, 'Ziffer'],
|
||||
[c.special, 'Sonderzeichen'],
|
||||
];
|
||||
return (
|
||||
<ul className="text-xs mt-2 space-y-0.5">
|
||||
{items.map(([ok, label]) => (
|
||||
<li key={label} className={ok ? 'text-green-700' : 'text-gray-500'}>
|
||||
{ok ? '✓' : '○'} {label}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
|
||||
export default function ChangeInitialPassword() {
|
||||
const { logout, user } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const [newPassword, setNewPassword] = useState('');
|
||||
const [repeat, setRepeat] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
const [isSaving, setIsSaving] = useState(false);
|
||||
|
||||
const c = checkComplexity(newPassword);
|
||||
const meetsComplexity = c.length && c.upper && c.lower && c.digit && c.special;
|
||||
const matches = newPassword.length > 0 && newPassword === repeat;
|
||||
const canSubmit = meetsComplexity && matches && !isSaving;
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError('');
|
||||
if (!canSubmit) return;
|
||||
setIsSaving(true);
|
||||
try {
|
||||
const res = await authApi.changeInitialPortalPassword(newPassword);
|
||||
if (!res.success) {
|
||||
throw new Error(res.error || 'Passwort konnte nicht geändert werden');
|
||||
}
|
||||
await logout();
|
||||
navigate('/login?changed=1', { replace: true });
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Fehler beim Setzen des Passworts');
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen flex items-center justify-center bg-gray-100 p-4">
|
||||
<Card className="w-full max-w-md">
|
||||
<div className="text-center mb-6">
|
||||
<h1 className="text-2xl font-bold text-gray-900">Neues Passwort vergeben</h1>
|
||||
<p className="text-gray-600 mt-2 text-sm">
|
||||
Hallo {user?.firstName || 'Kunde'}, Sie haben sich mit einem Einmalpasswort
|
||||
angemeldet. Bitte vergeben Sie jetzt Ihr eigenes Passwort. Danach werden Sie
|
||||
ausgeloggt und können sich mit dem neuen Passwort anmelden.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 p-3 bg-red-50 border border-red-200 text-red-700 rounded-lg text-sm">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<Input
|
||||
label="Neues Passwort"
|
||||
type="password"
|
||||
value={newPassword}
|
||||
onChange={(e) => setNewPassword(e.target.value)}
|
||||
required
|
||||
autoComplete="new-password"
|
||||
/>
|
||||
{newPassword.length > 0 && <ComplexityHint pw={newPassword} />}
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<Input
|
||||
label="Passwort wiederholen"
|
||||
type="password"
|
||||
value={repeat}
|
||||
onChange={(e) => setRepeat(e.target.value)}
|
||||
required
|
||||
autoComplete="new-password"
|
||||
/>
|
||||
{repeat.length > 0 && !matches && (
|
||||
<p className="text-xs text-red-600 mt-1">Passwörter stimmen nicht überein</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Button type="submit" className="w-full" disabled={!canSubmit}>
|
||||
{isSaving ? 'Speichere...' : 'Passwort setzen und ausloggen'}
|
||||
</Button>
|
||||
</form>
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,11 +1,13 @@
|
||||
import { useState } from 'react';
|
||||
import { useNavigate, Link } from 'react-router-dom';
|
||||
import { useNavigate, useSearchParams, Link } from 'react-router-dom';
|
||||
import { useAuth } from '../context/AuthContext';
|
||||
import Button from '../components/ui/Button';
|
||||
import Input from '../components/ui/Input';
|
||||
import Card from '../components/ui/Card';
|
||||
|
||||
export default function Login() {
|
||||
const [searchParams] = useSearchParams();
|
||||
const passwordChanged = searchParams.get('changed') === '1';
|
||||
const [email, setEmail] = useState('');
|
||||
const [password, setPassword] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
@@ -28,8 +30,13 @@ export default function Login() {
|
||||
}
|
||||
|
||||
try {
|
||||
await customerLogin(email, password);
|
||||
navigate('/');
|
||||
const portalUser = await customerLogin(email, password);
|
||||
// Einmalpasswort-Login → erzwungenes Passwort-Setzen vor Dashboard
|
||||
if (portalUser?.mustChangePassword) {
|
||||
navigate('/change-initial-password', { replace: true });
|
||||
} else {
|
||||
navigate('/');
|
||||
}
|
||||
} catch {
|
||||
// Beide fehlgeschlagen
|
||||
setError('Ungültige Anmeldedaten');
|
||||
@@ -45,6 +52,12 @@ export default function Login() {
|
||||
<p className="text-gray-600 mt-2">Melden Sie sich an</p>
|
||||
</div>
|
||||
|
||||
{passwordChanged && !error && (
|
||||
<div className="mb-4 p-4 bg-green-50 border border-green-200 text-green-700 rounded-lg text-sm">
|
||||
Passwort wurde geändert. Bitte mit dem neuen Passwort anmelden.
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 p-4 bg-red-50 border border-red-200 text-red-700 rounded-lg">
|
||||
{error}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useState, useEffect } from 'react';
|
||||
import { useParams, Link, useNavigate, useLocation } from 'react-router-dom';
|
||||
import { pushHistory, popHistory } from '../../utils/navigation';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { contractApi, uploadApi, meterApi, contractTaskApi, appSettingsApi, gdprApi, pdfTemplateApi } from '../../services/api';
|
||||
import { contractApi, uploadApi, meterApi, contractTaskApi, appSettingsApi, gdprApi, pdfTemplateApi, getAccessToken } from '../../services/api';
|
||||
import { ContractEmailsSection } from '../../components/email';
|
||||
import { ContractDetailModal, ContractHistorySection } from '../../components/contracts';
|
||||
import InvoicesSection from '../../components/contracts/InvoicesSection';
|
||||
@@ -1514,6 +1514,26 @@ export default function ContractDetail() {
|
||||
},
|
||||
});
|
||||
|
||||
// VVL = Vertragsverlängerung beim selben Anbieter (alle Daten 1:1 + Datum berechnet)
|
||||
const renewalMutation = useMutation({
|
||||
mutationFn: () => contractApi.createRenewal(contractId),
|
||||
onSuccess: (data) => {
|
||||
if (data.data) {
|
||||
navigate(`/contracts/${data.data.id}/edit`);
|
||||
} else {
|
||||
alert('VVL wurde erstellt, aber keine ID zurückgegeben');
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error('VVL Fehler:', error);
|
||||
alert(`Fehler beim Erstellen der VVL: ${error instanceof Error ? error.message : 'Unbekannter Fehler'}`);
|
||||
},
|
||||
});
|
||||
|
||||
// Dropdown-Toggle für VVL
|
||||
const [showFollowUpMenu, setShowFollowUpMenu] = useState(false);
|
||||
const [showVvlConfirm, setShowVvlConfirm] = useState(false);
|
||||
|
||||
// Un-Snooze Mutation
|
||||
const unsnoozeMutation = useMutation({
|
||||
mutationFn: () => contractApi.snooze(contractId, {}),
|
||||
@@ -1756,14 +1776,50 @@ export default function ContractDetail() {
|
||||
</Link>
|
||||
)}
|
||||
{hasPermission('contracts:create') && !c.followUpContract && (
|
||||
<Button
|
||||
variant="secondary"
|
||||
onClick={() => setShowFollowUpConfirm(true)}
|
||||
disabled={followUpMutation.isPending}
|
||||
>
|
||||
<Copy className="w-4 h-4 mr-2" />
|
||||
{followUpMutation.isPending ? 'Erstelle...' : 'Folgevertrag anlegen'}
|
||||
</Button>
|
||||
<div className="relative inline-flex">
|
||||
{/* Hauptaktion: Folgevertrag anlegen */}
|
||||
<Button
|
||||
variant="secondary"
|
||||
onClick={() => setShowFollowUpConfirm(true)}
|
||||
disabled={followUpMutation.isPending || renewalMutation.isPending}
|
||||
className="!rounded-r-none !border-r-0"
|
||||
>
|
||||
<Copy className="w-4 h-4 mr-2" />
|
||||
{followUpMutation.isPending ? 'Erstelle...' : 'Folgevertrag anlegen'}
|
||||
</Button>
|
||||
{/* Dropdown-Pfeil für VVL */}
|
||||
<Button
|
||||
variant="secondary"
|
||||
onClick={() => setShowFollowUpMenu(!showFollowUpMenu)}
|
||||
disabled={followUpMutation.isPending || renewalMutation.isPending}
|
||||
className="!rounded-l-none !px-2"
|
||||
title="Weitere Optionen"
|
||||
>
|
||||
<ChevronDown className="w-4 h-4" />
|
||||
</Button>
|
||||
{showFollowUpMenu && (
|
||||
<>
|
||||
{/* Click-outside-Overlay */}
|
||||
<div
|
||||
className="fixed inset-0 z-10"
|
||||
onClick={() => setShowFollowUpMenu(false)}
|
||||
/>
|
||||
<div className="absolute top-full right-0 mt-1 z-20 w-56 bg-white border border-gray-200 rounded-lg shadow-lg py-1">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setShowFollowUpMenu(false);
|
||||
setShowVvlConfirm(true);
|
||||
}}
|
||||
className="w-full text-left px-4 py-2 text-sm hover:bg-gray-100 flex items-center gap-2"
|
||||
>
|
||||
<Copy className="w-4 h-4 text-gray-500" />
|
||||
VVL anlegen
|
||||
</button>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{c.followUpContract && (
|
||||
<Link to={`/contracts/${c.followUpContract.id}`}>
|
||||
@@ -3077,6 +3133,53 @@ export default function ContractDetail() {
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
{/* VVL Bestätigung */}
|
||||
<Modal
|
||||
isOpen={showVvlConfirm}
|
||||
onClose={() => setShowVvlConfirm(false)}
|
||||
title="Vertragsverlängerung (VVL) anlegen"
|
||||
size="sm"
|
||||
>
|
||||
<div className="space-y-4">
|
||||
<p className="text-gray-700">
|
||||
Möchten Sie eine Vertragsverlängerung für diesen Vertrag anlegen?
|
||||
</p>
|
||||
<p className="text-sm text-gray-500">
|
||||
Alle Daten werden 1:1 übernommen (auch Provider, Tarif, Portal-
|
||||
Zugang, Preise und Vertragsdokumente). Das Startdatum wird auf
|
||||
den nächsten Laufzeit-Beginn berechnet (altes Startdatum +
|
||||
Vertragslaufzeit). Das <strong>Auftragsdokument</strong> wird
|
||||
<strong> nicht </strong> mitkopiert – das ist die neue,
|
||||
unterschriebene VVL, die Sie selbst hochladen.
|
||||
</p>
|
||||
{c.startDate && c.contractDuration?.description && (
|
||||
<div className="text-xs text-gray-500 bg-gray-50 p-2 rounded">
|
||||
Vorhersage: alter Beginn{' '}
|
||||
<strong>{new Date(c.startDate).toLocaleDateString('de-DE')}</strong> +{' '}
|
||||
<strong>{c.contractDuration.description}</strong>
|
||||
{' = '}neuer VVL-Beginn (siehe danach im Vertrag)
|
||||
</div>
|
||||
)}
|
||||
<div className="flex justify-end gap-3 pt-2">
|
||||
<Button
|
||||
variant="secondary"
|
||||
onClick={() => setShowVvlConfirm(false)}
|
||||
>
|
||||
Nein
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
setShowVvlConfirm(false);
|
||||
renewalMutation.mutate();
|
||||
}}
|
||||
disabled={renewalMutation.isPending}
|
||||
>
|
||||
{renewalMutation.isPending ? 'Erstelle...' : 'Ja, VVL anlegen'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
|
||||
{/* Status-Info Modal */}
|
||||
<StatusInfoModal isOpen={showStatusInfo} onClose={() => setShowStatusInfo(false)} />
|
||||
|
||||
@@ -3381,13 +3484,13 @@ function GenerateOrderButton({ contractId }: { contractId: number }) {
|
||||
setShowInputModal({ templateId, templateName });
|
||||
} else {
|
||||
// Direkt generieren (GET-Link)
|
||||
const token = localStorage.getItem('token');
|
||||
window.open(`${pdfTemplateApi.generateUrl(templateId, contractId)}?token=${token}`, '_blank');
|
||||
const token = getAccessToken();
|
||||
window.open(`${pdfTemplateApi.generateUrl(templateId, contractId)}?token=${token || ''}`, '_blank');
|
||||
}
|
||||
} catch {
|
||||
// Fallback: direkt generieren
|
||||
const token = localStorage.getItem('token');
|
||||
window.open(`${pdfTemplateApi.generateUrl(templateId, contractId)}?token=${token}`, '_blank');
|
||||
const token = getAccessToken();
|
||||
window.open(`${pdfTemplateApi.generateUrl(templateId, contractId)}?token=${token || ''}`, '_blank');
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3459,7 +3562,7 @@ function GenerateInputModal({ templateId, templateName, contractId, onClose }: {
|
||||
const inputs = inputsData?.data;
|
||||
|
||||
const handleGenerate = () => {
|
||||
const token = localStorage.getItem('token');
|
||||
const token = getAccessToken();
|
||||
const params = new URLSearchParams();
|
||||
params.set('token', token || '');
|
||||
if (stressfreiEmailId) params.set('stressfreiEmailId', stressfreiEmailId);
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useState, useEffect } from 'react';
|
||||
import { useParams, Link, useNavigate, useSearchParams, useLocation } from 'react-router-dom';
|
||||
import { pushHistory, popHistory } from '../../utils/navigation';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import toast from 'react-hot-toast';
|
||||
import { customerApi, addressApi, bankCardApi, documentApi, meterApi, uploadApi, contractApi, stressfreiEmailApi, emailProviderApi, gdprApi, StressfreiEmail, ContractTreeNode } from '../../services/api';
|
||||
import { EmailClientTab } from '../../components/email';
|
||||
import { useAuth } from '../../context/AuthContext';
|
||||
@@ -13,7 +14,7 @@ import Modal from '../../components/ui/Modal';
|
||||
import Input from '../../components/ui/Input';
|
||||
import Select from '../../components/ui/Select';
|
||||
import FileUpload from '../../components/ui/FileUpload';
|
||||
import { Edit, Plus, Trash2, MapPin, CreditCard, FileText, Gauge, Eye, EyeOff, Download, Globe, UserPlus, X, Search, Mail, Copy, Check, ChevronDown, ChevronRight, Info, Shield, ShieldCheck, ShieldX, ShieldAlert, Lock, ArrowLeft, Cake } from 'lucide-react';
|
||||
import { Edit, Plus, Trash2, MapPin, CreditCard, FileText, Gauge, Eye, EyeOff, Download, Globe, UserPlus, X, Search, Mail, Copy, Check, ChevronDown, ChevronRight, Info, Shield, ShieldCheck, ShieldX, ShieldAlert, Lock, ArrowLeft, Cake, RefreshCw, ExternalLink } from 'lucide-react';
|
||||
import CopyButton, { CopyableBlock } from '../../components/ui/CopyButton';
|
||||
import BirthdayManagementModal from '../../components/BirthdayManagementModal';
|
||||
import { formatDate } from '../../utils/dateFormat';
|
||||
@@ -353,6 +354,17 @@ export default function CustomerDetail({ portalCustomerId }: { portalCustomerId?
|
||||
{c.email}
|
||||
</a>
|
||||
<CopyButton value={c.email} />
|
||||
{(c.stressfreiEmails?.length ?? 0) > 0 && (
|
||||
<a
|
||||
href={`/customers/${c.id}?tab=stressfrei`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="text-gray-400 hover:text-blue-600 ml-1"
|
||||
title="Stressfrei-Wechseln-Adressen öffnen (neuer Tab). Nach Änderung der Stamm-E-Mail dort die Weiterleitungen synchronisieren."
|
||||
>
|
||||
<ExternalLink className="w-3.5 h-3.5" />
|
||||
</a>
|
||||
)}
|
||||
</dd>
|
||||
</div>
|
||||
)}
|
||||
@@ -1784,6 +1796,38 @@ function ContractsTab({
|
||||
);
|
||||
}
|
||||
|
||||
// Passwort-Komplexität – muss zur Backend-Regel in
|
||||
// backend/src/utils/passwordGenerator.ts:validatePasswordComplexity passen.
|
||||
function passwordMeetsComplexity(pw: string): boolean {
|
||||
return (
|
||||
pw.length >= 12 &&
|
||||
/[a-z]/.test(pw) &&
|
||||
/[A-Z]/.test(pw) &&
|
||||
/[0-9]/.test(pw) &&
|
||||
/[^A-Za-z0-9]/.test(pw)
|
||||
);
|
||||
}
|
||||
|
||||
// Live-Hinweis welche Komplexitäts-Anforderungen noch fehlen
|
||||
function PasswordComplexityHint({ password }: { password: string }) {
|
||||
const checks = [
|
||||
{ ok: password.length >= 12, label: '≥ 12 Zeichen' },
|
||||
{ ok: /[a-z]/.test(password), label: 'Kleinbuchstabe' },
|
||||
{ ok: /[A-Z]/.test(password), label: 'Großbuchstabe' },
|
||||
{ ok: /[0-9]/.test(password), label: 'Ziffer' },
|
||||
{ ok: /[^A-Za-z0-9]/.test(password), label: 'Sonderzeichen' },
|
||||
];
|
||||
return (
|
||||
<ul className="mt-2 text-xs space-y-0.5">
|
||||
{checks.map((c) => (
|
||||
<li key={c.label} className={c.ok ? 'text-green-600' : 'text-gray-500'}>
|
||||
{c.ok ? '✓' : '○'} {c.label}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
|
||||
// Gespeichertes Passwort anzeigen
|
||||
function StoredPasswordDisplay({ customerId }: { customerId: number }) {
|
||||
const [showStoredPassword, setShowStoredPassword] = useState(false);
|
||||
@@ -1886,10 +1930,35 @@ function PortalTab({
|
||||
onSuccess: () => {
|
||||
setNewPassword('');
|
||||
queryClient.invalidateQueries({ queryKey: ['customer-portal', customerId] });
|
||||
alert('Passwort wurde gesetzt');
|
||||
toast.success('Passwort wurde gesetzt');
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
alert(error.message);
|
||||
toast.error(error.message);
|
||||
},
|
||||
});
|
||||
|
||||
// Passwort generieren (16 Zeichen, komplex) – ins Input-Feld füllen
|
||||
const generatePasswordMutation = useMutation({
|
||||
mutationFn: () => customerApi.generatePortalPassword(customerId),
|
||||
onSuccess: (res) => {
|
||||
const generated = res.data?.password || '';
|
||||
setNewPassword(generated);
|
||||
setShowPassword(true);
|
||||
toast.success('Komplexes Passwort generiert – jetzt „Setzen" klicken.');
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.error(error.message);
|
||||
},
|
||||
});
|
||||
|
||||
// Zugangsdaten per E-Mail an den Kunden senden
|
||||
const sendCredentialsMutation = useMutation({
|
||||
mutationFn: () => customerApi.sendPortalCredentials(customerId),
|
||||
onSuccess: (res) => {
|
||||
toast.success(res.message || 'Zugangsdaten gesendet');
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
toast.error(error.message);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1991,7 +2060,7 @@ function PortalTab({
|
||||
type={showPassword ? 'text' : 'password'}
|
||||
value={newPassword}
|
||||
onChange={(e) => setNewPassword(e.target.value)}
|
||||
placeholder="Mindestens 6 Zeichen"
|
||||
placeholder="Mind. 12 Zeichen, Groß/Klein/Zahl/Sonderzeichen"
|
||||
disabled={!canEdit}
|
||||
/>
|
||||
<button
|
||||
@@ -2002,15 +2071,48 @@ function PortalTab({
|
||||
{showPassword ? <EyeOff className="w-4 h-4" /> : <Eye className="w-4 h-4" />}
|
||||
</button>
|
||||
</div>
|
||||
<Button
|
||||
variant="secondary"
|
||||
onClick={() => generatePasswordMutation.mutate()}
|
||||
disabled={!canEdit || generatePasswordMutation.isPending}
|
||||
title='Komplexes Passwort generieren (16 Zeichen, Groß/Klein/Zahl/Sonderzeichen). Wird ins Feld geschrieben – danach "Setzen" klicken.'
|
||||
>
|
||||
{generatePasswordMutation.isPending ? 'Generieren...' : 'Generieren'}
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => setPasswordMutation.mutate(newPassword)}
|
||||
disabled={!canEdit || newPassword.length < 6 || setPasswordMutation.isPending}
|
||||
disabled={!canEdit || !passwordMeetsComplexity(newPassword) || setPasswordMutation.isPending}
|
||||
title={passwordMeetsComplexity(newPassword) ? 'Passwort speichern' : 'Komplexität nicht erfüllt'}
|
||||
>
|
||||
{setPasswordMutation.isPending ? 'Speichern...' : 'Setzen'}
|
||||
</Button>
|
||||
</div>
|
||||
{/* Komplexitäts-Hinweise: zeigt live welche Anforderungen erfüllt sind */}
|
||||
{newPassword.length > 0 && !passwordMeetsComplexity(newPassword) && (
|
||||
<PasswordComplexityHint password={newPassword} />
|
||||
)}
|
||||
{portal?.hasPassword && (
|
||||
<StoredPasswordDisplay customerId={customerId} />
|
||||
<>
|
||||
<StoredPasswordDisplay customerId={customerId} />
|
||||
<div className="mt-3">
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => {
|
||||
if (confirm(
|
||||
'Aktuelles Portal-Passwort und Login-URL per E-Mail an den Kunden senden?\n\n' +
|
||||
'Hinweis: Das Passwort wird im Klartext in der E-Mail enthalten sein.'
|
||||
)) {
|
||||
sendCredentialsMutation.mutate();
|
||||
}
|
||||
}}
|
||||
disabled={!canEdit || sendCredentialsMutation.isPending}
|
||||
title="Login-URL + E-Mail + Passwort an die Kunden-E-Mail versenden"
|
||||
>
|
||||
{sendCredentialsMutation.isPending ? 'Sende...' : 'Zugangsdaten per E-Mail versenden'}
|
||||
</Button>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
@@ -2964,6 +3066,31 @@ function StressfreiEmailsTab({
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['customer', customerId] }),
|
||||
});
|
||||
|
||||
// Weiterleitungen am Provider neu setzen (Stamm-Email-Wechsel-Use-Case).
|
||||
// Wenn die Adresse hasMailbox=true ist, wird zusätzlich das im CRM
|
||||
// hinterlegte Passwort am Provider neu gesetzt (Self-Healing nach
|
||||
// manuellen Eingriffen am Provider).
|
||||
const syncForwardingMutation = useMutation({
|
||||
mutationFn: stressfreiEmailApi.syncForwarding,
|
||||
onSuccess: (res) => {
|
||||
const targets = res?.data?.forwardTargets || [];
|
||||
const passwordReset = res?.data?.passwordReset;
|
||||
const lines = [
|
||||
'Weiterleitungen aktualisiert:',
|
||||
...targets.map((t) => `• ${t}`),
|
||||
];
|
||||
if (passwordReset) lines.push('Mailbox-Passwort am Provider neu gesetzt.');
|
||||
toast.success(lines.join('\n'), { duration: 5000 });
|
||||
queryClient.invalidateQueries({ queryKey: ['customer', customerId] });
|
||||
},
|
||||
onError: (err: any) => {
|
||||
toast.error(
|
||||
err?.response?.data?.error || err?.message || 'Fehler beim Aktualisieren der Weiterleitungen',
|
||||
{ duration: 6000 },
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
const filtered = showInactive ? emails : emails.filter((e) => e.isActive);
|
||||
|
||||
return (
|
||||
@@ -3023,6 +3150,40 @@ function StressfreiEmailsTab({
|
||||
>
|
||||
<Edit className="w-4 h-4" />
|
||||
</Button>
|
||||
{emailItem.isProvisioned && (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
disabled={syncForwardingMutation.isPending}
|
||||
onClick={() => {
|
||||
const lines = [
|
||||
`Weiterleitungen für ${emailItem.email} jetzt neu setzen?`,
|
||||
'',
|
||||
'Alle bestehenden Weiterleitungen am Provider werden ersetzt durch:',
|
||||
'• die aktuelle Stamm-E-Mail des Kunden',
|
||||
'• unsere Service-Weiterleitungsadresse aus den Provider-Einstellungen',
|
||||
];
|
||||
if (emailItem.hasMailbox) {
|
||||
lines.push(
|
||||
'',
|
||||
'Zusätzlich wird das im CRM hinterlegte Mailbox-Passwort am Provider neu gesetzt.',
|
||||
);
|
||||
}
|
||||
if (confirm(lines.join('\n'))) {
|
||||
syncForwardingMutation.mutate(emailItem.id);
|
||||
}
|
||||
}}
|
||||
title={
|
||||
emailItem.hasMailbox
|
||||
? 'Weiterleitungen + Mailbox-Passwort synchronisieren. Nützlich nach Änderung der Kunden-Stamm-E-Mail oder nach manuellem Eingriff am Provider.'
|
||||
: 'Weiterleitungen synchronisieren – ersetzt die Forwards am Provider durch (Kunden-Stamm-E-Mail + Service-Adresse). Nützlich nach Änderung der Stamm-E-Mail.'
|
||||
}
|
||||
>
|
||||
<RefreshCw
|
||||
className={`w-4 h-4 ${syncForwardingMutation.isPending ? 'animate-spin' : ''}`}
|
||||
/>
|
||||
</Button>
|
||||
)}
|
||||
{emailItem.isActive ? (
|
||||
<Button
|
||||
variant="ghost"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { useAuth } from '../../context/AuthContext';
|
||||
import { gdprApi } from '../../services/api';
|
||||
import { gdprApi, getAccessToken } from '../../services/api';
|
||||
import type { ConsentType, ConsentStatus, CustomerConsent } from '../../types';
|
||||
import {
|
||||
Shield,
|
||||
@@ -93,7 +93,7 @@ export default function PortalPrivacy() {
|
||||
const consents = data?.data?.consents || [];
|
||||
const privacyPolicyHtml = data?.data?.privacyPolicyHtml || '';
|
||||
const allGranted = consents.every((c) => c.status === 'GRANTED');
|
||||
const token = localStorage.getItem('token');
|
||||
const token = getAccessToken();
|
||||
|
||||
return (
|
||||
<div>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useState } from 'react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { auditLogApi, AuditLogSearchParams } from '../../services/api';
|
||||
import { auditLogApi, AuditLogSearchParams, getAccessToken } from '../../services/api';
|
||||
import type { AuditLog, AuditAction, AuditSensitivity } from '../../types';
|
||||
import Card from '../../components/ui/Card';
|
||||
import Button from '../../components/ui/Button';
|
||||
@@ -301,7 +301,7 @@ export default function AuditLogs() {
|
||||
try {
|
||||
if (format === 'csv') {
|
||||
// CSV direkt als Download
|
||||
const token = localStorage.getItem('token');
|
||||
const token = getAccessToken();
|
||||
const params = new URLSearchParams();
|
||||
params.set('format', 'csv');
|
||||
if (filters.action) params.set('action', filters.action);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useState, useRef } from 'react';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { Database, Download, Upload, Trash2, RefreshCw, HardDrive, Clock, FileText, FolderOpen, Archive, AlertTriangle, Bomb } from 'lucide-react';
|
||||
import { backupApi, BackupInfo } from '../../services/api';
|
||||
import { backupApi, BackupInfo, getAccessToken } from '../../services/api';
|
||||
import { useAuth } from '../../context/AuthContext';
|
||||
import Button from '../../components/ui/Button';
|
||||
|
||||
@@ -90,7 +90,7 @@ export default function DatabaseBackup() {
|
||||
|
||||
// Download mit Auth-Token
|
||||
const handleDownload = async (name: string) => {
|
||||
const token = localStorage.getItem('token');
|
||||
const token = getAccessToken();
|
||||
const url = backupApi.getDownloadUrl(name);
|
||||
|
||||
try {
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
import { useState } from 'react';
|
||||
import { useRef, useState } from 'react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import Card from '../../components/ui/Card';
|
||||
import Button from '../../components/ui/Button';
|
||||
import {
|
||||
ArrowLeft,
|
||||
Download,
|
||||
Upload,
|
||||
Package,
|
||||
Info,
|
||||
Loader2,
|
||||
@@ -17,6 +18,7 @@ import {
|
||||
Calendar,
|
||||
FileType,
|
||||
FileText,
|
||||
FileCode,
|
||||
} from 'lucide-react';
|
||||
import api from '../../services/api';
|
||||
|
||||
@@ -27,6 +29,19 @@ interface PreviewCounts {
|
||||
contractDurations: number;
|
||||
contractCategories: number;
|
||||
pdfTemplates: number;
|
||||
appSettings: number;
|
||||
}
|
||||
|
||||
interface ImportResult {
|
||||
providers: number;
|
||||
tariffs: number;
|
||||
cancellationPeriods: number;
|
||||
contractDurations: number;
|
||||
contractCategories: number;
|
||||
pdfTemplates: number;
|
||||
pdfTemplatesSkipped: number;
|
||||
appSettings: number;
|
||||
warnings: string[];
|
||||
}
|
||||
|
||||
export default function FactoryDefaults() {
|
||||
@@ -34,6 +49,12 @@ export default function FactoryDefaults() {
|
||||
const [downloadError, setDownloadError] = useState<string | null>(null);
|
||||
const [downloadDone, setDownloadDone] = useState(false);
|
||||
|
||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||
const [importing, setImporting] = useState(false);
|
||||
const [importError, setImportError] = useState<string | null>(null);
|
||||
const [importResult, setImportResult] = useState<ImportResult | null>(null);
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const { data: previewData, isLoading } = useQuery({
|
||||
queryKey: ['factory-defaults-preview'],
|
||||
queryFn: async () => {
|
||||
@@ -86,9 +107,39 @@ export default function FactoryDefaults() {
|
||||
{ icon: Calendar, label: 'Laufzeiten', count: counts.contractDurations, color: 'text-pink-600' },
|
||||
{ icon: FileType, label: 'Vertragskategorien', count: counts.contractCategories, color: 'text-orange-600' },
|
||||
{ icon: FileText, label: 'PDF-Auftragsvorlagen', count: counts.pdfTemplates, color: 'text-green-600' },
|
||||
{ icon: FileCode, label: 'HTML-Templates', count: counts.appSettings ?? 0, color: 'text-teal-600' },
|
||||
]
|
||||
: [];
|
||||
|
||||
const handleImport = async (file: File) => {
|
||||
setImporting(true);
|
||||
setImportError(null);
|
||||
setImportResult(null);
|
||||
try {
|
||||
const formData = new FormData();
|
||||
formData.append('zip', file);
|
||||
const res = await api.post<{ success: boolean; data: ImportResult; error?: string }>(
|
||||
'/factory-defaults/import',
|
||||
formData,
|
||||
{ headers: { 'Content-Type': 'multipart/form-data' } },
|
||||
);
|
||||
if (!res.data.success) {
|
||||
throw new Error(res.data.error || 'Fehler beim Import');
|
||||
}
|
||||
setImportResult(res.data.data);
|
||||
// Caches invalidieren – neue Anbieter, Tarife, Vorlagen tauchen sofort
|
||||
// an anderer Stelle (Provider-Liste, Vertrag-Anlage, …) auf.
|
||||
queryClient.invalidateQueries();
|
||||
} catch (err: any) {
|
||||
setImportError(
|
||||
err?.response?.data?.error || err?.message || 'Fehler beim Import',
|
||||
);
|
||||
} finally {
|
||||
setImporting(false);
|
||||
if (fileInputRef.current) fileInputRef.current.value = '';
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="flex items-center gap-4 mb-6">
|
||||
@@ -109,14 +160,15 @@ export default function FactoryDefaults() {
|
||||
<div className="text-sm text-blue-900 space-y-1">
|
||||
<p className="font-medium">Was sind Factory-Defaults?</p>
|
||||
<p>
|
||||
Das sind <strong>reine Stammdaten-Kataloge</strong> wie Anbieter, Tarife,
|
||||
Kündigungsfristen, Vertragskategorien und PDF-Auftragsvorlagen. Du kannst sie
|
||||
exportieren, um sie in anderen OpenCRM-Installationen als Startpunkt zu
|
||||
verwenden.
|
||||
Das sind <strong>Stammdaten-Kataloge</strong> wie Anbieter, Tarife,
|
||||
Kündigungsfristen, Vertragskategorien, PDF-Auftragsvorlagen und die
|
||||
HTML-Standardtexte (Datenschutzerklärung, Impressum, Vollmacht-Vorlage,
|
||||
Website-Datenschutz). Du kannst sie exportieren, um sie in anderen
|
||||
OpenCRM-Installationen als Startpunkt zu verwenden.
|
||||
</p>
|
||||
<p className="pt-1">
|
||||
<strong>Nicht enthalten</strong> sind Kundendaten, Verträge, Dokumente, Emails
|
||||
oder Einstellungen – dafür gibt es den separaten{' '}
|
||||
<strong>Nicht enthalten</strong> sind Kundendaten, Verträge, Dokumente, E-Mails
|
||||
oder Konfigurationen (SMTP, Secrets) – dafür gibt es den separaten{' '}
|
||||
<Link to="/settings/database-backup" className="underline">
|
||||
Datenbank-Backup
|
||||
</Link>
|
||||
@@ -127,16 +179,10 @@ export default function FactoryDefaults() {
|
||||
|
||||
<Card title="Export" className="mb-6">
|
||||
<p className="text-sm text-gray-600 mb-4">
|
||||
Erstellt ein ZIP mit allen Kataloge-Daten + PDF-Vorlagen. Lade es herunter und
|
||||
entpacke den Inhalt in einer anderen Installation unter{' '}
|
||||
<code className="bg-gray-100 px-1.5 py-0.5 rounded text-xs">
|
||||
backend/factory-defaults/
|
||||
</code>
|
||||
, dann dort{' '}
|
||||
<code className="bg-gray-100 px-1.5 py-0.5 rounded text-xs">
|
||||
npm run seed:defaults
|
||||
</code>{' '}
|
||||
ausführen.
|
||||
Erstellt ein ZIP mit allen Kataloge-Daten, PDF-Auftragsvorlagen und den
|
||||
HTML-Standardtexten (Datenschutz / Impressum / Vollmacht). In einer anderen
|
||||
OpenCRM-Installation kannst du es dann unten unter <strong>Import</strong> wieder
|
||||
einspielen.
|
||||
</p>
|
||||
|
||||
{isLoading ? (
|
||||
@@ -191,34 +237,88 @@ export default function FactoryDefaults() {
|
||||
</Card>
|
||||
|
||||
<Card title="Import">
|
||||
<div className="space-y-3 text-sm text-gray-600">
|
||||
<p>
|
||||
Der Import läuft über ein Kommandozeilen-Script – dadurch bleibt klar, was wann
|
||||
passiert und es gibt keine ungeplanten Überschreibungen im Produktivbetrieb.
|
||||
</p>
|
||||
<ol className="list-decimal list-inside space-y-1 ml-2">
|
||||
<li>
|
||||
ZIP in einer beliebigen Installation herunterladen und den Inhalt nach{' '}
|
||||
<code className="bg-gray-100 px-1.5 py-0.5 rounded text-xs">
|
||||
backend/factory-defaults/
|
||||
</code>{' '}
|
||||
entpacken
|
||||
</li>
|
||||
<li>
|
||||
Optional mehrere ZIPs zusammenwerfen (JSON-Dateien werden automatisch gemerged)
|
||||
</li>
|
||||
<li>
|
||||
Im Backend-Ordner:{' '}
|
||||
<code className="bg-gray-100 px-1.5 py-0.5 rounded text-xs">
|
||||
npm run seed:defaults
|
||||
</code>
|
||||
</li>
|
||||
</ol>
|
||||
<p className="pt-2">
|
||||
Das Script läuft <strong>idempotent</strong> – gleiche Einträge werden per
|
||||
unique-Key aktualisiert, neue hinzugefügt. Kann beliebig oft ausgeführt werden.
|
||||
</p>
|
||||
<p className="text-sm text-gray-600 mb-4">
|
||||
Lade hier eine zuvor exportierte Factory-Defaults-ZIP hoch. Bestehende Einträge
|
||||
werden anhand des Unique-Keys (Name / Code) <strong>aktualisiert</strong>, neue
|
||||
werden angelegt. Es wird nichts gelöscht – der Vorgang ist idempotent.
|
||||
</p>
|
||||
|
||||
<input
|
||||
ref={fileInputRef}
|
||||
type="file"
|
||||
accept=".zip,application/zip,application/x-zip-compressed"
|
||||
className="hidden"
|
||||
onChange={(e) => {
|
||||
const f = e.target.files?.[0];
|
||||
if (f) handleImport(f);
|
||||
}}
|
||||
/>
|
||||
|
||||
<div className="flex items-center gap-3">
|
||||
<Button
|
||||
onClick={() => fileInputRef.current?.click()}
|
||||
disabled={importing}
|
||||
variant="secondary"
|
||||
>
|
||||
{importing ? (
|
||||
<>
|
||||
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
|
||||
Import läuft…
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<Upload className="w-4 h-4 mr-2" />
|
||||
ZIP hochladen
|
||||
</>
|
||||
)}
|
||||
</Button>
|
||||
<span className="text-xs text-gray-500">
|
||||
Alternativ:{' '}
|
||||
<code className="bg-gray-100 px-1.5 py-0.5 rounded">
|
||||
npm run seed:defaults
|
||||
</code>{' '}
|
||||
im Backend
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{importResult && (
|
||||
<div className="mt-4 p-3 bg-green-50 border border-green-200 rounded-lg text-sm text-green-800">
|
||||
<div className="flex items-center gap-2 font-medium mb-2">
|
||||
<Check className="w-4 h-4" />
|
||||
Import erfolgreich
|
||||
</div>
|
||||
<ul className="list-disc list-inside space-y-0.5 text-xs">
|
||||
<li>Anbieter: {importResult.providers}</li>
|
||||
<li>Tarife: {importResult.tariffs}</li>
|
||||
<li>Kündigungsfristen: {importResult.cancellationPeriods}</li>
|
||||
<li>Laufzeiten: {importResult.contractDurations}</li>
|
||||
<li>Vertragskategorien: {importResult.contractCategories}</li>
|
||||
<li>
|
||||
PDF-Vorlagen: {importResult.pdfTemplates}
|
||||
{importResult.pdfTemplatesSkipped > 0 &&
|
||||
` (${importResult.pdfTemplatesSkipped} übersprungen)`}
|
||||
</li>
|
||||
<li>HTML-Templates: {importResult.appSettings}</li>
|
||||
</ul>
|
||||
{importResult.warnings.length > 0 && (
|
||||
<div className="mt-2 pt-2 border-t border-green-200 text-amber-700 text-xs">
|
||||
<div className="font-medium mb-1">Hinweise:</div>
|
||||
<ul className="list-disc list-inside space-y-0.5">
|
||||
{importResult.warnings.map((w, i) => (
|
||||
<li key={i}>{w}</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{importError && (
|
||||
<div className="mt-3 p-3 bg-red-50 border border-red-200 rounded-lg flex items-start gap-2 text-sm text-red-700">
|
||||
<AlertCircle className="w-4 h-4 flex-shrink-0 mt-0.5" />
|
||||
<span>{importError}</span>
|
||||
</div>
|
||||
)}
|
||||
</Card>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -8,6 +8,7 @@ import Button from '../../components/ui/Button';
|
||||
import Select from '../../components/ui/Select';
|
||||
import { ArrowLeft, FileText, Users, CheckCircle, Clock, XCircle, AlertTriangle, Download, X, ChevronRight } from 'lucide-react';
|
||||
import { fileUrl } from '../../utils/fileUrl';
|
||||
import { useAuth } from '../../context/AuthContext';
|
||||
|
||||
const STATUS_OPTIONS = [
|
||||
{ value: '', label: 'Alle Status' },
|
||||
@@ -155,6 +156,7 @@ function ProcessModal({ request, onClose, onProcess, isPending }: ProcessModalPr
|
||||
export default function GDPRDashboard() {
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient();
|
||||
const { user } = useAuth();
|
||||
const [statusFilter, setStatusFilter] = useState<DeletionRequestStatus | ''>('');
|
||||
const [selectedRequest, setSelectedRequest] = useState<DataDeletionRequest | null>(null);
|
||||
|
||||
@@ -191,11 +193,10 @@ export default function GDPRDashboard() {
|
||||
|
||||
const handleProcess = (action: 'complete' | 'partial' | 'reject', reason?: string) => {
|
||||
if (!selectedRequest) return;
|
||||
const user = JSON.parse(localStorage.getItem('user') || '{}');
|
||||
processMutation.mutate({
|
||||
id: selectedRequest.id,
|
||||
data: {
|
||||
processedBy: user.email || 'System',
|
||||
processedBy: user?.email || 'System',
|
||||
action,
|
||||
retentionReason: reason,
|
||||
},
|
||||
|
||||
@@ -272,6 +272,16 @@ export default function Monitoring() {
|
||||
<option value={100}>100</option>
|
||||
<option value={200}>200</option>
|
||||
</select>
|
||||
<Button
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
onClick={() => queryClient.invalidateQueries({ queryKey: ['monitoring-events'] })}
|
||||
disabled={eventsLoading}
|
||||
title="Events jetzt neu laden. Auto-Refresh läuft sonst alle 30 s im Hintergrund."
|
||||
>
|
||||
<RefreshCw className={`w-4 h-4 mr-1 ${eventsLoading ? 'animate-spin' : ''}`} />
|
||||
Aktualisieren
|
||||
</Button>
|
||||
<Button variant="secondary" size="sm" onClick={() => setShowClearConfirm(true)}>
|
||||
<Trash2 className="w-4 h-4 mr-1" /> Log leeren
|
||||
</Button>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useState } from 'react';
|
||||
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { pdfTemplateApi, contractApi } from '../../services/api';
|
||||
import { pdfTemplateApi, contractApi, getAccessToken } from '../../services/api';
|
||||
import type { PdfTemplate, CrmField, Contract } from '../../types';
|
||||
import Card from '../../components/ui/Card';
|
||||
import Button from '../../components/ui/Button';
|
||||
@@ -276,7 +276,7 @@ function FieldMappingModal({ template, onClose }: { template: PdfTemplate; onClo
|
||||
<span className="text-gray-400 text-xs">[Feldname] zeigt wo das Feld in der PDF liegt</span>
|
||||
</div>
|
||||
<iframe
|
||||
src={`/api/pdf-templates/${template.id}/preview?token=${localStorage.getItem('token')}`}
|
||||
src={`/api/pdf-templates/${template.id}/preview?token=${getAccessToken() || ''}`}
|
||||
className="flex-1 w-full bg-white"
|
||||
title="PDF Vorschau mit Feldnamen"
|
||||
/>
|
||||
@@ -428,11 +428,11 @@ function TestPreviewModal({ template, onClose }: { template: PdfTemplate; onClos
|
||||
});
|
||||
|
||||
const contracts: Contract[] = contractsData?.data || [];
|
||||
const token = localStorage.getItem('token');
|
||||
const token = getAccessToken();
|
||||
|
||||
const handleGenerate = () => {
|
||||
if (!selectedContractId) return;
|
||||
const url = `${pdfTemplateApi.generateUrl(template.id, selectedContractId)}?token=${token}`;
|
||||
const url = `${pdfTemplateApi.generateUrl(template.id, selectedContractId)}?token=${token || ''}`;
|
||||
window.open(url, '_blank');
|
||||
};
|
||||
|
||||
|
||||
+150
-24
@@ -1,41 +1,112 @@
|
||||
import axios from 'axios';
|
||||
import type { ApiResponse, Customer, Contract, ContractTask, ContractTaskSubtask, ContractTaskStatus, SalesPlatform, CancellationPeriod, ContractDuration, ContractCategory, Provider, Tariff, User, Address, BankCard, IdentityDocument, Meter, MeterReading, Invoice, Role, PortalSettings, CustomerRepresentative, CustomerSummary, ContractHistoryEntry, AuditLog, AuditSensitivity, AuditRetentionPolicy, CustomerConsent, ConsentType, ConsentStatus, DataDeletionRequest, DeletionRequestStatus, GDPRDashboardStats, RepresentativeAuthorization } from '../types';
|
||||
|
||||
// ============================================================================
|
||||
// In-Memory-Token-Store
|
||||
// ============================================================================
|
||||
// Der Access-Token wird BEWUSST nicht in localStorage gespeichert (XSS-Schutz).
|
||||
// Stattdessen lebt er im Modul-State + wird über den /api/auth/refresh-Endpoint
|
||||
// nach Page-Reload neu geholt (Refresh-Token sitzt in einem httpOnly-Cookie,
|
||||
// das JavaScript nie sieht).
|
||||
let accessToken: string | null = null;
|
||||
const tokenListeners = new Set<(t: string | null) => void>();
|
||||
|
||||
export function setAccessToken(t: string | null): void {
|
||||
accessToken = t;
|
||||
tokenListeners.forEach((l) => l(t));
|
||||
}
|
||||
export function getAccessToken(): string | null {
|
||||
return accessToken;
|
||||
}
|
||||
export function subscribeToken(listener: (t: string | null) => void): () => void {
|
||||
tokenListeners.add(listener);
|
||||
return () => tokenListeners.delete(listener);
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Axios-Instance
|
||||
// ============================================================================
|
||||
const api = axios.create({
|
||||
baseURL: '/api',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
// withCredentials: Cookies werden bei same-origin-Requests mitgeschickt.
|
||||
// Wichtig für den /auth/refresh-Endpoint (liest den refresh_token-Cookie).
|
||||
withCredentials: true,
|
||||
});
|
||||
|
||||
// Add auth token to requests
|
||||
// Request: Bearer-Header aus dem in-memory-Store
|
||||
api.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('token');
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
if (accessToken) {
|
||||
config.headers.Authorization = `Bearer ${accessToken}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
// Handle auth errors and extract error messages
|
||||
// Refresh-Retry-Mechanismus für 401-Antworten.
|
||||
//
|
||||
// Wenn der Access-Token abgelaufen ist (15-min-Lifetime), antwortet jeder
|
||||
// API-Aufruf mit 401. Der Interceptor probiert dann einmal /auth/refresh →
|
||||
// holt neuen Access-Token (Refresh-Token kommt automatisch via httpOnly-Cookie)
|
||||
// → wiederholt den ursprünglichen Request transparent. Wenn der Refresh selbst
|
||||
// scheitert (echt abgemeldet / Cookie weg): wir leiten zur Login-Seite um.
|
||||
//
|
||||
// Concurrent-Request-Protection: wenn 401 mehrfach parallel kommt, gibt's
|
||||
// nur einen aktiven refresh-Aufruf; alle wartenden Requests teilen sich das
|
||||
// Ergebnis.
|
||||
let refreshInflight: Promise<string | null> | null = null;
|
||||
async function doRefresh(): Promise<string | null> {
|
||||
if (refreshInflight) return refreshInflight;
|
||||
refreshInflight = (async () => {
|
||||
try {
|
||||
const res = await axios.post<ApiResponse<{ token: string }>>(
|
||||
'/api/auth/refresh',
|
||||
{},
|
||||
{ withCredentials: true },
|
||||
);
|
||||
const newToken = res.data?.data?.token || null;
|
||||
setAccessToken(newToken);
|
||||
return newToken;
|
||||
} catch {
|
||||
setAccessToken(null);
|
||||
return null;
|
||||
} finally {
|
||||
refreshInflight = null;
|
||||
}
|
||||
})();
|
||||
return refreshInflight;
|
||||
}
|
||||
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
(error) => {
|
||||
// Bei 401 nur dann zur Login-Seite umleiten, wenn wir NICHT gerade auf der Login-Seite sind
|
||||
// Login-Endpunkte ausschließen, da 401 dort "falsches Passwort" bedeutet
|
||||
const isLoginEndpoint = error.config?.url?.includes('/auth/login') ||
|
||||
error.config?.url?.includes('/auth/customer-login');
|
||||
async (error) => {
|
||||
const original = error.config;
|
||||
const status = error.response?.status;
|
||||
const url: string = original?.url || '';
|
||||
|
||||
if (error.response?.status === 401 && !isLoginEndpoint) {
|
||||
localStorage.removeItem('token');
|
||||
localStorage.removeItem('user');
|
||||
window.location.href = '/login';
|
||||
// Auth-Endpoints selbst nicht refreshen – sonst Endlos-Schleife
|
||||
const isAuthEndpoint =
|
||||
url.includes('/auth/login') ||
|
||||
url.includes('/auth/customer-login') ||
|
||||
url.includes('/auth/refresh') ||
|
||||
url.includes('/auth/logout');
|
||||
|
||||
if (status === 401 && !isAuthEndpoint && !original?._retried) {
|
||||
original._retried = true;
|
||||
const newToken = await doRefresh();
|
||||
if (newToken) {
|
||||
original.headers = original.headers || {};
|
||||
original.headers.Authorization = `Bearer ${newToken}`;
|
||||
return api(original);
|
||||
}
|
||||
// Refresh fehlgeschlagen → echt abmelden + zur Login-Seite
|
||||
if (typeof window !== 'undefined' && !window.location.pathname.startsWith('/login')) {
|
||||
window.location.href = '/login';
|
||||
}
|
||||
}
|
||||
// Extract error message from response
|
||||
|
||||
const message = error.response?.data?.error || error.message || 'Ein Fehler ist aufgetreten';
|
||||
const enhancedError = new Error(message);
|
||||
return Promise.reject(enhancedError);
|
||||
}
|
||||
return Promise.reject(new Error(message));
|
||||
},
|
||||
);
|
||||
|
||||
// Auth
|
||||
@@ -52,6 +123,14 @@ export const authApi = {
|
||||
const res = await api.get<ApiResponse<User>>('/auth/me');
|
||||
return res.data;
|
||||
},
|
||||
logout: async () => {
|
||||
const res = await api.post<ApiResponse<void>>('/auth/logout');
|
||||
return res.data;
|
||||
},
|
||||
changeInitialPortalPassword: async (newPassword: string) => {
|
||||
const res = await api.post<ApiResponse<void>>('/auth/change-initial-portal-password', { newPassword });
|
||||
return res.data;
|
||||
},
|
||||
};
|
||||
|
||||
// Customers
|
||||
@@ -93,6 +172,14 @@ export const customerApi = {
|
||||
const res = await api.get<ApiResponse<{ password: string | null }>>(`/customers/${customerId}/portal/password`);
|
||||
return res.data;
|
||||
},
|
||||
generatePortalPassword: async (customerId: number) => {
|
||||
const res = await api.post<ApiResponse<{ password: string }>>(`/customers/${customerId}/portal/password/generate`);
|
||||
return res.data;
|
||||
},
|
||||
sendPortalCredentials: async (customerId: number) => {
|
||||
const res = await api.post<ApiResponse<void>>(`/customers/${customerId}/portal/send-credentials`);
|
||||
return res.data;
|
||||
},
|
||||
// Vertreter-Verwaltung
|
||||
getRepresentatives: async (customerId: number) => {
|
||||
const res = await api.get<ApiResponse<CustomerRepresentative[]>>(`/customers/${customerId}/representatives`);
|
||||
@@ -268,6 +355,7 @@ export interface StressfreiEmail {
|
||||
platform?: string;
|
||||
notes?: string;
|
||||
isActive: boolean;
|
||||
isProvisioned?: boolean;
|
||||
hasMailbox: boolean;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
@@ -426,6 +514,17 @@ export const stressfreiEmailApi = {
|
||||
const res = await api.post<ApiResponse<{ password: string }>>(`/stressfrei-emails/${id}/reset-password`);
|
||||
return res.data;
|
||||
},
|
||||
// Weiterleitungen neu setzen (z.B. nach Änderung der Kunden-Stamm-E-Mail).
|
||||
// Wenn die Adresse hasMailbox=true ist, wird zusätzlich das im CRM
|
||||
// hinterlegte Passwort am Provider neu gesetzt (Self-Healing).
|
||||
syncForwarding: async (id: number) => {
|
||||
const res = await api.post<ApiResponse<{
|
||||
forwardTargets: string[];
|
||||
customerEmail: string;
|
||||
passwordReset?: boolean;
|
||||
}>>(`/stressfrei-emails/${id}/sync-forwarding`);
|
||||
return res.data;
|
||||
},
|
||||
// E-Mails synchronisieren
|
||||
syncEmails: async (id: number, fullSync = false) => {
|
||||
const res = await api.post<ApiResponse<SyncResult>>(`/stressfrei-emails/${id}/sync`, {}, { params: { full: fullSync } });
|
||||
@@ -451,9 +550,28 @@ export const stressfreiEmailApi = {
|
||||
};
|
||||
|
||||
// Cached Email API (E-Mail-Client)
|
||||
export interface EmailFilterParams {
|
||||
accountId?: number;
|
||||
folder?: 'INBOX' | 'SENT';
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
// Suche / Filter (alle AND-verknüpft)
|
||||
search?: string;
|
||||
fromFilter?: string;
|
||||
toFilter?: string;
|
||||
subjectFilter?: string;
|
||||
bodyFilter?: string;
|
||||
attachmentNameFilter?: string;
|
||||
hasAttachments?: boolean;
|
||||
isRead?: boolean;
|
||||
isStarred?: boolean;
|
||||
receivedFrom?: string; // ISO date
|
||||
receivedTo?: string; // ISO date
|
||||
}
|
||||
|
||||
export const cachedEmailApi = {
|
||||
// E-Mails für Kunden abrufen
|
||||
getForCustomer: async (customerId: number, options?: { accountId?: number; folder?: 'INBOX' | 'SENT'; limit?: number; offset?: number }) => {
|
||||
getForCustomer: async (customerId: number, options?: EmailFilterParams) => {
|
||||
const res = await api.get<ApiResponse<CachedEmail[]>>(`/customers/${customerId}/emails`, { params: options });
|
||||
return res.data;
|
||||
},
|
||||
@@ -513,11 +631,15 @@ export const cachedEmailApi = {
|
||||
return res.data;
|
||||
},
|
||||
// Anhang-URL (view=true für inline anzeigen, sonst download)
|
||||
// Hinweis: gibt die URL mit dem aktuellen Access-Token als Query-Param zurück,
|
||||
// weil <iframe>/<a> keinen Authorization-Header senden können. Der Token läuft
|
||||
// nach 15 min ab – wenn Anhang dann geöffnet wird, kommt 401; UI muss in dem
|
||||
// Fall die URL frisch holen.
|
||||
getAttachmentUrl: (emailId: number, filename: string, view?: boolean) => {
|
||||
const token = localStorage.getItem('token');
|
||||
const token = getAccessToken();
|
||||
const encodedFilename = encodeURIComponent(filename);
|
||||
const viewParam = view ? '&view=true' : '';
|
||||
return `${api.defaults.baseURL}/emails/${emailId}/attachments/${encodedFilename}?token=${token}${viewParam}`;
|
||||
return `${api.defaults.baseURL}/emails/${emailId}/attachments/${encodedFilename}?token=${token || ''}${viewParam}`;
|
||||
},
|
||||
// Ungelesene E-Mails zählen
|
||||
getUnreadCount: async (params: { customerId?: number; contractId?: number }) => {
|
||||
@@ -657,6 +779,10 @@ export const contractApi = {
|
||||
const res = await api.post<ApiResponse<Contract>>(`/contracts/${id}/follow-up`);
|
||||
return res.data;
|
||||
},
|
||||
createRenewal: async (id: number) => {
|
||||
const res = await api.post<ApiResponse<Contract>>(`/contracts/${id}/renewal`);
|
||||
return res.data;
|
||||
},
|
||||
getPassword: async (id: number) => {
|
||||
const res = await api.get<ApiResponse<{ password: string }>>(`/contracts/${id}/password`);
|
||||
return res.data;
|
||||
|
||||
@@ -7,6 +7,7 @@ export interface User {
|
||||
customerId?: number;
|
||||
roles?: Role[];
|
||||
isCustomerPortal?: boolean;
|
||||
mustChangePassword?: boolean;
|
||||
representedCustomers?: CustomerSummary[];
|
||||
whatsappNumber?: string;
|
||||
telegramUsername?: string;
|
||||
|
||||
@@ -13,9 +13,11 @@
|
||||
* sauberere Lösung mit kurzlebigen Download-Tokens (signierte URLs)
|
||||
* wäre v1.1-Item.
|
||||
*/
|
||||
import { getAccessToken } from '../services/api';
|
||||
|
||||
export function fileUrl(path: string | null | undefined): string {
|
||||
if (!path) return '';
|
||||
const token = localStorage.getItem('token');
|
||||
const token = getAccessToken();
|
||||
const normalizedPath = path.startsWith('/') ? path : '/' + path;
|
||||
const base = `/api/files/download?path=${encodeURIComponent(normalizedPath)}`;
|
||||
if (!token) return base;
|
||||
|
||||
Reference in New Issue
Block a user