Skip to content

Commit

Permalink
feat: Setup trigger to insert team_member record
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Oct 9, 2023
1 parent 380f27e commit 459e21c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP FUNCTION IF EXISTS grant_new_user_template_team_access;
DROP TRIGGER grant_new_user_template_team_access on users;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
CREATE OR REPLACE FUNCTION grant_new_user_template_team_access() RETURNS trigger AS $$
DECLARE
templates_team_id INT;
BEGIN
SELECT id INTO templates_team_id FROM teams WHERE slug = 'templates';
IF templates_team_id IS NOT NULL THEN
INSERT INTO team_members (user_id, team_id, role) VALUES (NEW.id, templates_team_id, 'teamEditor');
END IF;

RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER grant_new_user_template_team_access AFTER INSERT ON users
FOR EACH ROW EXECUTE PROCEDURE grant_new_user_template_team_access();

COMMENT ON TRIGGER grant_new_user_template_team_access ON users
IS 'Automatically grant all new users teamEditor access to the shared Templates team';

-- Insert a record to team_members for all existing users
INSERT INTO
team_members (user_id, team_id, role)
SELECT
id,
29,
'teamEditor'
FROM
users;

0 comments on commit 459e21c

Please sign in to comment.