Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emilsedgh/085b19f7b6c317c6d0ad8fd1e33e06f5 to your computer and use it in GitHub Desktop.
Save emilsedgh/085b19f7b6c317c6d0ad8fd1e33e06f5 to your computer and use it in GitHub Desktop.
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