2020-10-24 14:30:29 +00:00
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- subscribers
|
|
|
|
-- name: get-subscriber
|
2021-02-13 12:25:10 +00:00
|
|
|
-- Get a single subscriber by id or UUID or email.
|
|
|
|
SELECT * FROM subscribers WHERE
|
|
|
|
CASE
|
|
|
|
WHEN $1 > 0 THEN id = $1
|
|
|
|
WHEN $2 != '' THEN uuid = $2::UUID
|
|
|
|
WHEN $3 != '' THEN email = $3
|
|
|
|
END;
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2018-10-29 09:50:49 +00:00
|
|
|
-- name: get-subscribers-by-emails
|
|
|
|
-- Get subscribers by emails.
|
|
|
|
SELECT * FROM subscribers WHERE email=ANY($1);
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- name: get-subscriber-lists
|
2019-12-01 12:18:36 +00:00
|
|
|
WITH sub AS (
|
|
|
|
SELECT id FROM subscribers WHERE CASE WHEN $1 > 0 THEN id = $1 ELSE uuid = $2 END
|
|
|
|
)
|
|
|
|
SELECT * FROM lists
|
|
|
|
LEFT JOIN subscriber_lists ON (lists.id = subscriber_lists.list_id)
|
|
|
|
WHERE subscriber_id = (SELECT id FROM sub)
|
2020-02-09 10:55:19 +00:00
|
|
|
-- Optional list IDs or UUIDs to filter.
|
2022-04-03 15:24:40 +00:00
|
|
|
AND (CASE WHEN CARDINALITY($3::INT[]) > 0 THEN id = ANY($3::INT[])
|
|
|
|
WHEN CARDINALITY($4::UUID[]) > 0 THEN uuid = ANY($4::UUID[])
|
2020-02-09 10:55:19 +00:00
|
|
|
ELSE TRUE
|
|
|
|
END)
|
2022-10-18 16:14:57 +00:00
|
|
|
AND (CASE WHEN $5 != '' THEN subscriber_lists.status = $5::subscription_status ELSE TRUE END)
|
2021-09-25 12:35:15 +00:00
|
|
|
AND (CASE WHEN $6 != '' THEN lists.optin = $6::list_optin ELSE TRUE END)
|
|
|
|
ORDER BY id;
|
2019-12-01 12:18:36 +00:00
|
|
|
|
|
|
|
-- name: get-subscriber-lists-lazy
|
2019-04-01 11:37:24 +00:00
|
|
|
-- Get lists associations of subscribers given a list of subscriber IDs.
|
|
|
|
-- This query is used to lazy load given a list of subscriber IDs.
|
|
|
|
-- The query returns results in the same order as the given subscriber IDs, and for non-existent subscriber IDs,
|
|
|
|
-- the query still returns a row with 0 values. Thus, for lazy loading, the application simply iterate on the results in
|
|
|
|
-- the same order as the list of campaigns it would've queried and attach the results.
|
|
|
|
WITH subs AS (
|
|
|
|
SELECT subscriber_id, JSON_AGG(
|
|
|
|
ROW_TO_JSON(
|
2022-11-09 16:10:11 +00:00
|
|
|
(SELECT l FROM (
|
|
|
|
SELECT
|
|
|
|
subscriber_lists.status AS subscription_status,
|
|
|
|
subscriber_lists.created_at AS subscription_created_at,
|
|
|
|
subscriber_lists.updated_at AS subscription_updated_at,
|
2023-07-22 07:58:45 +00:00
|
|
|
subscriber_lists.meta AS subscription_meta,
|
2022-11-09 16:10:11 +00:00
|
|
|
lists.*
|
|
|
|
) l)
|
2019-04-01 11:37:24 +00:00
|
|
|
)
|
|
|
|
) AS lists FROM lists
|
2018-10-25 13:51:47 +00:00
|
|
|
LEFT JOIN subscriber_lists ON (subscriber_lists.list_id = lists.id)
|
2019-04-01 11:37:24 +00:00
|
|
|
WHERE subscriber_lists.subscriber_id = ANY($1)
|
|
|
|
GROUP BY subscriber_id
|
|
|
|
)
|
|
|
|
SELECT id as subscriber_id,
|
|
|
|
COALESCE(s.lists, '[]') AS lists
|
|
|
|
FROM (SELECT id FROM UNNEST($1) AS id) x
|
2019-05-08 07:50:27 +00:00
|
|
|
LEFT JOIN subs AS s ON (s.subscriber_id = id)
|
|
|
|
ORDER BY ARRAY_POSITION($1, id);
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2022-10-18 16:14:57 +00:00
|
|
|
-- name: get-subscriptions
|
|
|
|
-- Retrieves all lists a subscriber is attached to.
|
|
|
|
-- if $3 is set to true, all lists are fetched including the subscriber's subscriptions.
|
|
|
|
-- subscription_status, and subscription_created_at are null in that case.
|
|
|
|
WITH sub AS (
|
|
|
|
SELECT id FROM subscribers WHERE CASE WHEN $1 > 0 THEN id = $1 ELSE uuid = $2 END
|
|
|
|
)
|
2023-07-22 07:58:45 +00:00
|
|
|
SELECT lists.*,
|
|
|
|
subscriber_lists.status as subscription_status,
|
|
|
|
subscriber_lists.created_at as subscription_created_at,
|
|
|
|
subscriber_lists.meta as subscription_meta
|
2022-10-18 16:14:57 +00:00
|
|
|
FROM lists LEFT JOIN subscriber_lists
|
|
|
|
ON (subscriber_lists.list_id = lists.id AND subscriber_lists.subscriber_id = (SELECT id FROM sub))
|
|
|
|
WHERE CASE WHEN $3 = TRUE THEN TRUE ELSE subscriber_lists.status IS NOT NULL END
|
|
|
|
ORDER BY subscriber_lists.status;
|
|
|
|
|
2018-11-05 13:45:29 +00:00
|
|
|
-- name: insert-subscriber
|
|
|
|
WITH sub AS (
|
2018-11-26 13:31:18 +00:00
|
|
|
INSERT INTO subscribers (uuid, email, name, status, attribs)
|
|
|
|
VALUES($1, $2, $3, $4, $5)
|
2022-07-30 14:41:59 +00:00
|
|
|
RETURNING id, status
|
2018-11-05 13:45:29 +00:00
|
|
|
),
|
2020-03-07 14:49:22 +00:00
|
|
|
listIDs AS (
|
|
|
|
SELECT id FROM lists WHERE
|
2022-04-03 15:24:40 +00:00
|
|
|
(CASE WHEN CARDINALITY($6::INT[]) > 0 THEN id=ANY($6)
|
2020-03-07 14:49:22 +00:00
|
|
|
ELSE uuid=ANY($7::UUID[]) END)
|
|
|
|
),
|
2018-11-05 13:45:29 +00:00
|
|
|
subs AS (
|
2018-11-26 13:31:18 +00:00
|
|
|
INSERT INTO subscriber_lists (subscriber_id, list_id, status)
|
|
|
|
VALUES(
|
|
|
|
(SELECT id FROM sub),
|
2020-03-07 14:49:22 +00:00
|
|
|
UNNEST(ARRAY(SELECT id FROM listIDs)),
|
2021-04-17 08:04:37 +00:00
|
|
|
(CASE WHEN $4='blocklisted' THEN 'unsubscribed'::subscription_status ELSE $8::subscription_status END)
|
2018-11-26 13:31:18 +00:00
|
|
|
)
|
2018-11-05 13:45:29 +00:00
|
|
|
ON CONFLICT (subscriber_id, list_id) DO UPDATE
|
2022-01-30 17:38:39 +00:00
|
|
|
SET updated_at=NOW(),
|
|
|
|
status=(
|
|
|
|
CASE WHEN $4='blocklisted' OR (SELECT status FROM sub)='blocklisted'
|
|
|
|
THEN 'unsubscribed'::subscription_status
|
|
|
|
ELSE $8::subscription_status END
|
|
|
|
)
|
2018-11-05 13:45:29 +00:00
|
|
|
)
|
|
|
|
SELECT id from sub;
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- name: upsert-subscriber
|
2018-11-05 11:29:09 +00:00
|
|
|
-- Upserts a subscriber where existing subscribers get their names and attributes overwritten.
|
2021-06-02 17:08:12 +00:00
|
|
|
-- If $7 = true, update values, otherwise, skip.
|
2018-11-05 13:45:29 +00:00
|
|
|
WITH sub AS (
|
2020-09-02 06:10:34 +00:00
|
|
|
INSERT INTO subscribers as s (uuid, email, name, attribs, status)
|
|
|
|
VALUES($1, $2, $3, $4, 'enabled')
|
2020-07-05 16:05:17 +00:00
|
|
|
ON CONFLICT (email)
|
|
|
|
DO UPDATE SET
|
2021-06-02 17:08:12 +00:00
|
|
|
name=(CASE WHEN $7 THEN $3 ELSE s.name END),
|
|
|
|
attribs=(CASE WHEN $7 THEN $4 ELSE s.attribs END),
|
2018-11-05 11:29:09 +00:00
|
|
|
updated_at=NOW()
|
2018-11-05 13:45:29 +00:00
|
|
|
RETURNING uuid, id
|
|
|
|
),
|
|
|
|
subs AS (
|
2021-06-02 17:08:12 +00:00
|
|
|
INSERT INTO subscriber_lists (subscriber_id, list_id, status)
|
|
|
|
VALUES((SELECT id FROM sub), UNNEST($5::INT[]), $6)
|
2018-11-05 11:29:09 +00:00
|
|
|
ON CONFLICT (subscriber_id, list_id) DO UPDATE
|
2021-06-06 12:01:29 +00:00
|
|
|
SET updated_at=NOW(), status=(CASE WHEN $7 THEN $6 ELSE subscriber_lists.status END)
|
2018-11-05 13:45:29 +00:00
|
|
|
)
|
|
|
|
SELECT uuid, id from sub;
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2020-08-01 11:15:29 +00:00
|
|
|
-- name: upsert-blocklist-subscriber
|
|
|
|
-- Upserts a subscriber where the update will only set the status to blocklisted
|
2018-11-05 11:29:09 +00:00
|
|
|
-- unlike upsert-subscribers where name and attributes are updated. In addition, all
|
|
|
|
-- existing subscriptions are marked as 'unsubscribed'.
|
2018-12-18 05:24:55 +00:00
|
|
|
-- This is used in the bulk importer.
|
2018-11-05 11:29:09 +00:00
|
|
|
WITH sub AS (
|
|
|
|
INSERT INTO subscribers (uuid, email, name, attribs, status)
|
2020-08-01 11:15:29 +00:00
|
|
|
VALUES($1, $2, $3, $4, 'blocklisted')
|
|
|
|
ON CONFLICT (email) DO UPDATE SET status='blocklisted', updated_at=NOW()
|
2018-11-05 11:29:09 +00:00
|
|
|
RETURNING id
|
|
|
|
)
|
|
|
|
UPDATE subscriber_lists SET status='unsubscribed', updated_at=NOW()
|
|
|
|
WHERE subscriber_id = (SELECT id FROM sub);
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- name: update-subscriber
|
2022-10-18 16:14:57 +00:00
|
|
|
UPDATE subscribers SET
|
|
|
|
email=(CASE WHEN $2 != '' THEN $2 ELSE email END),
|
|
|
|
name=(CASE WHEN $3 != '' THEN $3 ELSE name END),
|
|
|
|
status=(CASE WHEN $4 != '' THEN $4::subscriber_status ELSE status END),
|
|
|
|
attribs=(CASE WHEN $5 != '' THEN $5::JSONB ELSE attribs END),
|
|
|
|
updated_at=NOW()
|
|
|
|
WHERE id = $1;
|
|
|
|
|
|
|
|
-- name: update-subscriber-with-lists
|
2018-10-25 13:51:47 +00:00
|
|
|
-- Updates a subscriber's data, and given a list of list_ids, inserts subscriptions
|
|
|
|
-- for them while deleting existing subscriptions not in the list.
|
|
|
|
WITH s AS (
|
|
|
|
UPDATE subscribers SET
|
|
|
|
email=(CASE WHEN $2 != '' THEN $2 ELSE email END),
|
|
|
|
name=(CASE WHEN $3 != '' THEN $3 ELSE name END),
|
|
|
|
status=(CASE WHEN $4 != '' THEN $4::subscriber_status ELSE status END),
|
2021-03-10 15:50:26 +00:00
|
|
|
attribs=(CASE WHEN $5 != '' THEN $5::JSONB ELSE attribs END),
|
2018-10-25 13:51:47 +00:00
|
|
|
updated_at=NOW()
|
|
|
|
WHERE id = $1 RETURNING id
|
|
|
|
),
|
2022-07-30 14:41:59 +00:00
|
|
|
listIDs AS (
|
|
|
|
SELECT id FROM lists WHERE
|
|
|
|
(CASE WHEN CARDINALITY($6::INT[]) > 0 THEN id=ANY($6)
|
|
|
|
ELSE uuid=ANY($7::UUID[]) END)
|
2022-10-18 16:14:57 +00:00
|
|
|
),
|
|
|
|
d AS (
|
|
|
|
DELETE FROM subscriber_lists WHERE $9 = TRUE AND subscriber_id = $1 AND list_id != ALL(SELECT id FROM listIDs)
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
2018-11-26 13:31:18 +00:00
|
|
|
INSERT INTO subscriber_lists (subscriber_id, list_id, status)
|
|
|
|
VALUES(
|
|
|
|
(SELECT id FROM s),
|
2022-07-30 14:41:59 +00:00
|
|
|
UNNEST(ARRAY(SELECT id FROM listIDs)),
|
|
|
|
(CASE WHEN $4='blocklisted' THEN 'unsubscribed'::subscription_status ELSE $8::subscription_status END)
|
2018-11-26 13:31:18 +00:00
|
|
|
)
|
|
|
|
ON CONFLICT (subscriber_id, list_id) DO UPDATE
|
2023-07-12 17:39:10 +00:00
|
|
|
SET status = (
|
|
|
|
CASE
|
|
|
|
WHEN $4='blocklisted' THEN 'unsubscribed'::subscription_status
|
|
|
|
-- When subscriber is edited from the admin form, retain the status. Otherwise, a blocklisted
|
|
|
|
-- subscriber when being re-enabled, their subscription statuses change.
|
2023-11-11 13:16:38 +00:00
|
|
|
WHEN subscriber_lists.status = 'confirmed' THEN 'confirmed'
|
2023-07-12 17:39:10 +00:00
|
|
|
WHEN $9 = TRUE THEN subscriber_lists.status
|
|
|
|
ELSE $8::subscription_status
|
|
|
|
END
|
|
|
|
);
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
-- name: delete-subscribers
|
2019-07-20 07:24:30 +00:00
|
|
|
-- Delete one or more subscribers by ID or UUID.
|
|
|
|
DELETE FROM subscribers WHERE CASE WHEN ARRAY_LENGTH($1::INT[], 1) > 0 THEN id = ANY($1) ELSE uuid = ANY($2::UUID[]) END;
|
2018-12-18 05:24:55 +00:00
|
|
|
|
2022-09-03 07:48:02 +00:00
|
|
|
-- name: delete-blocklisted-subscribers
|
|
|
|
DELETE FROM subscribers WHERE status = 'blocklisted';
|
|
|
|
|
|
|
|
-- name: delete-orphan-subscribers
|
|
|
|
DELETE FROM subscribers a WHERE NOT EXISTS
|
|
|
|
(SELECT 1 FROM subscriber_lists b WHERE b.subscriber_id = a.id);
|
|
|
|
|
2020-08-01 11:15:29 +00:00
|
|
|
-- name: blocklist-subscribers
|
2018-12-18 05:24:55 +00:00
|
|
|
WITH b AS (
|
2020-08-01 11:15:29 +00:00
|
|
|
UPDATE subscribers SET status='blocklisted', updated_at=NOW()
|
2018-12-18 05:24:55 +00:00
|
|
|
WHERE id = ANY($1::INT[])
|
|
|
|
)
|
|
|
|
UPDATE subscriber_lists SET status='unsubscribed', updated_at=NOW()
|
|
|
|
WHERE subscriber_id = ANY($1::INT[]);
|
|
|
|
|
|
|
|
-- name: add-subscribers-to-lists
|
2022-02-04 18:33:28 +00:00
|
|
|
INSERT INTO subscriber_lists (subscriber_id, list_id, status)
|
|
|
|
(SELECT a, b, (CASE WHEN $3 != '' THEN $3::subscription_status ELSE 'unconfirmed' END) FROM UNNEST($1::INT[]) a, UNNEST($2::INT[]) b)
|
|
|
|
ON CONFLICT (subscriber_id, list_id) DO UPDATE SET status=(CASE WHEN $3 != '' THEN $3::subscription_status ELSE subscriber_lists.status END);
|
2018-12-18 05:24:55 +00:00
|
|
|
|
|
|
|
-- name: delete-subscriptions
|
|
|
|
DELETE FROM subscriber_lists
|
|
|
|
WHERE (subscriber_id, list_id) = ANY(SELECT a, b FROM UNNEST($1::INT[]) a, UNNEST($2::INT[]) b);
|
|
|
|
|
2019-12-01 12:18:36 +00:00
|
|
|
-- name: confirm-subscription-optin
|
|
|
|
WITH subID AS (
|
|
|
|
SELECT id FROM subscribers WHERE uuid = $1::UUID
|
|
|
|
),
|
|
|
|
listIDs AS (
|
|
|
|
SELECT id FROM lists WHERE uuid = ANY($2::UUID[])
|
|
|
|
)
|
2023-07-22 07:58:45 +00:00
|
|
|
UPDATE subscriber_lists SET status='confirmed', meta=meta || $3, updated_at=NOW()
|
2019-12-01 12:18:36 +00:00
|
|
|
WHERE subscriber_id = (SELECT id FROM subID) AND list_id = ANY(SELECT id FROM listIDs);
|
|
|
|
|
2018-12-18 05:24:55 +00:00
|
|
|
-- name: unsubscribe-subscribers-from-lists
|
2022-10-18 16:14:57 +00:00
|
|
|
WITH listIDs AS (
|
|
|
|
SELECT ARRAY(
|
|
|
|
SELECT id FROM lists WHERE
|
|
|
|
(CASE WHEN CARDINALITY($2::INT[]) > 0 THEN id=ANY($2) ELSE uuid=ANY($3::UUID[]) END)
|
|
|
|
) id
|
|
|
|
)
|
2018-12-18 05:24:55 +00:00
|
|
|
UPDATE subscriber_lists SET status='unsubscribed', updated_at=NOW()
|
2022-10-18 16:14:57 +00:00
|
|
|
WHERE (subscriber_id, list_id) = ANY(SELECT a, b FROM UNNEST($1::INT[]) a, UNNEST((SELECT id FROM listIDs)) b);
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2022-05-08 09:15:45 +00:00
|
|
|
-- name: unsubscribe-by-campaign
|
2018-10-25 13:51:47 +00:00
|
|
|
-- Unsubscribes a subscriber given a campaign UUID (from all the lists in the campaign) and the subscriber UUID.
|
2020-08-01 11:15:29 +00:00
|
|
|
-- If $3 is TRUE, then all subscriptions of the subscriber is blocklisted
|
2018-10-25 13:51:47 +00:00
|
|
|
-- and all existing subscriptions, irrespective of lists, unsubscribed.
|
|
|
|
WITH lists AS (
|
2018-10-30 05:47:50 +00:00
|
|
|
SELECT list_id FROM campaign_lists
|
|
|
|
LEFT JOIN campaigns ON (campaign_lists.campaign_id = campaigns.id)
|
|
|
|
WHERE campaigns.uuid = $1
|
2018-10-25 13:51:47 +00:00
|
|
|
),
|
|
|
|
sub AS (
|
2020-08-01 11:15:29 +00:00
|
|
|
UPDATE subscribers SET status = (CASE WHEN $3 IS TRUE THEN 'blocklisted' ELSE status END)
|
2018-10-25 13:51:47 +00:00
|
|
|
WHERE uuid = $2 RETURNING id
|
|
|
|
)
|
2022-11-01 16:16:07 +00:00
|
|
|
UPDATE subscriber_lists SET status = 'unsubscribed', updated_at=NOW() WHERE
|
2018-10-31 12:54:21 +00:00
|
|
|
subscriber_id = (SELECT id FROM sub) AND status != 'unsubscribed' AND
|
2018-10-30 05:47:50 +00:00
|
|
|
-- If $3 is false, unsubscribe from the campaign's lists, otherwise all lists.
|
2018-10-25 13:51:47 +00:00
|
|
|
CASE WHEN $3 IS FALSE THEN list_id = ANY(SELECT list_id FROM lists) ELSE list_id != 0 END;
|
|
|
|
|
2022-09-03 07:48:02 +00:00
|
|
|
-- name: delete-unconfirmed-subscriptions
|
|
|
|
WITH optins AS (
|
|
|
|
SELECT id FROM lists WHERE optin = 'double'
|
|
|
|
)
|
|
|
|
DELETE FROM subscriber_lists
|
|
|
|
WHERE status = 'unconfirmed' AND list_id IN (SELECT id FROM optins) AND created_at < $1;
|
|
|
|
|
2019-07-21 13:48:41 +00:00
|
|
|
-- privacy
|
|
|
|
-- name: export-subscriber-data
|
|
|
|
WITH prof AS (
|
2020-03-08 09:06:50 +00:00
|
|
|
SELECT id, uuid, email, name, attribs, status, created_at, updated_at FROM subscribers WHERE
|
2019-07-21 13:48:41 +00:00
|
|
|
CASE WHEN $1 > 0 THEN id = $1 ELSE uuid = $2 END
|
|
|
|
),
|
|
|
|
subs AS (
|
2020-03-08 09:06:50 +00:00
|
|
|
SELECT subscriber_lists.status AS subscription_status,
|
|
|
|
(CASE WHEN lists.type = 'private' THEN 'Private list' ELSE lists.name END) as name,
|
|
|
|
lists.type, subscriber_lists.created_at
|
|
|
|
FROM lists
|
2019-07-21 13:48:41 +00:00
|
|
|
LEFT JOIN subscriber_lists ON (subscriber_lists.list_id = lists.id)
|
|
|
|
WHERE subscriber_lists.subscriber_id = (SELECT id FROM prof)
|
|
|
|
),
|
|
|
|
views AS (
|
2020-03-08 09:06:50 +00:00
|
|
|
SELECT subject as campaign, COUNT(subscriber_id) as views FROM campaign_views
|
|
|
|
LEFT JOIN campaigns ON (campaigns.id = campaign_views.campaign_id)
|
|
|
|
WHERE subscriber_id = (SELECT id FROM prof)
|
2021-09-17 14:25:38 +00:00
|
|
|
GROUP BY campaigns.id ORDER BY campaigns.id
|
2019-07-21 13:48:41 +00:00
|
|
|
),
|
|
|
|
clicks AS (
|
2020-03-08 09:06:50 +00:00
|
|
|
SELECT url, COUNT(subscriber_id) as clicks FROM link_clicks
|
2019-07-21 13:48:41 +00:00
|
|
|
LEFT JOIN links ON (links.id = link_clicks.link_id)
|
|
|
|
WHERE subscriber_id = (SELECT id FROM prof)
|
2021-09-17 14:25:38 +00:00
|
|
|
GROUP BY links.id ORDER BY links.id
|
2019-07-21 13:48:41 +00:00
|
|
|
)
|
|
|
|
SELECT (SELECT email FROM prof) as email,
|
2020-03-08 09:06:50 +00:00
|
|
|
COALESCE((SELECT JSON_AGG(t) FROM prof t), '{}') AS profile,
|
|
|
|
COALESCE((SELECT JSON_AGG(t) FROM subs t), '[]') AS subscriptions,
|
|
|
|
COALESCE((SELECT JSON_AGG(t) FROM views t), '[]') AS campaign_views,
|
|
|
|
COALESCE((SELECT JSON_AGG(t) FROM clicks t), '[]') AS link_clicks;
|
2019-07-21 13:48:41 +00:00
|
|
|
|
2018-12-18 05:24:55 +00:00
|
|
|
-- Partial and RAW queries used to construct arbitrary subscriber
|
|
|
|
-- queries for segmentation follow.
|
|
|
|
|
|
|
|
-- name: query-subscribers
|
|
|
|
-- raw: true
|
|
|
|
-- Unprepared statement for issuring arbitrary WHERE conditions for
|
|
|
|
-- searching subscribers. While the results are sliced using offset+limit,
|
|
|
|
-- there's a COUNT() OVER() that still returns the total result count
|
|
|
|
-- for pagination in the frontend, albeit being a field that'll repeat
|
|
|
|
-- with every resultant row.
|
2020-07-05 13:53:45 +00:00
|
|
|
-- %s = arbitrary expression, %s = order by field, %s = order direction
|
2021-09-18 10:16:22 +00:00
|
|
|
SELECT subscribers.* FROM subscribers
|
2018-12-18 05:24:55 +00:00
|
|
|
LEFT JOIN subscriber_lists
|
|
|
|
ON (
|
|
|
|
-- Optional list filtering.
|
|
|
|
(CASE WHEN CARDINALITY($1::INT[]) > 0 THEN true ELSE false END)
|
|
|
|
AND subscriber_lists.subscriber_id = subscribers.id
|
|
|
|
)
|
2021-11-29 15:08:57 +00:00
|
|
|
WHERE (CARDINALITY($1) = 0 OR subscriber_lists.list_id = ANY($1::INT[]))
|
2022-12-25 08:34:43 +00:00
|
|
|
%query%
|
|
|
|
ORDER BY %order% OFFSET $2 LIMIT (CASE WHEN $3 < 1 THEN NULL ELSE $3 END);
|
2018-12-18 05:24:55 +00:00
|
|
|
|
2021-09-18 10:16:22 +00:00
|
|
|
-- name: query-subscribers-count
|
|
|
|
-- Replica of query-subscribers for obtaining the results count.
|
|
|
|
SELECT COUNT(*) AS total FROM subscribers
|
|
|
|
LEFT JOIN subscriber_lists
|
|
|
|
ON (
|
|
|
|
-- Optional list filtering.
|
|
|
|
(CASE WHEN CARDINALITY($1::INT[]) > 0 THEN true ELSE false END)
|
|
|
|
AND subscriber_lists.subscriber_id = subscribers.id
|
|
|
|
)
|
2021-11-29 15:08:57 +00:00
|
|
|
WHERE (CARDINALITY($1) = 0 OR subscriber_lists.list_id = ANY($1::INT[])) %s;
|
2021-09-18 10:16:22 +00:00
|
|
|
|
2021-01-23 12:53:29 +00:00
|
|
|
-- name: query-subscribers-for-export
|
|
|
|
-- raw: true
|
|
|
|
-- Unprepared statement for issuring arbitrary WHERE conditions for
|
|
|
|
-- searching subscribers to do bulk CSV export.
|
|
|
|
-- %s = arbitrary expression
|
2021-07-25 17:12:54 +00:00
|
|
|
SELECT subscribers.id,
|
|
|
|
subscribers.uuid,
|
|
|
|
subscribers.email,
|
|
|
|
subscribers.name,
|
|
|
|
subscribers.status,
|
|
|
|
subscribers.attribs,
|
|
|
|
subscribers.created_at,
|
|
|
|
subscribers.updated_at
|
|
|
|
FROM subscribers
|
2023-03-19 07:23:05 +00:00
|
|
|
LEFT JOIN subscriber_lists
|
2021-01-23 12:53:29 +00:00
|
|
|
ON (
|
|
|
|
-- Optional list filtering.
|
|
|
|
(CASE WHEN CARDINALITY($1::INT[]) > 0 THEN true ELSE false END)
|
2023-03-19 07:23:05 +00:00
|
|
|
AND subscriber_lists.subscriber_id = subscribers.id
|
2021-01-23 12:53:29 +00:00
|
|
|
)
|
2023-03-19 07:23:05 +00:00
|
|
|
WHERE subscriber_lists.list_id = ALL($1::INT[]) AND id > $2
|
2022-03-19 08:14:23 +00:00
|
|
|
AND (CASE WHEN CARDINALITY($3::INT[]) > 0 THEN id=ANY($3) ELSE true END)
|
2022-12-25 08:34:43 +00:00
|
|
|
%query%
|
2022-12-24 19:12:00 +00:00
|
|
|
ORDER BY subscribers.id ASC LIMIT (CASE WHEN $4 < 1 THEN NULL ELSE $4 END);
|
2021-01-23 12:53:29 +00:00
|
|
|
|
2018-12-18 05:24:55 +00:00
|
|
|
-- name: query-subscribers-template
|
|
|
|
-- raw: true
|
2020-08-01 11:15:29 +00:00
|
|
|
-- This raw query is reused in multiple queries (blocklist, add to list, delete)
|
2018-12-18 05:24:55 +00:00
|
|
|
-- etc., so it's kept has a raw template to be injected into other raw queries,
|
|
|
|
-- and for the same reason, it is not terminated with a semicolon.
|
|
|
|
--
|
|
|
|
-- All queries that embed this query should expect
|
|
|
|
-- $1=true/false (dry-run or not) and $2=[]INT (option list IDs).
|
|
|
|
-- That is, their positional arguments should start from $3.
|
|
|
|
SELECT subscribers.id FROM subscribers
|
|
|
|
LEFT JOIN subscriber_lists
|
|
|
|
ON (
|
|
|
|
-- Optional list filtering.
|
|
|
|
(CASE WHEN CARDINALITY($2::INT[]) > 0 THEN true ELSE false END)
|
|
|
|
AND subscriber_lists.subscriber_id = subscribers.id
|
|
|
|
)
|
|
|
|
WHERE subscriber_lists.list_id = ALL($2::INT[]) %s
|
|
|
|
LIMIT (CASE WHEN $1 THEN 1 END)
|
|
|
|
|
|
|
|
-- name: delete-subscribers-by-query
|
|
|
|
-- raw: true
|
|
|
|
WITH subs AS (%s)
|
|
|
|
DELETE FROM subscribers WHERE id=ANY(SELECT id FROM subs);
|
|
|
|
|
2020-08-01 11:15:29 +00:00
|
|
|
-- name: blocklist-subscribers-by-query
|
2018-10-25 13:51:47 +00:00
|
|
|
-- raw: true
|
2018-12-18 05:24:55 +00:00
|
|
|
WITH subs AS (%s),
|
|
|
|
b AS (
|
2020-08-01 11:15:29 +00:00
|
|
|
UPDATE subscribers SET status='blocklisted', updated_at=NOW()
|
2018-12-18 05:24:55 +00:00
|
|
|
WHERE id = ANY(SELECT id FROM subs)
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
2018-12-18 05:24:55 +00:00
|
|
|
UPDATE subscriber_lists SET status='unsubscribed', updated_at=NOW()
|
|
|
|
WHERE subscriber_id = ANY(SELECT id FROM subs);
|
|
|
|
|
|
|
|
-- name: add-subscribers-to-lists-by-query
|
|
|
|
-- raw: true
|
|
|
|
WITH subs AS (%s)
|
2018-10-25 13:51:47 +00:00
|
|
|
INSERT INTO subscriber_lists (subscriber_id, list_id)
|
2018-12-18 05:24:55 +00:00
|
|
|
(SELECT a, b FROM UNNEST(ARRAY(SELECT id FROM subs)) a, UNNEST($3::INT[]) b)
|
2018-10-25 13:51:47 +00:00
|
|
|
ON CONFLICT (subscriber_id, list_id) DO NOTHING;
|
|
|
|
|
2018-12-18 05:24:55 +00:00
|
|
|
-- name: delete-subscriptions-by-query
|
|
|
|
-- raw: true
|
|
|
|
WITH subs AS (%s)
|
|
|
|
DELETE FROM subscriber_lists
|
|
|
|
WHERE (subscriber_id, list_id) = ANY(SELECT a, b FROM UNNEST(ARRAY(SELECT id FROM subs)) a, UNNEST($3::INT[]) b);
|
|
|
|
|
|
|
|
-- name: unsubscribe-subscribers-from-lists-by-query
|
|
|
|
-- raw: true
|
|
|
|
WITH subs AS (%s)
|
|
|
|
UPDATE subscriber_lists SET status='unsubscribed', updated_at=NOW()
|
|
|
|
WHERE (subscriber_id, list_id) = ANY(SELECT a, b FROM UNNEST(ARRAY(SELECT id FROM subs)) a, UNNEST($3::INT[]) b);
|
|
|
|
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- lists
|
|
|
|
-- name: get-lists
|
2021-09-25 12:35:15 +00:00
|
|
|
SELECT * FROM lists WHERE (CASE WHEN $1 = '' THEN 1=1 ELSE type=$1::list_type END)
|
|
|
|
ORDER BY CASE WHEN $2 = 'id' THEN id END, CASE WHEN $2 = 'name' THEN name END;
|
2021-01-31 10:49:39 +00:00
|
|
|
|
|
|
|
-- name: query-lists
|
2020-11-21 11:59:24 +00:00
|
|
|
WITH ls AS (
|
|
|
|
SELECT COUNT(*) OVER () AS total, lists.* FROM lists
|
2022-04-03 15:24:40 +00:00
|
|
|
WHERE
|
|
|
|
CASE
|
|
|
|
WHEN $1 > 0 THEN id = $1
|
|
|
|
WHEN $2 != '' THEN uuid = $2::UUID
|
2022-11-27 17:31:15 +00:00
|
|
|
WHEN $3 != '' THEN to_tsvector(name) @@ to_tsquery ($3)
|
2022-04-03 15:24:40 +00:00
|
|
|
ELSE true
|
|
|
|
END
|
2022-12-24 19:12:00 +00:00
|
|
|
OFFSET $4 LIMIT (CASE WHEN $5 < 1 THEN NULL ELSE $5 END)
|
2020-11-21 11:59:24 +00:00
|
|
|
),
|
|
|
|
counts AS (
|
2023-11-11 16:56:25 +00:00
|
|
|
SELECT list_id, JSON_OBJECT_AGG(status, num) AS subscriber_statuses, SUM(num) AS subscriber_count
|
|
|
|
FROM (
|
|
|
|
SELECT list_id, status, COUNT(*) as num
|
|
|
|
FROM subscriber_lists
|
2022-02-02 18:33:31 +00:00
|
|
|
WHERE ($1 = 0 OR list_id = $1)
|
|
|
|
GROUP BY list_id, status
|
2023-11-11 16:56:25 +00:00
|
|
|
) AS subquery GROUP BY list_id
|
2020-11-21 11:59:24 +00:00
|
|
|
)
|
2022-02-02 18:33:31 +00:00
|
|
|
SELECT ls.*, subscriber_statuses FROM ls
|
2022-12-25 08:34:43 +00:00
|
|
|
LEFT JOIN counts ON (counts.list_id = ls.id) ORDER BY %order%;
|
2020-11-21 11:59:24 +00:00
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-12-01 12:18:36 +00:00
|
|
|
-- name: get-lists-by-optin
|
2020-02-03 07:48:26 +00:00
|
|
|
-- Can have a list of IDs or a list of UUIDs.
|
2020-03-07 14:49:22 +00:00
|
|
|
SELECT * FROM lists WHERE (CASE WHEN $1 != '' THEN optin=$1::list_optin ELSE TRUE END) AND
|
2020-02-03 07:48:26 +00:00
|
|
|
(CASE WHEN $2::INT[] IS NOT NULL THEN id = ANY($2::INT[])
|
|
|
|
WHEN $3::UUID[] IS NOT NULL THEN uuid = ANY($3::UUID[])
|
|
|
|
END) ORDER BY name;
|
2019-12-01 12:18:36 +00:00
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- name: create-list
|
2022-11-01 15:29:21 +00:00
|
|
|
INSERT INTO lists (uuid, name, type, optin, tags, description) VALUES($1, $2, $3, $4, $5, $6) RETURNING id;
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
-- name: update-list
|
|
|
|
UPDATE lists SET
|
|
|
|
name=(CASE WHEN $2 != '' THEN $2 ELSE name END),
|
|
|
|
type=(CASE WHEN $3 != '' THEN $3::list_type ELSE type END),
|
2019-12-01 12:18:36 +00:00
|
|
|
optin=(CASE WHEN $4 != '' THEN $4::list_optin ELSE optin END),
|
2020-08-01 07:39:52 +00:00
|
|
|
tags=$5::VARCHAR(100)[],
|
2022-11-01 15:29:21 +00:00
|
|
|
description=(CASE WHEN $6 != '' THEN $6 ELSE description END),
|
2018-10-25 13:51:47 +00:00
|
|
|
updated_at=NOW()
|
|
|
|
WHERE id = $1;
|
|
|
|
|
2019-05-14 16:36:14 +00:00
|
|
|
-- name: update-lists-date
|
|
|
|
UPDATE lists SET updated_at=NOW() WHERE id = ANY($1);
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- name: delete-lists
|
|
|
|
DELETE FROM lists WHERE id = ALL($1);
|
|
|
|
|
|
|
|
|
|
|
|
-- campaigns
|
|
|
|
-- name: create-campaign
|
|
|
|
-- This creates the campaign and inserts campaign_lists relationships.
|
2019-12-01 12:18:36 +00:00
|
|
|
WITH campLists AS (
|
|
|
|
-- Get the list_ids and their optin statuses for the campaigns found in the previous step.
|
2021-09-17 14:25:38 +00:00
|
|
|
SELECT lists.id AS list_id, campaign_id, optin FROM lists
|
2019-12-01 12:18:36 +00:00
|
|
|
INNER JOIN campaign_lists ON (campaign_lists.list_id = lists.id)
|
2022-01-04 16:46:21 +00:00
|
|
|
WHERE lists.id = ANY($14::INT[])
|
2020-02-03 07:38:00 +00:00
|
|
|
),
|
|
|
|
tpl AS (
|
2023-08-06 04:36:44 +00:00
|
|
|
-- If there's no template_id given, use the default template.
|
2022-01-04 16:46:21 +00:00
|
|
|
SELECT (CASE WHEN $13 = 0 THEN id ELSE $13 END) AS id FROM templates WHERE is_default IS TRUE
|
2019-12-01 12:18:36 +00:00
|
|
|
),
|
|
|
|
counts AS (
|
2018-11-01 12:20:03 +00:00
|
|
|
SELECT COALESCE(COUNT(id), 0) as to_send, COALESCE(MAX(id), 0) as max_sub_id
|
2018-10-25 13:51:47 +00:00
|
|
|
FROM subscribers
|
2022-01-04 16:46:21 +00:00
|
|
|
LEFT JOIN campLists ON (campLists.campaign_id = ANY($14::INT[]))
|
2019-12-01 12:18:36 +00:00
|
|
|
LEFT JOIN subscriber_lists ON (
|
|
|
|
subscriber_lists.status != 'unsubscribed' AND
|
|
|
|
subscribers.id = subscriber_lists.subscriber_id AND
|
|
|
|
subscriber_lists.list_id = campLists.list_id AND
|
|
|
|
|
|
|
|
-- For double opt-in lists, consider only 'confirmed' subscriptions. For single opt-ins,
|
|
|
|
-- any status except for 'unsubscribed' (already excluded above) works.
|
|
|
|
(CASE WHEN campLists.optin = 'double' THEN subscriber_lists.status = 'confirmed' ELSE true END)
|
|
|
|
)
|
2022-01-04 16:46:21 +00:00
|
|
|
WHERE subscriber_lists.list_id=ANY($14::INT[])
|
2018-10-25 13:51:47 +00:00
|
|
|
AND subscribers.status='enabled'
|
|
|
|
),
|
|
|
|
camp AS (
|
2022-11-03 05:37:26 +00:00
|
|
|
INSERT INTO campaigns (uuid, type, name, subject, from_email, body, altbody, content_type, send_at, headers, tags, messenger, template_id, to_send, max_subscriber_id, archive, archive_template_id, archive_meta)
|
2023-05-18 11:25:59 +00:00
|
|
|
SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12,
|
|
|
|
(SELECT id FROM tpl), (SELECT to_send FROM counts),
|
|
|
|
(SELECT max_sub_id FROM counts), $15,
|
|
|
|
(CASE WHEN $16 = 0 THEN (SELECT id FROM tpl) ELSE $16 END), $17
|
2018-10-25 13:51:47 +00:00
|
|
|
RETURNING id
|
2023-05-18 11:25:59 +00:00
|
|
|
),
|
|
|
|
med AS (
|
|
|
|
INSERT INTO campaign_media (campaign_id, media_id, filename)
|
|
|
|
(SELECT (SELECT id FROM camp), id, filename FROM media WHERE id=ANY($18::INT[]))
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
INSERT INTO campaign_lists (campaign_id, list_id, list_name)
|
2022-01-04 16:46:21 +00:00
|
|
|
(SELECT (SELECT id FROM camp), id, name FROM lists WHERE id=ANY($14::INT[]))
|
2018-10-25 13:51:47 +00:00
|
|
|
RETURNING (SELECT id FROM camp);
|
|
|
|
|
2019-03-30 07:01:24 +00:00
|
|
|
-- name: query-campaigns
|
2018-10-25 13:51:47 +00:00
|
|
|
-- Here, 'lists' is returned as an aggregated JSON array from campaign_lists because
|
|
|
|
-- the list reference may have been deleted.
|
2019-03-28 11:47:51 +00:00
|
|
|
-- While the results are sliced using offset+limit,
|
|
|
|
-- there's a COUNT() OVER() that still returns the total result count
|
|
|
|
-- for pagination in the frontend, albeit being a field that'll repeat
|
|
|
|
-- with every resultant row.
|
2021-01-30 09:29:21 +00:00
|
|
|
SELECT c.id, c.uuid, c.name, c.subject, c.from_email,
|
|
|
|
c.messenger, c.started_at, c.to_send, c.sent, c.type,
|
2022-01-04 16:46:21 +00:00
|
|
|
c.body, c.altbody, c.send_at, c.headers, c.status, c.content_type, c.tags,
|
2023-05-18 11:25:59 +00:00
|
|
|
c.template_id, c.archive, c.archive_template_id, c.archive_meta,
|
|
|
|
c.created_at, c.updated_at,
|
2020-10-24 14:30:29 +00:00
|
|
|
COUNT(*) OVER () AS total,
|
|
|
|
(
|
|
|
|
SELECT COALESCE(ARRAY_TO_JSON(ARRAY_AGG(l)), '[]') FROM (
|
|
|
|
SELECT COALESCE(campaign_lists.list_id, 0) AS id,
|
|
|
|
campaign_lists.list_name AS name
|
2021-01-30 09:29:21 +00:00
|
|
|
FROM campaign_lists WHERE campaign_lists.campaign_id = c.id
|
2019-04-23 17:06:20 +00:00
|
|
|
) l
|
|
|
|
) AS lists
|
2021-01-30 09:29:21 +00:00
|
|
|
FROM campaigns c
|
2019-04-01 11:37:24 +00:00
|
|
|
WHERE ($1 = 0 OR id = $1)
|
2022-04-03 15:24:40 +00:00
|
|
|
AND status=ANY(CASE WHEN CARDINALITY($2::campaign_status[]) != 0 THEN $2::campaign_status[] ELSE ARRAY[status] END)
|
2022-11-27 17:31:15 +00:00
|
|
|
AND ($3 = '' OR TO_TSVECTOR(CONCAT(name, ' ', subject)) @@ TO_TSQUERY($3))
|
2022-12-25 08:34:43 +00:00
|
|
|
ORDER BY %order% OFFSET $4 LIMIT (CASE WHEN $5 < 1 THEN NULL ELSE $5 END);
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-03-30 07:01:24 +00:00
|
|
|
-- name: get-campaign
|
2020-04-26 10:21:26 +00:00
|
|
|
SELECT campaigns.*,
|
|
|
|
COALESCE(templates.body, (SELECT body FROM templates WHERE is_default = true LIMIT 1)) AS template_body
|
|
|
|
FROM campaigns
|
2022-11-03 05:37:26 +00:00
|
|
|
LEFT JOIN templates ON (
|
|
|
|
CASE WHEN $3 = 'default' THEN templates.id = campaigns.template_id
|
|
|
|
ELSE templates.id = campaigns.archive_template_id END
|
|
|
|
)
|
2020-04-26 10:21:26 +00:00
|
|
|
WHERE CASE WHEN $1 > 0 THEN campaigns.id = $1 ELSE uuid = $2 END;
|
2019-04-01 11:37:24 +00:00
|
|
|
|
2022-11-03 05:37:26 +00:00
|
|
|
-- name: get-archived-campaigns
|
2023-04-08 04:09:10 +00:00
|
|
|
SELECT COUNT(*) OVER () AS total, campaigns.*,
|
|
|
|
COALESCE(templates.body, (SELECT body FROM templates WHERE is_default = true LIMIT 1)) AS template_body
|
|
|
|
FROM campaigns
|
|
|
|
LEFT JOIN templates ON (
|
|
|
|
CASE WHEN $3 = 'default' THEN templates.id = campaigns.template_id
|
|
|
|
ELSE templates.id = campaigns.archive_template_id END
|
|
|
|
)
|
|
|
|
WHERE campaigns.archive=true AND campaigns.type='regular' AND campaigns.status=ANY('{running, paused, finished}')
|
|
|
|
ORDER by campaigns.created_at DESC OFFSET $1 LIMIT $2;
|
2022-11-03 05:37:26 +00:00
|
|
|
|
2019-04-01 11:37:24 +00:00
|
|
|
-- name: get-campaign-stats
|
|
|
|
-- This query is used to lazy load campaign stats (views, counts, list of lists) given a list of campaign IDs.
|
|
|
|
-- The query returns results in the same order as the given campaign IDs, and for non-existent campaign IDs,
|
|
|
|
-- the query still returns a row with 0 values. Thus, for lazy loading, the application simply iterate on the results in
|
|
|
|
-- the same order as the list of campaigns it would've queried and attach the results.
|
|
|
|
WITH lists AS (
|
|
|
|
SELECT campaign_id, JSON_AGG(JSON_BUILD_OBJECT('id', list_id, 'name', list_name)) AS lists FROM campaign_lists
|
|
|
|
WHERE campaign_id = ANY($1) GROUP BY campaign_id
|
2023-05-18 11:25:59 +00:00
|
|
|
),
|
|
|
|
media AS (
|
|
|
|
SELECT campaign_id, JSON_AGG(JSON_BUILD_OBJECT('id', media_id, 'filename', filename)) AS media FROM campaign_media
|
|
|
|
WHERE campaign_id = ANY($1) GROUP BY campaign_id
|
|
|
|
),
|
|
|
|
views AS (
|
2019-03-30 07:01:24 +00:00
|
|
|
SELECT campaign_id, COUNT(campaign_id) as num FROM campaign_views
|
2019-04-01 11:37:24 +00:00
|
|
|
WHERE campaign_id = ANY($1)
|
2019-03-30 07:01:24 +00:00
|
|
|
GROUP BY campaign_id
|
|
|
|
),
|
|
|
|
clicks AS (
|
|
|
|
SELECT campaign_id, COUNT(campaign_id) as num FROM link_clicks
|
2019-04-01 11:37:24 +00:00
|
|
|
WHERE campaign_id = ANY($1)
|
2019-03-30 07:01:24 +00:00
|
|
|
GROUP BY campaign_id
|
2021-05-24 17:11:48 +00:00
|
|
|
),
|
|
|
|
bounces AS (
|
|
|
|
SELECT campaign_id, COUNT(campaign_id) as num FROM bounces
|
|
|
|
WHERE campaign_id = ANY($1)
|
|
|
|
GROUP BY campaign_id
|
2019-03-30 07:01:24 +00:00
|
|
|
)
|
2019-04-01 11:37:24 +00:00
|
|
|
SELECT id as campaign_id,
|
2019-03-30 07:01:24 +00:00
|
|
|
COALESCE(v.num, 0) AS views,
|
2019-04-01 11:37:24 +00:00
|
|
|
COALESCE(c.num, 0) AS clicks,
|
2021-05-24 17:11:48 +00:00
|
|
|
COALESCE(b.num, 0) AS bounces,
|
2023-05-18 11:25:59 +00:00
|
|
|
COALESCE(l.lists, '[]') AS lists,
|
|
|
|
COALESCE(m.media, '[]') AS media
|
2019-04-01 11:37:24 +00:00
|
|
|
FROM (SELECT id FROM UNNEST($1) AS id) x
|
|
|
|
LEFT JOIN lists AS l ON (l.campaign_id = id)
|
2023-05-18 11:25:59 +00:00
|
|
|
LEFT JOIN media AS m ON (m.campaign_id = id)
|
2019-04-01 11:37:24 +00:00
|
|
|
LEFT JOIN views AS v ON (v.campaign_id = id)
|
2019-05-08 07:50:27 +00:00
|
|
|
LEFT JOIN clicks AS c ON (c.campaign_id = id)
|
2021-05-24 17:11:48 +00:00
|
|
|
LEFT JOIN bounces AS b ON (b.campaign_id = id)
|
2019-05-08 07:50:27 +00:00
|
|
|
ORDER BY ARRAY_POSITION($1, id);
|
2019-03-30 07:01:24 +00:00
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- name: get-campaign-for-preview
|
2018-10-29 09:50:49 +00:00
|
|
|
SELECT campaigns.*, COALESCE(templates.body, (SELECT body FROM templates WHERE is_default = true LIMIT 1)) AS template_body,
|
|
|
|
(
|
|
|
|
SELECT COALESCE(ARRAY_TO_JSON(ARRAY_AGG(l)), '[]') FROM (
|
|
|
|
SELECT COALESCE(campaign_lists.list_id, 0) AS id,
|
|
|
|
campaign_lists.list_name AS name
|
|
|
|
FROM campaign_lists WHERE campaign_lists.campaign_id = campaigns.id
|
|
|
|
) l
|
|
|
|
) AS lists
|
2018-10-25 13:51:47 +00:00
|
|
|
FROM campaigns
|
2021-10-31 06:19:43 +00:00
|
|
|
LEFT JOIN templates ON (templates.id = (CASE WHEN $2=0 THEN campaigns.template_id ELSE $2 END))
|
2018-10-26 05:48:17 +00:00
|
|
|
WHERE campaigns.id = $1;
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2019-04-01 11:37:24 +00:00
|
|
|
-- name: get-campaign-status
|
2018-10-25 13:51:47 +00:00
|
|
|
SELECT id, status, to_send, sent, started_at, updated_at
|
|
|
|
FROM campaigns
|
|
|
|
WHERE status=$1;
|
|
|
|
|
|
|
|
-- name: next-campaigns
|
|
|
|
-- Retreives campaigns that are running (or scheduled and the time's up) and need
|
|
|
|
-- to be processed. It updates the to_send count and max_subscriber_id of the campaign,
|
|
|
|
-- that is, the total number of subscribers to be processed across all lists of a campaign.
|
|
|
|
-- Thus, it has a sideaffect.
|
|
|
|
-- In addition, it finds the max_subscriber_id, the upper limit across all lists of
|
|
|
|
-- a campaign. This is used to fetch and slice subscribers for the campaign in next-subscriber-campaigns.
|
|
|
|
WITH camps AS (
|
|
|
|
-- Get all running campaigns and their template bodies (if the template's deleted, the default template body instead)
|
|
|
|
SELECT campaigns.*, COALESCE(templates.body, (SELECT body FROM templates WHERE is_default = true LIMIT 1)) AS template_body
|
|
|
|
FROM campaigns
|
|
|
|
LEFT JOIN templates ON (templates.id = campaigns.template_id)
|
2020-03-08 09:59:53 +00:00
|
|
|
WHERE (status='running' OR (status='scheduled' AND NOW() >= campaigns.send_at))
|
2018-10-25 13:51:47 +00:00
|
|
|
AND NOT(campaigns.id = ANY($1::INT[]))
|
|
|
|
),
|
2019-12-01 12:18:36 +00:00
|
|
|
campLists AS (
|
|
|
|
-- Get the list_ids and their optin statuses for the campaigns found in the previous step.
|
2021-09-17 14:25:38 +00:00
|
|
|
SELECT lists.id AS list_id, campaign_id, optin FROM lists
|
2019-12-01 12:18:36 +00:00
|
|
|
INNER JOIN campaign_lists ON (campaign_lists.list_id = lists.id)
|
2018-11-01 12:21:29 +00:00
|
|
|
WHERE campaign_lists.campaign_id = ANY(SELECT id FROM camps)
|
2019-12-01 12:18:36 +00:00
|
|
|
),
|
2023-05-18 11:25:59 +00:00
|
|
|
campMedia AS (
|
|
|
|
-- Get the list_ids and their optin statuses for the campaigns found in the previous step.
|
|
|
|
SELECT campaign_id, ARRAY_AGG(campaign_media.media_id)::INT[] AS media_id FROM campaign_media
|
|
|
|
WHERE campaign_id = ANY(SELECT id FROM camps) AND media_id IS NOT NULL
|
|
|
|
GROUP BY campaign_id
|
|
|
|
),
|
2019-12-01 12:18:36 +00:00
|
|
|
counts AS (
|
|
|
|
-- For each campaign above, get the total number of subscribers and the max_subscriber_id
|
|
|
|
-- across all its lists.
|
|
|
|
SELECT id AS campaign_id,
|
|
|
|
COUNT(DISTINCT(subscriber_lists.subscriber_id)) AS to_send,
|
|
|
|
COALESCE(MAX(subscriber_lists.subscriber_id), 0) AS max_subscriber_id
|
|
|
|
FROM camps
|
|
|
|
LEFT JOIN campLists ON (campLists.campaign_id = camps.id)
|
|
|
|
LEFT JOIN subscriber_lists ON (
|
|
|
|
subscriber_lists.list_id = campLists.list_id AND
|
2020-02-03 07:39:11 +00:00
|
|
|
(CASE
|
|
|
|
-- For optin campaigns, only e-mail 'unconfirmed' subscribers belonging to 'double' optin lists.
|
|
|
|
WHEN camps.type = 'optin' THEN subscriber_lists.status = 'unconfirmed' AND campLists.optin = 'double'
|
2019-12-01 12:18:36 +00:00
|
|
|
|
2020-02-03 07:39:11 +00:00
|
|
|
-- For regular campaigns with double optin lists, only e-mail 'confirmed' subscribers.
|
|
|
|
WHEN campLists.optin = 'double' THEN subscriber_lists.status = 'confirmed'
|
|
|
|
|
|
|
|
-- For regular campaigns with non-double optin lists, e-mail everyone
|
|
|
|
-- except unsubscribed subscribers.
|
|
|
|
ELSE subscriber_lists.status != 'unsubscribed'
|
|
|
|
END)
|
2019-12-01 12:18:36 +00:00
|
|
|
)
|
2018-11-01 12:21:29 +00:00
|
|
|
GROUP BY camps.id
|
2018-10-25 13:51:47 +00:00
|
|
|
),
|
|
|
|
u AS (
|
2019-12-01 12:18:36 +00:00
|
|
|
-- For each campaign, update the to_send count and set the max_subscriber_id.
|
2018-10-25 13:51:47 +00:00
|
|
|
UPDATE campaigns AS ca
|
2018-10-26 07:51:45 +00:00
|
|
|
SET to_send = co.to_send,
|
2020-01-19 15:16:25 +00:00
|
|
|
status = (CASE WHEN status != 'running' THEN 'running' ELSE status END),
|
|
|
|
max_subscriber_id = co.max_subscriber_id,
|
|
|
|
started_at=(CASE WHEN ca.started_at IS NULL THEN NOW() ELSE ca.started_at END)
|
2018-10-25 13:51:47 +00:00
|
|
|
FROM (SELECT * FROM counts) co
|
2018-10-26 07:51:45 +00:00
|
|
|
WHERE ca.id = co.campaign_id
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
2023-05-18 11:25:59 +00:00
|
|
|
SELECT camps.*, campMedia.media_id FROM camps LEFT JOIN campMedia ON (campMedia.campaign_id = camps.id);
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2022-07-30 13:31:20 +00:00
|
|
|
-- name: get-campaign-analytics-unique-counts
|
2021-09-11 07:27:55 +00:00
|
|
|
WITH intval AS (
|
|
|
|
-- For intervals < a week, aggregate counts hourly, otherwise daily.
|
|
|
|
SELECT CASE WHEN (EXTRACT (EPOCH FROM ($3::TIMESTAMP - $2::TIMESTAMP)) / 86400) >= 7 THEN 'day' ELSE 'hour' END
|
2022-07-30 13:31:20 +00:00
|
|
|
),
|
|
|
|
uniqIDs AS (
|
|
|
|
SELECT DISTINCT ON(subscriber_id) subscriber_id, campaign_id, DATE_TRUNC((SELECT * FROM intval), created_at) AS "timestamp"
|
|
|
|
FROM %s
|
2021-09-11 07:27:55 +00:00
|
|
|
WHERE campaign_id=ANY($1) AND created_at >= $2 AND created_at <= $3
|
2022-07-30 13:31:20 +00:00
|
|
|
ORDER BY subscriber_id, "timestamp"
|
|
|
|
)
|
|
|
|
SELECT COUNT(*) AS "count", campaign_id, "timestamp"
|
|
|
|
FROM uniqIDs GROUP BY campaign_id, "timestamp";
|
2021-09-11 07:27:55 +00:00
|
|
|
|
2022-07-30 13:31:20 +00:00
|
|
|
-- name: get-campaign-analytics-counts
|
2022-02-01 18:10:03 +00:00
|
|
|
-- raw: true
|
2021-09-11 07:27:55 +00:00
|
|
|
WITH intval AS (
|
|
|
|
-- For intervals < a week, aggregate counts hourly, otherwise daily.
|
|
|
|
SELECT CASE WHEN (EXTRACT (EPOCH FROM ($3::TIMESTAMP - $2::TIMESTAMP)) / 86400) >= 7 THEN 'day' ELSE 'hour' END
|
|
|
|
)
|
2022-07-30 13:31:20 +00:00
|
|
|
SELECT campaign_id, COUNT(*) AS "count", DATE_TRUNC((SELECT * FROM intval), created_at) AS "timestamp"
|
|
|
|
FROM %s
|
2021-09-11 07:27:55 +00:00
|
|
|
WHERE campaign_id=ANY($1) AND created_at >= $2 AND created_at <= $3
|
|
|
|
GROUP BY campaign_id, "timestamp" ORDER BY "timestamp" ASC;
|
|
|
|
|
|
|
|
-- name: get-campaign-bounce-counts
|
|
|
|
WITH intval AS (
|
|
|
|
-- For intervals < a week, aggregate counts hourly, otherwise daily.
|
|
|
|
SELECT CASE WHEN (EXTRACT (EPOCH FROM ($3::TIMESTAMP - $2::TIMESTAMP)) / 86400) >= 7 THEN 'day' ELSE 'hour' END
|
|
|
|
)
|
|
|
|
SELECT campaign_id, COUNT(*) AS "count", DATE_TRUNC((SELECT * FROM intval), created_at) AS "timestamp"
|
|
|
|
FROM bounces
|
|
|
|
WHERE campaign_id=ANY($1) AND created_at >= $2 AND created_at <= $3
|
|
|
|
GROUP BY campaign_id, "timestamp" ORDER BY "timestamp" ASC;
|
|
|
|
|
|
|
|
-- name: get-campaign-link-counts
|
2022-02-01 18:10:03 +00:00
|
|
|
-- raw: true
|
|
|
|
-- %s = * or DISTINCT subscriber_id (prepared based on based on individual tracking=on/off). Prepared on boot.
|
|
|
|
SELECT COUNT(%s) AS "count", url
|
2021-09-11 07:27:55 +00:00
|
|
|
FROM link_clicks
|
|
|
|
LEFT JOIN links ON (link_clicks.link_id = links.id)
|
|
|
|
WHERE campaign_id=ANY($1) AND link_clicks.created_at >= $2 AND link_clicks.created_at <= $3
|
|
|
|
GROUP BY links.url ORDER BY "count" DESC LIMIT 50;
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- name: next-campaign-subscribers
|
|
|
|
-- Returns a batch of subscribers in a given campaign starting from the last checkpoint
|
|
|
|
-- (last_subscriber_id). Every fetch updates the checkpoint and the sent count, which means
|
|
|
|
-- every fetch returns a new batch of subscribers until all rows are exhausted.
|
2019-12-01 12:18:36 +00:00
|
|
|
WITH camps AS (
|
2022-02-05 17:11:43 +00:00
|
|
|
SELECT last_subscriber_id, max_subscriber_id, type FROM campaigns WHERE id = $1 AND status='running'
|
2018-10-25 13:51:47 +00:00
|
|
|
),
|
2019-12-01 12:18:36 +00:00
|
|
|
campLists AS (
|
2021-09-17 14:25:38 +00:00
|
|
|
SELECT lists.id AS list_id, optin FROM lists
|
2022-02-05 17:11:43 +00:00
|
|
|
LEFT JOIN campaign_lists ON (campaign_lists.list_id = lists.id)
|
2019-12-01 12:18:36 +00:00
|
|
|
WHERE campaign_lists.campaign_id = $1
|
|
|
|
),
|
2022-02-05 17:11:43 +00:00
|
|
|
subIDs AS (
|
|
|
|
SELECT DISTINCT ON (subscriber_lists.subscriber_id) subscriber_id, list_id, status FROM subscriber_lists
|
|
|
|
WHERE
|
|
|
|
-- ARRAY_AGG is 20x faster instead of a simple SELECT because the query planner
|
|
|
|
-- understands the CTE's cardinality after the scalar array conversion. Huh.
|
|
|
|
list_id = ANY((SELECT ARRAY_AGG(list_id) FROM campLists)::INT[]) AND
|
|
|
|
status != 'unsubscribed' AND
|
|
|
|
subscriber_id > (SELECT last_subscriber_id FROM camps) AND
|
|
|
|
subscriber_id <= (SELECT max_subscriber_id FROM camps)
|
|
|
|
ORDER BY subscriber_id LIMIT $2
|
|
|
|
),
|
2018-10-25 13:51:47 +00:00
|
|
|
subs AS (
|
2022-02-05 17:11:43 +00:00
|
|
|
SELECT subscribers.* FROM subIDs
|
|
|
|
LEFT JOIN campLists ON (campLists.list_id = subIDs.list_id)
|
2019-12-01 12:18:36 +00:00
|
|
|
INNER JOIN subscribers ON (
|
2020-08-01 11:15:29 +00:00
|
|
|
subscribers.status != 'blocklisted' AND
|
2022-02-05 17:11:43 +00:00
|
|
|
subscribers.id = subIDs.subscriber_id AND
|
2020-02-03 07:39:11 +00:00
|
|
|
|
|
|
|
(CASE
|
|
|
|
-- For optin campaigns, only e-mail 'unconfirmed' subscribers.
|
2022-02-05 17:11:43 +00:00
|
|
|
WHEN (SELECT type FROM camps) = 'optin' THEN subIDs.status = 'unconfirmed' AND campLists.optin = 'double'
|
2020-02-03 07:39:11 +00:00
|
|
|
|
|
|
|
-- For regular campaigns with double optin lists, only e-mail 'confirmed' subscribers.
|
2022-02-05 17:11:43 +00:00
|
|
|
WHEN campLists.optin = 'double' THEN subIDs.status = 'confirmed'
|
2020-02-03 07:39:11 +00:00
|
|
|
|
|
|
|
-- For regular campaigns with non-double optin lists, e-mail everyone
|
|
|
|
-- except unsubscribed subscribers.
|
2022-02-05 17:11:43 +00:00
|
|
|
ELSE subIDs.status != 'unsubscribed'
|
2020-02-03 07:39:11 +00:00
|
|
|
END)
|
2019-12-01 12:18:36 +00:00
|
|
|
)
|
2018-10-25 13:51:47 +00:00
|
|
|
),
|
|
|
|
u AS (
|
|
|
|
UPDATE campaigns
|
2019-12-01 12:18:36 +00:00
|
|
|
SET last_subscriber_id = (SELECT MAX(id) FROM subs),
|
|
|
|
sent = sent + (SELECT COUNT(id) FROM subs),
|
|
|
|
updated_at = NOW()
|
2018-10-25 13:51:47 +00:00
|
|
|
WHERE (SELECT COUNT(id) FROM subs) > 0 AND id=$1
|
|
|
|
)
|
|
|
|
SELECT * FROM subs;
|
|
|
|
|
2022-09-03 07:48:02 +00:00
|
|
|
-- name: delete-campaign-views
|
|
|
|
DELETE FROM campaign_views WHERE created_at < $1;
|
|
|
|
|
|
|
|
-- name: delete-campaign-link-clicks
|
|
|
|
DELETE FROM link_clicks WHERE created_at < $1;
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- name: get-one-campaign-subscriber
|
|
|
|
SELECT * FROM subscribers
|
|
|
|
LEFT JOIN subscriber_lists ON (subscribers.id = subscriber_lists.subscriber_id AND subscriber_lists.status != 'unsubscribed')
|
|
|
|
WHERE subscriber_lists.list_id=ANY(
|
|
|
|
SELECT list_id FROM campaign_lists where campaign_id=$1 AND list_id IS NOT NULL
|
|
|
|
)
|
2018-11-06 03:22:59 +00:00
|
|
|
ORDER BY RANDOM() LIMIT 1;
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
-- name: update-campaign
|
|
|
|
WITH camp AS (
|
|
|
|
UPDATE campaigns SET
|
2021-01-30 09:29:21 +00:00
|
|
|
name=$2,
|
|
|
|
subject=$3,
|
|
|
|
from_email=$4,
|
|
|
|
body=$5,
|
|
|
|
altbody=(CASE WHEN $6 = '' THEN NULL ELSE $6 END),
|
|
|
|
content_type=$7::content_type,
|
|
|
|
send_at=$8::TIMESTAMP WITH TIME ZONE,
|
|
|
|
status=(CASE WHEN NOT $9 THEN 'draft' ELSE status END),
|
2022-01-04 16:46:21 +00:00
|
|
|
headers=$10,
|
|
|
|
tags=$11::VARCHAR(100)[],
|
|
|
|
messenger=$12,
|
|
|
|
template_id=$13,
|
2022-11-03 05:37:26 +00:00
|
|
|
archive=$15,
|
|
|
|
archive_template_id=$16,
|
|
|
|
archive_meta=$17,
|
2018-10-25 13:51:47 +00:00
|
|
|
updated_at=NOW()
|
|
|
|
WHERE id = $1 RETURNING id
|
|
|
|
),
|
2023-05-18 11:25:59 +00:00
|
|
|
clists AS (
|
2019-03-30 07:01:24 +00:00
|
|
|
-- Reset list relationships
|
2022-01-04 16:46:21 +00:00
|
|
|
DELETE FROM campaign_lists WHERE campaign_id = $1 AND NOT(list_id = ANY($14))
|
2023-05-18 11:25:59 +00:00
|
|
|
),
|
|
|
|
med AS (
|
|
|
|
DELETE FROM campaign_media WHERE campaign_id = $1
|
|
|
|
AND media_id IS NULL or NOT(media_id = ANY($18)) RETURNING media_id
|
|
|
|
),
|
|
|
|
medi AS (
|
|
|
|
INSERT INTO campaign_media (campaign_id, media_id, filename)
|
|
|
|
(SELECT $1 AS campaign_id, id, filename FROM media WHERE id=ANY($18::INT[]))
|
|
|
|
ON CONFLICT (campaign_id, media_id) DO NOTHING
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
INSERT INTO campaign_lists (campaign_id, list_id, list_name)
|
2022-01-04 16:46:21 +00:00
|
|
|
(SELECT $1 as campaign_id, id, name FROM lists WHERE id=ANY($14::INT[]))
|
2018-10-25 13:51:47 +00:00
|
|
|
ON CONFLICT (campaign_id, list_id) DO UPDATE SET list_name = EXCLUDED.list_name;
|
|
|
|
|
|
|
|
-- name: update-campaign-counts
|
|
|
|
UPDATE campaigns SET
|
|
|
|
to_send=(CASE WHEN $2 != 0 THEN $2 ELSE to_send END),
|
|
|
|
sent=(CASE WHEN $3 != 0 THEN $3 ELSE sent END),
|
|
|
|
last_subscriber_id=(CASE WHEN $4 != 0 THEN $4 ELSE last_subscriber_id END),
|
|
|
|
updated_at=NOW()
|
|
|
|
WHERE id=$1;
|
|
|
|
|
|
|
|
-- name: update-campaign-status
|
|
|
|
UPDATE campaigns SET status=$2, updated_at=NOW() WHERE id = $1;
|
|
|
|
|
2022-11-03 05:37:26 +00:00
|
|
|
-- name: update-campaign-archive
|
|
|
|
UPDATE campaigns SET
|
|
|
|
archive=$2,
|
|
|
|
archive_template_id=(CASE WHEN $3 > 0 THEN $3 ELSE archive_template_id END),
|
|
|
|
archive_meta=(CASE WHEN $4::TEXT != '' THEN $4::JSONB ELSE archive_meta END),
|
|
|
|
updated_at=NOW()
|
|
|
|
WHERE id=$1;
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- name: delete-campaign
|
2020-10-10 16:38:13 +00:00
|
|
|
DELETE FROM campaigns WHERE id=$1;
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2018-11-02 07:50:32 +00:00
|
|
|
-- name: register-campaign-view
|
|
|
|
WITH view AS (
|
|
|
|
SELECT campaigns.id as campaign_id, subscribers.id AS subscriber_id FROM campaigns
|
2020-10-18 12:03:34 +00:00
|
|
|
LEFT JOIN subscribers ON (CASE WHEN $2::TEXT != '' THEN subscribers.uuid = $2::UUID ELSE FALSE END)
|
2018-11-02 07:50:32 +00:00
|
|
|
WHERE campaigns.uuid = $1
|
|
|
|
)
|
|
|
|
INSERT INTO campaign_views (campaign_id, subscriber_id)
|
|
|
|
VALUES((SELECT campaign_id FROM view), (SELECT subscriber_id FROM view));
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- users
|
|
|
|
-- name: get-users
|
|
|
|
SELECT * FROM users WHERE $1 = 0 OR id = $1 OFFSET $2 LIMIT $3;
|
|
|
|
|
|
|
|
-- name: create-user
|
|
|
|
INSERT INTO users (email, name, password, type, status) VALUES($1, $2, $3, $4, $5) RETURNING id;
|
|
|
|
|
|
|
|
-- name: update-user
|
|
|
|
UPDATE users SET
|
|
|
|
email=(CASE WHEN $2 != '' THEN $2 ELSE email END),
|
|
|
|
name=(CASE WHEN $3 != '' THEN $3 ELSE name END),
|
|
|
|
password=(CASE WHEN $4 != '' THEN $4 ELSE password END),
|
|
|
|
type=(CASE WHEN $5 != '' THEN $5::user_type ELSE type END),
|
|
|
|
status=(CASE WHEN $6 != '' THEN $6::user_status ELSE status END),
|
|
|
|
updated_at=NOW()
|
|
|
|
WHERE id = $1;
|
|
|
|
|
|
|
|
-- name: delete-user
|
|
|
|
-- Delete a user, except for the primordial super admin.
|
|
|
|
DELETE FROM users WHERE $1 != 1 AND id=$1;
|
|
|
|
|
|
|
|
|
|
|
|
-- templates
|
|
|
|
-- name: get-templates
|
|
|
|
-- Only if the second param ($2) is true, body is returned.
|
2022-07-02 10:00:17 +00:00
|
|
|
SELECT id, name, type, subject, (CASE WHEN $2 = false THEN body ELSE '' END) as body,
|
2018-10-25 13:51:47 +00:00
|
|
|
is_default, created_at, updated_at
|
2022-07-02 10:00:17 +00:00
|
|
|
FROM templates WHERE ($1 = 0 OR id = $1) AND ($3 = '' OR type = $3::template_type)
|
2018-10-25 13:51:47 +00:00
|
|
|
ORDER BY created_at;
|
|
|
|
|
|
|
|
-- name: create-template
|
2022-07-02 10:00:17 +00:00
|
|
|
INSERT INTO templates (name, type, subject, body) VALUES($1, $2, $3, $4) RETURNING id;
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
-- name: update-template
|
|
|
|
UPDATE templates SET
|
|
|
|
name=(CASE WHEN $2 != '' THEN $2 ELSE name END),
|
2022-07-03 05:29:40 +00:00
|
|
|
subject=(CASE WHEN $3 != '' THEN $3 ELSE name END),
|
|
|
|
body=(CASE WHEN $4 != '' THEN $4 ELSE body END),
|
2018-10-25 13:51:47 +00:00
|
|
|
updated_at=NOW()
|
|
|
|
WHERE id = $1;
|
|
|
|
|
|
|
|
-- name: set-default-template
|
|
|
|
WITH u AS (
|
2022-07-02 10:00:17 +00:00
|
|
|
UPDATE templates SET is_default=true WHERE id=$1 AND type='campaign' RETURNING id
|
2018-10-25 13:51:47 +00:00
|
|
|
)
|
|
|
|
UPDATE templates SET is_default=false WHERE id != $1;
|
|
|
|
|
|
|
|
-- name: delete-template
|
2022-04-03 15:24:40 +00:00
|
|
|
-- Delete a template as long as there's more than one. On deletion, set all campaigns
|
2018-11-01 18:37:02 +00:00
|
|
|
-- with that template to the default template instead.
|
|
|
|
WITH tpl AS (
|
|
|
|
DELETE FROM templates WHERE id = $1 AND (SELECT COUNT(id) FROM templates) > 1 AND is_default = false RETURNING id
|
|
|
|
),
|
|
|
|
def AS (
|
2022-07-02 11:25:42 +00:00
|
|
|
SELECT id FROM templates WHERE is_default = true AND type='campaign' LIMIT 1
|
|
|
|
),
|
|
|
|
up AS (
|
|
|
|
UPDATE campaigns SET template_id = (SELECT id FROM def) WHERE (SELECT id FROM tpl) > 0 AND template_id = $1
|
2018-11-01 18:37:02 +00:00
|
|
|
)
|
2022-07-02 11:25:42 +00:00
|
|
|
SELECT id FROM tpl;
|
2018-11-01 18:37:02 +00:00
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
|
|
|
|
-- media
|
|
|
|
-- name: insert-media
|
2023-05-18 11:25:59 +00:00
|
|
|
INSERT INTO media (uuid, filename, thumb, content_type, provider, meta, created_at) VALUES($1, $2, $3, $4, $5, $6, NOW()) RETURNING id;
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2023-05-21 09:49:12 +00:00
|
|
|
-- name: query-media
|
|
|
|
SELECT COUNT(*) OVER () AS total, * FROM media
|
|
|
|
WHERE ($1 = '' OR filename ILIKE $1) AND provider=$2 ORDER BY created_at DESC OFFSET $3 LIMIT $4;
|
2018-10-25 13:51:47 +00:00
|
|
|
|
2022-04-03 15:24:40 +00:00
|
|
|
-- name: get-media
|
|
|
|
SELECT * FROM media WHERE CASE WHEN $1 > 0 THEN id = $1 ELSE uuid = $2 END;
|
|
|
|
|
2018-10-25 13:51:47 +00:00
|
|
|
-- name: delete-media
|
|
|
|
DELETE FROM media WHERE id=$1 RETURNING filename;
|
|
|
|
|
2018-10-31 12:54:21 +00:00
|
|
|
-- links
|
|
|
|
-- name: create-link
|
|
|
|
INSERT INTO links (uuid, url) VALUES($1, $2) ON CONFLICT (url) DO UPDATE SET url=EXCLUDED.url RETURNING uuid;
|
|
|
|
|
|
|
|
-- name: register-link-click
|
2020-10-24 08:33:37 +00:00
|
|
|
WITH link AS(
|
|
|
|
SELECT id, url FROM links WHERE uuid = $1
|
2018-10-31 12:54:21 +00:00
|
|
|
)
|
2020-10-24 08:33:37 +00:00
|
|
|
INSERT INTO link_clicks (campaign_id, subscriber_id, link_id) VALUES(
|
|
|
|
(SELECT id FROM campaigns WHERE uuid = $2),
|
|
|
|
(SELECT id FROM subscribers WHERE
|
|
|
|
(CASE WHEN $3::TEXT != '' THEN subscribers.uuid = $3::UUID ELSE FALSE END)
|
|
|
|
),
|
|
|
|
(SELECT id FROM link)
|
|
|
|
) RETURNING (SELECT url FROM link);
|
2018-10-31 12:54:21 +00:00
|
|
|
|
2020-07-04 16:55:02 +00:00
|
|
|
-- name: get-dashboard-charts
|
|
|
|
WITH clicks AS (
|
2018-11-05 05:49:08 +00:00
|
|
|
SELECT JSON_AGG(ROW_TO_JSON(row))
|
2021-09-18 10:16:22 +00:00
|
|
|
FROM (
|
|
|
|
WITH viewDates AS (
|
|
|
|
SELECT TIMEZONE('UTC', created_at)::DATE AS to_date,
|
|
|
|
TIMEZONE('UTC', created_at)::DATE - INTERVAL '30 DAY' AS from_date
|
|
|
|
FROM link_clicks ORDER BY id DESC LIMIT 1
|
|
|
|
)
|
|
|
|
SELECT COUNT(*) AS count, created_at::DATE as date FROM link_clicks
|
|
|
|
-- use > between < to force the use of the date index.
|
|
|
|
WHERE TIMEZONE('UTC', created_at)::DATE BETWEEN (SELECT from_date FROM viewDates) AND (SELECT to_date FROM viewDates)
|
|
|
|
GROUP by date ORDER BY date
|
2018-11-05 05:49:08 +00:00
|
|
|
) row
|
|
|
|
),
|
|
|
|
views AS (
|
|
|
|
SELECT JSON_AGG(ROW_TO_JSON(row))
|
2021-09-18 10:16:22 +00:00
|
|
|
FROM (
|
|
|
|
WITH viewDates AS (
|
|
|
|
SELECT TIMEZONE('UTC', created_at)::DATE AS to_date,
|
|
|
|
TIMEZONE('UTC', created_at)::DATE - INTERVAL '30 DAY' AS from_date
|
|
|
|
FROM campaign_views ORDER BY id DESC LIMIT 1
|
|
|
|
)
|
|
|
|
SELECT COUNT(*) AS count, created_at::DATE as date FROM campaign_views
|
|
|
|
-- use > between < to force the use of the date index.
|
|
|
|
WHERE TIMEZONE('UTC', created_at)::DATE BETWEEN (SELECT from_date FROM viewDates) AND (SELECT to_date FROM viewDates)
|
|
|
|
GROUP by date ORDER BY date
|
2018-11-05 05:49:08 +00:00
|
|
|
) row
|
|
|
|
)
|
2020-07-04 16:55:02 +00:00
|
|
|
SELECT JSON_BUILD_OBJECT('link_clicks', COALESCE((SELECT * FROM clicks), '[]'),
|
|
|
|
'campaign_views', COALESCE((SELECT * FROM views), '[]'));
|
|
|
|
|
|
|
|
-- name: get-dashboard-counts
|
2021-09-18 10:16:22 +00:00
|
|
|
WITH subs AS (
|
|
|
|
SELECT COUNT(*) AS num, status FROM subscribers GROUP BY status
|
|
|
|
)
|
2020-07-04 16:55:02 +00:00
|
|
|
SELECT JSON_BUILD_OBJECT('subscribers', JSON_BUILD_OBJECT(
|
2021-09-18 10:16:22 +00:00
|
|
|
'total', (SELECT SUM(num) FROM subs),
|
|
|
|
'blocklisted', (SELECT num FROM subs WHERE status='blocklisted'),
|
2020-07-04 16:55:02 +00:00
|
|
|
'orphans', (
|
|
|
|
SELECT COUNT(id) FROM subscribers
|
|
|
|
LEFT JOIN subscriber_lists ON (subscribers.id = subscriber_lists.subscriber_id)
|
|
|
|
WHERE subscriber_lists.subscriber_id IS NULL
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'lists', JSON_BUILD_OBJECT(
|
|
|
|
'total', (SELECT COUNT(*) FROM lists),
|
|
|
|
'private', (SELECT COUNT(*) FROM lists WHERE type='private'),
|
|
|
|
'public', (SELECT COUNT(*) FROM lists WHERE type='public'),
|
|
|
|
'optin_single', (SELECT COUNT(*) FROM lists WHERE optin='single'),
|
|
|
|
'optin_double', (SELECT COUNT(*) FROM lists WHERE optin='double')
|
|
|
|
),
|
|
|
|
'campaigns', JSON_BUILD_OBJECT(
|
|
|
|
'total', (SELECT COUNT(*) FROM campaigns),
|
|
|
|
'by_status', (
|
|
|
|
SELECT JSON_OBJECT_AGG (status, num) FROM
|
|
|
|
(SELECT status, COUNT(*) AS num FROM campaigns GROUP BY status) r
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'messages', (SELECT SUM(sent) AS messages FROM campaigns));
|
2020-07-08 11:00:14 +00:00
|
|
|
|
|
|
|
-- name: get-settings
|
|
|
|
SELECT JSON_OBJECT_AGG(key, value) AS settings
|
|
|
|
FROM (
|
|
|
|
SELECT * FROM settings ORDER BY key
|
|
|
|
) t;
|
|
|
|
|
|
|
|
-- name: update-settings
|
|
|
|
UPDATE settings AS s SET value = c.value
|
2020-08-07 13:58:23 +00:00
|
|
|
-- For each key in the incoming JSON map, update the row with the key and its value.
|
2020-07-08 11:00:14 +00:00
|
|
|
FROM(SELECT * FROM JSONB_EACH($1)) AS c(key, value) WHERE s.key = c.key;
|
2021-05-24 17:11:48 +00:00
|
|
|
|
|
|
|
-- name: record-bounce
|
|
|
|
-- Insert a bounce and count the bounces for the subscriber and either unsubscribe them,
|
|
|
|
WITH sub AS (
|
|
|
|
SELECT id, status FROM subscribers WHERE CASE WHEN $1 != '' THEN uuid = $1::UUID ELSE email = $2 END
|
|
|
|
),
|
|
|
|
camp AS (
|
|
|
|
SELECT id FROM campaigns WHERE $3 != '' AND uuid = $3::UUID
|
|
|
|
),
|
|
|
|
num AS (
|
2021-08-22 10:12:54 +00:00
|
|
|
-- Add a +1 to include the current insertion that is happening.
|
2023-04-11 06:03:13 +00:00
|
|
|
SELECT COUNT(*) + 1 AS num FROM bounces WHERE subscriber_id = (SELECT id FROM sub) AND type = $4
|
2021-05-24 17:11:48 +00:00
|
|
|
),
|
|
|
|
-- block1 and block2 will run when $8 = 'blocklist' and the number of bounces exceed $8.
|
|
|
|
block1 AS (
|
|
|
|
UPDATE subscribers SET status='blocklisted'
|
|
|
|
WHERE $9 = 'blocklist' AND (SELECT num FROM num) >= $8 AND id = (SELECT id FROM sub) AND (SELECT status FROM sub) != 'blocklisted'
|
|
|
|
),
|
|
|
|
block2 AS (
|
|
|
|
UPDATE subscriber_lists SET status='unsubscribed'
|
2023-04-11 06:03:13 +00:00
|
|
|
WHERE $9 = 'unsubscribe' AND (SELECT num FROM num) >= $8 AND subscriber_id = (SELECT id FROM sub) AND (SELECT status FROM sub) != 'blocklisted'
|
|
|
|
),
|
|
|
|
bounce AS (
|
|
|
|
-- Record the bounce if the subscriber is not already blocklisted;
|
|
|
|
INSERT INTO bounces (subscriber_id, campaign_id, type, source, meta, created_at)
|
|
|
|
SELECT (SELECT id FROM sub), (SELECT id FROM camp), $4, $5, $6, $7
|
|
|
|
WHERE NOT EXISTS (SELECT 1 WHERE (SELECT status FROM sub) = 'blocklisted' OR (SELECT num FROM num) > $8)
|
2021-05-24 17:11:48 +00:00
|
|
|
)
|
|
|
|
-- This delete will only run when $9 = 'delete' and the number of bounces exceed $8.
|
|
|
|
DELETE FROM subscribers
|
|
|
|
WHERE $9 = 'delete' AND (SELECT num FROM num) >= $8 AND id = (SELECT id FROM sub);
|
|
|
|
|
|
|
|
-- name: query-bounces
|
|
|
|
SELECT COUNT(*) OVER () AS total,
|
|
|
|
bounces.id,
|
|
|
|
bounces.type,
|
|
|
|
bounces.source,
|
|
|
|
bounces.meta,
|
|
|
|
bounces.created_at,
|
|
|
|
bounces.subscriber_id,
|
|
|
|
subscribers.uuid AS subscriber_uuid,
|
|
|
|
subscribers.email AS email,
|
|
|
|
subscribers.email AS email,
|
|
|
|
(
|
|
|
|
CASE WHEN bounces.campaign_id IS NOT NULL
|
|
|
|
THEN JSON_BUILD_OBJECT('id', bounces.campaign_id, 'name', campaigns.name)
|
|
|
|
ELSE NULL END
|
|
|
|
) AS campaign
|
|
|
|
FROM bounces
|
|
|
|
LEFT JOIN subscribers ON (subscribers.id = bounces.subscriber_id)
|
|
|
|
LEFT JOIN campaigns ON (campaigns.id = bounces.campaign_id)
|
|
|
|
WHERE ($1 = 0 OR bounces.id = $1)
|
|
|
|
AND ($2 = 0 OR bounces.campaign_id = $2)
|
|
|
|
AND ($3 = 0 OR bounces.subscriber_id = $3)
|
|
|
|
AND ($4 = '' OR bounces.source = $4)
|
2022-12-25 08:34:43 +00:00
|
|
|
ORDER BY %order% OFFSET $5 LIMIT $6;
|
2021-05-24 17:11:48 +00:00
|
|
|
|
|
|
|
-- name: delete-bounces
|
2022-04-03 15:24:40 +00:00
|
|
|
DELETE FROM bounces WHERE CARDINALITY($1::INT[]) = 0 OR id = ANY($1);
|
2021-05-24 17:11:48 +00:00
|
|
|
|
|
|
|
-- name: delete-bounces-by-subscriber
|
|
|
|
WITH sub AS (
|
|
|
|
SELECT id FROM subscribers WHERE CASE WHEN $1 > 0 THEN id = $1 ELSE uuid = $2 END
|
|
|
|
)
|
|
|
|
DELETE FROM bounces WHERE subscriber_id = (SELECT id FROM sub);
|
2022-11-03 05:37:26 +00:00
|
|
|
|
2023-06-24 07:37:13 +00:00
|
|
|
|
|
|
|
-- name: get-db-info
|
|
|
|
SELECT JSON_BUILD_OBJECT('version', (SELECT VERSION()),
|
2023-08-27 07:50:05 +00:00
|
|
|
'size_mb', (SELECT ROUND(pg_database_size((SELECT CURRENT_DATABASE()))/(1024^2)))) AS info;
|