|
1 | 1 | ALTER TABLE notification_templates ADD COLUMN enabled_by_default boolean DEFAULT TRUE NOT NULL;
|
2 | 2 |
|
3 |
| --- Disable 'workspace created' notification by default |
4 |
| -UPDATE notification_templates |
5 |
| -SET enabled_by_default = FALSE |
6 |
| -WHERE id = '281fdf73-c6d6-4cbb-8ff5-888baf8a2fff'; |
7 |
| - |
8 |
| --- Disable 'workspace manually updated' notification by default |
9 |
| -UPDATE notification_templates |
10 |
| -SET enabled_by_default = FALSE |
11 |
| -WHERE id = 'd089fe7b-d5c5-4c0c-aaf5-689859f7d392'; |
12 |
| - |
13 | 3 | CREATE OR REPLACE FUNCTION inhibit_enqueue_if_disabled()
|
14 | 4 | RETURNS TRIGGER AS
|
15 | 5 | $$
|
16 | 6 | BEGIN
|
17 |
| - -- Fail the insertion if the user has disabled this notification. |
| 7 | + -- Fail the insertion if one of the following: |
| 8 | + -- * the user has disabled this notification. |
| 9 | + -- * the notification template is disabled by default and hasn't |
| 10 | + -- been explicitly enabled by the user. |
18 | 11 | IF EXISTS (SELECT 1
|
19 | 12 | FROM notification_preferences
|
20 | 13 | WHERE disabled = TRUE
|
21 | 14 | AND user_id = NEW.user_id
|
22 |
| - AND notification_template_id = NEW.notification_template_id) THEN |
23 |
| - RAISE EXCEPTION 'cannot enqueue message: user has disabled this notification'; |
24 |
| - END IF; |
25 |
| - |
26 |
| - -- Fails if the notification template is disabled by default and the |
27 |
| - -- user hasn't explicitly enabled it. |
28 |
| - IF (NOT EXISTS (SELECT 1 |
29 |
| - FROM notification_preferences |
30 |
| - WHERE disabled = FALSE |
31 |
| - AND user_id = NEW.user_id |
32 |
| - AND notification_template_id = NEW.notification_template_id)) |
33 |
| - AND (EXISTS (SELECT 1 |
34 |
| - FROM notification_templates |
35 |
| - WHERE id = NEW.notification_template_id |
36 |
| - AND enabled_by_default = FALSE)) THEN |
37 |
| - RAISE EXCEPTION 'cannot enqueue message: user has disabled this notification'; |
| 15 | + AND notification_template_id = NEW.notification_template_id) |
| 16 | + OR (NOT EXISTS (SELECT 1 |
| 17 | + FROM notification_preferences |
| 18 | + WHERE disabled = FALSE |
| 19 | + AND user_id = NEW.user_id |
| 20 | + AND notification_template_id = NEW.notification_template_id)) |
| 21 | + AND (EXISTS (SELECT 1 |
| 22 | + FROM notification_templates |
| 23 | + WHERE id = NEW.notification_template_id |
| 24 | + AND enabled_by_default = FALSE) ) THEN |
| 25 | + RAISE EXCEPTION 'cannot enqueue message: notification is not enabled'; |
38 | 26 | END IF;
|
39 | 27 |
|
40 | 28 | RETURN NEW;
|
|
0 commit comments