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
@@ -11,6 +11,60 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
const closeActiveContactGrants = `-- name: CloseActiveContactGrants :execrows
|
||||
WITH targets AS (
|
||||
SELECT contact_grants.id
|
||||
FROM contact_grants
|
||||
WHERE
|
||||
contact_grants.candidate_id = $3
|
||||
AND (
|
||||
$4::uuid IS NULL
|
||||
OR contact_grants.organization_id = $4::uuid
|
||||
)
|
||||
AND (
|
||||
$5::uuid [] IS NULL
|
||||
OR contact_grants.consent_record_id = ANY($5::uuid [])
|
||||
)
|
||||
AND contact_grants.revoked_at IS NULL
|
||||
ORDER BY contact_grants.id
|
||||
FOR NO KEY UPDATE
|
||||
)
|
||||
UPDATE contact_grants
|
||||
SET
|
||||
revoked_at = greatest(contact_grants.granted_at, $1::timestamptz),
|
||||
revocation_reason = $2
|
||||
FROM targets
|
||||
WHERE contact_grants.id = targets.id
|
||||
`
|
||||
|
||||
type CloseActiveContactGrantsParams struct {
|
||||
RevokedAt pgtype.Timestamptz
|
||||
RevocationReason pgtype.Text
|
||||
CandidateID pgtype.UUID
|
||||
OrganizationID pgtype.UUID
|
||||
ConsentRecordIds []pgtype.UUID
|
||||
}
|
||||
|
||||
// Closes the active grants of one candidate with one reason. A nil organization
|
||||
// closes every organization of the candidate, and a non-nil list of consent
|
||||
// records closes only the grants that those records gave. The grant rows are
|
||||
// locked in identifier order, so two closures that overlap take their locks in
|
||||
// the same order. The revocation time never precedes the grant time, which the
|
||||
// grant checks. A grant that is already closed keeps its first stamp.
|
||||
func (q *Queries) CloseActiveContactGrants(ctx context.Context, arg CloseActiveContactGrantsParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, closeActiveContactGrants,
|
||||
arg.RevokedAt,
|
||||
arg.RevocationReason,
|
||||
arg.CandidateID,
|
||||
arg.OrganizationID,
|
||||
arg.ConsentRecordIds,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getContactGrantByInvitation = `-- name: GetContactGrantByInvitation :one
|
||||
SELECT
|
||||
id,
|
||||
@@ -109,3 +163,72 @@ func (q *Queries) InsertContactGrant(ctx context.Context, arg InsertContactGrant
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const lockContactGrant = `-- name: LockContactGrant :one
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
candidate_id,
|
||||
source_invitation_id,
|
||||
consent_record_id,
|
||||
terms_version,
|
||||
fields,
|
||||
consent_text_version,
|
||||
granted_at,
|
||||
revoked_at,
|
||||
revocation_reason
|
||||
FROM contact_grants
|
||||
WHERE id = $1
|
||||
FOR SHARE
|
||||
`
|
||||
|
||||
// Share-locks one grant for the duration of a contact read and returns its
|
||||
// stored state. The caller decides whether the state still allows the read.
|
||||
func (q *Queries) LockContactGrant(ctx context.Context, id pgtype.UUID) (ContactGrant, error) {
|
||||
row := q.db.QueryRow(ctx, lockContactGrant, id)
|
||||
var i ContactGrant
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.OrganizationID,
|
||||
&i.CandidateID,
|
||||
&i.SourceInvitationID,
|
||||
&i.ConsentRecordID,
|
||||
&i.TermsVersion,
|
||||
&i.Fields,
|
||||
&i.ConsentTextVersion,
|
||||
&i.GrantedAt,
|
||||
&i.RevokedAt,
|
||||
&i.RevocationReason,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const lockCurrentContactConsentRecord = `-- name: LockCurrentContactConsentRecord :one
|
||||
SELECT consent_records.id
|
||||
FROM consent_records
|
||||
WHERE
|
||||
consent_records.id = $1
|
||||
AND consent_records.subject_id = $2
|
||||
AND consent_records.organization_id = $3
|
||||
AND consent_records.purpose = 'c4'
|
||||
AND consent_records.revoked_at IS NULL
|
||||
FOR SHARE
|
||||
`
|
||||
|
||||
type LockCurrentContactConsentRecordParams struct {
|
||||
ConsentRecordID pgtype.UUID
|
||||
CandidateID pgtype.UUID
|
||||
OrganizationID pgtype.UUID
|
||||
}
|
||||
|
||||
// Share-locks the consent C4 record of a grant for the duration of a contact
|
||||
// read. The read locks the record before the grant, the order that a C4
|
||||
// revocation follows when it stamps the record and then closes its grants. No
|
||||
// row means the record is revoked or belongs to another scope, which closes
|
||||
// the read.
|
||||
func (q *Queries) LockCurrentContactConsentRecord(ctx context.Context, arg LockCurrentContactConsentRecordParams) (pgtype.UUID, error) {
|
||||
row := q.db.QueryRow(ctx, lockCurrentContactConsentRecord, arg.ConsentRecordID, arg.CandidateID, arg.OrganizationID)
|
||||
var id pgtype.UUID
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
Reference in new issue
Block a user