-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Setup trigger to insert team_member record
- Loading branch information
1 parent
380f27e
commit 459e21c
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
hasura.planx.uk/migrations/1696884217237_grant_new_user_template_team_access/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
28 changes: 28 additions & 0 deletions
28
hasura.planx.uk/migrations/1696884217237_grant_new_user_template_team_access/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |