Compare commits

..

7 Commits
v1.1.0 ... main

Author SHA1 Message Date
duffyduck 77602bb4ac contracts: VVL (Vertragsverlängerung) als Split-Button neben Folgevertrag
VVL = Vertragsverlängerung beim selben Anbieter (vs. Folgevertrag = i.d.R.
Anbieterwechsel).

Im Gegensatz zu createFollowUpContract wird ALLES kopiert:
- Provider, Tarif, Portal-Username/Passwort (verschlüsselt)
- Preise (basePrice/unitPrice/bonus etc.)
- Notes, Commission, Internet-Zugangsdaten, SIP-Daten, SIM-PINs
- ContractDocuments (1:1, gleiche Datei-Referenz)
- Detail-Tabellen (Energy/Internet/Mobile/TV/CarInsurance) komplett

Berechnet:
- newStartDate = oldStartDate + Vertragslaufzeit (Monate aus
  ContractDuration.code/description geparsed: "24M" / "24 Monate" / "2J")
- newEndDate = newStartDate + Laufzeit
- status = DRAFT (User bestätigt manuell)

NICHT kopiert:
- documentType "Auftragsformular" (das wird neu unterschrieben)
- cancellation*-Felder (alter Cancel-Flow nicht relevant)

Frontend:
- Split-Button: Hauptaktion "Folgevertrag anlegen" + ChevronDown-Pfeil
- Dropdown: "VVL anlegen" mit Bestätigungs-Modal
- Modal zeigt Vorhersage des neuen Startdatums (alter Start +
  Vertragslaufzeit als Hinweis)

History-Einträge wie bei Folgevertrag, mit eigenem VVL-Wording.
Doppel-Schutz: maximal 1 Folge-/VVL-Vertrag pro Vorgänger.

Live-verifiziert:
- Contract #17 (FIBER, 2026-05-01, 24M) → VVL mit Start 2028-05-01 ✓
- Provider/Tarif/Preise/Credentials 1:1 übernommen
- 2 Dokumente kopiert (außer Auftragsformular)
- History-Einträge in beiden Verträgen vorhanden

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 09:12:39 +02:00
duffyduck e763952a84 adminer: Theme-Bootstrap für Designs mit non-Standard CSS-Filenamen
Bug: ADMINER_DESIGN=dracula (oder adminer-dark) zeigte das Default-
Theme. Das offizielle Adminer-Image symverlinkt nur designs/.../adminer.css,
aber manche Designs haben adminer-dark.css, sodass der Symlink ins Leere
lief.

Fix: eigener entrypoint, der das erste .css im gewählten Design verlinkt
(unabhängig vom Filename). Anschließend wird der Original-entrypoint.sh
ausgeführt.

Live-verifiziert: dracula → adminer-dark.css symlink ok, HTML lädt
adminer.css mit 13 KB Theme-CSS.

Plus: .env.example listet alle ~28 verfügbaren Designs als Kommentar
und schlägt 'dracula' als Default vor.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 10:26:20 +02:00
duffyduck 3823f8aa50 backup: SecurityEvent-Tabelle im Backup + Restore mit aufnehmen
Bug: Die in Runde 10 hinzugefügte SecurityEvent-Tabelle (Monitoring) war
nicht im Backup-Service erfasst – beim Backup wurden 43 von 44 Tabellen
gesichert, beim Restore die SecurityEvent-Daten nicht zurückgespielt.

3 Stellen ergänzt:
- tables-Liste (createBackup): SecurityEvent wird jetzt mit findMany abgegriffen
- delete-Order (restoreBackup): securityEvent.deleteMany vor dem Wiederbefüllen
- restoreOrder: SecurityEvent.upsert nach AuditLog

Live-verifiziert: neues Backup enthält SecurityEvent.json mit 152 Einträgen.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 10:19:18 +02:00
duffyduck 0671565433 docker: Runtime auf node:20-slim (Alpine→Debian) – Prisma+TLS-Kompatibilität
Bug: Im Container schlug Prisma + mariadb-Auth fehl.
- Prisma-Engine `linux-musl` braucht libssl.so.1.1 → Alpine 3.19+ hat
  nur openssl 3 → "shared library libssl.so.1.1 not found"
