Created
May 21, 2017 13:00
-
-
Save emilsedgh/085b19f7b6c317c6d0ad8fd1e33e06f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT 'message' AS type, | |
messages.id AS mid, | |
messages.id AS id, | |
messages.*, | |
( | |
SELECT ARRAY_AGG(attachment) | |
FROM attachments_eav | |
WHERE object = messages.id | |
) AS attachments, | |
( | |
SELECT ARRAY_AGG("user") FROM notifications_users WHERE acked_at IS NOT NULL AND notification IN | |
( | |
SELECT id FROM notifications WHERE object_class = 'Message' AND object = mid | |
UNION | |
SELECT notification FROM messages WHERE id = mid | |
) | |
) AS acked_by, | |
( | |
SELECT JSON_AGG(d.*) FROM | |
( | |
SELECT "user", | |
type AS delivery_type, | |
'notification_delivery' as type, | |
created_at | |
FROM notifications_deliveries | |
WHERE notification IN | |
( | |
SELECT id FROM notifications WHERE object_class = 'Message' AND object = mid | |
UNION | |
SELECT notification FROM messages WHERE id = mid | |
) | |
) d | |
) AS deliveries, | |
EXTRACT(EPOCH FROM created_at) AS created_at, | |
EXTRACT(EPOCH FROM updated_at) AS updated_at | |
FROM messages | |
JOIN unnest($1::uuid[]) WITH ORDINALITY t(mid, ord) ON messages.id = mid | |
ORDER BY t.ord |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment