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

@@ -207,3 +207,48 @@ func storedContactGrantRow(row sqlc.ContactGrant) (domain.StoredContactGrant, er
}
return stored, nil
}
// LockForRead locks the C4 record before the grant, the order of the consent
// revocation, so a read and a revocation of the same consent cannot wait for
// each other in opposite orders. The locked grant is restored through the
// domain like GetByInvitation, and it must still name the grant that was read.
func (repository contactGrantRepository) LockForRead(
ctx context.Context,
grant domain.ContactGrant,
) (domain.ContactGrant, bool, error) {
const location = "contactGrantRepository.LockForRead"
if grant.IsZero() {
return domain.ContactGrant{}, false, fmt.Errorf("%s - grant must be built by the domain: %w", location, domain.ErrInvalidContactGrant)
}
consentCurrent := true
_, err := repository.queries.LockCurrentContactConsentRecord(ctx, sqlc.LockCurrentContactConsentRecordParams{
ConsentRecordID: pgUUID(grant.ConsentRecordID()), CandidateID: pgUUID(grant.CandidateID()),
OrganizationID: pgUUID(grant.OrganizationID()),
})
switch {
case errors.Is(err, pgx.ErrNoRows):
consentCurrent = false
case err != nil:
return domain.ContactGrant{}, false, fmt.Errorf("%s - lock consent C4 record: %w", location, mapInvitationError(err))
}
row, err := repository.queries.LockContactGrant(ctx, pgUUID(grant.ID()))
if errors.Is(err, pgx.ErrNoRows) {
return domain.ContactGrant{}, false, fmt.Errorf("%s - lock contact grant: %w", location, outbound.ErrContactGrantNotFound)
}
if err != nil {
return domain.ContactGrant{}, false, fmt.Errorf("%s - lock contact grant: %w", location, mapInvitationError(err))
}
stored, err := storedContactGrantRow(row)
if err != nil {
return domain.ContactGrant{}, false, fmt.Errorf("%s - read stored grant: %w", location, err)
}
current, err := domain.RestoreContactGrant(stored)
if err != nil {
return domain.ContactGrant{}, false, fmt.Errorf("%s - restore contact grant: %w", location, err)
}
if current.OrganizationID() != grant.OrganizationID() || current.CandidateID() != grant.CandidateID() ||
current.InvitationID() != grant.InvitationID() || current.ConsentRecordID() != grant.ConsentRecordID() {
return domain.ContactGrant{}, false, fmt.Errorf("%s - locked grant has another scope: %w", location, domain.ErrInvalidContactGrant)
}
return current, consentCurrent, nil
}