- mariadb-client unter Alpine warf "TLS/SSL error: SSL is required"

Fix: alle Stages (Frontend-build, Backend-build, Runtime) auf
node:20-slim (Debian-bookworm). glibc + openssl 3 ABI-kompatibel,
Prisma generiert linux-debian-Engine korrekt.

Plus: .dockerignore um data/, plesktest/, backup-Klone erweitert
(Build-Context war u.a. wegen MariaDB-Files mit restricted Permissions
nicht lesbar).
Plus: docker-compose.yml: version: '3.8' für docker-compose v1
Kompatibilität.

Live-verifiziert: docker-compose up -d --build → alle 3 Container
healthy, Login funktioniert, alte DB-Daten (3 Kunden, 15 Verträge,
144 SecurityEvents) erhalten via Volume-zu-Bind-Mount-Migration.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 20:05:37 +02:00
duffyduck e145edaa90 docker: zentrale .env + Compose mit MariaDB+OpenCRM+Adminer + Bind-Mounts
Big Move: vom backend-only-Setup zum vollständigen Container-Stack.

📁 Neue Struktur
- /.env (lokal, nicht getrackt) – zentrale Konfiguration für Dev + Docker
- /.env.example – Template mit allen Variablen
- /data/{db,uploads,factory-defaults,backups}/ – Bind-Mounts statt Volumes
  (auf Wunsch: Daten bleiben im Projektverzeichnis)
- /backend/Dockerfile – Multi-Stage Build (Frontend + Backend)
- /backend/docker-entrypoint.sh – wartet auf DB, prisma db push, optional seed

🐳 docker-compose.yml (neu konsolidiert)
- mariadb 10.11 mit Bind-Mount ./data/db
- opencrm-app (Backend serviert Frontend statisch in production)
- adminer mit Theme pepa-linha-dark als DB-UI
- Ports + Pfade + Secrets alle aus .env

🔧 Backend
- index.ts dotenv-Loader: lädt zuerst Root /.env, dann backend/.env als
  Fallback. Funktioniert nahtlos für npm run dev und für Container.
- backend/.env.example als Legacy-Fallback dokumentiert

📝 README
- Quick-Start mit Docker als empfohlener Default (3 Befehle)
- Tabelle der Daten-Verzeichnisse
- Hinweis auf RUN_SEED=true beim ersten Start

⚙ Konfigurierbar via .env
- OPENCRM_PORT (Backend extern), ADMINER_PORT (DB-UI), DB_PORT
- Daten-Pfade (DATA_DIR, DB_DATA_DIR, UPLOADS_DIR etc.)
- DB_NAME/USER/PASSWORD, JWT_SECRET, ENCRYPTION_KEY
- ADMINER_DESIGN (Theme-Auswahl)

Hinweis: Vor dem ersten `docker compose up -d` muss das laufende
`npm run dev`-Backend gestoppt werden (Port + DB-Conflict). Das alte
Volume `opencrm_mariadb_data` bleibt unangetastet als Notfall-Backup.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 18:53:19 +02:00
duffyduck 3b4a680326 chore: backend/.env aus Git entfernt + .gitignore klargestellt
backend/.env war seit "first commit" getrackt (mit echten Secrets:
JWT_SECRET, ENCRYPTION_KEY, DB-Password). Das Pattern .env war zwar
in .gitignore, wirkte aber nicht rückwirkend.

- git rm --cached backend/.env (Datei bleibt lokal)
- backend/.gitignore + frontend/.gitignore: explizite !.env.example
  Whitelist zur Klarstellung
- Neue Root-.gitignore mit gemeinsamen Patterns (Env, OS, IDE, Logs)

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 18:35:00 +02:00
duffyduck 389b878dbd Monitoring: Threshold-Debounce auf sliding-window (statt floor-to-hour)
Bug: zweimal CRITICAL-Alert für dieselbe Brute-Force-Erkennung kam an.

Ursache: detectThresholds() hat als Cutoff für den "existing"-Check
floor(now, hour) genutzt. Bei Stundenwechsel resettete der Bucket
und der nächste Cron-Lauf fand nichts mehr "in der aktuellen Stunde"
→ erzeugte zweites SUSPICIOUS-Event → zweite Mail.

