package scope import ( "errors" "github.com/google/uuid" "github.com/jackc/pgx/v5/pgtype" ) var ErrNilID = errors.New("scope requires a non-nil ID") // ScopeFilter is the resolved filter that repositories use to populate sqlc params. // Fields are pgtype.UUID to map directly to sqlc generated param structs. // A nil/invalid UUID means "not filtered by this dimension". type ScopeFilter struct { BusinessID pgtype.UUID TenantID pgtype.UUID } // Scope is the interface threaded through all layers. // Only concrete types in this package implement it. type Scope interface { Filter() ScopeFilter sealed() } // BusinessScope scopes data to a single business. type BusinessScope struct { businessID uuid.UUID } func NewBusinessScope(id uuid.UUID) (BusinessScope, error) { if id == uuid.Nil { return BusinessScope{}, ErrNilID } return BusinessScope{businessID: id}, nil } func (s BusinessScope) Filter() ScopeFilter { return ScopeFilter{ BusinessID: uuidToPgtype(s.businessID), } } func (BusinessScope) sealed() {} // TenantScope scopes data to an entire tenant. type TenantScope struct { tenantID uuid.UUID } func NewTenantScope(id uuid.UUID) (TenantScope, error) { if id == uuid.Nil { return TenantScope{}, ErrNilID } return TenantScope{tenantID: id}, nil } func (s TenantScope) Filter() ScopeFilter { return ScopeFilter{ TenantID: uuidToPgtype(s.tenantID), } } func (TenantScope) sealed() {} // SystemScope is for rare cross-tenant queries. // It does NOT implement Scope — it's used explicitly via separate repo methods. type SystemScope struct{} func NewSystemScope() SystemScope { return SystemScope{} } func uuidToPgtype(id uuid.UUID) pgtype.UUID { return pgtype.UUID{Bytes: id, Valid: true} }