feat(invitations): send invitations into the candidate inbox atomically
This commit is contained in:
1 parent
fc35e27af5
commit
1a067221f7
46 files changed
+3276
-113
No files matched your search
@@ -232,7 +232,11 @@ func RestoreInvitation(stored StoredInvitation) (Invitation, error) {
|
||||
return Invitation{}, fmt.Errorf("%s - validate terms: %w", location, err)
|
||||
}
|
||||
stored.Terms = stored.Terms.Clone()
|
||||
// Every time of an invitation is kept in UTC, whatever the time zone of
|
||||
// the storage session, so a response is the same after a read.
|
||||
stored.CreatedAt, stored.UpdatedAt = stored.CreatedAt.UTC(), stored.UpdatedAt.UTC()
|
||||
stored.ResponseDeadlineAt, stored.DeliveredAt = utcInvitationTime(stored.ResponseDeadlineAt), utcInvitationTime(stored.DeliveredAt)
|
||||
stored.ViewedAt, stored.RespondedAt = utcInvitationTime(stored.ViewedAt), utcInvitationTime(stored.RespondedAt)
|
||||
if stored.Need != nil {
|
||||
need := *stored.Need
|
||||
stored.Need = &need
|
||||
@@ -286,6 +290,14 @@ func storedInvitationLifecycleDefect(stored StoredInvitation) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func utcInvitationTime(value *time.Time) *time.Time {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
converted := value.UTC()
|
||||
return &converted
|
||||
}
|
||||
|
||||
func validInvitationTime(value time.Time) bool {
|
||||
return !value.IsZero() && value.Year() >= 1 && value.Year() <= 9999
|
||||
}
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestDeliverStartsTheDeadlineAtTheDeliveryForEveryPeriod(t *testing.T) {
|
||||
for _, period := range []CalendarDays{3, 7, 14} {
|
||||
input := validInvitationDraftInput()
|
||||
input.Content.ResponsePeriod = period
|
||||
draft, err := NewInvitationDraft(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
deliveredAt := input.At.Add(time.Hour)
|
||||
delivered, err := draft.Deliver(deliveredAt)
|
||||
if err != nil {
|
||||
t.Fatalf("Deliver(%d days) error = %v", period, err)
|
||||
}
|
||||
snapshot := delivered.Snapshot()
|
||||
if snapshot.State != InvitationDelivered || snapshot.Version != draft.Version()+1 || snapshot.TermsVersion != draft.TermsVersion() ||
|
||||
!snapshot.DeliveredAt.Equal(deliveredAt) || !snapshot.ResponseDeadlineAt.Equal(deliveredAt.Add(time.Duration(period)*24*time.Hour)) ||
|
||||
!snapshot.UpdatedAt.Equal(deliveredAt) || !delivered.IsDelivered() || draft.IsDelivered() {
|
||||
t.Fatalf("delivered %d-day invitation = %+v", period, snapshot)
|
||||
}
|
||||
event, err := delivered.DeliveryEvent(input.ActorID)
|
||||
if err != nil {
|
||||
t.Fatalf("DeliveryEvent() error = %v", err)
|
||||
}
|
||||
if event.Kind != InvitationEventDelivered || event.Version != snapshot.Version || len(event.ChangedFields) != 0 ||
|
||||
!event.OccurredAt.Equal(deliveredAt) {
|
||||
t.Fatalf("delivery event = %+v", event)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliverRefusesAnythingButAnUnsentDraft(t *testing.T) {
|
||||
input := validInvitationDraftInput()
|
||||
draft, err := NewInvitationDraft(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
if _, err := draft.Deliver(input.At.Add(-time.Second)); !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("Deliver(before the last change) error = %v, want ErrInvalidInvitation", err)
|
||||
}
|
||||
delivered, err := draft.Deliver(input.At)
|
||||
if err != nil {
|
||||
t.Fatalf("Deliver() error = %v", err)
|
||||
}
|
||||
if _, err := delivered.Deliver(input.At.Add(time.Hour)); !errors.Is(err, ErrInvitationNotDraft) {
|
||||
t.Fatalf("Deliver(delivered) error = %v, want ErrInvitationNotDraft", err)
|
||||
}
|
||||
if _, err := draft.DeliveryEvent(input.ActorID); !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("DeliveryEvent(draft) error = %v, want ErrInvalidInvitation", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCandidateSummaryCarriesNoCandidateIdentifier(t *testing.T) {
|
||||
input := validInvitationDraftInput()
|
||||
draft, err := NewInvitationDraft(input)
|
||||
if err != nil {
|
||||
t.Fatalf("NewInvitationDraft() error = %v", err)
|
||||
}
|
||||
if _, err := draft.CandidateSummary(InvitationOrganizationRef{ID: input.OrganizationID, Name: "Sender"}); !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("CandidateSummary(draft) error = %v, want ErrInvalidInvitation", err)
|
||||
}
|
||||
delivered, err := draft.Deliver(input.At)
|
||||
if err != nil {
|
||||
t.Fatalf("Deliver() error = %v", err)
|
||||
}
|
||||
if _, err := delivered.CandidateSummary(InvitationOrganizationRef{ID: uuid.New(), Name: "Other"}); !errors.Is(err, ErrInvalidInvitation) {
|
||||
t.Fatalf("CandidateSummary(other organization) error = %v, want ErrInvalidInvitation", err)
|
||||
}
|
||||
summary, err := delivered.CandidateSummary(InvitationOrganizationRef{ID: input.OrganizationID, Name: "Sender"})
|
||||
if err != nil {
|
||||
t.Fatalf("CandidateSummary() error = %v", err)
|
||||
}
|
||||
if summary.State != InvitationDelivered || summary.RoleTitle != input.Content.Terms.RoleTitle || !summary.DeliveredAt.Equal(input.At) {
|
||||
t.Fatalf("summary = %+v", summary)
|
||||
}
|
||||
for _, text := range []string{fmt.Sprint(summary), fmt.Sprintf("%+v", summary), fmt.Sprintf("%#v", summary)} {
|
||||
if strings.Contains(text, input.CandidateID.String()) || strings.Contains(text, summary.RoleTitle) {
|
||||
t.Fatalf("formatted summary leaks its content: %s", text)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user