Skip to content

Commit e9d904d

Browse files
committed
Add kind to notification_templates and query to retrieve templates by kind
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent e6e4702 commit e9d904d

File tree

14 files changed

+179
-33
lines changed

14 files changed

+179
-33
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,14 @@ func (q *querier) GetNotificationTemplateById(ctx context.Context, id uuid.UUID)
14811481
return q.db.GetNotificationTemplateById(ctx, id)
14821482
}
14831483

1484+
func (q *querier) GetNotificationTemplatesByKind(ctx context.Context, kind database.NotificationTemplateKind) ([]database.NotificationTemplate, error) {
1485+
// TODO: restrict 'system' kind to admins only?
1486+
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceNotificationTemplate); err != nil {
1487+
return nil, err
1488+
}
1489+
return q.db.GetNotificationTemplatesByKind(ctx, kind)
1490+
}
1491+
14841492
func (q *querier) GetNotificationsSettings(ctx context.Context) (string, error) {
14851493
// No authz checks
14861494
return q.db.GetNotificationsSettings(ctx)
@@ -3025,8 +3033,8 @@ func (q *querier) UpdateMemberRoles(ctx context.Context, arg database.UpdateMemb
30253033
return q.db.UpdateMemberRoles(ctx, arg)
30263034
}
30273035

3028-
// TODO: how to restrict this to admins?
30293036
func (q *querier) UpdateNotificationTemplateMethodById(ctx context.Context, arg database.UpdateNotificationTemplateMethodByIdParams) (database.NotificationTemplate, error) {
3037+
// TODO: how to restrict this to admins?
30303038
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceNotificationTemplate); err != nil {
30313039
return database.NotificationTemplate{}, err
30323040
}

coderd/database/dbmem/dbmem.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2709,8 +2709,12 @@ func (q *FakeQuerier) GetNotificationMessagesByStatus(_ context.Context, arg dat
27092709
return out, nil
27102710
}
27112711

2712-
func (q *FakeQuerier) GetNotificationTemplateById(_ context.Context, id uuid.UUID) (database.NotificationTemplate, error) {
2713-
return database.NotificationTemplate{ID: id, Name: "fake"}, nil
2712+
func (*FakeQuerier) GetNotificationTemplateById(_ context.Context, _ uuid.UUID) (database.NotificationTemplate, error) {
2713+
return database.NotificationTemplate{}, ErrUnimplemented
2714+
}
2715+
2716+
func (q *FakeQuerier) GetNotificationTemplatesByKind(ctx context.Context, kind database.NotificationTemplateKind) ([]database.NotificationTemplate, error) {
2717+
return nil, ErrUnimplemented
27142718
}
27152719

27162720
func (q *FakeQuerier) GetNotificationsSettings(_ context.Context) (string, error) {
@@ -7541,16 +7545,8 @@ func (q *FakeQuerier) UpdateMemberRoles(_ context.Context, arg database.UpdateMe
75417545
return database.OrganizationMember{}, sql.ErrNoRows
75427546
}
75437547

7544-
func (q *FakeQuerier) UpdateNotificationTemplateMethodById(_ context.Context, arg database.UpdateNotificationTemplateMethodByIdParams) (database.NotificationTemplate, error) {
7545-
err := validateDatabaseType(arg)
7546-
if err != nil {
7547-
return database.NotificationTemplate{}, err
7548-
}
7549-
7550-
return database.NotificationTemplate{
7551-
ID: arg.ID,
7552-
Method: arg.Method,
7553-
}, nil
7548+
func (*FakeQuerier) UpdateNotificationTemplateMethodById(_ context.Context, _ database.UpdateNotificationTemplateMethodByIdParams) (database.NotificationTemplate, error) {
7549+
return database.NotificationTemplate{}, ErrUnimplemented
75547550
}
75557551

75567552
func (q *FakeQuerier) UpdateOAuth2ProviderAppByID(_ context.Context, arg database.UpdateOAuth2ProviderAppByIDParams) (database.OAuth2ProviderApp, error) {

coderd/database/dbmetrics/dbmetrics.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

Lines changed: 22 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dump.sql

Lines changed: 9 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
DROP TABLE IF EXISTS notification_preferences;
2-
31
ALTER TABLE notification_templates
4-
DROP COLUMN IF EXISTS method;
2+
DROP COLUMN IF EXISTS method,
3+
DROP COLUMN IF EXISTS kind;
4+
5+
DROP TABLE IF EXISTS notification_preferences;
6+
DROP TYPE IF EXISTS notification_template_kind;
57

68
DROP TRIGGER IF EXISTS inhibit_enqueue_if_disabled_trigger ON notification_messages;
79
DROP FUNCTION IF EXISTS inhibit_enqueue_if_disabled;

coderd/database/migrations/000231_notification_preferences.up.sql renamed to coderd/database/migrations/000232_notification_preferences.up.sql

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ CREATE TABLE notification_preferences
1111
ALTER TABLE notification_preferences
1212
ADD CONSTRAINT unique_user_notification_template UNIQUE (user_id, notification_template_id);
1313

14-
-- Allow per-template notification method (enterprise only).
14+
-- Add a new type (to be expanded upon later) which specifies the kind of notification template.
15+
CREATE TYPE notification_template_kind AS ENUM (
16+
'system'
17+
);
18+
1519
ALTER TABLE notification_templates
16-
ADD COLUMN method notification_method;
20+
-- Allow per-template notification method (enterprise only).
21+
ADD COLUMN method notification_method,
22+
-- Update all existing notification templates to be system templates.
23+
ADD COLUMN kind notification_template_kind DEFAULT 'system'::notification_template_kind NOT NULL;
1724
COMMENT ON COLUMN notification_templates.method IS 'NULL defers to the deployment-level method';
1825

1926
-- No equivalent in down migration because ENUM values cannot be deleted.
@@ -45,4 +52,4 @@ CREATE TRIGGER inhibit_enqueue_if_disabled_trigger
4552
EXECUTE FUNCTION inhibit_enqueue_if_disabled();
4653

4754
-- Allow modifications to notification templates to be audited.
48-
ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'notification_template';
55+
ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'notification_template';

coderd/database/models.go

Lines changed: 57 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 41 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/notifications.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,7 @@ RETURNING *;
167167
SELECT *
168168
FROM notification_templates
169169
WHERE id = @id::uuid;
170+
171+
-- name: GetNotificationTemplatesByKind :many
172+
SELECT * FROM notification_templates
173+
WHERE kind = @kind::notification_template_kind;

0 commit comments

Comments
 (0)