feat: Variantenmanagement — Sub-Projekte mit GAP-Analyse

Backend:
- parent_project_id auf iace_projects (DB + Go Struct)
- POST/GET /variants + GET /variant-gap Endpoints
- GAP-Analyse: Differenz Hazards/Massnahmen/Kategorien

Frontend:
- VariantPanel auf Projekt-Uebersicht
- Variante erstellen Dialog
- Sidebar-Anzeige (Variantenanzahl / Basis-Link)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Benjamin Admin
2026-05-09 10:47:01 +02:00
parent 2143840ee7
commit 8682522212
8 changed files with 592 additions and 11 deletions
@@ -308,3 +308,88 @@ func (h *IACEHandler) InitFromProfile(c *gin.Context) {
"regulations_triggered": triggeredRegulations,
})
}
// ============================================================================
// Variant Management
// ============================================================================
// CreateVariant handles POST /projects/:id/variants
// Creates a new variant sub-project linked to the given base project.
func (h *IACEHandler) CreateVariant(c *gin.Context) {
parentID, err := uuid.Parse(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid project ID"})
return
}
// Verify parent project exists
parent, err := h.store.GetProject(c.Request.Context(), parentID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if parent == nil {
c.JSON(http.StatusNotFound, gin.H{"error": "parent project not found"})
return
}
var req iace.CreateProjectRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Force the parent link
req.ParentProjectID = &parentID
project, err := h.store.CreateProject(c.Request.Context(), parent.TenantID, req)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{"project": project})
}
// ListVariants handles GET /projects/:id/variants
// Returns all variant sub-projects for a given base project.
func (h *IACEHandler) ListVariants(c *gin.Context) {
parentID, err := uuid.Parse(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid project ID"})
return
}
variants, err := h.store.ListVariants(c.Request.Context(), parentID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
if variants == nil {
variants = []iace.Project{}
}
c.JSON(http.StatusOK, iace.ProjectListResponse{
Projects: variants,
Total: len(variants),
})
}
// GetVariantGap handles GET /projects/:id/variant-gap
// Returns a gap analysis comparing a variant against its base project.
func (h *IACEHandler) GetVariantGap(c *gin.Context) {
variantID, err := uuid.Parse(c.Param("id"))
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid project ID"})
return
}
gap, err := h.store.GetVariantGap(c.Request.Context(), variantID)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gap)
}
+3
View File
@@ -371,6 +371,9 @@ func registerIACERoutes(v1 *gin.RouterGroup, h *handlers.IACEHandler) {
iaceRoutes.PUT("/projects/:id", h.UpdateProject)
iaceRoutes.DELETE("/projects/:id", h.ArchiveProject)
iaceRoutes.POST("/projects/:id/init-from-profile", h.InitFromProfile)
iaceRoutes.POST("/projects/:id/variants", h.CreateVariant)
iaceRoutes.GET("/projects/:id/variants", h.ListVariants)
iaceRoutes.GET("/projects/:id/variant-gap", h.GetVariantGap)
iaceRoutes.POST("/projects/:id/completeness-check", h.CheckCompleteness)
iaceRoutes.POST("/projects/:id/components", h.CreateComponent)
iaceRoutes.GET("/projects/:id/components", h.ListComponents)
@@ -12,6 +12,7 @@ import (
// CreateProjectRequest is the API request for creating a new IACE project
type CreateProjectRequest struct {
ParentProjectID *uuid.UUID `json:"parent_project_id,omitempty"`
MachineName string `json:"machine_name" binding:"required"`
MachineType string `json:"machine_type" binding:"required"`
Manufacturer string `json:"manufacturer" binding:"required"`
@@ -199,6 +200,28 @@ type ValidateMitigationHierarchyResponse struct {
Warnings []string `json:"warnings,omitempty"`
}
// VariantGap compares a variant project against its base project
type VariantGap struct {
BaseProject ProjectSummary `json:"base_project"`
Variant ProjectSummary `json:"variant"`
Gap GapDetail `json:"gap"`
}
// ProjectSummary is a lightweight project view for gap comparisons
type ProjectSummary struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
HazardCount int `json:"hazard_count"`
MeasureCount int `json:"measure_count"`
}
// GapDetail describes the delta between variant and base project
type GapDetail struct {
AdditionalHazards int `json:"additional_hazards"`
AdditionalMeasures int `json:"additional_measures"`
CategoriesAffected []string `json:"categories_affected"`
}
// CompletenessGate represents a single gate in the project completeness checklist
type CompletenessGate struct {
ID string `json:"id"`
@@ -15,6 +15,7 @@ import (
type Project struct {
ID uuid.UUID `json:"id"`
TenantID uuid.UUID `json:"tenant_id"`
ParentProjectID *uuid.UUID `json:"parent_project_id,omitempty"`
MachineName string `json:"machine_name"`
MachineType string `json:"machine_type"`
Manufacturer string `json:"manufacturer"`
+159 -10
View File
@@ -19,6 +19,7 @@ func (s *Store) CreateProject(ctx context.Context, tenantID uuid.UUID, req Creat
project := &Project{
ID: uuid.New(),
TenantID: tenantID,
ParentProjectID: req.ParentProjectID,
MachineName: req.MachineName,
MachineType: req.MachineType,
Manufacturer: req.Manufacturer,
@@ -33,18 +34,19 @@ func (s *Store) CreateProject(ctx context.Context, tenantID uuid.UUID, req Creat
_, err := s.pool.Exec(ctx, `
INSERT INTO iace_projects (
id, tenant_id, machine_name, machine_type, manufacturer,
id, tenant_id, parent_project_id, machine_name, machine_type, manufacturer,
description, narrative_text, status, ce_marking_target,
completeness_score, risk_summary, triggered_regulations, metadata,
created_at, updated_at, archived_at
) VALUES (
$1, $2, $3, $4, $5,
$6, $7, $8, $9,
$10, $11, $12, $13,
$14, $15, $16
$1, $2, $3, $4, $5, $6,
$7, $8, $9, $10,
$11, $12, $13, $14,
$15, $16, $17
)
`,
project.ID, project.TenantID, project.MachineName, project.MachineType, project.Manufacturer,
project.ID, project.TenantID, project.ParentProjectID,
project.MachineName, project.MachineType, project.Manufacturer,
project.Description, project.NarrativeText, string(project.Status), project.CEMarkingTarget,
project.CompletenessScore, nil, project.TriggeredRegulations, project.Metadata,
project.CreatedAt, project.UpdatedAt, project.ArchivedAt,
@@ -64,13 +66,13 @@ func (s *Store) GetProject(ctx context.Context, id uuid.UUID) (*Project, error)
err := s.pool.QueryRow(ctx, `
SELECT
id, tenant_id, machine_name, machine_type, manufacturer,
id, tenant_id, parent_project_id, machine_name, machine_type, manufacturer,
description, narrative_text, status, ce_marking_target,
completeness_score, risk_summary, triggered_regulations, metadata,
created_at, updated_at, archived_at
FROM iace_projects WHERE id = $1
`, id).Scan(
&p.ID, &p.TenantID, &p.MachineName, &p.MachineType, &p.Manufacturer,
&p.ID, &p.TenantID, &p.ParentProjectID, &p.MachineName, &p.MachineType, &p.Manufacturer,
&p.Description, &p.NarrativeText, &status, &p.CEMarkingTarget,
&p.CompletenessScore, &riskSummary, &triggeredRegulations, &metadata,
&p.CreatedAt, &p.UpdatedAt, &p.ArchivedAt,
@@ -94,7 +96,7 @@ func (s *Store) GetProject(ctx context.Context, id uuid.UUID) (*Project, error)
func (s *Store) ListProjects(ctx context.Context, tenantID uuid.UUID) ([]Project, error) {
rows, err := s.pool.Query(ctx, `
SELECT
id, tenant_id, machine_name, machine_type, manufacturer,
id, tenant_id, parent_project_id, machine_name, machine_type, manufacturer,
description, narrative_text, status, ce_marking_target,
completeness_score, risk_summary, triggered_regulations, metadata,
created_at, updated_at, archived_at
@@ -113,7 +115,7 @@ func (s *Store) ListProjects(ctx context.Context, tenantID uuid.UUID) ([]Project
var riskSummary, triggeredRegulations, metadata []byte
err := rows.Scan(
&p.ID, &p.TenantID, &p.MachineName, &p.MachineType, &p.Manufacturer,
&p.ID, &p.TenantID, &p.ParentProjectID, &p.MachineName, &p.MachineType, &p.Manufacturer,
&p.Description, &p.NarrativeText, &status, &p.CEMarkingTarget,
&p.CompletenessScore, &riskSummary, &triggeredRegulations, &metadata,
&p.CreatedAt, &p.UpdatedAt, &p.ArchivedAt,
@@ -231,3 +233,150 @@ func (s *Store) UpdateProjectCompleteness(ctx context.Context, id uuid.UUID, sco
return nil
}
// ListVariants returns all variant sub-projects for a given parent project
func (s *Store) ListVariants(ctx context.Context, parentID uuid.UUID) ([]Project, error) {
rows, err := s.pool.Query(ctx, `
SELECT
id, tenant_id, parent_project_id, machine_name, machine_type, manufacturer,
description, narrative_text, status, ce_marking_target,
completeness_score, risk_summary, triggered_regulations, metadata,
created_at, updated_at, archived_at
FROM iace_projects WHERE parent_project_id = $1
ORDER BY created_at DESC
`, parentID)
if err != nil {
return nil, fmt.Errorf("list variants: %w", err)
}
defer rows.Close()
var variants []Project
for rows.Next() {
var p Project
var status string
var riskSummary, triggeredRegulations, metadata []byte
err := rows.Scan(
&p.ID, &p.TenantID, &p.ParentProjectID, &p.MachineName, &p.MachineType, &p.Manufacturer,
&p.Description, &p.NarrativeText, &status, &p.CEMarkingTarget,
&p.CompletenessScore, &riskSummary, &triggeredRegulations, &metadata,
&p.CreatedAt, &p.UpdatedAt, &p.ArchivedAt,
)
if err != nil {
return nil, fmt.Errorf("list variants scan: %w", err)
}
p.Status = ProjectStatus(status)
json.Unmarshal(riskSummary, &p.RiskSummary)
json.Unmarshal(triggeredRegulations, &p.TriggeredRegulations)
json.Unmarshal(metadata, &p.Metadata)
variants = append(variants, p)
}
return variants, nil
}
// GetVariantGap computes the gap analysis between a variant and its base project.
// It counts hazards and measures in each, and returns the categories affected.
func (s *Store) GetVariantGap(ctx context.Context, variantID uuid.UUID) (*VariantGap, error) {
variant, err := s.GetProject(ctx, variantID)
if err != nil {
return nil, fmt.Errorf("get variant: %w", err)
}
if variant == nil {
return nil, fmt.Errorf("variant project not found")
}
if variant.ParentProjectID == nil {
return nil, fmt.Errorf("project is not a variant (no parent_project_id)")
}
parent, err := s.GetProject(ctx, *variant.ParentProjectID)
if err != nil {
return nil, fmt.Errorf("get parent: %w", err)
}
if parent == nil {
return nil, fmt.Errorf("parent project not found")
}
// Count hazards and measures for both projects
parentHazards, parentMeasures, err := s.countHazardsAndMeasures(ctx, parent.ID)
if err != nil {
return nil, fmt.Errorf("count parent stats: %w", err)
}
variantHazards, variantMeasures, err := s.countHazardsAndMeasures(ctx, variant.ID)
if err != nil {
return nil, fmt.Errorf("count variant stats: %w", err)
}
// Get unique hazard categories in the variant
categories, err := s.getVariantHazardCategories(ctx, variant.ID)
if err != nil {
return nil, fmt.Errorf("get variant categories: %w", err)
}
return &VariantGap{
BaseProject: ProjectSummary{
ID: parent.ID,
Name: parent.MachineName,
HazardCount: parentHazards,
MeasureCount: parentMeasures,
},
Variant: ProjectSummary{
ID: variant.ID,
Name: variant.MachineName,
HazardCount: variantHazards,
MeasureCount: variantMeasures,
},
Gap: GapDetail{
AdditionalHazards: variantHazards,
AdditionalMeasures: variantMeasures,
CategoriesAffected: categories,
},
}, nil
}
// countHazardsAndMeasures returns (hazardCount, measureCount) for a project
func (s *Store) countHazardsAndMeasures(ctx context.Context, projectID uuid.UUID) (int, int, error) {
var hazardCount int
err := s.pool.QueryRow(ctx,
`SELECT COUNT(*) FROM iace_hazards WHERE project_id = $1`, projectID,
).Scan(&hazardCount)
if err != nil {
return 0, 0, err
}
var measureCount int
err = s.pool.QueryRow(ctx, `
SELECT COUNT(*) FROM iace_mitigations m
JOIN iace_hazards h ON m.hazard_id = h.id
WHERE h.project_id = $1
`, projectID).Scan(&measureCount)
if err != nil {
return 0, 0, err
}
return hazardCount, measureCount, nil
}
// getVariantHazardCategories returns distinct hazard categories for a project
func (s *Store) getVariantHazardCategories(ctx context.Context, projectID uuid.UUID) ([]string, error) {
rows, err := s.pool.Query(ctx,
`SELECT DISTINCT category FROM iace_hazards WHERE project_id = $1 ORDER BY category`,
projectID,
)
if err != nil {
return nil, err
}
defer rows.Close()
var categories []string
for rows.Next() {
var cat string
if err := rows.Scan(&cat); err != nil {
return nil, err
}
categories = append(categories, cat)
}
return categories, nil
}