|
1 | 1 | -- Consent queries |
2 | 2 |
|
3 | 3 | -- name: GetConsentByID :one |
4 | | -SELECT id, key, version, title, body, published_at, created_at, updated_at |
| 4 | +SELECT id, key, version, title, body, url, published_at, is_remote, managed_by, created_at, updated_at |
5 | 5 | FROM consents WHERE id = @id::text; |
6 | 6 |
|
7 | 7 | -- name: GetConsentsByIDs :many |
8 | | -SELECT id, key, version, title, body, published_at, created_at, updated_at |
| 8 | +SELECT id, key, version, title, body, url, published_at, is_remote, managed_by, created_at, updated_at |
9 | 9 | FROM consents WHERE id = ANY(@ids::text[]); |
10 | 10 |
|
11 | 11 | -- name: GetLatestPublishedConsentByKey :one |
12 | | -SELECT id, key, version, title, body, published_at, created_at, updated_at |
| 12 | +SELECT id, key, version, title, body, url, published_at, is_remote, managed_by, created_at, updated_at |
13 | 13 | FROM consents |
14 | 14 | WHERE key = @key::text AND published_at IS NOT NULL AND published_at <= now() |
15 | 15 | ORDER BY version DESC LIMIT 1; |
16 | 16 |
|
17 | 17 | -- name: GetAllLatestPublishedConsents :many |
18 | | -SELECT DISTINCT ON (key) id, key, version, title, body, published_at, created_at, updated_at |
| 18 | +SELECT DISTINCT ON (key) id, key, version, title, body, url, published_at, is_remote, managed_by, created_at, updated_at |
19 | 19 | FROM consents |
20 | 20 | WHERE published_at IS NOT NULL AND published_at <= now() |
21 | 21 | ORDER BY key, version DESC; |
22 | 22 |
|
| 23 | +-- name: GetLatestConsentByKey :one |
| 24 | +SELECT id, key, version, title, body, url, published_at, is_remote, managed_by, created_at, updated_at |
| 25 | +FROM consents |
| 26 | +WHERE key = @key::text |
| 27 | +ORDER BY version DESC |
| 28 | +LIMIT 1; |
| 29 | + |
23 | 30 | -- name: CreateConsent :one |
24 | | -INSERT INTO consents (id, key, version, title, body, published_at) |
25 | | -VALUES (@id::text, @key::text, @version::int, @title::text, @body::text, @published_at) |
26 | | -RETURNING id, key, version, title, body, published_at, created_at, updated_at; |
| 31 | +INSERT INTO consents (id, key, version, title, body, url, published_at, is_remote, managed_by) |
| 32 | +VALUES (@id::text, @key::text, @version::int, @title::text, @body::text, @url, @published_at, @is_remote::bool, @managed_by) |
| 33 | +RETURNING id, key, version, title, body, url, published_at, is_remote, managed_by, created_at, updated_at; |
27 | 34 |
|
28 | 35 | -- name: UpdateConsent :one |
29 | 36 | UPDATE consents SET |
30 | 37 | title = CASE WHEN @title::text = '' THEN title ELSE @title::text END, |
31 | 38 | body = CASE WHEN @body::text = '' THEN body ELSE @body::text END, |
| 39 | + url = CASE WHEN @url::text = '' THEN url ELSE @url::text END, |
32 | 40 | published_at = @published_at, |
33 | 41 | updated_at = now() |
34 | 42 | WHERE id = @id::text |
35 | | -RETURNING id, key, version, title, body, published_at, created_at, updated_at; |
| 43 | +RETURNING id, key, version, title, body, url, published_at, is_remote, managed_by, created_at, updated_at; |
36 | 44 |
|
37 | 45 | -- name: GetNextVersionForConsentKey :one |
38 | 46 | SELECT COALESCE(MAX(version), 0) + 1 as next_version FROM consents WHERE key = @key::text; |
39 | 47 |
|
40 | | --- User consent queries |
41 | | - |
42 | | --- name: GetUserConsentsByUserID :many |
43 | | -SELECT uc.id, uc.user_id, uc.consent_id, uc.accepted_at, uc.created_at, |
44 | | - c.key as consent_key, c.version as consent_version |
45 | | -FROM user_consents uc |
46 | | -INNER JOIN consents c ON uc.consent_id = c.id |
47 | | -WHERE uc.user_id = @user_id::text; |
48 | | - |
49 | | --- name: GetUserConsentsByUserIDs :many |
50 | | -SELECT uc.id, uc.user_id, uc.consent_id, uc.accepted_at, uc.created_at, |
51 | | - c.key as consent_key, c.version as consent_version |
52 | | -FROM user_consents uc |
53 | | -INNER JOIN consents c ON uc.consent_id = c.id |
54 | | -WHERE uc.user_id = ANY(@user_ids::text[]); |
55 | | - |
56 | | --- name: GetUserConsentByUserAndConsent :one |
57 | | -SELECT id, user_id, consent_id, accepted_at, created_at |
58 | | -FROM user_consents WHERE user_id = @user_id::text AND consent_id = @consent_id::text; |
59 | | - |
60 | | --- name: CreateUserConsent :one |
61 | | -INSERT INTO user_consents (id, user_id, consent_id, accepted_at) |
62 | | -VALUES (@id::text, @user_id::text, @consent_id::text, @accepted_at) |
63 | | -RETURNING id, user_id, consent_id, accepted_at, created_at; |
64 | | - |
65 | | --- name: GetMissingConsentsForUser :many |
66 | | -SELECT c.id, c.key, c.version, c.title, c.body, c.published_at, c.created_at, c.updated_at |
| 48 | +-- User consent history queries |
| 49 | + |
| 50 | +-- name: CreateUserConsentHistory :one |
| 51 | +INSERT INTO user_consent_history ( |
| 52 | + id, user_id, consent_id, consent_key, action, occurred_at, |
| 53 | + source, external_consent_id, external_timestamp |
| 54 | +) |
| 55 | +VALUES ( |
| 56 | + @id::text, @user_id::text, @consent_id::text, @consent_key::text, |
| 57 | + @action::text, @occurred_at, @source, @external_consent_id, @external_timestamp |
| 58 | +) |
| 59 | +RETURNING id, user_id, consent_id, consent_key, action, occurred_at, |
| 60 | + source, external_consent_id, external_timestamp; |
| 61 | + |
| 62 | +-- name: GetLatestUserConsentActionByKey :one |
| 63 | +SELECT id, user_id, consent_id, consent_key, action, occurred_at, |
| 64 | + source, external_consent_id, external_timestamp |
| 65 | +FROM user_consent_history |
| 66 | +WHERE user_id = @user_id::text AND consent_key = @consent_key::text |
| 67 | +ORDER BY occurred_at DESC |
| 68 | +LIMIT 1; |
| 69 | + |
| 70 | +-- name: GetCurrentUserConsentStatusesByUsers :many |
| 71 | +-- Gets the latest action for each consent key for multiple users |
| 72 | +SELECT DISTINCT ON (user_id, consent_key) |
| 73 | + id, user_id, consent_id, consent_key, action, occurred_at, source |
| 74 | +FROM user_consent_history |
| 75 | +WHERE user_id = ANY(@user_ids::text[]) |
| 76 | +ORDER BY user_id, consent_key, occurred_at DESC; |
| 77 | + |
| 78 | +-- name: GetUserConsentHistoryByUserAndKey :many |
| 79 | +SELECT id, user_id, consent_id, consent_key, action, occurred_at, |
| 80 | + source, external_consent_id, external_timestamp |
| 81 | +FROM user_consent_history |
| 82 | +WHERE user_id = @user_id::text AND consent_key = @consent_key::text |
| 83 | +ORDER BY occurred_at DESC; |
| 84 | + |
| 85 | +-- name: GetUserConsentHistoryByUser :many |
| 86 | +SELECT id, user_id, consent_id, consent_key, action, occurred_at, |
| 87 | + source, external_consent_id, external_timestamp |
| 88 | +FROM user_consent_history |
| 89 | +WHERE user_id = @user_id::text |
| 90 | +ORDER BY occurred_at DESC; |
| 91 | + |
| 92 | +-- name: GetUserConsentHistoryByUsers :many |
| 93 | +SELECT id, user_id, consent_id, consent_key, action, occurred_at, |
| 94 | + source, external_consent_id, external_timestamp |
| 95 | +FROM user_consent_history |
| 96 | +WHERE user_id = ANY(@user_ids::text[]) |
| 97 | +ORDER BY user_id, occurred_at DESC; |
| 98 | + |
| 99 | +-- name: GetMissingConsentsForUserWithRejections :many |
| 100 | +-- Gets consents that user has never acted upon (no history) |
| 101 | +SELECT c.id, c.key, c.version, c.title, c.body, c.url, c.published_at, |
| 102 | + c.is_remote, c.managed_by, c.created_at, c.updated_at |
67 | 103 | FROM ( |
68 | | - SELECT DISTINCT ON (key) id, key, version, title, body, published_at, created_at, updated_at |
| 104 | + SELECT DISTINCT ON (key) id, key, version, title, body, url, published_at, |
| 105 | + is_remote, managed_by, created_at, updated_at |
69 | 106 | FROM consents |
70 | 107 | WHERE published_at IS NOT NULL AND published_at <= now() |
71 | 108 | ORDER BY key, version DESC |
72 | 109 | ) c |
73 | 110 | WHERE NOT EXISTS ( |
74 | | - SELECT 1 FROM user_consents uc WHERE uc.user_id = @user_id::text AND uc.consent_id = c.id |
| 111 | + SELECT 1 FROM user_consent_history uch |
| 112 | + WHERE uch.user_id = @user_id::text |
| 113 | + AND uch.consent_key = c.key |
75 | 114 | ); |
76 | 115 |
|
77 | 116 | -- Translation queries |
|
0 commit comments