Fix: gleitendes 60min-Fenster (now - 60min). Pro IP gibt es jetzt
zuverlässig max. 1 CRITICAL-Alert pro Stunde, unabhängig von der
absoluten Uhrzeit.

Live-verifiziert in DB: zwei Alerts kamen um 07:41 und 08:00 –
genau das Pattern, das der Stunden-Reset erzeugt.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-01 10:11:52 +02:00
23 changed files with 807 additions and 36 deletions

View File

@ -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

55
.env.example Normal file
View File

@ -0,0 +1,55 @@
# 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 ==============
DB_NAME=opencrm
DB_USER=opencrm
DB_PASSWORD=change-this-password
DB_ROOT_PASSWORD=change-this-root-password
# Connection-String fürs Backend (im Container = Service-Name `db`)
# Im Dev-Modus (npm run dev) lokal: localhost:DB_PORT
DATABASE_URL=mysql://root:change-this-root-password@localhost:3306/opencrm
# ============== SECURITY ==============
# JWT-Secret: min. 32 Zeichen. Generieren: openssl rand -hex 64
JWT_SECRET=change-this-to-a-very-long-random-secret-please-rotate-before-production
JWT_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=
# ============== 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

37
.gitignore vendored Normal file
View File

@ -0,0 +1,37 @@
# 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

View File

@ -47,32 +47,62 @@ Web-basiertes CRM-System für Kundenverwaltung mit Verträgen (Energie, Telekomm
> - 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.

View File

@ -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"

3
backend/.gitignore vendored
View File

@ -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/*

65
backend/Dockerfile Normal file
View File

@ -0,0 +1,65 @@
# 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
# 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"]

27
backend/docker-entrypoint.sh Executable file
View File

@ -0,0 +1,27 @@
#!/bin/sh
# Beim Container-Start: Schema in DB pushen (idempotent) + (optional) seed.
# RUN_SEED=true beim ersten Start setzen, danach wieder auf false.
set -e
echo "[entrypoint] Warte auf Datenbank…"
# Prisma versucht selbst Connect; einfacher Retry-Loop um Race-Bedingungen
# beim parallelen Container-Start abzufangen.
TRIES=30
until npx prisma db push --skip-generate --accept-data-loss 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-Schema synced"
if [ "${RUN_SEED:-false}" = "true" ]; then
echo "[entrypoint] RUN_SEED=true seede DB"
npx prisma db seed || echo "[entrypoint] Seed fehlgeschlagen oder schon gelaufen ignoriert"
fi
echo "[entrypoint] Starte Backend…"
exec "$@"

View File

@ -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);

View File

@ -4,6 +4,16 @@ import helmet from 'helmet';
import path from 'path';
import dotenv from 'dotenv';
// .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.
dotenv.config({ path: path.resolve(__dirname, '../../.env') }); // Root /.env
dotenv.config({ path: path.resolve(__dirname, '../.env') }); // backend/.env (Fallback)
dotenv.config(); // cwd-relativ (Container etc.)
import authRoutes from './routes/auth.routes.js';
import customerRoutes from './routes/customer.routes.js';
import addressRoutes from './routes/address.routes.js';
@ -43,8 +53,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)');

View File

@ -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);

View File

@ -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;

View File

@ -765,6 +765,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({

View File

@ -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,
});
}

View File

@ -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;

0
data/backups/.gitkeep Normal file
View File

0
data/db/.gitkeep Normal file
View File

View File

0
data/uploads/.gitkeep Normal file
View File

View File

@ -1,3 +1,18 @@
# 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:
@ -6,20 +21,82 @@ 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:
# Connection ins Container-Netzwerk (Service-Name = Hostname)
DATABASE_URL: "mysql://root:${DB_ROOT_PASSWORD}@db:3306/${DB_NAME}"
JWT_SECRET: ${JWT_SECRET}
JWT_EXPIRES_IN: ${JWT_EXPIRES_IN:-7d}
ENCRYPTION_KEY: ${ENCRYPTION_KEY}
NODE_ENV: production
PORT: 3001
LISTEN_ADDR: 0.0.0.0
CORS_ORIGINS: ${CORS_ORIGINS:-}
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"

3
frontend/.gitignore vendored
View File

@ -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

View File

@ -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)} />

View File

@ -657,6 +657,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;