feat(invitations): send invitations into the candidate inbox atomically

This commit is contained in:
shira committed 2026-10-09 16:50:36 +03:00
1 parent fc35e27af5
commit 1a067221f7
46 files changed
+3276 -113

No files matched your search

@@ -41,7 +41,12 @@ type testState struct {
events []domain.InvitationEvent
idempotency map[[outbound.IdempotencyDigestSize]byte]outbound.IdempotencyRecord
results map[[outbound.IdempotencyDigestSize]byte]domain.OrganizationInvitationView
sendResults map[[outbound.IdempotencyDigestSize]byte]outbound.InvitationSendResult
needVersions map[[3]string]bool
// charged lists the invitations charged against the organization quota,
// and notified the delivered invitations that were notified.
charged []uuid.UUID
notified []uuid.UUID
}
func newTestState() testState {
@@ -50,6 +55,7 @@ func newTestState() testState {
invitations: map[uuid.UUID]domain.Invitation{}, terms: map[termsKey]domain.InvitationTerms{},
idempotency: map[[outbound.IdempotencyDigestSize]byte]outbound.IdempotencyRecord{},
results: map[[outbound.IdempotencyDigestSize]byte]domain.OrganizationInvitationView{},
sendResults: map[[outbound.IdempotencyDigestSize]byte]outbound.InvitationSendResult{},
needVersions: map[[3]string]bool{},
}
}
@@ -75,9 +81,14 @@ func (state testState) clone() testState {
for scope, result := range state.results {
cloned.results[scope] = result
}
for scope, result := range state.sendResults {
cloned.sendResults[scope] = result
}
for key, found := range state.needVersions {
cloned.needVersions[key] = found
}
cloned.charged = slices.Clone(state.charged)
cloned.notified = slices.Clone(state.notified)
return cloned
}
@@ -129,6 +140,9 @@ func (unit *testUnit) Invitations() outbound.InvitationRepository {
func (unit *testUnit) InvitationCreationResults() outbound.InvitationCreationResultRepository {
return testInvitations{state: unit.state, failResultSave: unit.failResultSave}
}
func (unit *testUnit) InvitationSendResults() outbound.InvitationSendResultRepository {
return testInvitations{state: unit.state}
}
type testUsers struct {
outbound.UserRepository
@@ -158,6 +172,14 @@ func (repository testOrganizations) LockActiveMembership(_ context.Context, orga
return membership, nil
}
func (repository testOrganizations) GetForMember(_ context.Context, organizationID, userID uuid.UUID) (domain.Organization, domain.Membership, error) {
membership, ok := repository.state.memberships[[2]uuid.UUID{organizationID, userID}]
if !ok || !membership.GrantsAccess() {
return domain.Organization{}, domain.Membership{}, outbound.ErrOrganizationNotFound
}
return domain.Organization{}, membership, nil
}
type testQuotas struct {
outbound.CompanyQuotaRepository
}
@@ -194,6 +216,7 @@ func (repository testIdempotency) PurgeExpired(_ context.Context, at time.Time,
if !record.ExpiresAt.After(at) {
delete(repository.state.idempotency, scope)
delete(repository.state.results, scope)
delete(repository.state.sendResults, scope)
deleted++
}
}
@@ -255,6 +278,52 @@ func (repository testInvitations) SaveDraft(_ context.Context, expectedVersion i
return nil
}
func (repository testInvitations) Deliver(_ context.Context, expectedVersion int64, next domain.Invitation, event domain.InvitationEvent) error {
current, ok := repository.state.invitations[next.ID()]
if !ok || current.Version() != expectedVersion || current.State() != domain.InvitationDraft {
return outbound.ErrInvitationVersionConflict
}
repository.state.invitations[next.ID()] = next
repository.state.events = append(repository.state.events, event)
return nil
}
func (repository testInvitations) ListForCandidate(_ context.Context, query outbound.CandidateInvitationListQuery) (outbound.CandidateInvitationPage, error) {
var items []outbound.CandidateInvitationEntry
for _, invitation := range repository.state.invitations {
if invitation.CandidateID() == query.CandidateID && invitation.IsDelivered() {
items = append(items, outbound.CandidateInvitationEntry{Invitation: invitation, OrganizationName: "Test organization"})
}
}
page := outbound.CandidateInvitationPage{HasMore: len(items) > int(query.Limit)}
if page.HasMore {
items = items[:query.Limit]
}
page.Items = items
return page, nil
}
func (repository testInvitations) GetForCandidate(_ context.Context, candidateID, invitationID uuid.UUID) (domain.Invitation, error) {
invitation, ok := repository.state.invitations[invitationID]
if !ok || invitation.CandidateID() != candidateID || !invitation.IsDelivered() {
return domain.Invitation{}, outbound.ErrInvitationNotFound
}
return invitation, nil
}
func (repository testInvitations) SaveSendResult(_ context.Context, scope [outbound.IdempotencyDigestSize]byte, result outbound.InvitationSendResult) error {
repository.state.sendResults[scope] = result
return nil
}
func (repository testInvitations) GetSendResult(_ context.Context, scope [outbound.IdempotencyDigestSize]byte) (outbound.InvitationSendResult, error) {
result, ok := repository.state.sendResults[scope]
if !ok {
return outbound.InvitationSendResult{}, outbound.ErrInvitationSendResultNotFound
}
return result, nil
}
func (repository testInvitations) NeedVersionExists(_ context.Context, organizationID, needID uuid.UUID, version int64) (bool, error) {
return repository.state.needVersions[[3]string{organizationID.String(), needID.String(), fmt.Sprint(version)}], nil
}
@@ -276,8 +345,11 @@ func (repository testInvitations) GetCreationResult(_ context.Context, scope [ou
}
type harness struct {
service *Service
transactor *testTransactor
service *Service
transactor *testTransactor
// quotaLeft is the number of new invitations the organization quota
// still allows.
quotaLeft int
key domain.CandidateViewPseudonymKey
actorID uuid.UUID
organizationID uuid.UUID
@@ -305,7 +377,7 @@ func newHarness(t *testing.T) *harness {
}
test := &harness{
transactor: &testTransactor{state: state, policy: testPolicyVersion(t, now)},
key: key, actorID: actorID, organizationID: organizationID, visible: map[string]uuid.UUID{},
key: key, actorID: actorID, organizationID: organizationID, visible: map[string]uuid.UUID{}, quotaLeft: 10,
setTime: func(value time.Time) { current = value },
}
test.service, err = NewService(Dependencies{
@@ -317,6 +389,18 @@ func newHarness(t *testing.T) *harness {
}
return candidateID, nil
},
Quota: func(_ context.Context, unit outbound.UnitOfWork, _ domain.Organization, invitationID uuid.UUID) (outbound.CompanyQuotaDecision, error) {
state := unit.(*testUnit).state
if slices.Contains(state.charged, invitationID) {
return outbound.CompanyQuotaDecision{Allowed: true, Reused: true}, nil
}
if len(state.charged) >= test.quotaLeft {
return outbound.CompanyQuotaDecision{RetryAt: current.Add(time.Hour)}, nil
}
state.charged = append(state.charged, invitationID)
return outbound.CompanyQuotaDecision{Allowed: true}, nil
},
Notifier: testNotifier{},
})
if err != nil {
t.Fatalf("NewService() error = %v", err)
@@ -337,6 +421,14 @@ func (test *harness) showCandidate(t *testing.T) (string, uuid.UUID) {
return handle, candidateID
}
type testNotifier struct{}
func (testNotifier) InvitationDelivered(_ context.Context, unit outbound.UnitOfWork, invitation domain.Invitation) error {
state := unit.(*testUnit).state
state.notified = append(state.notified, invitation.ID())
return nil
}
func testPolicyVersion(t *testing.T, at time.Time) domain.PolicyVersion {
t.Helper()
draft, err := domain.NewPolicyVersionDraft(domain.NewPolicyVersionDraftInput{
@@ -543,6 +635,10 @@ func TestOnlyTheActiveOwnerOfAnEmployerAccountMayWorkWithInvitations(t *testing.
if _, err := test.service.Get(t.Context(), tt.actor, test.organizationID, uuid.New()); !errors.Is(err, tt.want) {
t.Errorf("Get() error = %v, want %v", err, tt.want)
}
send := SendCommand{ActorID: tt.actor, OrganizationID: test.organizationID, InvitationID: uuid.New(), IdempotencyKey: "send-" + tt.name, ExpectedVersion: 1}
if _, err := test.service.Send(t.Context(), send); !errors.Is(err, tt.want) {
t.Errorf("Send() error = %v, want %v", err, tt.want)
}
})
}
if state := test.transactor.snapshot(); len(state.invitations) != 0 {