75 lines
3.7 KiB
Go
75 lines
3.7 KiB
Go
package outbound
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
)
|
|
|
|
var (
|
|
// ErrPolicyVersionNotFound reports that no version matches: an unknown
|
|
// ID, a family without versions, or no version effective at the time.
|
|
ErrPolicyVersionNotFound = errors.New("policy version not found")
|
|
|
|
// ErrPolicyVersionConflict reports that the family already has a version
|
|
// with the same number, or the ID is taken. Nothing was stored and the
|
|
// surrounding transaction stays usable; a publisher computes the next
|
|
// number again and retries.
|
|
ErrPolicyVersionConflict = errors.New("policy version already exists")
|
|
|
|
// ErrPolicyVersionBackdated reports a scheduled effective time earlier
|
|
// than the publication time that the database assigned.
|
|
ErrPolicyVersionBackdated = errors.New("policy version effective time precedes its publication")
|
|
|
|
// ErrPolicyVersionImmutable reports an attempt to change or remove a
|
|
// stored version. The database refuses every UPDATE, DELETE, and
|
|
// TRUNCATE of policy versions; publish a new version instead.
|
|
ErrPolicyVersionImmutable = errors.New("policy version is immutable")
|
|
)
|
|
|
|
// PolicyRepository stores immutable policy versions. It has no update or
|
|
// delete method. UnitOfWork binds it to a transaction; an adapter may also
|
|
// provide it outside a transaction, where every call is its own statement.
|
|
// Every method that takes a time rejects the zero time, and every method
|
|
// that takes a family rejects an unknown one with
|
|
// domain.ErrInvalidPolicyVersion before it reads storage. A returned version
|
|
// passed domain.RestorePolicyVersion, including its document hash check, so
|
|
// a row changed outside the application fails with
|
|
// domain.ErrPolicyDocumentIntegrity and a row of an unknown document layout
|
|
// fails with domain.ErrInvalidPolicy.
|
|
//
|
|
// A record built under a policy stores the ID of the version it used and
|
|
// reads that version with GetByID later. It never recomputes the version
|
|
// with GetEffective, whose answer for a past time changes when a version
|
|
// scheduled for that time is superseded by a later publication.
|
|
type PolicyRepository interface {
|
|
// Create stores a numbered draft and returns the stored version with the
|
|
// publication time from the database clock. A draft without an effective
|
|
// time becomes effective at publication. It returns an error wrapping
|
|
// ErrPolicyVersionConflict when the family already has that number or the
|
|
// ID exists, without aborting a surrounding transaction, and
|
|
// ErrPolicyVersionBackdated when the scheduled effective time precedes
|
|
// the publication time.
|
|
Create(ctx context.Context, draft domain.PolicyVersionDraft) (domain.PolicyVersion, error)
|
|
|
|
// GetByID returns ErrPolicyVersionNotFound when no version has the ID.
|
|
GetByID(ctx context.Context, id domain.PolicyVersionID) (domain.PolicyVersion, error)
|
|
|
|
// GetEffective returns the newest publication among the versions of the
|
|
// family whose effective time is not after at: the highest number, not
|
|
// the latest effective time. A version scheduled for later therefore
|
|
// never undoes a version published after it. It returns
|
|
// ErrPolicyVersionNotFound when no version is effective yet.
|
|
GetEffective(ctx context.Context, key domain.PolicyKey, at time.Time) (domain.PolicyVersion, error)
|
|
|
|
// GetLatest returns the version with the highest number in the family,
|
|
// whether or not it is effective yet, or ErrPolicyVersionNotFound.
|
|
GetLatest(ctx context.Context, key domain.PolicyKey) (domain.PolicyVersion, error)
|
|
|
|
// HasVersions reports whether the family has any version. It reads no
|
|
// document, so it works even when a stored document cannot be decoded.
|
|
HasVersions(ctx context.Context, key domain.PolicyKey) (bool, error)
|
|
}
|