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
@@ -0,0 +1,188 @@
|
||||
-- +goose Up
|
||||
-- A notification names the record it is about (its subject), so the sending
|
||||
-- worker can re-check the rights the notice depends on before it calls the
|
||||
-- transport. The subject is fixed when the request is stored and is copied to
|
||||
-- the delivery, where the content trigger keeps it unchanged. The three
|
||||
-- columns are set together. Only the kind invitation exists so far. A
|
||||
-- notification without a subject stays valid: the sending worker cancels it
|
||||
-- with source_closed, because it has no right to check.
|
||||
--
|
||||
-- Everything here is additive: existing rows keep NULL subjects.
|
||||
ALTER TABLE mail_requests
|
||||
ADD COLUMN subject_kind text,
|
||||
ADD COLUMN subject_id uuid,
|
||||
ADD COLUMN subject_organization_id uuid,
|
||||
ADD CONSTRAINT mail_requests_subject_complete CHECK (
|
||||
(subject_kind IS NULL) = (subject_id IS NULL)
|
||||
AND (subject_kind IS NULL) = (subject_organization_id IS NULL)
|
||||
AND (subject_kind IS NULL OR subject_kind IN ('invitation'))
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN mail_requests.subject_kind IS
|
||||
'The kind of record the notification is about, NULL when it names none; the sending worker checks the rights of that kind.';
|
||||
COMMENT ON COLUMN mail_requests.subject_id IS
|
||||
'The record the notification is about, for example the invitation; an opaque ID, never an address or a contact.';
|
||||
COMMENT ON COLUMN mail_requests.subject_organization_id IS
|
||||
'The organization the subject record belongs to, for example the organization that sent the invitation.';
|
||||
|
||||
ALTER TABLE deliveries
|
||||
ADD COLUMN subject_kind text,
|
||||
ADD COLUMN subject_id uuid,
|
||||
ADD COLUMN subject_organization_id uuid,
|
||||
ADD CONSTRAINT deliveries_subject_complete CHECK (
|
||||
(subject_kind IS NULL) = (subject_id IS NULL)
|
||||
AND (subject_kind IS NULL) = (subject_organization_id IS NULL)
|
||||
AND (subject_kind IS NULL OR subject_kind IN ('invitation'))
|
||||
AND (subject_kind IS NULL OR kind = 'notification')
|
||||
);
|
||||
|
||||
COMMENT ON COLUMN deliveries.subject_kind IS
|
||||
'The kind of record a notification is about; set only for a notification, NULL when it names none. The product sending worker re-checks its rights before the send.';
|
||||
COMMENT ON COLUMN deliveries.subject_id IS
|
||||
'The record a notification is about; an opaque ID, never an address or a contact.';
|
||||
COMMENT ON COLUMN deliveries.subject_organization_id IS
|
||||
'The organization the subject record belongs to.';
|
||||
|
||||
-- The subject is part of what the delivery sends and is fixed, like the
|
||||
-- recipient and the content. This is the function of migration 00012 with the
|
||||
-- three subject columns added to the unchanged tuple.
|
||||
-- +goose StatementBegin
|
||||
CREATE OR REPLACE FUNCTION deliveries_refuse_content_change() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF (
|
||||
NEW.id, NEW.source_event_id, NEW.recipient_user_id,
|
||||
NEW.recipient_email, NEW.kind, NEW.template_version, NEW.subject,
|
||||
NEW.content_sha256,
|
||||
NEW.idempotency_key, NEW.created_at, NEW.email_token_id,
|
||||
NEW.subject_kind, NEW.subject_id, NEW.subject_organization_id
|
||||
) IS DISTINCT FROM (
|
||||
OLD.id, OLD.source_event_id, OLD.recipient_user_id,
|
||||
OLD.recipient_email, OLD.kind, OLD.template_version, OLD.subject,
|
||||
OLD.content_sha256,
|
||||
OLD.idempotency_key, OLD.created_at, OLD.email_token_id,
|
||||
OLD.subject_kind, OLD.subject_id, OLD.subject_organization_id
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'deliveries_content_immutable',
|
||||
TABLE = 'deliveries',
|
||||
MESSAGE = 'delivery content and recipient are immutable';
|
||||
END IF;
|
||||
IF (NEW.text_body, NEW.html_body) IS DISTINCT FROM (OLD.text_body, OLD.html_body) THEN
|
||||
IF NOT (
|
||||
OLD.body_redacted_at IS NULL AND NEW.body_redacted_at IS NOT NULL
|
||||
AND NEW.text_body = '' AND NEW.html_body = ''
|
||||
AND NEW.status IN ('accepted', 'rejected', 'cancelled')
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'deliveries_content_immutable',
|
||||
TABLE = 'deliveries',
|
||||
MESSAGE = 'delivery content and recipient are immutable';
|
||||
END IF;
|
||||
ELSIF OLD.body_redacted_at IS DISTINCT FROM NEW.body_redacted_at THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'deliveries_content_immutable',
|
||||
TABLE = 'deliveries',
|
||||
MESSAGE = 'delivery content and recipient are immutable';
|
||||
END IF;
|
||||
IF (
|
||||
(NEW.provider, NEW.provider_account)
|
||||
IS DISTINCT FROM (OLD.provider, OLD.provider_account)
|
||||
) AND (OLD.attempt_count <> 0 OR NEW.attempt_count <> 0) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'deliveries_provider_pinned',
|
||||
TABLE = 'deliveries',
|
||||
MESSAGE = 'the provider account of a delivery is fixed after its first attempt';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- Subjects name records whose rights a later version checks; dropping them
|
||||
-- would send notices that were not checked. Refuse while any row names one.
|
||||
-- +goose StatementBegin
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (SELECT 1 FROM deliveries WHERE subject_kind IS NOT NULL)
|
||||
OR EXISTS (SELECT 1 FROM mail_requests WHERE subject_kind IS NOT NULL) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'feature_not_supported',
|
||||
MESSAGE = 'notification subjects are stored; cancel or export the notifications that name one before this rollback';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- The function of migration 00012 without the subject columns.
|
||||
-- +goose StatementBegin
|
||||
CREATE OR REPLACE FUNCTION deliveries_refuse_content_change() RETURNS trigger
|
||||
LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF (
|
||||
NEW.id, NEW.source_event_id, NEW.recipient_user_id,
|
||||
NEW.recipient_email, NEW.kind, NEW.template_version, NEW.subject,
|
||||
NEW.content_sha256,
|
||||
NEW.idempotency_key, NEW.created_at, NEW.email_token_id
|
||||
) IS DISTINCT FROM (
|
||||
OLD.id, OLD.source_event_id, OLD.recipient_user_id,
|
||||
OLD.recipient_email, OLD.kind, OLD.template_version, OLD.subject,
|
||||
OLD.content_sha256,
|
||||
OLD.idempotency_key, OLD.created_at, OLD.email_token_id
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'deliveries_content_immutable',
|
||||
TABLE = 'deliveries',
|
||||
MESSAGE = 'delivery content and recipient are immutable';
|
||||
END IF;
|
||||
IF (NEW.text_body, NEW.html_body) IS DISTINCT FROM (OLD.text_body, OLD.html_body) THEN
|
||||
IF NOT (
|
||||
OLD.body_redacted_at IS NULL AND NEW.body_redacted_at IS NOT NULL
|
||||
AND NEW.text_body = '' AND NEW.html_body = ''
|
||||
AND NEW.status IN ('accepted', 'rejected', 'cancelled')
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'deliveries_content_immutable',
|
||||
TABLE = 'deliveries',
|
||||
MESSAGE = 'delivery content and recipient are immutable';
|
||||
END IF;
|
||||
ELSIF OLD.body_redacted_at IS DISTINCT FROM NEW.body_redacted_at THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'deliveries_content_immutable',
|
||||
TABLE = 'deliveries',
|
||||
MESSAGE = 'delivery content and recipient are immutable';
|
||||
END IF;
|
||||
IF (
|
||||
(NEW.provider, NEW.provider_account)
|
||||
IS DISTINCT FROM (OLD.provider, OLD.provider_account)
|
||||
) AND (OLD.attempt_count <> 0 OR NEW.attempt_count <> 0) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'integrity_constraint_violation',
|
||||
CONSTRAINT = 'deliveries_provider_pinned',
|
||||
TABLE = 'deliveries',
|
||||
MESSAGE = 'the provider account of a delivery is fixed after its first attempt';
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
ALTER TABLE deliveries DROP CONSTRAINT deliveries_subject_complete;
|
||||
ALTER TABLE deliveries
|
||||
DROP COLUMN subject_organization_id,
|
||||
DROP COLUMN subject_id,
|
||||
DROP COLUMN subject_kind;
|
||||
|
||||
ALTER TABLE mail_requests DROP CONSTRAINT mail_requests_subject_complete;
|
||||
ALTER TABLE mail_requests
|
||||
DROP COLUMN subject_organization_id,
|
||||
DROP COLUMN subject_id,
|
||||
DROP COLUMN subject_kind;
|
||||
@@ -0,0 +1,111 @@
|
||||
-- +goose Up
|
||||
-- Product notifications get their own sending worker. It claims them through
|
||||
-- deliveries_product_due_idx, re-checks the rights of their subject before
|
||||
-- the transport is called, and cancels a notice whose basis no longer holds
|
||||
-- with one of the reasons below. A cancelled notification loses its bodies,
|
||||
-- as a token mail does, so a withdrawn notice keeps no text in the table.
|
||||
-- The service worker and deliveries_due_idx do not change.
|
||||
|
||||
ALTER TABLE deliveries DROP CONSTRAINT deliveries_cancel_reason_matches_status;
|
||||
ALTER TABLE deliveries ADD CONSTRAINT deliveries_cancel_reason_matches_status CHECK (
|
||||
(status = 'cancelled') = (cancel_reason IS NOT NULL)
|
||||
AND (
|
||||
cancel_reason IS NULL
|
||||
OR cancel_reason IN (
|
||||
'token_missing',
|
||||
'token_purpose_mismatch',
|
||||
'token_owner_mismatch',
|
||||
'token_consumed',
|
||||
'token_revoked',
|
||||
'token_expired',
|
||||
'account_not_current',
|
||||
'recipient_mismatch',
|
||||
'content_invalid',
|
||||
'processing_failed',
|
||||
'rights_revoked',
|
||||
'recipient_ineligible',
|
||||
'source_closed'
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
-- A redacted mail has no bodies. A token mail may be redacted once it is
|
||||
-- terminal; a notification once it is cancelled.
|
||||
ALTER TABLE deliveries DROP CONSTRAINT deliveries_body_present;
|
||||
ALTER TABLE deliveries ADD CONSTRAINT deliveries_body_present CHECK (
|
||||
(body_redacted_at IS NULL AND (text_body <> '' OR html_body <> ''))
|
||||
OR (
|
||||
body_redacted_at IS NOT NULL
|
||||
AND text_body = '' AND html_body = ''
|
||||
AND (
|
||||
(
|
||||
kind IN ('verification', 'password_reset')
|
||||
AND status IN ('accepted', 'rejected', 'cancelled')
|
||||
)
|
||||
OR (kind = 'notification' AND status = 'cancelled')
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
-- Serves the claim of due product notifications. The predicate names the
|
||||
-- kind literal the product claim repeats, so an idle poll of either worker
|
||||
-- reads only its own index and never the other lane's backlog.
|
||||
CREATE INDEX deliveries_product_due_idx
|
||||
ON deliveries (next_attempt_at, id)
|
||||
WHERE
|
||||
status IN ('pending', 'waiting_quota', 'unknown')
|
||||
AND next_attempt_at IS NOT NULL
|
||||
AND kind = 'notification';
|
||||
|
||||
-- +goose Down
|
||||
DROP INDEX deliveries_product_due_idx;
|
||||
|
||||
-- Rows that name the reasons of this version, or redacted notifications,
|
||||
-- cannot satisfy the checks of the earlier schema. Refuse before changing
|
||||
-- them, so the rollback never rewrites a cancellation.
|
||||
-- +goose StatementBegin
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM deliveries
|
||||
WHERE cancel_reason IN ('rights_revoked', 'recipient_ineligible', 'source_closed')
|
||||
OR (kind = 'notification' AND body_redacted_at IS NOT NULL)
|
||||
) THEN
|
||||
RAISE EXCEPTION USING
|
||||
ERRCODE = 'feature_not_supported',
|
||||
MESSAGE = 'notifications cancelled by a rights check are stored; export or resend them before this rollback';
|
||||
END IF;
|
||||
END;
|
||||
$$;
|
||||
-- +goose StatementEnd
|
||||
|
||||
ALTER TABLE deliveries DROP CONSTRAINT deliveries_body_present;
|
||||
ALTER TABLE deliveries ADD CONSTRAINT deliveries_body_present CHECK (
|
||||
(body_redacted_at IS NULL AND (text_body <> '' OR html_body <> ''))
|
||||
OR (
|
||||
body_redacted_at IS NOT NULL
|
||||
AND text_body = '' AND html_body = ''
|
||||
AND kind IN ('verification', 'password_reset')
|
||||
AND status IN ('accepted', 'rejected', 'cancelled')
|
||||
)
|
||||
);
|
||||
|
||||
ALTER TABLE deliveries DROP CONSTRAINT deliveries_cancel_reason_matches_status;
|
||||
ALTER TABLE deliveries ADD CONSTRAINT deliveries_cancel_reason_matches_status CHECK (
|
||||
(status = 'cancelled') = (cancel_reason IS NOT NULL)
|
||||
AND (
|
||||
cancel_reason IS NULL
|
||||
OR cancel_reason IN (
|
||||
'token_missing',
|
||||
'token_purpose_mismatch',
|
||||
'token_owner_mismatch',
|
||||
'token_consumed',
|
||||
'token_revoked',
|
||||
'token_expired',
|
||||
'account_not_current',
|
||||
'recipient_mismatch',
|
||||
'content_invalid',
|
||||
'processing_failed'
|
||||
)
|
||||
)
|
||||
);
|
||||
+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