feat(invitations): check contact grants on every read, cap pending invitations, and expire them at the deadline
This commit is contained in:
1 parent
676b3c6e24
commit
12a7213d4a
67 files changed
+4255
-149
No files matched your search
@@ -66,10 +66,11 @@ type sendOutcome struct {
|
||||
|
||||
// Send delivers the current terms version of a draft to the candidate's
|
||||
// inbox. In one transaction it checks the owner, the key, the invitation, its
|
||||
// version and state, and the current visibility of the candidate, spends one
|
||||
// invitation of the organization quota, records the delivery with its
|
||||
// deadline and the audit event, and writes the outbox events of the
|
||||
// notification. The internal sending step is never visible: a success means
|
||||
// version and state, the current visibility of the candidate, the pending limit
|
||||
// of the need, and the pause after a decline; it then spends one invitation of
|
||||
// the organization quota, records the delivery with its deadline, the audit
|
||||
// event, and the expiry job at the deadline, and writes the outbox events of
|
||||
// the notification. The internal sending step is never visible: a success means
|
||||
// the invitation is already delivered. A send of an invitation that was
|
||||
// already delivered answers with its current state and charges nothing.
|
||||
func (service *Service) Send(ctx context.Context, command SendCommand) (domain.OrganizationInvitationView, error) {
|
||||
@@ -126,8 +127,9 @@ func (service *Service) prepareSend(command SendCommand) (sendRequest, error) {
|
||||
|
||||
// sendInTransaction follows the order of checks of the contract after the
|
||||
// HTTP boundary: the owner, the key, the invitation (404, stored), the repeat
|
||||
// of a completed send (200), the version (412, not stored), the state and the
|
||||
// visibility of the candidate (409, stored), and the quota (429, not stored).
|
||||
// of a completed send (200), the version (412, not stored), the state, the
|
||||
// visibility of the candidate, the pending limit, and the pause (409, stored),
|
||||
// and the quota (429, not stored).
|
||||
// The locks follow the order of every invitation command: the organization,
|
||||
// the membership, the invitation row, and then the cohort of the cards.
|
||||
func (service *Service) sendInTransaction(
|
||||
@@ -170,23 +172,52 @@ func (service *Service) sendInTransaction(
|
||||
if current.State() != domain.InvitationDraft {
|
||||
return service.refuseSend(ctx, unit, command, request, outbound.InvitationSendStateConflict)
|
||||
}
|
||||
visible, err := service.candidateStillVisible(ctx, unit, command, current)
|
||||
at, admitted, err := service.admitSend(ctx, unit, command, current)
|
||||
if err != nil {
|
||||
return sendOutcome{}, err
|
||||
}
|
||||
if !visible {
|
||||
if !admitted {
|
||||
return service.refuseSend(ctx, unit, command, request, outbound.InvitationSendStateConflict)
|
||||
}
|
||||
if err := service.chargeQuota(ctx, unit, command, current); err != nil {
|
||||
return sendOutcome{}, err
|
||||
}
|
||||
delivered, err := service.deliver(ctx, unit, command, current, request.now)
|
||||
delivered, err := service.deliver(ctx, unit, command, current, at)
|
||||
if err != nil {
|
||||
return sendOutcome{}, err
|
||||
}
|
||||
return service.completeSend(ctx, unit, request, delivered)
|
||||
}
|
||||
|
||||
// admitSend applies the rules that refuse a send with a state conflict: the
|
||||
// visibility of the candidate, then the pending limit of the need and the pause
|
||||
// after a decline. It returns the time of the decision, which the caller uses for
|
||||
// the delivery, and whether the send is admitted. The decision is made at the time
|
||||
// the send holds the organization lock, not at the time it started to wait for it.
|
||||
func (service *Service) admitSend(
|
||||
ctx context.Context,
|
||||
unit outbound.UnitOfWork,
|
||||
command SendCommand,
|
||||
current domain.Invitation,
|
||||
) (time.Time, bool, error) {
|
||||
visible, err := service.candidateStillVisible(ctx, unit, command, current)
|
||||
if err != nil {
|
||||
return time.Time{}, false, err
|
||||
}
|
||||
if !visible {
|
||||
return time.Time{}, false, nil
|
||||
}
|
||||
at, err := service.now(sendLocation)
|
||||
if err != nil {
|
||||
return time.Time{}, false, err
|
||||
}
|
||||
limited, err := service.sendLimitsReached(ctx, unit, command, current, at)
|
||||
if err != nil {
|
||||
return time.Time{}, false, err
|
||||
}
|
||||
return at, !limited, nil
|
||||
}
|
||||
|
||||
// candidateStillVisible runs the visibility barrier of the card read again:
|
||||
// a draft keeps its candidate after a revoked publication C2, a hiding of the
|
||||
// organization, or a cohort that fell below its minimum, and none of them may
|
||||
@@ -258,6 +289,11 @@ func (service *Service) deliver(
|
||||
}
|
||||
return domain.Invitation{}, fmt.Errorf("%s - store delivery: %w", sendLocation, err)
|
||||
}
|
||||
// The expiry at the deadline is scheduled in the delivery transaction, so no
|
||||
// delivered invitation exists without its job.
|
||||
if err := scheduleResponseDeadline(ctx, unit, delivered); err != nil {
|
||||
return domain.Invitation{}, fmt.Errorf("%s - schedule response deadline: %w", sendLocation, err)
|
||||
}
|
||||
if err := service.notifier.InvitationDelivered(ctx, unit, delivered); err != nil {
|
||||
return domain.Invitation{}, fmt.Errorf("%s - record notification: %w", sendLocation, err)
|
||||
}
|
||||
@@ -271,7 +307,7 @@ func (service *Service) completeSend(
|
||||
request sendRequest,
|
||||
invitation domain.Invitation,
|
||||
) (sendOutcome, error) {
|
||||
view, err := service.view(invitation)
|
||||
view, err := service.view(ctx, unit, invitation)
|
||||
if err != nil {
|
||||
return sendOutcome{}, err
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user