Files
lincodex/backend/internal/domain/membership.go
T

230 lines
8.7 KiB
Go

package domain
import (
"errors"
"fmt"
"slices"
"time"
"github.com/google/uuid"
)
// ErrInvalidMembership reports a membership, role, or state that breaks a
// rule, including a stored membership that does not match one.
var ErrInvalidMembership = errors.New("invalid membership")
// OrganizationRole is the role of a member in one organization (PRD 2.2 and
// the FR07 matrix). The role belongs to the membership, never to the
// organization profile. Values match the OrganizationRole schema of the
// public contract.
type OrganizationRole string
const (
// OrganizationRoleOwner manages the organization and its members.
OrganizationRoleOwner OrganizationRole = "owner"
// OrganizationRoleRecruiter leads the selection of candidates.
OrganizationRoleRecruiter OrganizationRole = "recruiter"
// OrganizationRoleHiringManager acts on the assigned needs.
OrganizationRoleHiringManager OrganizationRole = "hiring_manager"
// OrganizationRoleTechnicalExpert refines the requirements of the
// assigned needs.
OrganizationRoleTechnicalExpert OrganizationRole = "technical_expert"
)
// OrganizationRoles lists every role, the owner first.
func OrganizationRoles() []OrganizationRole {
return []OrganizationRole{
OrganizationRoleOwner,
OrganizationRoleRecruiter,
OrganizationRoleHiringManager,
OrganizationRoleTechnicalExpert,
}
}
// ParseOrganizationRole accepts a value of the OrganizationRole contract
// enum.
func ParseOrganizationRole(value string) (OrganizationRole, error) {
role := OrganizationRole(value)
if !slices.Contains(OrganizationRoles(), role) {
return "", fmt.Errorf("domain.ParseOrganizationRole - unknown role: %w", ErrInvalidMembership)
}
return role, nil
}
// CanManageOrganization reports whether the role changes the organization
// profile and manages its members. The FR07 matrix gives this to the owner
// only. It is the single rule of managing; the rest of the matrix (needs,
// invitations, contacts, bids) is applied by the operations it governs, and
// no role grants anything through this method alone.
func (role OrganizationRole) CanManageOrganization() bool {
return role == OrganizationRoleOwner
}
// ManagerRoles lists the roles for which CanManageOrganization is true.
func ManagerRoles() []OrganizationRole {
managers := make([]OrganizationRole, 0, 1)
for _, role := range OrganizationRoles() {
if role.CanManageOrganization() {
managers = append(managers, role)
}
}
return managers
}
// MembershipState is the state of a membership. Only an active membership
// gives access. Values match the MembershipState schema of the public
// contract.
type MembershipState string
const (
// MembershipStateActive is a member who works in the organization.
MembershipStateActive MembershipState = "active"
// MembershipStateSuspended is a member whose access is stopped and may be
// restored.
MembershipStateSuspended MembershipState = "suspended"
// MembershipStateRemoved is a membership that the owner or the platform
// ended.
MembershipStateRemoved MembershipState = "removed"
// MembershipStateLeft is a membership that the member ended.
MembershipStateLeft MembershipState = "left"
)
// ParseMembershipState accepts a value of the MembershipState contract enum.
func ParseMembershipState(value string) (MembershipState, error) {
state := MembershipState(value)
switch state {
case MembershipStateActive, MembershipStateSuspended, MembershipStateRemoved, MembershipStateLeft:
return state, nil
default:
return "", fmt.Errorf("domain.ParseMembershipState - unknown state: %w", ErrInvalidMembership)
}
}
// GrantsAccess reports whether a membership in the state gives access to its
// organization. Every state but active answers as if the account were no
// member.
func (state MembershipState) GrantsAccess() bool { return state == MembershipStateActive }
// MembershipInput describes a new membership.
type MembershipInput struct {
OrganizationID uuid.UUID
UserID uuid.UUID
Role OrganizationRole
// At is the application clock at creation.
At time.Time
}
// Membership is the role of one account in one organization. An account has
// at most one membership per organization, so storage keeps the pair unique.
// Storage reconstructs it from trusted rows with RestoreMembership; new ones
// come from NewMembership.
type Membership struct {
id uuid.UUID
organizationID uuid.UUID
userID uuid.UUID
role OrganizationRole
state MembershipState
createdAt time.Time
updatedAt time.Time
}
// NewMembership creates an active membership with a time-ordered UUIDv7
// identifier. It checks the shape only: whether the account may hold the
// role, for example whether it is an employer account, is a rule of the
// operation that adds the member.
func NewMembership(input MembershipInput) (Membership, error) {
const location = "domain.NewMembership"
if _, err := ParseOrganizationRole(string(input.Role)); err != nil {
return Membership{}, fmt.Errorf("%s - validate role: %w", location, err)
}
switch {
case input.OrganizationID == uuid.Nil:
return Membership{}, fmt.Errorf("%s - organization ID must not be nil: %w", location, ErrInvalidMembership)
case input.UserID == uuid.Nil:
return Membership{}, fmt.Errorf("%s - user ID must not be nil: %w", location, ErrInvalidMembership)
case input.At.IsZero():
return Membership{}, fmt.Errorf("%s - creation time must be set: %w", location, ErrInvalidMembership)
}
id, err := uuid.NewV7()
if err != nil {
return Membership{}, fmt.Errorf("%s - generate membership ID: %w", location, err)
}
return Membership{
id: id,
organizationID: input.OrganizationID,
userID: input.UserID,
role: input.Role,
state: MembershipStateActive,
createdAt: input.At,
updatedAt: input.At,
}, nil
}
// NewOwnerMembership creates the active owner membership of the account that
// creates an organization.
func NewOwnerMembership(organizationID, userID uuid.UUID, at time.Time) (Membership, error) {
return NewMembership(MembershipInput{OrganizationID: organizationID, UserID: userID, Role: OrganizationRoleOwner, At: at})
}
// StoredMembership carries the columns of a stored membership.
type StoredMembership struct {
ID uuid.UUID
OrganizationID uuid.UUID
UserID uuid.UUID
Role string
State string
CreatedAt time.Time
UpdatedAt time.Time
}
// RestoreMembership validates a stored membership, so a row changed outside
// the application fails with ErrInvalidMembership.
func RestoreMembership(stored StoredMembership) (Membership, error) {
const location = "domain.RestoreMembership"
role, err := ParseOrganizationRole(stored.Role)
if err != nil {
return Membership{}, fmt.Errorf("%s - validate role: %w", location, err)
}
state, err := ParseMembershipState(stored.State)
if err != nil {
return Membership{}, fmt.Errorf("%s - validate state: %w", location, err)
}
switch {
case stored.ID == uuid.Nil || stored.OrganizationID == uuid.Nil || stored.UserID == uuid.Nil:
return Membership{}, fmt.Errorf("%s - identifiers must not be nil: %w", location, ErrInvalidMembership)
case stored.CreatedAt.IsZero() || stored.UpdatedAt.Before(stored.CreatedAt):
return Membership{}, fmt.Errorf("%s - times must be set and ordered: %w", location, ErrInvalidMembership)
}
return Membership{
id: stored.ID,
organizationID: stored.OrganizationID,
userID: stored.UserID,
role: role,
state: state,
createdAt: stored.CreatedAt,
updatedAt: stored.UpdatedAt,
}, nil
}
func (membership Membership) ID() uuid.UUID { return membership.id }
func (membership Membership) OrganizationID() uuid.UUID { return membership.organizationID }
func (membership Membership) UserID() uuid.UUID { return membership.userID }
func (membership Membership) Role() OrganizationRole { return membership.role }
func (membership Membership) State() MembershipState { return membership.state }
func (membership Membership) CreatedAt() time.Time { return membership.createdAt }
func (membership Membership) UpdatedAt() time.Time { return membership.updatedAt }
// GrantsAccess reports whether the membership gives access to its
// organization: it is active. It says nothing about what the role may do.
func (membership Membership) GrantsAccess() bool { return membership.state.GrantsAccess() }
// CanManageOrganization reports whether the membership is active and its
// role manages the organization.
func (membership Membership) CanManageOrganization() bool {
return membership.GrantsAccess() && membership.role.CanManageOrganization()
}
// IsZero reports a Membership not built by NewMembership or
// RestoreMembership.
func (membership Membership) IsZero() bool { return membership.id == uuid.Nil }