package domain import ( "fmt" "log/slog" "time" "github.com/google/uuid" ) // InvitationEventDelivered is the audit event of a send. Sending itself is an // internal step and never a state: the event records the version at which // the draft became a delivered invitation. const InvitationEventDelivered InvitationEventKind = "delivered" // IsDelivered reports whether the invitation has reached the candidate's // inbox. A delivery is never undone, so every later state keeps it. func (invitation Invitation) IsDelivered() bool { return invitation.record.DeliveredAt != nil } // Deliver returns the draft as the next version in the state delivered. The // response deadline starts at the delivery: deliveredAt plus the response // period of the draft, in whole calendar days. The deadline is fixed here and // nothing else moves it, so a notification or its retries cannot change it. func (invitation Invitation) Deliver(deliveredAt time.Time) (Invitation, error) { const location = "Invitation.Deliver" if invitation.State() != InvitationDraft || invitation.IsDelivered() { return Invitation{}, fmt.Errorf("%s - only an unsent draft can be delivered: %w", location, ErrInvitationNotDraft) } if !validInvitationTime(deliveredAt) || deliveredAt.Before(invitation.record.UpdatedAt) { return Invitation{}, fmt.Errorf("%s - delivery time must be set and not before the last change: %w", location, ErrInvalidInvitation) } deliveredAt = deliveredAt.UTC() deadline := deliveredAt.Add(CalendarDays(invitation.record.ResponsePeriodDays).Duration()) next := invitation.Stored() next.State = string(InvitationDelivered) next.Version++ next.UpdatedAt = deliveredAt next.DeliveredAt = &deliveredAt next.ResponseDeadlineAt = &deadline delivered, err := RestoreInvitation(next) if err != nil { return Invitation{}, fmt.Errorf("%s - restore delivered invitation: %w", location, err) } return delivered, nil } // DeliveryEvent builds the audit event of the delivered version this // invitation has just reached. It names the actor who sent it and no field. func (invitation Invitation) DeliveryEvent(actorID uuid.UUID) (InvitationEvent, error) { const location = "Invitation.DeliveryEvent" if actorID == uuid.Nil { return InvitationEvent{}, fmt.Errorf("%s - actor must be set: %w", location, ErrInvalidInvitation) } if invitation.State() != InvitationDelivered || !invitation.IsDelivered() { return InvitationEvent{}, fmt.Errorf("%s - the invitation must be delivered: %w", location, ErrInvalidInvitation) } id, err := uuid.NewV7() if err != nil { return InvitationEvent{}, fmt.Errorf("%s - generate event ID: %w", location, err) } return InvitationEvent{ ID: id, InvitationID: invitation.record.ID, OrganizationID: invitation.record.OrganizationID, Version: invitation.record.Version, TermsVersion: invitation.record.TermsVersion, Kind: InvitationEventDelivered, ActorID: actorID, ChangedFields: []InvitationChangedField{}, OccurredAt: *invitation.record.DeliveredAt, }, nil } // InvitationOrganizationRef names the organization that sent an invitation, // as the candidate sees it. type InvitationOrganizationRef struct { ID uuid.UUID Name string } // CandidateInvitationSummary is an invitation in the inbox of the candidate it // is addressed to: the organization, the state, the versions, the role, the // salary range, the work formats, and the deadline. It has no candidate // identifier, handle, need, or salary indicator. type CandidateInvitationSummary struct { ID uuid.UUID Organization InvitationOrganizationRef State InvitationState ExpiryReason *InvitationExpiryReason Version int64 TermsVersion int64 RoleTitle string Salary SalaryRange WorkFormats []NeedWorkFormat ResponseDeadlineAt time.Time DeliveredAt time.Time ViewedAt *time.Time UpdatedAt time.Time } // CandidateSummary returns the inbox entry of a delivered invitation for the // organization that sent it. func (invitation Invitation) CandidateSummary(organization InvitationOrganizationRef) (CandidateInvitationSummary, error) { const location = "Invitation.CandidateSummary" stored := invitation.Stored() if stored.DeliveredAt == nil || stored.ResponseDeadlineAt == nil { return CandidateInvitationSummary{}, fmt.Errorf("%s - only a delivered invitation is in the inbox: %w", location, ErrInvalidInvitation) } if organization.ID != stored.OrganizationID || organization.Name == "" { return CandidateInvitationSummary{}, fmt.Errorf("%s - the organization must be the sender: %w", location, ErrInvalidInvitation) } snapshot := invitation.Snapshot() return CandidateInvitationSummary{ ID: stored.ID, Organization: organization, State: snapshot.State, ExpiryReason: snapshot.ExpiryReason, Version: stored.Version, TermsVersion: stored.TermsVersion, RoleTitle: stored.Terms.RoleTitle, Salary: stored.Terms.Salary, WorkFormats: stored.Terms.WorkFormats, ResponseDeadlineAt: stored.ResponseDeadlineAt.UTC(), DeliveredAt: stored.DeliveredAt.UTC(), ViewedAt: stored.ViewedAt, UpdatedAt: stored.UpdatedAt, }, nil } func (summary CandidateInvitationSummary) String() string { return redactedInvitation } func (summary CandidateInvitationSummary) GoString() string { return redactedInvitation } func (summary CandidateInvitationSummary) Format(state fmt.State, _ rune) { writeRedacted(state, redactedInvitation) } func (summary CandidateInvitationSummary) LogValue() slog.Value { return slog.StringValue(redactedInvitation) }