package consent import ( "context" "errors" "fmt" "time" "github.com/google/uuid" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" ) // QueryService reads published consent texts and the current account's ledger. // The repository is a PostgreSQL adapter outside a transaction; history reads // always receive the subject ID selected by the authenticated HTTP boundary. type QueryService struct { repositories outbound.ConsentRepositories clock func() time.Time } func NewQueryService(repositories outbound.ConsentRepositories, clock func() time.Time) *QueryService { if repositories == nil { panic("consent.NewQueryService - repositories must not be nil") } if clock == nil { panic("consent.NewQueryService - clock must not be nil") } return &QueryService{repositories: repositories, clock: clock} } func (service *QueryService) CurrentText(ctx context.Context, purpose domain.ConsentPurpose) (domain.ConsentText, error) { if _, err := domain.ParseConsentPurpose(string(purpose)); err != nil { return domain.ConsentText{}, fmt.Errorf("QueryService.CurrentText - validate purpose: %w", err) } if purpose.OrganizationScoped() { return domain.ConsentText{}, fmt.Errorf("QueryService.CurrentText - c4 text requires an organization snapshot: %w", ErrScopedConsentRequired) } text, err := service.repositories.ConsentTexts().Current(ctx, purpose, service.clock()) if err != nil { return domain.ConsentText{}, fmt.Errorf("QueryService.CurrentText - read current text: %w", err) } return text, nil } func (service *QueryService) List(ctx context.Context, query outbound.ConsentListQuery) (outbound.ConsentPage, error) { if query.SubjectID == uuid.Nil { return outbound.ConsentPage{}, fmt.Errorf("QueryService.List - subject is required: %w", domain.ErrInvalidConsent) } if query.Limit < 1 || query.Limit > outbound.MaxConsentPageSize { return outbound.ConsentPage{}, fmt.Errorf("QueryService.List - limit is out of range: %w", domain.ErrInvalidConsent) } if query.State != "" { if _, err := domain.ParseConsentState(string(query.State)); err != nil { return outbound.ConsentPage{}, fmt.Errorf("QueryService.List - validate state: %w", err) } } if query.After != nil && (query.After.ID == uuid.Nil || query.After.GrantedAt.IsZero()) { return outbound.ConsentPage{}, fmt.Errorf("QueryService.List - cursor position is invalid: %w", domain.ErrInvalidConsent) } page, err := service.repositories.ConsentLedger().List(ctx, query) if err != nil { return outbound.ConsentPage{}, fmt.Errorf("QueryService.List - read ledger: %w", err) } return page, nil } // HasCurrentConsent is the fail-closed check used by consumers that must not // proceed without a current grant. A missing record is a denial; a storage // failure is returned so the caller must also deny instead of assuming consent. func (service *QueryService) HasCurrentConsent(ctx context.Context, subjectID uuid.UUID, purpose domain.ConsentPurpose) (bool, error) { if subjectID == uuid.Nil { return false, fmt.Errorf("QueryService.HasCurrentConsent - subject is required: %w", domain.ErrInvalidConsent) } if purpose.OrganizationScoped() { return false, fmt.Errorf("QueryService.HasCurrentConsent - c4 needs an organization scope: %w", domain.ErrInvalidConsent) } if _, err := domain.ParseConsentPurpose(string(purpose)); err != nil { return false, fmt.Errorf("QueryService.HasCurrentConsent - validate purpose: %w", err) } _, err := service.repositories.ConsentLedger().Current(ctx, subjectID, purpose) if errors.Is(err, outbound.ErrConsentNotFound) { return false, nil } if err != nil { return false, fmt.Errorf("QueryService.HasCurrentConsent - read current consent: %w", err) } return true, nil }