98 lines
3.6 KiB
Go
98 lines
3.6 KiB
Go
package outbound
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
)
|
|
|
|
var (
|
|
// ErrNeedNotFound reports that the need does not exist in organizationID.
|
|
ErrNeedNotFound = errors.New("organization need not found")
|
|
|
|
// ErrNeedAlreadyExists reports that a need with the supplied ID is already
|
|
// stored. Creation idempotency is handled by the application usecase.
|
|
ErrNeedAlreadyExists = errors.New("organization need already exists")
|
|
|
|
// ErrNeedVersionConflict reports a stale expected need version. The
|
|
// surrounding transaction remains safe to roll back and the API maps the
|
|
// conflict to its current 412 contract.
|
|
ErrNeedVersionConflict = errors.New("organization need changed concurrently")
|
|
|
|
// ErrNeedCreationResultNotFound reports a missing immutable response for a
|
|
// live idempotency record.
|
|
ErrNeedCreationResultNotFound = errors.New("organization need creation result not found")
|
|
)
|
|
|
|
// MaxNeedPageSize is the largest organization-scoped page in the contract.
|
|
const MaxNeedPageSize = 100
|
|
|
|
// NeedRepositories exposes tenant-scoped need storage bound to one
|
|
// UnitOfWork transaction.
|
|
type NeedRepositories interface {
|
|
Needs() NeedRepository
|
|
NeedLists() NeedListRepository
|
|
NeedCreationResults() NeedCreationResultRepository
|
|
}
|
|
|
|
// NeedRepository stores the current organization need and its immutable
|
|
// version snapshots. Every operation scopes by both organization and need ID.
|
|
type NeedRepository interface {
|
|
// Create inserts version 1 and its initial snapshot in the surrounding
|
|
// transaction. The organization must already exist.
|
|
Create(ctx context.Context, need domain.Need) error
|
|
|
|
// Get returns the current snapshot in organizationID, or ErrNeedNotFound.
|
|
Get(ctx context.Context, organizationID, needID uuid.UUID) (domain.Need, error)
|
|
|
|
// GetVersion returns one immutable historical snapshot scoped to its
|
|
// organization and need. It returns ErrNeedNotFound for a missing version.
|
|
GetVersion(ctx context.Context, organizationID, needID uuid.UUID, version int64) (domain.Need, error)
|
|
|
|
// Save appends the single next version and atomically advances the current
|
|
// pointer when it still equals expectedVersion. The caller's transaction
|
|
// rolls back both writes if either write fails.
|
|
Save(ctx context.Context, organizationID, needID uuid.UUID, expectedVersion int64, next domain.Need) error
|
|
}
|
|
|
|
// NeedListRepository reads one bounded, organization-scoped page of needs.
|
|
type NeedListRepository interface {
|
|
List(ctx context.Context, query NeedListQuery) (NeedPage, error)
|
|
}
|
|
|
|
// NeedCreationResultRepository stores the immutable public result produced by
|
|
// a successful idempotent create command. The scope references the generic
|
|
// idempotency record, so it is removed when that record is purged.
|
|
type NeedCreationResultRepository interface {
|
|
SaveCreationResult(ctx context.Context, scope [IdempotencyDigestSize]byte, snapshot domain.NeedResponseSnapshot) error
|
|
GetCreationResult(ctx context.Context, scope [IdempotencyDigestSize]byte) (domain.NeedResponseSnapshot, error)
|
|
}
|
|
|
|
// NeedListQuery selects one page within a single organization.
|
|
type NeedListQuery struct {
|
|
OrganizationID uuid.UUID
|
|
After *NeedListPosition
|
|
Limit int32
|
|
}
|
|
|
|
// NeedListPosition is the stable newest-first ordering key for pagination.
|
|
type NeedListPosition struct {
|
|
CreatedAt time.Time
|
|
NeedID uuid.UUID
|
|
}
|
|
|
|
// NeedPage contains one organization-scoped page of needs.
|
|
type NeedPage struct {
|
|
Items []domain.Need
|
|
HasMore bool
|
|
}
|
|
|
|
// NeedCreationPosition returns the pagination key for one need.
|
|
func NeedCreationPosition(need domain.Need) NeedListPosition {
|
|
return NeedListPosition{CreatedAt: need.CreatedAt(), NeedID: need.ID()}
|
|
}
|