package outbound import ( "context" "errors" "time" "github.com/google/uuid" "lincodex/backend/internal/domain" ) var ( ErrCandidateCardAccessNotFound = errors.New("candidate card access not found") ErrCandidateC2Unavailable = errors.New("candidate C2 publication is unavailable") ErrContactAccessClosed = errors.New("contact access is closed") ) const MaxCandidateCardAccessPageSize = 100 // PrivacyViewRepository adds the transaction-bound locks, owner preview, and // access audit operations needed by candidate-card reads. Employer reads must // lock candidate state in this order: account, current C2, publication. type PrivacyViewRepository interface { PrivacyCohortRepository ContactAccessRepository // LockCandidateAccountForView conflicts with the account lock used by the // hidden-organization limit trigger. It keeps a hide from committing while // a card read is authorized. LockCandidateAccountForView(ctx context.Context, candidateID uuid.UUID) error // LockCurrentC2ForView returns and share-locks the newest active C2 record. // Revocation either completes before this call and is observed as missing, // or waits until the read transaction commits. LockCurrentC2ForView(ctx context.Context, candidateID uuid.UUID) (uuid.UUID, error) // LockVisiblePublicationForView share-locks the publication and its C2 row // only while the candidate remains published, backed by c2RecordID, and // not hidden from organizationID. LockVisiblePublicationForView(ctx context.Context, organizationID, candidateID, c2RecordID uuid.UUID) (bool, error) // LockPublicationForPreview locks the candidate's currently effective own // publication without applying any employer hiding or cohort context. LockPublicationForPreview(ctx context.Context, candidateID uuid.UUID) (bool, error) // ReadCandidateViewSourceForPreview reads the current private projection // inputs after the caller locks the effective C2 and publication. ReadCandidateViewSourceForPreview(ctx context.Context, candidateID uuid.UUID) (domain.CandidateViewSource, error) // RecordCandidateCardAccess appends the access audit in the same transaction // as the quota reservation and visibility check. RecordCandidateCardAccess(ctx context.Context, access domain.CandidateCardAccess) error // ListCandidateCardAccess returns one owner-scoped page, newest first. ListCandidateCardAccess(ctx context.Context, query CandidateCardAccessListQuery) (CandidateCardAccessListPage, error) } // ContactAccessRepository is the transaction-bound final read boundary for // contact values. The implementation must lock the current invitation grant // and its matching C4 through the caller's transaction, then return only the // allowlisted fields. A workflow status is never evidence of a grant. type ContactAccessRepository interface { CurrentFields(ctx context.Context, organizationID, candidateID uuid.UUID) (domain.ContactFieldSet, error) // CloseForVisibility synchronously closes current grants for the candidate // and, when organizationID is nonzero, only that organization. It must run // in the same transaction as the visibility change. A later publication or // unhide never reopens a closed grant. CloseForVisibility(ctx context.Context, candidateID, organizationID uuid.UUID, at time.Time) error } // ClosedContactAccessRepository is the production default until the separate // invitation-grant persistence is implemented. type ClosedContactAccessRepository struct{} func (ClosedContactAccessRepository) CurrentFields(context.Context, uuid.UUID, uuid.UUID) (domain.ContactFieldSet, error) { return domain.ContactFieldSet{}, ErrContactAccessClosed } func (ClosedContactAccessRepository) CloseForVisibility(context.Context, uuid.UUID, uuid.UUID, time.Time) error { return nil } // CandidateCardAccessListQuery selects one candidate-owned history page. type CandidateCardAccessListQuery struct { CandidateID uuid.UUID After *CandidateCardAccessPosition Limit int32 } // CandidateCardAccessPosition is the descending access history key. type CandidateCardAccessPosition struct { OccurredAt time.Time ID uuid.UUID } // CandidateCardAccessListPage is one page of the candidate's access history. type CandidateCardAccessListPage struct { Items []domain.CandidateCardAccess OrganizationNames map[uuid.UUID]string HasMore bool } // CandidateCardAccessPositionOf creates the continuation key for an event. func CandidateCardAccessPositionOf(access domain.CandidateCardAccess) CandidateCardAccessPosition { return CandidateCardAccessPosition{OccurredAt: access.OccurredAt(), ID: access.ID()} }