566 lines
19 KiB
Go
566 lines
19 KiB
Go
// Package candidateprivacy implements candidate-owned publication and privacy
|
|
// settings. Every command is scoped to the authenticated candidate.
|
|
package candidateprivacy
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
var (
|
|
ErrForbidden = errors.New("candidate privacy operation is forbidden")
|
|
ErrEmailNotVerified = errors.New("candidate email is not verified")
|
|
ErrPrivacyServiceUnavailable = errors.New("candidate privacy service is unavailable")
|
|
ErrHiddenOrganizationNotFound = errors.New("hidden organization not found")
|
|
)
|
|
|
|
// Dependencies configure the candidate privacy settings service.
|
|
type Dependencies struct {
|
|
Transactor outbound.Transactor
|
|
Clock func() time.Time
|
|
}
|
|
|
|
// Service applies candidate-owned privacy settings in one transaction.
|
|
type Service struct {
|
|
transactor outbound.Transactor
|
|
clock func() time.Time
|
|
}
|
|
|
|
func NewService(deps Dependencies) (*Service, error) {
|
|
if deps.Transactor == nil || deps.Clock == nil {
|
|
return nil, fmt.Errorf("candidateprivacy.NewService - dependencies are missing: %w", ErrPrivacyServiceUnavailable)
|
|
}
|
|
return &Service{transactor: deps.Transactor, clock: deps.Clock}, nil
|
|
}
|
|
|
|
func (service *Service) GetMyPrivacySettings(ctx context.Context, userID uuid.UUID) (outbound.CandidatePrivacySettings, error) {
|
|
const location = "candidateprivacy.Service.GetMyPrivacySettings"
|
|
var result outbound.CandidatePrivacySettings
|
|
at, err := service.now()
|
|
if err != nil {
|
|
return result, err
|
|
}
|
|
err = service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := authorizeCandidate(ctx, unit, userID); err != nil {
|
|
return err
|
|
}
|
|
repository, err := settingsRepository(unit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result, err = repository.ReadSettings(ctx, userID)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - read candidate settings: %w", location, err)
|
|
}
|
|
result.AsOf = at
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return outbound.CandidatePrivacySettings{}, fmt.Errorf("%s - run transaction: %w", location, err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (service *Service) SetMyPublication(
|
|
ctx context.Context,
|
|
userID uuid.UUID,
|
|
expectedVersion int64,
|
|
state domain.PublicationState,
|
|
fields []domain.CardField,
|
|
) (domain.Publication, error) {
|
|
const location = "candidateprivacy.Service.SetMyPublication"
|
|
request, err := domain.NewPublicationRequest(state, fields)
|
|
if err != nil {
|
|
return domain.Publication{}, err
|
|
}
|
|
at, err := service.now()
|
|
if err != nil {
|
|
return domain.Publication{}, err
|
|
}
|
|
var result domain.Publication
|
|
err = service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := authorizeCandidate(ctx, unit, userID); err != nil {
|
|
return err
|
|
}
|
|
repository, err := settingsRepository(unit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
activeC2, err := currentC2ForPublication(ctx, repository, userID, location)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
settings, err := repository.ReadSettings(ctx, userID)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - read current publication: %w", location, err)
|
|
}
|
|
change, err := settings.Publication.Apply(request, expectedVersion, activeC2, at)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result = change.Publication
|
|
if !change.Changed {
|
|
return nil
|
|
}
|
|
return persistPublicationChange(ctx, unit, repository, userID, expectedVersion, change, at, location)
|
|
})
|
|
if err != nil {
|
|
return domain.Publication{}, fmt.Errorf("%s - run transaction: %w", location, err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func currentC2ForPublication(
|
|
ctx context.Context,
|
|
repository outbound.PrivacySettingsRepository,
|
|
userID uuid.UUID,
|
|
location string,
|
|
) (uuid.UUID, error) {
|
|
activeC2, err := repository.LockCurrentC2ForPublication(ctx, userID)
|
|
if errors.Is(err, outbound.ErrConsentNotFound) {
|
|
return uuid.Nil, nil
|
|
}
|
|
if err != nil {
|
|
return uuid.Nil, fmt.Errorf("%s - lock current C2: %w", location, err)
|
|
}
|
|
return activeC2, nil
|
|
}
|
|
|
|
func persistPublicationChange(
|
|
ctx context.Context,
|
|
unit outbound.UnitOfWork,
|
|
repository outbound.PrivacySettingsRepository,
|
|
userID uuid.UUID,
|
|
expectedVersion int64,
|
|
change domain.PublicationChange,
|
|
at time.Time,
|
|
location string,
|
|
) error {
|
|
if err := repository.SavePublication(ctx, change.Publication, expectedVersion); err != nil {
|
|
return fmt.Errorf("%s - save publication: %w", location, err)
|
|
}
|
|
if change.Event == domain.PrivacyEventPublicationClosed {
|
|
if err := closeContactGrants(ctx, unit.PrivacyView(), userID, uuid.Nil, at, location, "publication close"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
event, err := domain.NewPublicationEvent(change, at)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - build publication history: %w", location, err)
|
|
}
|
|
if err := repository.RecordPrivacyEvent(ctx, event); err != nil {
|
|
return fmt.Errorf("%s - record publication history: %w", location, err)
|
|
}
|
|
if change.Event != domain.PrivacyEventPublicationClosed {
|
|
return nil
|
|
}
|
|
if err := appendVisibilityClosed(ctx, unit, userID, uuid.Nil, "publication_closed", at, event.ID()); err != nil {
|
|
return fmt.Errorf("%s - append visibility closure intent: %w", location, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func closeContactGrants(
|
|
ctx context.Context,
|
|
repository outbound.ContactAccessRepository,
|
|
candidateID, organizationID uuid.UUID,
|
|
at time.Time,
|
|
location, reason string,
|
|
) error {
|
|
if repository == nil {
|
|
return ErrPrivacyServiceUnavailable
|
|
}
|
|
if err := repository.CloseForVisibility(ctx, candidateID, organizationID, at); err != nil {
|
|
return fmt.Errorf("%s - close contact grants after %s: %w", location, reason, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (service *Service) SetMySearchStatus(
|
|
ctx context.Context,
|
|
userID uuid.UUID,
|
|
expectedVersion int64,
|
|
value domain.SearchStatusValue,
|
|
) (domain.SearchStatus, time.Time, error) {
|
|
const location = "candidateprivacy.Service.SetMySearchStatus"
|
|
at, err := service.now()
|
|
if err != nil {
|
|
return domain.SearchStatus{}, time.Time{}, err
|
|
}
|
|
var result domain.SearchStatus
|
|
err = service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := authorizeCandidate(ctx, unit, userID); err != nil {
|
|
return err
|
|
}
|
|
repository, err := settingsRepository(unit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
settings, err := repository.ReadSettings(ctx, userID)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - read current search status: %w", location, err)
|
|
}
|
|
var confirmation domain.SearchStatusConfirmation
|
|
if value == domain.SearchStatusLooking && expectedVersion == settings.SearchStatus.Version() {
|
|
policyVersion, err := unit.Policies().GetEffective(ctx, domain.ProductPolicyKey, at)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - read effective product policy: %w", location, err)
|
|
}
|
|
confirmation, err = domain.NewSearchStatusConfirmation(policyVersion.ID(), policyVersion.Policy().SearchStatus)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - build search status confirmation: %w", location, err)
|
|
}
|
|
}
|
|
change, err := settings.SearchStatus.Apply(value, expectedVersion, confirmation, at)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result = change.Status
|
|
if !change.Changed {
|
|
return nil
|
|
}
|
|
if err := repository.SaveSearchStatus(ctx, change.Status, expectedVersion, at); err != nil {
|
|
return fmt.Errorf("%s - save search status: %w", location, err)
|
|
}
|
|
event, err := domain.NewSearchStatusEvent(change, at)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - build search status history: %w", location, err)
|
|
}
|
|
if err := repository.RecordPrivacyEvent(ctx, event); err != nil {
|
|
return fmt.Errorf("%s - record search status history: %w", location, err)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return domain.SearchStatus{}, time.Time{}, fmt.Errorf("%s - run transaction: %w", location, err)
|
|
}
|
|
return result, at, nil
|
|
}
|
|
|
|
func (service *Service) ListMyPrivacyHistory(
|
|
ctx context.Context,
|
|
query outbound.CandidatePrivacyEventListQuery,
|
|
) (outbound.CandidatePrivacyEventPage, error) {
|
|
const location = "candidateprivacy.Service.ListMyPrivacyHistory"
|
|
if err := validatePage(query.UserID, query.Limit, query.After); err != nil {
|
|
return outbound.CandidatePrivacyEventPage{}, err
|
|
}
|
|
var result outbound.CandidatePrivacyEventPage
|
|
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := authorizeCandidate(ctx, unit, query.UserID); err != nil {
|
|
return err
|
|
}
|
|
repository, err := settingsRepository(unit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result, err = repository.ListPrivacyEvents(ctx, query)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - list privacy history: %w", location, err)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return outbound.CandidatePrivacyEventPage{}, fmt.Errorf("%s - run transaction: %w", location, err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (service *Service) ListMyHiddenOrganizations(
|
|
ctx context.Context,
|
|
query outbound.HiddenOrganizationListQuery,
|
|
) (outbound.HiddenOrganizationPage, error) {
|
|
const location = "candidateprivacy.Service.ListMyHiddenOrganizations"
|
|
if err := validatePage(query.UserID, query.Limit, query.After); err != nil {
|
|
return outbound.HiddenOrganizationPage{}, err
|
|
}
|
|
var result outbound.HiddenOrganizationPage
|
|
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := authorizeCandidate(ctx, unit, query.UserID); err != nil {
|
|
return err
|
|
}
|
|
repository, err := settingsRepository(unit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result, err = repository.ListHiddenOrganizations(ctx, query)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - list hidden organizations: %w", location, err)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return outbound.HiddenOrganizationPage{}, fmt.Errorf("%s - run transaction: %w", location, err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (service *Service) HideOrganization(
|
|
ctx context.Context,
|
|
userID, organizationID uuid.UUID,
|
|
) (outbound.HiddenOrganizationRecord, error) {
|
|
const location = "candidateprivacy.Service.HideOrganization"
|
|
at, err := service.now()
|
|
if err != nil {
|
|
return outbound.HiddenOrganizationRecord{}, err
|
|
}
|
|
var result outbound.HiddenOrganizationRecord
|
|
err = service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := authorizeCandidate(ctx, unit, userID); err != nil {
|
|
return err
|
|
}
|
|
repository, err := settingsRepository(unit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
result, err = service.hideOrganizationInTransaction(ctx, unit, repository, userID, organizationID, at, location)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return outbound.HiddenOrganizationRecord{}, fmt.Errorf("%s - run transaction: %w", location, err)
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (service *Service) hideOrganizationInTransaction(
|
|
ctx context.Context,
|
|
unit outbound.UnitOfWork,
|
|
repository outbound.PrivacySettingsRepository,
|
|
userID, organizationID uuid.UUID,
|
|
at time.Time,
|
|
location string,
|
|
) (outbound.HiddenOrganizationRecord, error) {
|
|
visible, err := repository.CandidateCanSeeOrganization(ctx, userID, organizationID)
|
|
if err != nil {
|
|
return outbound.HiddenOrganizationRecord{}, fmt.Errorf("%s - verify organization visibility: %w", location, err)
|
|
}
|
|
result := outbound.HiddenOrganizationRecord{OrganizationID: visible.ID, OrganizationName: visible.Name}
|
|
current, err := repository.GetHiddenOrganization(ctx, userID, organizationID)
|
|
if err == nil {
|
|
result.HiddenAt = current.HiddenAt
|
|
return result, closeContactGrants(ctx, unit.PrivacyView(), userID, organizationID, at, location, "organization hiding")
|
|
}
|
|
if !errors.Is(err, outbound.ErrCandidatePrivacySettingsNotFound) {
|
|
return outbound.HiddenOrganizationRecord{}, fmt.Errorf("%s - read existing hiding: %w", location, err)
|
|
}
|
|
hiddenAt, created, err := createOrganizationHiding(ctx, repository, userID, organizationID, at, location)
|
|
if err != nil {
|
|
return outbound.HiddenOrganizationRecord{}, err
|
|
}
|
|
result.HiddenAt = hiddenAt
|
|
if !created {
|
|
return result, nil
|
|
}
|
|
if err := persistOrganizationHiding(ctx, unit, repository, userID, organizationID, at, location); err != nil {
|
|
return outbound.HiddenOrganizationRecord{}, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func createOrganizationHiding(
|
|
ctx context.Context,
|
|
repository outbound.PrivacySettingsRepository,
|
|
userID, organizationID uuid.UUID,
|
|
at time.Time,
|
|
location string,
|
|
) (time.Time, bool, error) {
|
|
count, err := repository.CountHiddenOrganizations(ctx, userID)
|
|
if err != nil {
|
|
return time.Time{}, false, fmt.Errorf("%s - count hidden organizations: %w", location, err)
|
|
}
|
|
if count >= domain.MaxHiddenOrganizations {
|
|
return time.Time{}, false, outbound.ErrCandidateHiddenOrganizationLimit
|
|
}
|
|
hidden, err := domain.NewHiddenOrganization(userID, organizationID, at)
|
|
if err != nil {
|
|
return time.Time{}, false, err
|
|
}
|
|
created, err := repository.HideOrganization(ctx, hidden)
|
|
if err != nil {
|
|
return time.Time{}, false, fmt.Errorf("%s - store hiding: %w", location, err)
|
|
}
|
|
if created {
|
|
return at, true, nil
|
|
}
|
|
current, err := repository.GetHiddenOrganization(ctx, userID, organizationID)
|
|
if err != nil {
|
|
return time.Time{}, false, fmt.Errorf("%s - read concurrent hiding: %w", location, err)
|
|
}
|
|
return current.HiddenAt, false, nil
|
|
}
|
|
|
|
func persistOrganizationHiding(
|
|
ctx context.Context,
|
|
unit outbound.UnitOfWork,
|
|
repository outbound.PrivacySettingsRepository,
|
|
userID, organizationID uuid.UUID,
|
|
at time.Time,
|
|
location string,
|
|
) error {
|
|
if err := closeContactGrants(ctx, unit.PrivacyView(), userID, organizationID, at, location, "organization hiding"); err != nil {
|
|
return err
|
|
}
|
|
event, err := domain.NewOrganizationEvent(domain.PrivacyEventOrganizationHidden, userID, organizationID, at)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - build hiding history: %w", location, err)
|
|
}
|
|
if err := repository.RecordPrivacyEvent(ctx, event); err != nil {
|
|
return fmt.Errorf("%s - record hiding history: %w", location, err)
|
|
}
|
|
if err := appendVisibilityClosed(ctx, unit, userID, organizationID, "organization_hidden", at, event.ID()); err != nil {
|
|
return fmt.Errorf("%s - append visibility closure intent: %w", location, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (service *Service) UnhideOrganization(ctx context.Context, userID, organizationID uuid.UUID) error {
|
|
const location = "candidateprivacy.Service.UnhideOrganization"
|
|
at, err := service.now()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := authorizeCandidate(ctx, unit, userID); err != nil {
|
|
return err
|
|
}
|
|
repository, err := settingsRepository(unit)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
deleted, err := repository.UnhideOrganization(ctx, userID, organizationID)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - delete hiding: %w", location, err)
|
|
}
|
|
if !deleted {
|
|
return nil
|
|
}
|
|
event, err := domain.NewOrganizationEvent(domain.PrivacyEventOrganizationUnhidden, userID, organizationID, at)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - build unhiding history: %w", location, err)
|
|
}
|
|
if err := repository.RecordPrivacyEvent(ctx, event); err != nil {
|
|
return fmt.Errorf("%s - record unhiding history: %w", location, err)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("%s - run transaction: %w", location, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (service *Service) now() (time.Time, error) {
|
|
at := service.clock().UTC().Truncate(time.Microsecond)
|
|
if at.IsZero() {
|
|
return time.Time{}, fmt.Errorf("candidateprivacy.Service - clock returned zero: %w", ErrPrivacyServiceUnavailable)
|
|
}
|
|
return at, nil
|
|
}
|
|
|
|
func validatePage(userID uuid.UUID, limit int32, after any) error {
|
|
if userID == uuid.Nil || limit < 1 || limit > outbound.MaxPrivacyPageSize {
|
|
return domain.ErrInvalidVisibility
|
|
}
|
|
switch position := after.(type) {
|
|
case nil:
|
|
case *outbound.CandidatePrivacyEventPosition:
|
|
if position != nil && (position.ID == uuid.Nil || position.OccurredAt.IsZero()) {
|
|
return domain.ErrInvalidVisibility
|
|
}
|
|
case *outbound.HiddenOrganizationPosition:
|
|
if position != nil && (position.OrganizationID == uuid.Nil || position.HiddenAt.IsZero()) {
|
|
return domain.ErrInvalidVisibility
|
|
}
|
|
default:
|
|
return domain.ErrInvalidVisibility
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func authorizeCandidate(ctx context.Context, unit outbound.UnitOfWork, userID uuid.UUID) error {
|
|
if userID == uuid.Nil {
|
|
return ErrForbidden
|
|
}
|
|
user, err := unit.Users().GetByID(ctx, userID)
|
|
if errors.Is(err, outbound.ErrUserNotFound) {
|
|
return ErrForbidden
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("candidateprivacy.authorizeCandidate - read account: %w", err)
|
|
}
|
|
if user.Scenario != domain.ScenarioCandidate {
|
|
return ErrForbidden
|
|
}
|
|
if !user.EmailVerified() {
|
|
return ErrEmailNotVerified
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func settingsRepository(unit outbound.UnitOfWork) (outbound.PrivacySettingsRepository, error) {
|
|
repository := unit.PrivacySettings()
|
|
if repository == nil {
|
|
return nil, ErrPrivacyServiceUnavailable
|
|
}
|
|
return repository, nil
|
|
}
|
|
|
|
type visibilityClosedPayload struct {
|
|
CandidateID uuid.UUID `json:"candidate_id"`
|
|
OrganizationID *uuid.UUID `json:"organization_id"`
|
|
Reason string `json:"reason"`
|
|
OccurredAt time.Time `json:"occurred_at"`
|
|
PrivacyEventID uuid.UUID `json:"privacy_event_id"`
|
|
}
|
|
|
|
func appendVisibilityClosed(
|
|
ctx context.Context,
|
|
unit outbound.UnitOfWork,
|
|
candidateID, organizationID uuid.UUID,
|
|
reason string,
|
|
at time.Time,
|
|
privacyEventID uuid.UUID,
|
|
) error {
|
|
const location = "candidateprivacy.appendVisibilityClosed"
|
|
if candidateID == uuid.Nil || privacyEventID == uuid.Nil || at.IsZero() ||
|
|
(reason != "publication_closed" && reason != "organization_hidden") ||
|
|
(reason == "organization_hidden" && organizationID == uuid.Nil) ||
|
|
(reason == "publication_closed" && organizationID != uuid.Nil) {
|
|
return fmt.Errorf("%s - closure facts are invalid: %w", location, domain.ErrInvalidDomainEvent)
|
|
}
|
|
var organization *uuid.UUID
|
|
if organizationID != uuid.Nil {
|
|
organization = &organizationID
|
|
}
|
|
payload, err := json.Marshal(visibilityClosedPayload{
|
|
CandidateID: candidateID, OrganizationID: organization, Reason: reason,
|
|
OccurredAt: at.UTC().Truncate(time.Microsecond), PrivacyEventID: privacyEventID,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("%s - encode payload", location)
|
|
}
|
|
event, err := domain.NewDomainEvent(domain.DomainEventInput{
|
|
EventType: "privacy.visibility_closed", SchemaVersion: 1,
|
|
AggregateType: "privacy_event", AggregateID: privacyEventID, AggregateVersion: 1, Payload: payload,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("%s - build event: %w", location, err)
|
|
}
|
|
writer := unit.Outbox()
|
|
if writer == nil {
|
|
return fmt.Errorf("%s - transactional outbox is unavailable: %w", location, ErrPrivacyServiceUnavailable)
|
|
}
|
|
if err := writer.Append(ctx, event); err != nil {
|
|
return fmt.Errorf("%s - append event: %w", location, err)
|
|
}
|
|
return nil
|
|
}
|