Skip to content

Commit fd7ed60

Browse files
chore: create new migration, refactor inhibit logic, change error message
1 parent 013a626 commit fd7ed60

4 files changed

+34
-28
lines changed

coderd/database/migrations/000283_allow_disabling_notification_templates_by_default.up.sql

+15-27
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,28 @@
11
ALTER TABLE notification_templates ADD COLUMN enabled_by_default boolean DEFAULT TRUE NOT NULL;
22

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-
133
CREATE OR REPLACE FUNCTION inhibit_enqueue_if_disabled()
144
RETURNS TRIGGER AS
155
$$
166
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.
1811
IF EXISTS (SELECT 1
1912
FROM notification_preferences
2013
WHERE disabled = TRUE
2114
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';
3826
END IF;
3927

4028
RETURN NEW;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Enable 'workspace created' notification by default
2+
UPDATE notification_templates
3+
SET enabled_by_default = TRUE
4+
WHERE id = '281fdf73-c6d6-4cbb-8ff5-888baf8a2fff';
5+
6+
-- Enable 'workspace manually updated' notification by default
7+
UPDATE notification_templates
8+
SET enabled_by_default = TRUE
9+
WHERE id = 'd089fe7b-d5c5-4c0c-aaf5-689859f7d392';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Disable 'workspace created' notification by default
2+
UPDATE notification_templates
3+
SET enabled_by_default = FALSE
4+
WHERE id = '281fdf73-c6d6-4cbb-8ff5-888baf8a2fff';
5+
6+
-- Disable 'workspace manually updated' notification by default
7+
UPDATE notification_templates
8+
SET enabled_by_default = FALSE
9+
WHERE id = 'd089fe7b-d5c5-4c0c-aaf5-689859f7d392';

coderd/notifications/enqueuer.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
)
2121

2222
var (
23-
ErrCannotEnqueueDisabledNotification = xerrors.New("user has disabled this notification")
23+
ErrCannotEnqueueDisabledNotification = xerrors.New("notification is not enabled")
2424
ErrDuplicate = xerrors.New("duplicate notification")
2525
)
2626

0 commit comments

Comments
 (0)