Files
Holzleitner---Backend--aktu…/migrations/0006_cars.sql
Dennis Nemec 438040acce Initial: Rust-Backend mit Clean Architecture (domain/application/infrastructure/api)
Vier-Crate-Workspace mit:
- Domain: Account, Car, Tour, Delivery, DeliveryItem, DeliveryNote, Customer,
  Article, Warehouse, ScanState, AuditAction — alle mit serde + feature-gated
  utoipa::ToSchema.
- Application: Ports (TourRepository, DeliveryRepository, ScanRepository,
  DeliveryNoteRepository, CarRepository, AuthService) und Use Cases.
- Infrastructure: Postgres-Adapter via sqlx (PgTourRepository etc.) +
  Keycloak-AuthService mit JWKS-Cache + OIDC-Discovery.
- API: Axum 0.8, utoipa-OpenAPI + Swagger-UI, JWT-Bearer-Middleware,
  AuthenticatedUser-Extractor.

Endpoints:
- GET /me/tours/today, /tours/{id}, /accounts/{pn}, /me/cars, /health
- POST /sync/tour, /scans (bulk + idempotent via clientScanId),
  /deliveries/{id}/{hold,resume,cancel,complete,notes}, /me/cars
- PUT /tours/{id}/delivery-order, /deliveries/{id}/assigned-car, /me/cars/{id}
- PATCH /me/cars/{id}

Datenmodell:
- 6 Migrationen (accounts, tours/deliveries/items + Stammdaten,
  scan_audit mit clientScanId-UNIQUE, state_reason refactor,
  delivery_notes, cars + FKs nachziehen).
- Business-stabile Beleg-Keys (belegart_id, belegnummer) für ERP-Sync.
- Append-only scan_audit + embedded scan_state als doppelte Wahrheit.

Dev-Setup:
- docker-compose mit Postgres 17 + Keycloak 26
- Keycloak-Realm 'holzleitner' mit Public-Client (PKCE), Testfahrer
  (PN 1001) + Audience-/Personalnummer-Mapper
2026-05-14 22:28:31 +02:00

40 lines
1.5 KiB
SQL

-- Fahrzeuge eines Subunternehmer-Accounts. Kein ERP-Spiegel — die App
-- pflegt die Fahrzeuge selbst.
--
-- Im Audit-Log und an Lieferungen war `car_id` bislang ohne FK
-- geführt (Spalten existieren in 0002/0003/0005). Diese Migration
-- zieht die FKs nach. Bestehende NULL-Werte bleiben gültig.
--
-- `(account_id, plate)` ist fachlich eindeutig — pro Account keine
-- doppelten Kennzeichen.
CREATE TABLE cars (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
account_id BIGINT NOT NULL REFERENCES accounts(personalnummer),
plate TEXT NOT NULL,
active BOOLEAN NOT NULL DEFAULT TRUE
);
CREATE UNIQUE INDEX cars_account_plate ON cars(account_id, plate);
CREATE INDEX cars_account ON cars(account_id);
-- FKs nachziehen — alle Spalten bleiben NULLABLE, weil
-- Audit-Einträge der Vor-Cars-Phase keinen car_id haben.
ALTER TABLE deliveries
ADD CONSTRAINT deliveries_assigned_car_fk
FOREIGN KEY (assigned_car_id) REFERENCES cars(id);
ALTER TABLE delivery_notes
ADD CONSTRAINT delivery_notes_author_car_fk
FOREIGN KEY (author_car_id) REFERENCES cars(id);
ALTER TABLE scan_audit
ADD CONSTRAINT scan_audit_actor_car_fk
FOREIGN KEY (actor_car_id) REFERENCES cars(id);
-- Seed: zwei Fahrzeuge für Testfahrer (PN 1001) — bewusst zwei,
-- damit der Flow "Auswählen ab 2 Autos" testbar ist.
INSERT INTO cars (id, account_id, plate, active) VALUES
('77777777-7777-7777-7777-777777777771', 1001, 'BGL-HZ 100', TRUE),
('77777777-7777-7777-7777-777777777772', 1001, 'BGL-HZ 200', TRUE);