// Package triggerapi haengt INT-20 (Trigger-/Automatisierungssystem) ein: // Verwaltung unter /triggers. Nutzt ausschliesslich internal/trigger, keine // eigene Fachlogik. Wie alle nicht dokument-gebundenen Routen (siehe // internal/autoruleapi) verlangt sie eine Identitaet (moduleauth) und die // Rechte dms.trigger.read/dms.trigger.write ueber die zentrale // Policy-Pruefung (x-permission in openapi.json); es gibt keine // Eigentuemerpruefung, weil Trigger nicht an ein Dokument gebunden sind. package triggerapi import ( "encoding/json" "errors" "io" "net/http" "github.com/jackc/pgx/v5/pgxpool" "gitea.perlbach24.de/scripte/nexarch/dms/internal/authz" "gitea.perlbach24.de/scripte/nexarch/dms/internal/trigger" ) type Server struct { pool *pgxpool.Pool vault trigger.Vault tenantSlug string } func NewServer(pool *pgxpool.Pool, vault trigger.Vault, tenantSlug string) *Server { return &Server{pool: pool, vault: vault, tenantSlug: tenantSlug} } func (s *Server) RegisterRoutes(mux *http.ServeMux) { mux.HandleFunc("POST /triggers", s.handleCreate) mux.HandleFunc("GET /triggers", s.handleList) mux.HandleFunc("GET /triggers/{id}", s.handleGet) mux.HandleFunc("DELETE /triggers/{id}", s.handleDelete) mux.HandleFunc("POST /triggers/{id}/enable", s.handleSetEnabled(true)) mux.HandleFunc("POST /triggers/{id}/disable", s.handleSetEnabled(false)) } func writeError(w http.ResponseWriter, status int, code, message string) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) _ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]string{"code": code, "message": message}}) } func caller(r *http.Request) string { c, _ := authz.Identity(r) return c } type dto struct { ID string `json:"id"` Name string `json:"name"` Event string `json:"event"` FilenamePattern *string `json:"filename_pattern,omitempty"` DocumentTypeID *string `json:"document_type_id,omitempty"` ActionType string `json:"action_type"` ActionTagID *string `json:"action_tag_id,omitempty"` ActionDocumentTypeID *string `json:"action_document_type_id,omitempty"` WebhookURL *string `json:"webhook_url,omitempty"` Enabled bool `json:"enabled"` CreatedBy string `json:"created_by"` } func toDTO(t trigger.Trigger) dto { return dto{ID: t.ID, Name: t.Name, Event: t.Event, FilenamePattern: t.FilenamePattern, DocumentTypeID: t.DocumentTypeID, ActionType: t.ActionType, ActionTagID: t.ActionTagID, ActionDocumentTypeID: t.ActionDocumentTypeID, WebhookURL: t.WebhookURL, Enabled: t.Enabled, CreatedBy: t.CreatedBy} } type createRequest struct { Name string `json:"name"` Event string `json:"event"` FilenamePattern *string `json:"filename_pattern"` DocumentTypeID *string `json:"document_type_id"` ActionType string `json:"action_type"` ActionTagID *string `json:"action_tag_id"` ActionDocumentTypeID *string `json:"action_document_type_id"` WebhookURL *string `json:"webhook_url"` WebhookSecret string `json:"webhook_secret"` } func (s *Server) handleCreate(w http.ResponseWriter, r *http.Request) { c := caller(r) if c == "" { writeError(w, http.StatusBadRequest, "invalid_request", "fehlende identitaet") return } var req createRequest if err := json.NewDecoder(io.LimitReader(r.Body, 8192)).Decode(&req); err != nil { writeError(w, http.StatusBadRequest, "invalid_request", "ungueltiger anfragekoerper") return } t := trigger.Trigger{ Name: req.Name, Event: req.Event, FilenamePattern: req.FilenamePattern, DocumentTypeID: req.DocumentTypeID, ActionType: req.ActionType, ActionTagID: req.ActionTagID, ActionDocumentTypeID: req.ActionDocumentTypeID, WebhookURL: req.WebhookURL, } out, err := trigger.Create(r.Context(), s.pool, s.vault, s.tenantSlug, c, t, req.WebhookSecret) if errors.Is(err, trigger.ErrInvalid) { writeError(w, http.StatusBadRequest, "invalid_request", err.Error()) return } if err != nil { writeError(w, http.StatusInternalServerError, "internal_error", err.Error()) return } w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusCreated) _ = json.NewEncoder(w).Encode(toDTO(out)) } func (s *Server) handleList(w http.ResponseWriter, r *http.Request) { list, err := trigger.List(r.Context(), s.pool) if err != nil { writeError(w, http.StatusInternalServerError, "internal_error", err.Error()) return } out := make([]dto, 0, len(list)) for _, t := range list { out = append(out, toDTO(t)) } w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(out) } func (s *Server) handleGet(w http.ResponseWriter, r *http.Request) { t, err := trigger.Get(r.Context(), s.pool, r.PathValue("id")) if errors.Is(err, trigger.ErrNotFound) { writeError(w, http.StatusNotFound, "not_found", "trigger nicht gefunden") return } if err != nil { writeError(w, http.StatusInternalServerError, "internal_error", err.Error()) return } w.Header().Set("Content-Type", "application/json") _ = json.NewEncoder(w).Encode(toDTO(t)) } func (s *Server) handleDelete(w http.ResponseWriter, r *http.Request) { c := caller(r) if c == "" { writeError(w, http.StatusBadRequest, "invalid_request", "fehlende identitaet") return } err := trigger.Delete(r.Context(), s.pool, c, r.PathValue("id")) if errors.Is(err, trigger.ErrNotFound) { writeError(w, http.StatusNotFound, "not_found", "trigger nicht gefunden") return } if err != nil { writeError(w, http.StatusInternalServerError, "internal_error", err.Error()) return } w.WriteHeader(http.StatusNoContent) } func (s *Server) handleSetEnabled(enabled bool) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { c := caller(r) if c == "" { writeError(w, http.StatusBadRequest, "invalid_request", "fehlende identitaet") return } err := trigger.SetEnabled(r.Context(), s.pool, c, r.PathValue("id"), enabled) if errors.Is(err, trigger.ErrNotFound) { writeError(w, http.StatusNotFound, "not_found", "trigger nicht gefunden") return } if err != nil { writeError(w, http.StatusInternalServerError, "internal_error", err.Error()) return } w.WriteHeader(http.StatusNoContent) } }