feat: add assessment, privacy and support flows
This commit is contained in:
1 parent
ee3738ee73
commit
e4f24fa202
339 files changed
+56454
-1126
No files matched your search
@@ -0,0 +1,565 @@
|
||||
// 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
|
||||
}
|
||||
@@ -0,0 +1,273 @@
|
||||
package candidateprivacy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
func TestPublicationCloseWritesHistoryAndTransactionalClosureIntentOnce(t *testing.T) {
|
||||
const now = "2026-10-07T12:00:00.123456Z"
|
||||
at := mustTime(t, now)
|
||||
candidateID, consentID := uuid.New(), uuid.New()
|
||||
initial := mustPublished(t, candidateID, consentID, at.Add(-time.Hour))
|
||||
transactor := &privacyTestTransactor{
|
||||
user: domain.User{ID: candidateID, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: timePointer(at)},
|
||||
publication: initial,
|
||||
}
|
||||
service := mustPrivacyService(t, transactor, at)
|
||||
|
||||
closed, err := service.SetMyPublication(context.Background(), candidateID, initial.Version(), domain.PublicationClosed, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("SetMyPublication(close) error = %v", err)
|
||||
}
|
||||
if closed.StoredState() != domain.PublicationClosed || closed.Version() != initial.Version()+1 {
|
||||
t.Fatalf("close result = %s v%d, want closed v%d", closed.StoredState(), closed.Version(), initial.Version()+1)
|
||||
}
|
||||
if len(transactor.history) != 1 || len(transactor.outboxEvents) != 1 || len(transactor.closureCalls) != 1 {
|
||||
t.Fatalf("committed close effects = history %d, outbox %d, grants %d; want one each", len(transactor.history), len(transactor.outboxEvents), len(transactor.closureCalls))
|
||||
}
|
||||
if got := transactor.sequence; fmt.Sprint(got) != "[close history outbox]" {
|
||||
t.Fatalf("closure order = %v, want close -> history -> outbox", got)
|
||||
}
|
||||
assertVisibilityClosedEvent(t, transactor.outboxEvents[0], candidateID, uuid.Nil, transactor.history[0].ID(), at)
|
||||
|
||||
_, err = service.SetMyPublication(context.Background(), candidateID, closed.Version(), domain.PublicationClosed, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("repeated SetMyPublication(close) error = %v", err)
|
||||
}
|
||||
if len(transactor.history) != 1 || len(transactor.outboxEvents) != 1 || len(transactor.closureCalls) != 1 {
|
||||
t.Fatalf("repeated close effects = history %d, outbox %d, grants %d; want no duplicate intent", len(transactor.history), len(transactor.outboxEvents), len(transactor.closureCalls))
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatePageAcceptsTypedNilInitialCursors(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
var privacyCursor *outbound.CandidatePrivacyEventPosition
|
||||
var hiddenCursor *outbound.HiddenOrganizationPosition
|
||||
userID := uuid.New()
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
after any
|
||||
}{
|
||||
{name: "privacy history", after: privacyCursor},
|
||||
{name: "hidden organizations", after: hiddenCursor},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
t.Parallel()
|
||||
if err := validatePage(userID, 20, test.after); err != nil {
|
||||
t.Fatalf("validatePage(initial cursor) error = %v, want nil", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicationCloseRollsBackWhenClosureIntentCannotBeStored(t *testing.T) {
|
||||
at := mustTime(t, "2026-10-07T12:00:00.123456Z")
|
||||
candidateID, consentID := uuid.New(), uuid.New()
|
||||
initial := mustPublished(t, candidateID, consentID, at.Add(-time.Hour))
|
||||
transactor := &privacyTestTransactor{
|
||||
user: domain.User{ID: candidateID, Scenario: domain.ScenarioCandidate, EmailVerifiedAt: timePointer(at)},
|
||||
publication: initial, outboxError: errors.New("outbox unavailable"),
|
||||
}
|
||||
service := mustPrivacyService(t, transactor, at)
|
||||
|
||||
if _, err := service.SetMyPublication(context.Background(), candidateID, initial.Version(), domain.PublicationClosed, nil); err == nil {
|
||||
t.Fatal("SetMyPublication(close) error = nil, want closure intent failure")
|
||||
}
|
||||
if transactor.publication.Version() != initial.Version() || transactor.publication.StoredState() != domain.PublicationPublished {
|
||||
t.Fatalf("publication after failed transaction = %s v%d, want unchanged published v%d", transactor.publication.StoredState(), transactor.publication.Version(), initial.Version())
|
||||
}
|
||||
if len(transactor.history) != 0 || len(transactor.outboxEvents) != 0 || len(transactor.closureCalls) != 0 {
|
||||
t.Fatalf("effects after failed transaction = history %d, outbox %d, grants %d; want rollback", len(transactor.history), len(transactor.outboxEvents), len(transactor.closureCalls))
|
||||
}
|
||||
}
|
||||
|
||||
func mustPrivacyService(t *testing.T, transactor outbound.Transactor, at time.Time) *Service {
|
||||
t.Helper()
|
||||
service, err := NewService(Dependencies{Transactor: transactor, Clock: func() time.Time { return at }})
|
||||
if err != nil {
|
||||
t.Fatalf("NewService() error = %v", err)
|
||||
}
|
||||
return service
|
||||
}
|
||||
|
||||
func mustPublished(t *testing.T, candidateID, consentID uuid.UUID, at time.Time) domain.Publication {
|
||||
t.Helper()
|
||||
publication, err := domain.NewPublication(candidateID)
|
||||
if err != nil {
|
||||
t.Fatalf("NewPublication() error = %v", err)
|
||||
}
|
||||
request, err := domain.NewPublicationRequest(domain.PublicationPublished, []domain.CardField{domain.CardFieldStack})
|
||||
if err != nil {
|
||||
t.Fatalf("NewPublicationRequest() error = %v", err)
|
||||
}
|
||||
change, err := publication.Apply(request, 0, consentID, at)
|
||||
if err != nil || !change.Changed {
|
||||
t.Fatalf("Apply(publish) = %+v, %v, want change", change, err)
|
||||
}
|
||||
return change.Publication
|
||||
}
|
||||
|
||||
func assertVisibilityClosedEvent(t *testing.T, event domain.DomainEvent, candidateID, organizationID, privacyEventID uuid.UUID, at time.Time) {
|
||||
t.Helper()
|
||||
if event.EventType() != "privacy.visibility_closed" || event.SchemaVersion() != 1 ||
|
||||
event.AggregateType() != "privacy_event" || event.AggregateID() != privacyEventID || event.AggregateVersion() != 1 {
|
||||
t.Fatalf("closure event identity = %s v%d %s/%s v%d", event.EventType(), event.SchemaVersion(), event.AggregateType(), event.AggregateID(), event.AggregateVersion())
|
||||
}
|
||||
var payload 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"`
|
||||
}
|
||||
if err := json.Unmarshal(event.Payload(), &payload); err != nil {
|
||||
t.Fatalf("decode closure payload: %v", err)
|
||||
}
|
||||
if payload.CandidateID != candidateID || payload.Reason != "publication_closed" ||
|
||||
payload.PrivacyEventID != privacyEventID || !payload.OccurredAt.Equal(at) || payload.OrganizationID != nil || organizationID != uuid.Nil {
|
||||
t.Fatalf("closure payload = %+v", payload)
|
||||
}
|
||||
}
|
||||
|
||||
func mustTime(t *testing.T, value string) time.Time {
|
||||
t.Helper()
|
||||
at, err := time.Parse(time.RFC3339Nano, value)
|
||||
if err != nil {
|
||||
t.Fatalf("parse test time: %v", err)
|
||||
}
|
||||
return at
|
||||
}
|
||||
|
||||
func timePointer(value time.Time) *time.Time { return &value }
|
||||
|
||||
type privacyTestTransactor struct {
|
||||
user domain.User
|
||||
publication domain.Publication
|
||||
history []domain.PrivacyEvent
|
||||
outboxEvents []domain.DomainEvent
|
||||
closureCalls []privacyClosureCall
|
||||
sequence []string
|
||||
outboxError error
|
||||
}
|
||||
|
||||
type privacyClosureCall struct {
|
||||
candidateID uuid.UUID
|
||||
organizationID uuid.UUID
|
||||
at time.Time
|
||||
}
|
||||
|
||||
func (transactor *privacyTestTransactor) WithinTransaction(ctx context.Context, fn func(context.Context, outbound.UnitOfWork) error) error {
|
||||
draft := &privacyTestTransactor{
|
||||
user: transactor.user, publication: transactor.publication,
|
||||
history: append([]domain.PrivacyEvent(nil), transactor.history...),
|
||||
outboxEvents: append([]domain.DomainEvent(nil), transactor.outboxEvents...),
|
||||
closureCalls: append([]privacyClosureCall(nil), transactor.closureCalls...),
|
||||
sequence: append([]string(nil), transactor.sequence...), outboxError: transactor.outboxError,
|
||||
}
|
||||
unit := privacyTestUnit{
|
||||
users: privacyTestUsers{user: draft.user},
|
||||
settings: privacyTestSettings{transactor: draft},
|
||||
view: privacyTestView{transactor: draft},
|
||||
outbox: privacyTestOutbox{transactor: draft},
|
||||
}
|
||||
if err := fn(ctx, unit); err != nil {
|
||||
return err
|
||||
}
|
||||
transactor.publication = draft.publication
|
||||
transactor.history = draft.history
|
||||
transactor.outboxEvents = draft.outboxEvents
|
||||
transactor.closureCalls = draft.closureCalls
|
||||
transactor.sequence = draft.sequence
|
||||
return nil
|
||||
}
|
||||
|
||||
type privacyTestUnit struct {
|
||||
outbound.UnitOfWork
|
||||
|
||||
users outbound.UserRepository
|
||||
settings outbound.PrivacySettingsRepository
|
||||
view outbound.PrivacyViewRepository
|
||||
outbox outbound.OutboxWriter
|
||||
}
|
||||
|
||||
func (unit privacyTestUnit) Users() outbound.UserRepository { return unit.users }
|
||||
func (unit privacyTestUnit) PrivacySettings() outbound.PrivacySettingsRepository {
|
||||
return unit.settings
|
||||
}
|
||||
func (unit privacyTestUnit) PrivacyView() outbound.PrivacyViewRepository { return unit.view }
|
||||
func (unit privacyTestUnit) Outbox() outbound.OutboxWriter { return unit.outbox }
|
||||
|
||||
type privacyTestUsers struct {
|
||||
outbound.UserRepository
|
||||
|
||||
user domain.User
|
||||
}
|
||||
|
||||
func (users privacyTestUsers) GetByID(context.Context, uuid.UUID) (domain.User, error) {
|
||||
return users.user, nil
|
||||
}
|
||||
|
||||
type privacyTestSettings struct {
|
||||
outbound.PrivacySettingsRepository
|
||||
|
||||
transactor *privacyTestTransactor
|
||||
}
|
||||
|
||||
func (settings privacyTestSettings) ReadSettings(context.Context, uuid.UUID) (outbound.CandidatePrivacySettings, error) {
|
||||
return outbound.CandidatePrivacySettings{Publication: settings.transactor.publication}, nil
|
||||
}
|
||||
|
||||
func (settings privacyTestSettings) LockCurrentC2ForPublication(context.Context, uuid.UUID) (uuid.UUID, error) {
|
||||
return settings.transactor.publication.ConsentRecordID(), nil
|
||||
}
|
||||
|
||||
func (settings privacyTestSettings) SavePublication(_ context.Context, publication domain.Publication, _ int64) error {
|
||||
settings.transactor.publication = publication
|
||||
return nil
|
||||
}
|
||||
|
||||
func (settings privacyTestSettings) RecordPrivacyEvent(_ context.Context, event domain.PrivacyEvent) error {
|
||||
settings.transactor.history = append(settings.transactor.history, event)
|
||||
settings.transactor.sequence = append(settings.transactor.sequence, "history")
|
||||
return nil
|
||||
}
|
||||
|
||||
type privacyTestView struct {
|
||||
outbound.PrivacyViewRepository
|
||||
|
||||
transactor *privacyTestTransactor
|
||||
}
|
||||
|
||||
func (view privacyTestView) CloseForVisibility(_ context.Context, candidateID, organizationID uuid.UUID, at time.Time) error {
|
||||
view.transactor.closureCalls = append(view.transactor.closureCalls, privacyClosureCall{
|
||||
candidateID: candidateID, organizationID: organizationID, at: at,
|
||||
})
|
||||
view.transactor.sequence = append(view.transactor.sequence, "close")
|
||||
return nil
|
||||
}
|
||||
|
||||
type privacyTestOutbox struct {
|
||||
outbound.OutboxWriter
|
||||
|
||||
transactor *privacyTestTransactor
|
||||
}
|
||||
|
||||
func (outbox privacyTestOutbox) Append(_ context.Context, events ...domain.DomainEvent) error {
|
||||
if outbox.transactor.outboxError != nil {
|
||||
return outbox.transactor.outboxError
|
||||
}
|
||||
outbox.transactor.outboxEvents = append(outbox.transactor.outboxEvents, events...)
|
||||
outbox.transactor.sequence = append(outbox.transactor.sequence, "outbox")
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user