Fix Create methods: respect user-provided IDs and set timestamps

All service-layer Create methods (team, owner, target, issuer,
certificate) were unconditionally overwriting user-provided IDs with
auto-generated ones and leaving CreatedAt/UpdatedAt as zero values.

This caused three user-visible bugs:
- POST /api/v1/teams with {"id": "t-demo"} returned a generated ID
  like "team-1773601137949154216" instead of "t-demo"
- POST /api/v1/owners referencing the user-provided team_id failed
  with Internal Server Error (FK constraint on non-existent generated ID)
- created_at/updated_at came back as "0001-01-01T00:00:00Z"

Fix: all 9 affected Create methods (both context-aware and handler
interface variants) now check if ID is empty before generating, and
set timestamps to time.Now() if zero-valued. Follows the existing
correct pattern in policy.go CreateRule/CreatePolicy.

Also removes two stale temp files (audit.go.* and issuer.go.*) that
were accidentally committed to the repo.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
shankar0123
2026-03-15 14:52:48 -04:00
parent d539361d4c
commit 3f6b0aa995
7 changed files with 95 additions and 286 deletions
+21 -2
View File
@@ -3,6 +3,7 @@ package service
import (
"context"
"fmt"
"time"
"github.com/shankar0123/certctl/internal/domain"
"github.com/shankar0123/certctl/internal/repository"
@@ -65,7 +66,16 @@ func (s *IssuerService) Create(ctx context.Context, issuer *domain.Issuer, actor
return fmt.Errorf("issuer name is required")
}
issuer.ID = generateID("issuer")
if issuer.ID == "" {
issuer.ID = generateID("issuer")
}
now := time.Now()
if issuer.CreatedAt.IsZero() {
issuer.CreatedAt = now
}
if issuer.UpdatedAt.IsZero() {
issuer.UpdatedAt = now
}
if err := s.issuerRepo.Create(ctx, issuer); err != nil {
return fmt.Errorf("failed to create issuer: %w", err)
}
@@ -160,7 +170,16 @@ func (s *IssuerService) GetIssuer(id string) (*domain.Issuer, error) {
// CreateIssuer creates a new issuer (handler interface method).
func (s *IssuerService) CreateIssuer(issuer domain.Issuer) (*domain.Issuer, error) {
issuer.ID = generateID("issuer")
if issuer.ID == "" {
issuer.ID = generateID("issuer")
}
now := time.Now()
if issuer.CreatedAt.IsZero() {
issuer.CreatedAt = now
}
if issuer.UpdatedAt.IsZero() {
issuer.UpdatedAt = now
}
if err := s.issuerRepo.Create(context.Background(), &issuer); err != nil {
return nil, fmt.Errorf("failed to create issuer: %w", err)
}