|
| 1 | +-- Consent queries |
| 2 | + |
| 3 | +-- name: GetConsentByID :one |
| 4 | +SELECT id, key, version, title, body, published_at, created_at, updated_at |
| 5 | +FROM consents WHERE id = @id::text; |
| 6 | + |
| 7 | +-- name: GetConsentsByIDs :many |
| 8 | +SELECT id, key, version, title, body, published_at, created_at, updated_at |
| 9 | +FROM consents WHERE id = ANY(@ids::text[]); |
| 10 | + |
| 11 | +-- name: GetLatestPublishedConsentByKey :one |
| 12 | +SELECT id, key, version, title, body, published_at, created_at, updated_at |
| 13 | +FROM consents |
| 14 | +WHERE key = @key::text AND published_at IS NOT NULL AND published_at <= now() |
| 15 | +ORDER BY version DESC LIMIT 1; |
| 16 | + |
| 17 | +-- name: GetAllLatestPublishedConsents :many |
| 18 | +SELECT DISTINCT ON (key) id, key, version, title, body, published_at, created_at, updated_at |
| 19 | +FROM consents |
| 20 | +WHERE published_at IS NOT NULL AND published_at <= now() |
| 21 | +ORDER BY key, version DESC; |
| 22 | + |
| 23 | +-- 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; |
| 27 | + |
| 28 | +-- name: UpdateConsent :one |
| 29 | +UPDATE consents SET |
| 30 | + title = CASE WHEN @title::text = '' THEN title ELSE @title::text END, |
| 31 | + body = CASE WHEN @body::text = '' THEN body ELSE @body::text END, |
| 32 | + published_at = @published_at, |
| 33 | + updated_at = now() |
| 34 | +WHERE id = @id::text |
| 35 | +RETURNING id, key, version, title, body, published_at, created_at, updated_at; |
| 36 | + |
| 37 | +-- name: GetNextVersionForConsentKey :one |
| 38 | +SELECT COALESCE(MAX(version), 0) + 1 as next_version FROM consents WHERE key = @key::text; |
| 39 | + |
| 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 |
| 67 | +FROM ( |
| 68 | + SELECT DISTINCT ON (key) id, key, version, title, body, published_at, created_at, updated_at |
| 69 | + FROM consents |
| 70 | + WHERE published_at IS NOT NULL AND published_at <= now() |
| 71 | + ORDER BY key, version DESC |
| 72 | +) c |
| 73 | +WHERE NOT EXISTS ( |
| 74 | + SELECT 1 FROM user_consents uc WHERE uc.user_id = @user_id::text AND uc.consent_id = c.id |
| 75 | +); |
| 76 | + |
| 77 | +-- Translation queries |
| 78 | + |
| 79 | +-- name: GetConsentTranslationsByIDs :many |
| 80 | +SELECT consent_id, language_code, title, body |
| 81 | +FROM consent_translations |
| 82 | +WHERE consent_id = ANY(@entity_ids::text[]) |
| 83 | + AND language_code = @language_code::text; |
| 84 | + |
| 85 | +-- name: DeleteConsentTranslations :exec |
| 86 | +DELETE FROM consent_translations WHERE consent_id = @consent_id::text; |
| 87 | + |
| 88 | +-- name: UpsertConsentTranslation :one |
| 89 | +INSERT INTO consent_translations (consent_id, language_code, title, body) |
| 90 | +VALUES (@consent_id::text, @language_code::text, @title, @body) |
| 91 | +ON CONFLICT (consent_id, language_code) DO UPDATE SET |
| 92 | + title = EXCLUDED.title, |
| 93 | + body = EXCLUDED.body, |
| 94 | + updated_at = now() |
| 95 | +RETURNING consent_id, language_code, title, body, created_at, updated_at; |
0 commit comments