feat(invitations): check contact grants on every read, cap pending invitations, and expire them at the deadline

This commit is contained in:
shira committed 2026-10-09 19:06:50 +03:00
1 parent 676b3c6e24
commit 12a7213d4a
67 files changed
+4255 -149

No files matched your search

+34 -8
View File
@@ -62,13 +62,15 @@ type declineFingerprint struct {
// structured reason. In one transaction it checks the candidate, the key,
// the invitation, the repeat of the same decline, a terminal state or a
// passed deadline, the version, and the state, then moves the invitation to
// declined, stores the decline, and appends the audit event. The decline is
// the candidate's answer for the responsiveness metrics; it creates no
// consent and no contact grant, and the organization sees only the state and
// the time of the answer.
// declined, stores the decline, and appends the audit event. A due expiry is
// applied first. The decline is the candidate's answer for the responsiveness
// metrics; it creates no consent and no contact grant, and the organization
// sees only the state and the time of the answer.
//
// Locks: the idempotency record of the candidate's own key and then only the
// invitation row, never the organization, as for the view.
// Locks: the idempotency record of the candidate's own key, then the
// organization lock, then the invitation row. The organization lock is what
// orders a decline against a send of the same pair, so a send never misses a
// decline that committed before it counted the pause.
func (service *Service) DeclineMine(ctx context.Context, command DeclineCommand) (domain.CandidateInvitationView, error) {
request, err := service.prepareDecline(command)
if err != nil {
@@ -146,6 +148,20 @@ func (service *Service) declineInTransaction(
if !created {
return service.replayDecline(ctx, unit, request, command, stored)
}
// A decline changes the pause of the organization and the candidate, so it
// takes the organization lock before the invitation row, as every command
// that may change a pair does. The organization of an invitation never
// changes, so the unlocked read names the lock to take.
unlocked, err := readCandidateEntry(ctx, unit, command.ActorID, command.InvitationID, false)
if errors.Is(err, outbound.ErrInvitationNotFound) {
return refuseDecline(ctx, unit, command, request, outbound.InvitationDeclineNotFound)
}
if err != nil {
return declineOutcome{}, err
}
if err := lockOrganizationFirst(ctx, unit, unlocked.Invitation.OrganizationID(), declineLocation); err != nil {
return declineOutcome{}, err
}
entry, err := readCandidateEntry(ctx, unit, command.ActorID, command.InvitationID, true)
if errors.Is(err, outbound.ErrInvitationNotFound) {
return refuseDecline(ctx, unit, command, request, outbound.InvitationDeclineNotFound)
@@ -158,7 +174,9 @@ func (service *Service) declineInTransaction(
// declineFound applies the checks that follow a found invitation: the repeat
// of the same decline, a terminal state or a passed deadline, the version,
// and the state, and then stores the decline.
// and the state, and then stores the decline. A delivered invitation whose
// deadline has passed is expired first, so the late decline answers with the
// expired state.
func (service *Service) declineFound(
ctx context.Context,
unit outbound.UnitOfWork,
@@ -166,8 +184,16 @@ func (service *Service) declineFound(
request declineRequest,
entry outbound.CandidateInvitationEntry,
) (declineOutcome, error) {
now, err := service.now(declineLocation)
if err != nil {
return declineOutcome{}, err
}
entry, err = settleCandidateEntry(ctx, unit, entry, now)
if err != nil {
return declineOutcome{}, fmt.Errorf("%s - expire due invitation: %w", declineLocation, err)
}
current := entry.Invitation
at := notBefore(request.now, current.UpdatedAt())
at := notBefore(now, current.UpdatedAt())
if current.State() == domain.InvitationDeclined {
previous, err := unit.Invitations().GetDecline(ctx, current.OrganizationID(), current.ID())
if err != nil {