feat(notifications): send product notifications with a rights check right before each send
This commit is contained in:
1 parent
a856066316
commit
623d41b969
59 files changed
+4125
-1126
No files matched your search
+129
-10
@@ -24,6 +24,9 @@ INSERT INTO deliveries (
|
||||
last_attempt_at,
|
||||
provider_message_id,
|
||||
email_token_id,
|
||||
subject_kind,
|
||||
subject_id,
|
||||
subject_organization_id,
|
||||
version,
|
||||
created_at,
|
||||
updated_at
|
||||
@@ -47,6 +50,9 @@ INSERT INTO deliveries (
|
||||
sqlc.narg(last_attempt_at),
|
||||
sqlc.narg(provider_message_id),
|
||||
sqlc.narg(email_token_id),
|
||||
sqlc.narg(subject_kind),
|
||||
sqlc.narg(subject_id),
|
||||
sqlc.narg(subject_organization_id),
|
||||
sqlc.arg(version),
|
||||
sqlc.arg(created_at),
|
||||
sqlc.arg(updated_at)
|
||||
@@ -82,7 +88,10 @@ SELECT
|
||||
claimed_until,
|
||||
first_send_started_at,
|
||||
failure_count,
|
||||
body_redacted_at
|
||||
body_redacted_at,
|
||||
subject_kind,
|
||||
subject_id,
|
||||
subject_organization_id
|
||||
FROM deliveries
|
||||
WHERE id = sqlc.arg(id);
|
||||
|
||||
@@ -209,21 +218,21 @@ SET
|
||||
text_body = CASE
|
||||
WHEN
|
||||
sqlc.arg(status)::text = 'cancelled'
|
||||
AND deliveries.kind IN ('verification', 'password_reset')
|
||||
AND deliveries.kind IN ('verification', 'password_reset', 'notification')
|
||||
THEN ''
|
||||
ELSE deliveries.text_body
|
||||
END,
|
||||
html_body = CASE
|
||||
WHEN
|
||||
sqlc.arg(status)::text = 'cancelled'
|
||||
AND deliveries.kind IN ('verification', 'password_reset')
|
||||
AND deliveries.kind IN ('verification', 'password_reset', 'notification')
|
||||
THEN ''
|
||||
ELSE deliveries.html_body
|
||||
END,
|
||||
body_redacted_at = CASE
|
||||
WHEN
|
||||
sqlc.arg(status)::text = 'cancelled'
|
||||
AND deliveries.kind IN ('verification', 'password_reset')
|
||||
AND deliveries.kind IN ('verification', 'password_reset', 'notification')
|
||||
THEN sqlc.arg(updated_at)
|
||||
ELSE deliveries.body_redacted_at
|
||||
END,
|
||||
@@ -338,6 +347,107 @@ RETURNING
|
||||
claimed.version,
|
||||
claimed.created_at,
|
||||
claimed.updated_at,
|
||||
claimed.subject_kind,
|
||||
claimed.subject_id,
|
||||
claimed.subject_organization_id,
|
||||
candidate.previous_claimed_until,
|
||||
candidate.since_first_send_seconds,
|
||||
candidate.manual_retry_pending,
|
||||
candidate.manual_retry_cycle_started,
|
||||
candidate.send_start_unsettled,
|
||||
candidate.has_uncertain_attempt;
|
||||
|
||||
-- Claims the most overdue product notification, under the same rules as
|
||||
-- ClaimDueDelivery: a lease in the database clock, the configured provider
|
||||
-- account or a rebind of a delivery before its first attempt, and no live
|
||||
-- lease. It reads deliveries_product_due_idx, whose predicate repeats the
|
||||
-- literal kind below, so the backlog of service mail never enters the scan.
|
||||
-- A notification is claimed here whatever its subject; the sending worker
|
||||
-- checks the subject's rights after the claim and cancels a notice whose
|
||||
-- basis no longer holds.
|
||||
-- name: ClaimDueProductDelivery :one
|
||||
WITH candidate AS (
|
||||
SELECT
|
||||
due.id,
|
||||
due.claimed_until AS previous_claimed_until,
|
||||
coalesce(
|
||||
extract(EPOCH FROM clock_timestamp() - due.first_send_started_at),
|
||||
-1
|
||||
)::float8 AS since_first_send_seconds,
|
||||
(due.manual_retry_authorized_at IS NOT NULL AND due.manual_retry_started_at IS NULL)
|
||||
AS manual_retry_pending,
|
||||
(due.manual_retry_started_at IS NOT NULL)::boolean AS manual_retry_cycle_started,
|
||||
(
|
||||
due.send_started_attempt_count IS NOT NULL
|
||||
AND due.send_started_attempt_count = due.attempt_count
|
||||
)::boolean AS send_start_unsettled,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM delivery_attempts AS prior_attempt
|
||||
WHERE prior_attempt.delivery_id = due.id
|
||||
AND prior_attempt.outcome = 'unknown'
|
||||
)::boolean AS has_uncertain_attempt
|
||||
FROM deliveries AS due
|
||||
WHERE
|
||||
due.kind = 'notification'
|
||||
AND due.status IN ('pending', 'waiting_quota', 'unknown')
|
||||
AND due.next_attempt_at IS NOT NULL
|
||||
AND due.next_attempt_at <= sqlc.arg(now)::timestamptz
|
||||
AND (
|
||||
(
|
||||
due.provider = sqlc.arg(provider)
|
||||
AND due.provider_account = sqlc.arg(provider_account)
|
||||
)
|
||||
OR (
|
||||
due.provider = sqlc.arg(rebind_provider)::text
|
||||
AND due.attempt_count = 0
|
||||
)
|
||||
)
|
||||
AND (
|
||||
due.claim_owner IS NULL
|
||||
OR due.claimed_until <= clock_timestamp()
|
||||
)
|
||||
ORDER BY due.next_attempt_at, due.id
|
||||
LIMIT 1
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
|
||||
UPDATE deliveries AS claimed
|
||||
SET
|
||||
claim_owner = sqlc.arg(lease_owner)::uuid,
|
||||
claimed_until = clock_timestamp()
|
||||
+ make_interval(secs => sqlc.arg(lease_seconds)::float8)
|
||||
FROM candidate
|
||||
WHERE claimed.id = candidate.id
|
||||
RETURNING
|
||||
claimed.id,
|
||||
claimed.source_event_id,
|
||||
claimed.recipient_user_id,
|
||||
claimed.recipient_email,
|
||||
claimed.kind,
|
||||
claimed.template_version,
|
||||
claimed.subject,
|
||||
claimed.text_body,
|
||||
claimed.html_body,
|
||||
claimed.content_sha256,
|
||||
claimed.provider,
|
||||
claimed.provider_account,
|
||||
claimed.idempotency_key,
|
||||
claimed.status,
|
||||
claimed.attempt_count,
|
||||
claimed.next_attempt_at,
|
||||
claimed.last_attempt_at,
|
||||
claimed.provider_message_id,
|
||||
claimed.email_token_id,
|
||||
claimed.cancel_reason,
|
||||
claimed.body_redacted_at,
|
||||
claimed.failure_count,
|
||||
claimed.version,
|
||||
claimed.created_at,
|
||||
claimed.updated_at,
|
||||
claimed.subject_kind,
|
||||
claimed.subject_id,
|
||||
claimed.subject_organization_id,
|
||||
candidate.previous_claimed_until,
|
||||
candidate.since_first_send_seconds,
|
||||
candidate.manual_retry_pending,
|
||||
@@ -492,14 +602,14 @@ SET
|
||||
claim_owner = NULL,
|
||||
claimed_until = NULL,
|
||||
text_body = CASE
|
||||
WHEN deliveries.kind IN ('verification', 'password_reset') THEN '' ELSE deliveries.text_body
|
||||
WHEN deliveries.kind IN ('verification', 'password_reset', 'notification') THEN '' ELSE deliveries.text_body
|
||||
END,
|
||||
html_body = CASE
|
||||
WHEN deliveries.kind IN ('verification', 'password_reset') THEN '' ELSE deliveries.html_body
|
||||
WHEN deliveries.kind IN ('verification', 'password_reset', 'notification') THEN '' ELSE deliveries.html_body
|
||||
END,
|
||||
body_redacted_at = CASE
|
||||
WHEN
|
||||
deliveries.kind IN ('verification', 'password_reset')
|
||||
deliveries.kind IN ('verification', 'password_reset', 'notification')
|
||||
THEN greatest(deliveries.updated_at, sqlc.arg(now)::timestamptz)
|
||||
ELSE deliveries.body_redacted_at
|
||||
END,
|
||||
@@ -552,7 +662,7 @@ SET
|
||||
WHEN
|
||||
sqlc.arg(count_failure)::boolean
|
||||
AND deliveries.failure_count + 1 >= sqlc.arg(max_failures)::integer
|
||||
AND deliveries.kind IN ('verification', 'password_reset')
|
||||
AND deliveries.kind IN ('verification', 'password_reset', 'notification')
|
||||
THEN ''
|
||||
ELSE deliveries.text_body
|
||||
END,
|
||||
@@ -560,7 +670,7 @@ SET
|
||||
WHEN
|
||||
sqlc.arg(count_failure)::boolean
|
||||
AND deliveries.failure_count + 1 >= sqlc.arg(max_failures)::integer
|
||||
AND deliveries.kind IN ('verification', 'password_reset')
|
||||
AND deliveries.kind IN ('verification', 'password_reset', 'notification')
|
||||
THEN ''
|
||||
ELSE deliveries.html_body
|
||||
END,
|
||||
@@ -568,7 +678,7 @@ SET
|
||||
WHEN
|
||||
sqlc.arg(count_failure)::boolean
|
||||
AND deliveries.failure_count + 1 >= sqlc.arg(max_failures)::integer
|
||||
AND deliveries.kind IN ('verification', 'password_reset')
|
||||
AND deliveries.kind IN ('verification', 'password_reset', 'notification')
|
||||
THEN greatest(deliveries.updated_at, sqlc.arg(now)::timestamptz)
|
||||
ELSE deliveries.body_redacted_at
|
||||
END,
|
||||
@@ -692,6 +802,9 @@ INSERT INTO mail_requests (
|
||||
subject,
|
||||
body,
|
||||
link_path,
|
||||
subject_kind,
|
||||
subject_id,
|
||||
subject_organization_id,
|
||||
created_at
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
@@ -700,6 +813,9 @@ INSERT INTO mail_requests (
|
||||
sqlc.arg(subject),
|
||||
sqlc.arg(body),
|
||||
sqlc.arg(link_path),
|
||||
sqlc.narg(subject_kind),
|
||||
sqlc.narg(subject_id),
|
||||
sqlc.narg(subject_organization_id),
|
||||
sqlc.arg(created_at)
|
||||
);
|
||||
|
||||
@@ -716,6 +832,9 @@ SELECT
|
||||
subject,
|
||||
body,
|
||||
link_path,
|
||||
subject_kind,
|
||||
subject_id,
|
||||
subject_organization_id,
|
||||
created_at
|
||||
FROM mail_requests
|
||||
WHERE id = sqlc.arg(id)
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
-- Reads the standing of an invitation notice in one statement, for the rights
|
||||
-- check before the send. The recipient is matched by ID; the invitation must
|
||||
-- be addressed to that recipient and belong to the organization of the
|
||||
-- subject, otherwise it is not found. It reads no address and no contact:
|
||||
-- the recipient address is returned only as its comparison key. The hiding
|
||||
-- is the candidate's own current state. The publication is read through the
|
||||
-- effective view, so a publication whose C2 consent was revoked does not count
|
||||
-- as current. A missing account or invitation gives false flags and NULL
|
||||
-- columns.
|
||||
-- name: GetInvitationNoticeStanding :one
|
||||
SELECT
|
||||
(recipient.id IS NOT NULL)::boolean AS recipient_found,
|
||||
coalesce(recipient.email_verified_at IS NOT NULL, false)::boolean AS recipient_email_verified,
|
||||
coalesce(recipient.email_normalized, '')::text AS recipient_email_key,
|
||||
(invitation.id IS NOT NULL)::boolean AS invitation_found,
|
||||
invitation.organization_id AS invitation_organization_id,
|
||||
coalesce(invitation.state, '')::text AS invitation_state,
|
||||
invitation.response_deadline_at AS invitation_response_deadline_at,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM candidate_hidden_organizations AS hidden
|
||||
WHERE
|
||||
hidden.user_id = sqlc.arg(recipient_user_id)
|
||||
AND hidden.organization_id = sqlc.arg(organization_id)
|
||||
)::boolean AS organization_hidden,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM candidate_effective_publications AS publication
|
||||
WHERE publication.user_id = sqlc.arg(recipient_user_id)
|
||||
)::boolean AS publication_published
|
||||
FROM (SELECT sqlc.arg(recipient_user_id)::uuid AS user_id) AS target
|
||||
LEFT JOIN users AS recipient
|
||||
ON recipient.id = target.user_id
|
||||
LEFT JOIN invitations AS invitation
|
||||
ON invitation.id = sqlc.arg(invitation_id)
|
||||
AND invitation.candidate_id = target.user_id
|
||||
AND invitation.organization_id = sqlc.arg(organization_id);
|
||||
Reference in new issue
Block a user