Collapses the 3 UPC appeal proceeding_types (upc.apl.merits 7 rules,
upc.apl.cost 2, upc.apl.order 7 = 16 total across 3 codes) into ONE
unified upc.apl proceeding type + a per-rule applies_to_target[]
discriminator. The verfahrensablauf picker now shows one "Berufung"
tile; after picking it, the user selects which decision the appeal is
directed AT via a 5-chip group (Endentscheidung / Kostenentscheidung /
Anordnung / Schadensbemessung / Bucheinsicht) and the engine filters
rules whose applies_to_target contains the picked slug.
m's 2026-05-26 decision: Schadensbemessung-as-appeal is a NEW first-
class target with its OWN rule set (no shared inheritance from
merits). The 5 enum values are all defined + addressable; for now
schadensbemessung and bucheinsicht return empty timelines until rules
are seeded in a follow-up slice (likely via /admin/rules or pairing
with t-paliad-193 orphan-concept-seed).
Migration 134 (additive only):
- ADD proceeding_types.appeal_target text (CHECK on 5 slugs OR NULL)
- ADD deadline_rules.applies_to_target text[] (CHECK each element
in the 5 slugs)
- INSERT the unified upc.apl row (inherits sort/color from
upc.apl.merits)
- Audit-first RAISE NOTICE pass listing every row about to be
touched + a post-migration sanity check
- Reassign rule rows: merits → applies_to_target={endentscheidung},
cost → {kostenentscheidung}, order → {anordnung}
- Archive (is_active=false, NOT DELETE) the 3 old proceeding_types
so historical FKs stay intact
- Down migration restores is_active=true on the 3 old types, points
rules back by their applies_to_target stamp, drops the unified
row, drops both columns. Safe.
Package additions (pkg/litigationplanner):
- AppealTarget* constants + AppealTargets[] ordered list +
IsValidAppealTarget(s) predicate (silent no-op on unknown slugs
so a stale frontend chip doesn't break the render)
- ProceedingType.AppealTarget *string field (top-level marker;
NULL on non-appeal proceedings)
- Rule.AppliesToTarget pq.StringArray field (per-row applies-to set)
- CalcOptions.AppealTarget string (engine filter — when set,
keeps only rules whose AppliesToTarget contains the slug)
Engine filter runs after ApplyRuleOverrides but before the rule walk
so the existing condition_expr / spawn / appellant-context machinery
operates on the filtered subset transparently.
paliad-side wiring:
- deadline_rule_service.go: ruleColumns + proceedingTypeColumns
extended to scan the new columns
- handlers/fristenrechner.go: AppealTarget JSON field on the
request payload, threaded into CalcOptions
Frontend (verfahrensablauf surface only):
- Single "Berufung" tile replaces the 3 separate Berufung tiles
- New 5-chip appeal-target row, shown only when upc.apl is picked
- URL state ?target=<slug>; default endentscheidung when none set
- APPELLANT_AXIS_PROCEEDINGS updated: upc.apl.* (3 entries) →
upc.apl (1 entry)
- i18n keys (DE + EN) for the new tile + the 5 chip labels +
the "Worauf richtet sich die Berufung?" / "Appeal against:" prompt
- calculateDeadlines threads appealTarget through to the API
Acceptance:
- go build clean, go test all green (existing test suite — no new
tests on the engine filter as a follow-up; the migration's
sanity-check DO block guards the rule-reassignment count)
- Live audit before drafting confirmed: 3 active UPC appeal
proceeding_types, 16 rules total, primary_party already conforms
to 4-value vocab on all proceeding-bound rules
302 lines
12 KiB
Go
302 lines
12 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"mgit.msbls.de/m/paliad/internal/models"
|
|
"mgit.msbls.de/m/paliad/internal/services"
|
|
)
|
|
|
|
// Fristenrechner page handler: serves the static HTML. No DB dependency.
|
|
//
|
|
// Back-compat: the pre-split sidebar entry for "Verfahrensablauf" pointed at
|
|
// /tools/fristenrechner?path=a. After the t-paliad-179 split, that landing is
|
|
// owned by /tools/verfahrensablauf. A naked ?path=a (no Akte context — i.e.
|
|
// no ?project=) is the bookmarked-legacy-entry case → 302 to the new route.
|
|
// ?project=<uuid>&path=a is the Akte-mode internal wizard pathway and stays
|
|
// on /tools/fristenrechner so the wizard state survives a refresh.
|
|
func handleFristenrechnerPage(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
if q.Get("path") == "a" && q.Get("project") == "" {
|
|
http.Redirect(w, r, "/tools/verfahrensablauf", http.StatusFound)
|
|
return
|
|
}
|
|
http.ServeFile(w, r, "dist/fristenrechner.html")
|
|
}
|
|
|
|
// Verfahrensablauf page handler (t-paliad-179 Slice 1): the dedicated
|
|
// abstract-browse surface for procedural shape. No DB dependency — the page
|
|
// shell is static HTML; the calculator API still drives the timeline render.
|
|
func handleVerfahrensablaufPage(w http.ResponseWriter, r *http.Request) {
|
|
http.ServeFile(w, r, "dist/verfahrensablauf.html")
|
|
}
|
|
|
|
// POST /api/tools/fristenrechner — calculate the UI timeline for a proceeding.
|
|
//
|
|
// Phase C: routes through FristenrechnerService which pulls rules from
|
|
// paliad.deadline_rules. When DATABASE_URL is unset, returns 503; the page
|
|
// itself still renders because it's static HTML.
|
|
func handleFristenrechnerAPI(w http.ResponseWriter, r *http.Request) {
|
|
if dbSvc == nil || dbSvc.fristenrechner == nil {
|
|
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
|
|
"error": "Fristenrechner ist vorübergehend nicht verfügbar (keine Datenbank).",
|
|
})
|
|
return
|
|
}
|
|
var req struct {
|
|
ProceedingType string `json:"proceedingType"`
|
|
TriggerDate string `json:"triggerDate"`
|
|
PriorityDate string `json:"priorityDate,omitempty"`
|
|
Flags []string `json:"flags,omitempty"`
|
|
AnchorOverrides map[string]string `json:"anchorOverrides,omitempty"`
|
|
CourtID string `json:"courtId,omitempty"`
|
|
// t-paliad-265: per-event-card choices. Two parallel inputs:
|
|
// - ProjectID lets the server pull persisted choices from
|
|
// paliad.project_event_choices (project-bound /tools/fristenrechner).
|
|
// - PerCardChoices lets the unbound /tools/verfahrensablauf
|
|
// send an inline-CSV-decoded list straight off the URL
|
|
// without persisting. When both are present the inline list
|
|
// wins (what-if exploration overrides the saved state).
|
|
ProjectID string `json:"projectId,omitempty"`
|
|
PerCardChoices []services.UpsertEventChoiceInput `json:"perCardChoices,omitempty"`
|
|
// t-paliad-290 (m/paliad#122): re-surface previously-hidden
|
|
// optional cards. When true the calculator marks skipped rows
|
|
// with UIDeadline.IsHidden instead of dropping them; descendants
|
|
// stay in the result list. Default false preserves the legacy
|
|
// suppression. HiddenCount on the response is independent.
|
|
IncludeHidden bool `json:"includeHidden,omitempty"`
|
|
// Slice B1 / m/paliad#124 §18.1: narrows the unified UPC
|
|
// Berufung (upc.apl) timeline to the rule subset whose
|
|
// applies_to_target contains the requested slug. Empty = no
|
|
// filter. Valid values: endentscheidung | kostenentscheidung
|
|
// | anordnung | schadensbemessung | bucheinsicht. Unknown
|
|
// slugs are silently dropped (no filter) so a stale frontend
|
|
// chip doesn't 400 the request.
|
|
AppealTarget string `json:"appealTarget,omitempty"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Ungültige Anfrage"})
|
|
return
|
|
}
|
|
if req.ProceedingType == "" || req.TriggerDate == "" {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "proceedingType und triggerDate sind erforderlich"})
|
|
return
|
|
}
|
|
|
|
// Fold per-card choices into the CalcOptions addendum. The inline
|
|
// PerCardChoices wins over the persisted ProjectID lookup when both
|
|
// are non-empty.
|
|
var addendum services.CalcOptionsAddendum
|
|
if len(req.PerCardChoices) > 0 {
|
|
choices := make([]models.ProjectEventChoice, 0, len(req.PerCardChoices))
|
|
for _, c := range req.PerCardChoices {
|
|
choices = append(choices, models.ProjectEventChoice{
|
|
SubmissionCode: c.SubmissionCode,
|
|
ChoiceKind: c.ChoiceKind,
|
|
ChoiceValue: c.ChoiceValue,
|
|
})
|
|
}
|
|
addendum = services.ToCalcOptionsAddendum(choices)
|
|
} else if req.ProjectID != "" && dbSvc.eventChoice != nil {
|
|
if pid, err := uuid.Parse(req.ProjectID); err == nil {
|
|
if uid, ok := requireUser(w, r); ok {
|
|
if choices, err := dbSvc.eventChoice.ListForProject(r.Context(), uid, pid); err == nil {
|
|
addendum = services.ToCalcOptionsAddendum(choices)
|
|
}
|
|
// Visibility-filtered lookup: a non-visible project
|
|
// returns ErrNotVisible from ListForProject; in that
|
|
// case we project without per-card overlays rather
|
|
// than 404 — the timeline itself is non-PII data.
|
|
}
|
|
}
|
|
}
|
|
|
|
resp, err := dbSvc.fristenrechner.Calculate(r.Context(), req.ProceedingType, req.TriggerDate, services.CalcOptions{
|
|
PriorityDateStr: req.PriorityDate,
|
|
Flags: req.Flags,
|
|
AnchorOverrides: req.AnchorOverrides,
|
|
CourtID: req.CourtID,
|
|
PerCardAppellant: addendum.PerCardAppellant,
|
|
SkipRules: addendum.SkipRules,
|
|
IncludeCCRFor: addendum.IncludeCCRFor,
|
|
IncludeHidden: req.IncludeHidden,
|
|
AppealTarget: req.AppealTarget,
|
|
})
|
|
if err != nil {
|
|
if errors.Is(err, services.ErrUnknownProceedingType) {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "unbekannter Verfahrenstyp: " + req.ProceedingType})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// POST /api/tools/fristenrechner/calculate-rule — single-rule calc for
|
|
// the v4 (t-paliad-136 Phase B) result-card click flow.
|
|
//
|
|
// Body: { ruleId? } OR { proceedingCode, ruleLocalCode }, plus
|
|
// triggerDate (YYYY-MM-DD, required) and flags? (string array,
|
|
// optional condition_flag inputs).
|
|
//
|
|
// Returns a RuleCalculation (see services.RuleCalculation) — the rule
|
|
// metadata + computed dueDate / originalDate / adjustmentReason. Used by
|
|
// the result-card calc panel; distinct from the full-timeline endpoint
|
|
// at POST /api/tools/fristenrechner.
|
|
func handleFristenrechnerCalculateRule(w http.ResponseWriter, r *http.Request) {
|
|
if dbSvc == nil || dbSvc.fristenrechner == nil {
|
|
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
|
|
"error": "Fristenrechner ist vorübergehend nicht verfügbar (keine Datenbank).",
|
|
})
|
|
return
|
|
}
|
|
var req struct {
|
|
RuleID string `json:"ruleId"`
|
|
ProceedingCode string `json:"proceedingCode"`
|
|
RuleLocalCode string `json:"ruleLocalCode"`
|
|
TriggerDate string `json:"triggerDate"`
|
|
Flags []string `json:"flags,omitempty"`
|
|
CourtID string `json:"courtId,omitempty"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Ungültige Anfrage"})
|
|
return
|
|
}
|
|
if req.TriggerDate == "" {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "triggerDate ist erforderlich"})
|
|
return
|
|
}
|
|
if req.RuleID == "" && (req.ProceedingCode == "" || req.RuleLocalCode == "") {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{
|
|
"error": "Entweder ruleId oder (proceedingCode + ruleLocalCode) ist erforderlich",
|
|
})
|
|
return
|
|
}
|
|
|
|
resp, err := dbSvc.fristenrechner.CalculateRule(r.Context(), services.CalcRuleParams{
|
|
RuleID: req.RuleID,
|
|
ProceedingCode: req.ProceedingCode,
|
|
RuleLocalCode: req.RuleLocalCode,
|
|
TriggerDate: req.TriggerDate,
|
|
Flags: req.Flags,
|
|
CourtID: req.CourtID,
|
|
})
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, services.ErrUnknownRule):
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "unbekannte Regel"})
|
|
case errors.Is(err, services.ErrUnknownProceedingType):
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "unbekannter Verfahrenstyp: " + req.ProceedingCode})
|
|
default:
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
}
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// GET /api/tools/proceeding-types — metadata list for the wizard buttons.
|
|
// Returns 503 with an empty array when DATABASE_URL is unset so the page
|
|
// still renders (buttons are server-rendered from tsx and don't depend on
|
|
// this endpoint for existence, only for dynamic list updates).
|
|
func handleProceedingTypes(w http.ResponseWriter, r *http.Request) {
|
|
if dbSvc == nil || dbSvc.fristenrechner == nil {
|
|
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
|
|
"error": "Verfahrenstypen vorübergehend nicht verfügbar (keine Datenbank).",
|
|
})
|
|
return
|
|
}
|
|
types, err := dbSvc.fristenrechner.ListFristenrechnerTypes(r.Context())
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "konnte Verfahrenstypen nicht laden"})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, types)
|
|
}
|
|
|
|
// GET /api/tools/trigger-events — list active UPC trigger events for the
|
|
// "Was kommt nach…" mode picker. Sorted alphabetically by name.
|
|
func handleTriggerEventsList(w http.ResponseWriter, r *http.Request) {
|
|
if dbSvc == nil || dbSvc.eventDeadline == nil {
|
|
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
|
|
"error": "Trigger-Ereignisse vorübergehend nicht verfügbar (keine Datenbank).",
|
|
})
|
|
return
|
|
}
|
|
events, err := dbSvc.eventDeadline.ListTriggerEvents(r.Context())
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "konnte Trigger-Ereignisse nicht laden"})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, events)
|
|
}
|
|
|
|
// POST /api/tools/event-deadlines — compute all deadlines flowing from a
|
|
// trigger event + date. Body: {"triggerEventId": <int>, "triggerDate": "YYYY-MM-DD"}.
|
|
func handleEventDeadlinesCalculate(w http.ResponseWriter, r *http.Request) {
|
|
if dbSvc == nil || dbSvc.eventDeadline == nil {
|
|
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
|
|
"error": "Fristenrechner ist vorübergehend nicht verfügbar (keine Datenbank).",
|
|
})
|
|
return
|
|
}
|
|
var req struct {
|
|
TriggerEventID int64 `json:"triggerEventId"`
|
|
TriggerDate string `json:"triggerDate"`
|
|
CourtID string `json:"courtId"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "Ungültige Anfrage"})
|
|
return
|
|
}
|
|
if req.TriggerEventID <= 0 || req.TriggerDate == "" {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "triggerEventId und triggerDate sind erforderlich"})
|
|
return
|
|
}
|
|
resp, err := dbSvc.eventDeadline.Calculate(r.Context(), req.TriggerEventID, req.TriggerDate, req.CourtID)
|
|
if err != nil {
|
|
if errors.Is(err, services.ErrUnknownTriggerEvent) {
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": "unbekanntes Trigger-Ereignis"})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusBadRequest, map[string]string{"error": err.Error()})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// GET /api/tools/courts — list active courts for the Fristenrechner court
|
|
// picker. Optional ?courtType=UPC-LD filter narrows to a single tier so the
|
|
// UI can render only the courts compatible with the selected proceeding.
|
|
// Returns the deadline-computation slice (id, code, names, country, regime,
|
|
// court_type, sort_order) — NOT the full Gerichtsverzeichnis catalog. The
|
|
// rich addresses / phone / languages payload still lives at /api/courts.
|
|
func handleCourtsList(w http.ResponseWriter, r *http.Request) {
|
|
if dbSvc == nil || dbSvc.courts == nil {
|
|
writeJSON(w, http.StatusServiceUnavailable, map[string]string{
|
|
"error": "Gerichte vorübergehend nicht verfügbar (keine Datenbank).",
|
|
})
|
|
return
|
|
}
|
|
courtType := r.URL.Query().Get("courtType")
|
|
var (
|
|
courts []services.Court
|
|
err error
|
|
)
|
|
if courtType != "" {
|
|
courts, err = dbSvc.courts.ByCourtType(courtType)
|
|
} else {
|
|
courts, err = dbSvc.courts.All()
|
|
}
|
|
if err != nil {
|
|
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "konnte Gerichte nicht laden"})
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, courts)
|
|
}
|