From 442dec934108ebb15dd9fe9497a1c84a3a16fa2f Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Sun, 8 Mar 2020 14:36:50 +0530 Subject: [PATCH] Fix broken subscriber data export --- queries.sql | 38 ++++++++------------- static/email-templates/subscriber-data.html | 2 +- static/public/subscriber-data.html | 2 +- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/queries.sql b/queries.sql index 55fd56e..f07bfad 100644 --- a/queries.sql +++ b/queries.sql @@ -185,42 +185,34 @@ UPDATE subscriber_lists SET status = 'unsubscribed' WHERE -- privacy -- name: export-subscriber-data WITH prof AS ( - SELECT uuid, email, name, attribs, status, created_at, updated_at FROM subscribers WHERE + SELECT id, uuid, email, name, attribs, status, created_at, updated_at FROM subscribers WHERE CASE WHEN $1 > 0 THEN id = $1 ELSE uuid = $2 END ), subs AS ( - SELECT JSON_AGG( - ROW_TO_JSON( - (SELECT l FROM ( - 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 - ) l) - ) - ) AS lists FROM lists + 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 LEFT JOIN subscriber_lists ON (subscriber_lists.list_id = lists.id) WHERE subscriber_lists.subscriber_id = (SELECT id FROM prof) - GROUP BY subscriber_id ), views AS ( - SELECT JSON_AGG(t) AS views FROM - (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) - GROUP BY campaigns.id ORDER BY id) t + 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) + GROUP BY campaigns.id ORDER BY id ), clicks AS ( - SELECT JSON_AGG(t) AS views FROM - (SELECT url, COUNT(subscriber_id) as clicks FROM link_clicks + SELECT url, COUNT(subscriber_id) as clicks FROM link_clicks LEFT JOIN links ON (links.id = link_clicks.link_id) WHERE subscriber_id = (SELECT id FROM prof) - GROUP BY links.id ORDER BY id) t + GROUP BY links.id ORDER BY id ) SELECT (SELECT email FROM prof) as email, - COALESCE((SELECT JSON_AGG(t) AS profile FROM prof t), '{}') AS profile, - COALESCE((SELECT * FROM subs), '[]') AS subscriptions, - COALESCE((SELECT * FROM views), '[]') AS campaign_views, - COALESCE((SELECT * FROM clicks), '[]') AS link_clicks; + 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; -- Partial and RAW queries used to construct arbitrary subscriber -- queries for segmentation follow. diff --git a/static/email-templates/subscriber-data.html b/static/email-templates/subscriber-data.html index 3873aba..afd8fec 100644 --- a/static/email-templates/subscriber-data.html +++ b/static/email-templates/subscriber-data.html @@ -2,7 +2,7 @@ {{ template "header" . }}

Your data

- A copy of all data recorded on you is attached as a file in the JSON format. + A copy of all data recorded on you is attached as a file in JSON format. It can be viewed in a text editor.

{{ template "footer" }} diff --git a/static/public/subscriber-data.html b/static/public/subscriber-data.html index 3873aba..afd8fec 100644 --- a/static/public/subscriber-data.html +++ b/static/public/subscriber-data.html @@ -2,7 +2,7 @@ {{ template "header" . }}

Your data

- A copy of all data recorded on you is attached as a file in the JSON format. + A copy of all data recorded on you is attached as a file in JSON format. It can be viewed in a text editor.

{{ template "footer" }}