package invitation import ( "context" "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) } 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 = candidateView(ctx, unit, entry) 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 = candidateView(ctx, unit, entry) 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. func candidateView(ctx context.Context, unit outbound.UnitOfWork, entry outbound.CandidateInvitationEntry) (domain.CandidateInvitationView, error) { const location = "invitation.candidateView" var decline *domain.InvitationDecline if entry.Invitation.State() == domain.InvitationDeclined { stored, err := unit.Invitations().GetDecline(ctx, entry.Invitation.OrganizationID(), entry.Invitation.ID()) if err != nil { return domain.CandidateInvitationView{}, fmt.Errorf("%s - read decline: %w", location, err) } decline = &stored } view, err := entry.Invitation.CandidateView(domain.InvitationOrganizationRef{ ID: entry.Invitation.OrganizationID(), Name: entry.OrganizationName, }, decline) if err != nil { return domain.CandidateInvitationView{}, fmt.Errorf("%s - build view: %w", location, err) } return view, 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 }