-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathtemplates.sql
More file actions
59 lines (53 loc) · 2.25 KB
/
templates.sql
File metadata and controls
59 lines (53 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-- templates
-- name: get-templates
-- Only if the second param ($2 - noBody) is true, body and body_source is returned.
SELECT id, name, type, subject,
(CASE WHEN $2 = false THEN body ELSE '' END) as body,
(CASE WHEN $2 = false THEN body_source ELSE NULL END) as body_source,
is_default, created_at, updated_at
FROM templates WHERE ($1 = 0 OR id = $1) AND ($3 = '' OR type = $3::template_type)
ORDER BY created_at;
-- name: query-templates
-- Retrieves templates with pagination, search, and sorting.
-- $1: noBody - if true, blank out body and body_source
-- $2: type filter (empty string = all types)
-- $3: search query (for name matching)
-- $4: offset
-- $5: limit
SELECT COUNT(*) OVER () AS total, id, name, type, subject,
(CASE WHEN $1 = false THEN body ELSE '' END) as body,
(CASE WHEN $1 = false THEN body_source ELSE NULL END) as body_source,
is_default, created_at, updated_at
FROM templates
WHERE ($2 = '' OR type = $2::template_type)
AND ($3 = '' OR name ILIKE $3)
ORDER BY %order%
OFFSET $4 LIMIT (CASE WHEN $5 < 1 THEN NULL ELSE $5 END);
-- name: create-template
INSERT INTO templates (name, type, subject, body, body_source) VALUES($1, $2, $3, $4, $5) RETURNING id;
-- name: update-template
UPDATE templates SET
name=(CASE WHEN $2 != '' THEN $2 ELSE name END),
subject=(CASE WHEN $3 != '' THEN $3 ELSE name END),
body=(CASE WHEN $4 != '' THEN $4 ELSE body END),
body_source=(CASE WHEN $5 != '' THEN $5 ELSE body_source END),
updated_at=NOW()
WHERE id = $1;
-- name: set-default-template
WITH u AS (
UPDATE templates SET is_default=true WHERE id=$1 AND type='campaign' RETURNING id
)
UPDATE templates SET is_default=false WHERE id != $1;
-- name: delete-template
-- Delete a template as long as there's more than one. On deletion, set all campaigns
-- with that template to the default template instead.
WITH tpl AS (
DELETE FROM templates WHERE id = $1 AND (SELECT COUNT(id) FROM templates) > 1 AND is_default = false RETURNING id
),
def AS (
SELECT id FROM templates WHERE is_default = true AND (type='campaign' OR type='campaign_visual') LIMIT 1
),
up AS (
UPDATE campaigns SET template_id = (SELECT id FROM def) WHERE (SELECT id FROM tpl) > 0 AND template_id = $1
)
SELECT id FROM tpl;