251 lines
9.6 KiB
Go
251 lines
9.6 KiB
Go
package invitation
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
const (
|
|
getMineLocation = "invitation.Service.GetMine"
|
|
viewMineLocation = "invitation.Service.ViewMine"
|
|
)
|
|
|
|
// GetMine reads one invitation delivered to the session candidate with the
|
|
// current terms. Reading has no side effect: it never records a view, so a
|
|
// prefetch or a cache refresh never changes what the organization sees. A
|
|
// draft, an invitation of another candidate, and an unknown identifier are
|
|
// not found alike.
|
|
func (service *Service) GetMine(ctx context.Context, actorID, invitationID uuid.UUID) (domain.CandidateInvitationView, error) {
|
|
if actorID == uuid.Nil {
|
|
return domain.CandidateInvitationView{}, fmt.Errorf("%s - actor ID must be set: %w", getMineLocation, ErrForbidden)
|
|
}
|
|
now, err := service.now(getMineLocation)
|
|
if err != nil {
|
|
return domain.CandidateInvitationView{}, err
|
|
}
|
|
var view domain.CandidateInvitationView
|
|
err = service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := authorizeCandidate(ctx, unit, actorID); err != nil {
|
|
return err
|
|
}
|
|
entry, err := readCandidateEntry(ctx, unit, actorID, invitationID, false)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
view, err = service.candidateView(ctx, unit, entry, now)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return domain.CandidateInvitationView{}, fmt.Errorf("%s - run transaction: %w", getMineLocation, err)
|
|
}
|
|
return view, nil
|
|
}
|
|
|
|
// ViewMine records that the session candidate opened the terms. A delivered
|
|
// invitation moves to viewed with the next version and keeps its deadline;
|
|
// every other state, and a view at the deadline or later, changes nothing and
|
|
// returns the invitation as it is. The command is idempotent by its effect,
|
|
// so it takes neither a key nor an expected version.
|
|
//
|
|
// Locks: only the invitation row, never the organization. A candidate
|
|
// command therefore never holds the row while it waits for an organization
|
|
// lock, and an organization command that holds the organization lock and
|
|
// waits for the row cannot deadlock with it.
|
|
func (service *Service) ViewMine(ctx context.Context, actorID, invitationID uuid.UUID) (domain.CandidateInvitationView, error) {
|
|
if actorID == uuid.Nil {
|
|
return domain.CandidateInvitationView{}, fmt.Errorf("%s - actor ID must be set: %w", viewMineLocation, ErrForbidden)
|
|
}
|
|
now, err := service.now(viewMineLocation)
|
|
if err != nil {
|
|
return domain.CandidateInvitationView{}, err
|
|
}
|
|
var view domain.CandidateInvitationView
|
|
err = service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := authorizeCandidate(ctx, unit, actorID); err != nil {
|
|
return err
|
|
}
|
|
entry, err := readCandidateEntry(ctx, unit, actorID, invitationID, true)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
current := entry.Invitation
|
|
viewed, changed, err := current.RecordView(notBefore(now, current.UpdatedAt()))
|
|
if err != nil {
|
|
return fmt.Errorf("%s - record view: %w", viewMineLocation, err)
|
|
}
|
|
if changed {
|
|
event, err := viewed.AnswerEvent(actorID)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - build audit event: %w", viewMineLocation, err)
|
|
}
|
|
if err := unit.Invitations().RecordView(ctx, current.Version(), viewed, event); err != nil {
|
|
return fmt.Errorf("%s - store view: %w", viewMineLocation, err)
|
|
}
|
|
entry.Invitation = viewed
|
|
}
|
|
view, err = service.candidateView(ctx, unit, entry, now)
|
|
return err
|
|
})
|
|
if err != nil {
|
|
return domain.CandidateInvitationView{}, fmt.Errorf("%s - run transaction: %w", viewMineLocation, err)
|
|
}
|
|
return view, nil
|
|
}
|
|
|
|
// readCandidateEntry reads, and with forUpdate locks, a delivered invitation
|
|
// addressed to the candidate. The nil identifier finds nothing.
|
|
func readCandidateEntry(
|
|
ctx context.Context,
|
|
unit outbound.UnitOfWork,
|
|
actorID, invitationID uuid.UUID,
|
|
forUpdate bool,
|
|
) (outbound.CandidateInvitationEntry, error) {
|
|
const location = "invitation.readCandidateEntry"
|
|
if invitationID == uuid.Nil {
|
|
return outbound.CandidateInvitationEntry{}, fmt.Errorf("%s - the nil identifier names no invitation: %w", location, outbound.ErrInvitationNotFound)
|
|
}
|
|
read := unit.Invitations().GetCandidateEntry
|
|
if forUpdate {
|
|
read = unit.Invitations().GetCandidateEntryForUpdate
|
|
}
|
|
entry, err := read(ctx, actorID, invitationID)
|
|
if err != nil {
|
|
return outbound.CandidateInvitationEntry{}, fmt.Errorf("%s - read invitation: %w", location, err)
|
|
}
|
|
return entry, nil
|
|
}
|
|
|
|
// candidateView builds the candidate's response with the own decline of a
|
|
// declined invitation. The organization is shown by its current name even
|
|
// when the candidate has hidden the profile from it: the hiding stops the
|
|
// organization from seeing the candidate, while the candidate needs to know
|
|
// who sent the offer to answer it. Pushed content, such as the inbox
|
|
// notification, follows the hiding instead.
|
|
//
|
|
// The consent offer is set while an acceptance is possible, and the contact
|
|
// grant once the invitation is accepted. The grant is readable only while the
|
|
// organization may read the profile, so a hidden profile shows the grant as
|
|
// blocked by visibility. at is the time of the command that builds the view.
|
|
func (service *Service) candidateView(
|
|
ctx context.Context,
|
|
unit outbound.UnitOfWork,
|
|
entry outbound.CandidateInvitationEntry,
|
|
at time.Time,
|
|
) (domain.CandidateInvitationView, error) {
|
|
const location = "invitation.Service.candidateView"
|
|
current := entry.Invitation
|
|
var decline *domain.InvitationDecline
|
|
if current.State() == domain.InvitationDeclined {
|
|
stored, err := unit.Invitations().GetDecline(ctx, current.OrganizationID(), current.ID())
|
|
if err != nil {
|
|
return domain.CandidateInvitationView{}, fmt.Errorf("%s - read decline: %w", location, err)
|
|
}
|
|
decline = &stored
|
|
}
|
|
view, err := current.CandidateView(domain.InvitationOrganizationRef{
|
|
ID: current.OrganizationID(), Name: entry.OrganizationName,
|
|
}, decline)
|
|
if err != nil {
|
|
return domain.CandidateInvitationView{}, fmt.Errorf("%s - build view: %w", location, err)
|
|
}
|
|
switch {
|
|
case current.State() == domain.InvitationAccepted:
|
|
grant, err := unit.ContactGrants().GetByInvitation(ctx, current.OrganizationID(), current.ID())
|
|
if err != nil {
|
|
return domain.CandidateInvitationView{}, fmt.Errorf("%s - read contact grant: %w", location, err)
|
|
}
|
|
visible, err := candidateVisibleTo(ctx, unit, current.CandidateID(), current.OrganizationID())
|
|
if err != nil {
|
|
return domain.CandidateInvitationView{}, fmt.Errorf("%s - read visibility: %w", location, err)
|
|
}
|
|
grantView := grant.CandidateView(visible)
|
|
view.ContactGrant = &grantView
|
|
case acceptancePossible(current, at):
|
|
offer, offered, err := contactConsentOffer(ctx, unit, current, entry.OrganizationName, at)
|
|
if err != nil {
|
|
return domain.CandidateInvitationView{}, fmt.Errorf("%s - build consent offer: %w", location, err)
|
|
}
|
|
if offered {
|
|
view.ContactConsent = &offer
|
|
}
|
|
}
|
|
return view, nil
|
|
}
|
|
|
|
// acceptancePossible reports the states and the time in which the candidate
|
|
// may accept: a delivered or viewed invitation before its deadline.
|
|
func acceptancePossible(invitation domain.Invitation, at time.Time) bool {
|
|
state := invitation.State()
|
|
return (state == domain.InvitationDelivered || state == domain.InvitationViewed) &&
|
|
invitation.IsDelivered() && !invitation.ResponseDeadlinePassed(at)
|
|
}
|
|
|
|
// candidateVisibleTo reports whether the organization may read the profile of
|
|
// the candidate now: the publication is effective, and the candidate has not
|
|
// hidden the organization. The read model of the profile applies C2 and the
|
|
// revocation of C2; the hiding is a record of its own.
|
|
func candidateVisibleTo(ctx context.Context, unit outbound.UnitOfWork, candidateID, organizationID uuid.UUID) (bool, error) {
|
|
const location = "invitation.candidateVisibleTo"
|
|
settings, err := unit.PrivacySettings().ReadSettings(ctx, candidateID)
|
|
if err != nil {
|
|
return false, fmt.Errorf("%s - read privacy settings: %w", location, err)
|
|
}
|
|
if settings.Publication.State() != domain.PublicationPublished {
|
|
return false, nil
|
|
}
|
|
_, err = unit.PrivacySettings().GetHiddenOrganization(ctx, candidateID, organizationID)
|
|
switch {
|
|
case err == nil:
|
|
return false, nil
|
|
case errors.Is(err, outbound.ErrCandidatePrivacySettingsNotFound):
|
|
return true, nil
|
|
default:
|
|
return false, fmt.Errorf("%s - read hidden organization: %w", location, err)
|
|
}
|
|
}
|
|
|
|
// contactConsentOffer builds the consent offer of an invitation: the rendered
|
|
// C4 text and the kinds that the profile holds. It reports false when the
|
|
// profile holds no kind, because a grant must cover at least one field.
|
|
func contactConsentOffer(
|
|
ctx context.Context,
|
|
unit outbound.UnitOfWork,
|
|
invitation domain.Invitation,
|
|
organizationName string,
|
|
at time.Time,
|
|
) (domain.ContactConsentOffer, bool, error) {
|
|
profile, err := unit.CandidateProfiles().Get(ctx, invitation.CandidateID())
|
|
if err != nil {
|
|
return domain.ContactConsentOffer{}, false, fmt.Errorf("read candidate profile: %w", err)
|
|
}
|
|
held := heldContactFields(profile)
|
|
if len(held) == 0 {
|
|
return domain.ContactConsentOffer{}, false, nil
|
|
}
|
|
text, err := unit.ConsentTexts().Current(ctx, domain.ConsentPurposeContactDisclosure, at)
|
|
if err != nil {
|
|
return domain.ContactConsentOffer{}, false, fmt.Errorf("read consent C4 text: %w", err)
|
|
}
|
|
offer, err := domain.NewContactConsentOffer(text, organizationName, held)
|
|
if err != nil {
|
|
return domain.ContactConsentOffer{}, false, fmt.Errorf("build offer: %w", err)
|
|
}
|
|
return offer, true, nil
|
|
}
|
|
|
|
// notBefore keeps a clock that stepped back from recording a change before
|
|
// the last one.
|
|
func notBefore(now, last time.Time) time.Time {
|
|
if now.Before(last) {
|
|
return last
|
|
}
|
|
return now
|
|
}
|