package policy import ( "context" "errors" "fmt" "lincodex/backend/internal/domain" "lincodex/backend/internal/port/outbound" ) // ErrNotDemoPolicy reports a latest product policy that is not a demonstration // version. The operator command never builds on a calibrated version. var ErrNotDemoPolicy = errors.New("the latest product policy is not a demonstration version") // ErrPolicyChanged reports that another publication took the next version // number while the section was being added. Nothing was stored; the command // can be run again. var ErrPolicyChanged = errors.New("the product policy changed during publication, run the command again") // MatchingResult reports what EnsureDemoMatchingSection did. Version is the // version that carries the matching section: the new one when Created, the // latest one otherwise. type MatchingResult struct { Created bool Version domain.PolicyVersion } // EnsureDemoMatchingSection makes the latest demonstration version carry the // demo matching section. A version that already has the section is left alone. // Otherwise it publishes the next version: a copy of the latest one with // domain.DemoMatchingPolicy added, so no other value changes. It refuses a // latest version that is not a demonstration version and a family without a // version, because the section is only an addition to a policy that exists. func (publisher *Publisher) EnsureDemoMatchingSection(ctx context.Context, author domain.PolicyAuthor) (MatchingResult, error) { const location = "Publisher.EnsureDemoMatchingSection" latest, err := publisher.policies.GetLatest(ctx, domain.ProductPolicyKey) if errors.Is(err, outbound.ErrPolicyVersionNotFound) { return MatchingResult{}, fmt.Errorf("%s - the product policy has no version to extend: %w", location, err) } if err != nil { return MatchingResult{}, fmt.Errorf("%s - read latest version: %w", location, err) } current := latest.Policy() if current.Matching != nil { return MatchingResult{Version: latest}, nil } if latest.Kind() != domain.PolicyKindDemo { return MatchingResult{}, fmt.Errorf("%s - latest version %d is %s: %w", location, latest.Version(), latest.Kind(), ErrNotDemoPolicy) } matching := domain.DemoMatchingPolicy() current.Matching = &matching // The new version is built on the version that was read, so it takes the // next number exactly once. A taken number means the base is no longer the // latest, and the operator decides again instead of the command retrying. draft, err := domain.NewPolicyVersionDraft(domain.NewPolicyVersionDraftInput{ Key: domain.ProductPolicyKey, Kind: domain.PolicyKindDemo, Author: author, Policy: current, }) if err != nil { return MatchingResult{}, fmt.Errorf("%s - build the extended version: %w", location, err) } numbered, err := draft.WithVersion(latest.Version() + 1) if err != nil { return MatchingResult{}, fmt.Errorf("%s - number the extended version: %w", location, err) } published, err := publisher.policies.Create(ctx, numbered) if errors.Is(err, outbound.ErrPolicyVersionConflict) { return MatchingResult{}, fmt.Errorf("%s - version %d is taken: %w", location, numbered.Version(), ErrPolicyChanged) } if err != nil { return MatchingResult{}, fmt.Errorf("%s - store the extended version: %w", location, err) } return MatchingResult{Created: true, Version: published}, nil }