Fix broken subscriber data export

This commit is contained in:
Kailash Nadh 2020-03-08 14:36:50 +05:30
parent 892d5d2a20
commit 442dec9341
3 changed files with 17 additions and 25 deletions

View File

@ -185,42 +185,34 @@ UPDATE subscriber_lists SET status = 'unsubscribed' WHERE
-- privacy -- privacy
-- name: export-subscriber-data -- name: export-subscriber-data
WITH prof AS ( 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 CASE WHEN $1 > 0 THEN id = $1 ELSE uuid = $2 END
), ),
subs AS ( subs AS (
SELECT JSON_AGG(
ROW_TO_JSON(
(SELECT l FROM (
SELECT subscriber_lists.status AS subscription_status, SELECT subscriber_lists.status AS subscription_status,
(CASE WHEN lists.type = 'private' THEN 'Private list' ELSE lists.name END) as name, (CASE WHEN lists.type = 'private' THEN 'Private list' ELSE lists.name END) as name,
lists.type, subscriber_lists.created_at lists.type, subscriber_lists.created_at
) l) FROM lists
)
) AS lists FROM lists
LEFT JOIN subscriber_lists ON (subscriber_lists.list_id = lists.id) LEFT JOIN subscriber_lists ON (subscriber_lists.list_id = lists.id)
WHERE subscriber_lists.subscriber_id = (SELECT id FROM prof) WHERE subscriber_lists.subscriber_id = (SELECT id FROM prof)
GROUP BY subscriber_id
), ),
views AS ( views AS (
SELECT JSON_AGG(t) AS views FROM SELECT subject as campaign, COUNT(subscriber_id) as views FROM campaign_views
(SELECT subject as campaign, COUNT(subscriber_id) as views FROM campaign_views
LEFT JOIN campaigns ON (campaigns.id = campaign_views.campaign_id) LEFT JOIN campaigns ON (campaigns.id = campaign_views.campaign_id)
WHERE subscriber_id = (SELECT id FROM prof) WHERE subscriber_id = (SELECT id FROM prof)
GROUP BY campaigns.id ORDER BY id) t GROUP BY campaigns.id ORDER BY id
), ),
clicks AS ( 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) LEFT JOIN links ON (links.id = link_clicks.link_id)
WHERE subscriber_id = (SELECT id FROM prof) 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, SELECT (SELECT email FROM prof) as email,
COALESCE((SELECT JSON_AGG(t) AS profile FROM prof t), '{}') AS profile, COALESCE((SELECT JSON_AGG(t) FROM prof t), '{}') AS profile,
COALESCE((SELECT * FROM subs), '[]') AS subscriptions, COALESCE((SELECT JSON_AGG(t) FROM subs t), '[]') AS subscriptions,
COALESCE((SELECT * FROM views), '[]') AS campaign_views, COALESCE((SELECT JSON_AGG(t) FROM views t), '[]') AS campaign_views,
COALESCE((SELECT * FROM clicks), '[]') AS link_clicks; COALESCE((SELECT JSON_AGG(t) FROM clicks t), '[]') AS link_clicks;
-- Partial and RAW queries used to construct arbitrary subscriber -- Partial and RAW queries used to construct arbitrary subscriber
-- queries for segmentation follow. -- queries for segmentation follow.

View File

@ -2,7 +2,7 @@
{{ template "header" . }} {{ template "header" . }}
<h2>Your data</h2> <h2>Your data</h2>
<p> <p>
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. It can be viewed in a text editor.
</p> </p>
{{ template "footer" }} {{ template "footer" }}

View File

@ -2,7 +2,7 @@
{{ template "header" . }} {{ template "header" . }}
<h2>Your data</h2> <h2>Your data</h2>
<p> <p>
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. It can be viewed in a text editor.
</p> </p>
{{ template "footer" }} {{ template "footer" }}