mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
feat: add notification preferences database & audit support (#14100)
This commit is contained in:
@ -0,0 +1,9 @@
|
||||
ALTER TABLE notification_templates
|
||||
DROP COLUMN IF EXISTS method,
|
||||
DROP COLUMN IF EXISTS kind;
|
||||
|
||||
DROP TABLE IF EXISTS notification_preferences;
|
||||
DROP TYPE IF EXISTS notification_template_kind;
|
||||
|
||||
DROP TRIGGER IF EXISTS inhibit_enqueue_if_disabled ON notification_messages;
|
||||
DROP FUNCTION IF EXISTS inhibit_enqueue_if_disabled;
|
@ -0,0 +1,52 @@
|
||||
CREATE TABLE notification_preferences
|
||||
(
|
||||
user_id uuid REFERENCES users ON DELETE CASCADE NOT NULL,
|
||||
notification_template_id uuid REFERENCES notification_templates ON DELETE CASCADE NOT NULL,
|
||||
disabled bool NOT NULL DEFAULT FALSE,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (user_id, notification_template_id)
|
||||
);
|
||||
|
||||
-- Add a new type (to be expanded upon later) which specifies the kind of notification template.
|
||||
CREATE TYPE notification_template_kind AS ENUM (
|
||||
'system'
|
||||
);
|
||||
|
||||
ALTER TABLE notification_templates
|
||||
-- Allow per-template notification method (enterprise only).
|
||||
ADD COLUMN method notification_method,
|
||||
-- Update all existing notification templates to be system templates.
|
||||
ADD COLUMN kind notification_template_kind DEFAULT 'system'::notification_template_kind NOT NULL;
|
||||
COMMENT ON COLUMN notification_templates.method IS 'NULL defers to the deployment-level method';
|
||||
|
||||
-- No equivalent in down migration because ENUM values cannot be deleted.
|
||||
ALTER TYPE notification_message_status ADD VALUE IF NOT EXISTS 'inhibited';
|
||||
|
||||
-- Function to prevent enqueuing notifications unnecessarily.
|
||||
CREATE OR REPLACE FUNCTION inhibit_enqueue_if_disabled()
|
||||
RETURNS TRIGGER AS
|
||||
$$
|
||||
BEGIN
|
||||
-- Fail the insertion if the user has disabled this notification.
|
||||
IF EXISTS (SELECT 1
|
||||
FROM notification_preferences
|
||||
WHERE disabled = TRUE
|
||||
AND user_id = NEW.user_id
|
||||
AND notification_template_id = NEW.notification_template_id) THEN
|
||||
RAISE EXCEPTION 'cannot enqueue message: user has disabled this notification';
|
||||
END IF;
|
||||
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Trigger to execute above function on insertion.
|
||||
CREATE TRIGGER inhibit_enqueue_if_disabled
|
||||
BEFORE INSERT
|
||||
ON notification_messages
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION inhibit_enqueue_if_disabled();
|
||||
|
||||
-- Allow modifications to notification templates to be audited.
|
||||
ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'notification_template';
|
5
coderd/database/migrations/testdata/fixtures/000238_notifications_preferences.up.sql
vendored
Normal file
5
coderd/database/migrations/testdata/fixtures/000238_notifications_preferences.up.sql
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
INSERT INTO notification_templates (id, name, title_template, body_template, "group")
|
||||
VALUES ('a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', 'A', 'title', 'body', 'Group 1') ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT INTO notification_preferences (user_id, notification_template_id, disabled, created_at, updated_at)
|
||||
VALUES ('a0061a8e-7db7-4585-838c-3116a003dd21', 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11', FALSE, '2024-07-15 10:30:00+00', '2024-07-15 10:30:00+00');
|
Reference in New Issue
Block a user