Skip to content

Commit 616cdf4

Browse files
committed
feat(schema): use default team for invited users without specific teams
- apply_pending_team_memberships now adds users to default_new_user_team_id when they're invited without specific team assignments - Also acts as fallback for users joining via org code - Admin can configure default team via organization settings
1 parent 6bc44ad commit 616cdf4

File tree

1 file changed

+34
-4
lines changed

1 file changed

+34
-4
lines changed

supabase/schema.sql

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6258,6 +6258,7 @@ CREATE TRIGGER claim_pending_membership_trigger
62586258
EXECUTE FUNCTION claim_pending_membership();
62596259

62606260
-- Function to add team memberships, vault access, and workflow roles after user is created
6261+
-- If no teams specified in invite, adds user to org's default_new_user_team_id (if configured)
62616262
CREATE OR REPLACE FUNCTION apply_pending_team_memberships(p_user_id UUID)
62626263
RETURNS void AS $$
62636264
DECLARE
@@ -6266,9 +6267,11 @@ DECLARE
62666267
vault_id UUID;
62676268
role_id UUID;
62686269
user_email TEXT;
6270+
user_org_id UUID;
6271+
default_team UUID;
62696272
BEGIN
6270-
-- Get the user's email to find their pending membership
6271-
SELECT email INTO user_email FROM users WHERE id = p_user_id;
6273+
-- Get the user's email and org_id to find their pending membership
6274+
SELECT email, org_id INTO user_email, user_org_id FROM users WHERE id = p_user_id;
62726275

62736276
-- Find the pending membership by email (not claimed_by, since that's set here)
62746277
SELECT * INTO pending
@@ -6282,14 +6285,27 @@ BEGIN
62826285
UPDATE pending_org_members
62836286
SET claimed_at = NOW(), claimed_by = p_user_id
62846287
WHERE id = pending.id;
6285-
-- Add user to each pre-assigned team
6286-
IF pending.team_ids IS NOT NULL THEN
6288+
6289+
-- Add user to pre-assigned teams, OR to default team if none specified
6290+
IF pending.team_ids IS NOT NULL AND array_length(pending.team_ids, 1) > 0 THEN
6291+
-- Invited with specific teams - add to those
62876292
FOREACH team_id IN ARRAY pending.team_ids
62886293
LOOP
62896294
INSERT INTO team_members (team_id, user_id, added_by)
62906295
VALUES (team_id, p_user_id, pending.created_by)
62916296
ON CONFLICT (team_id, user_id) DO NOTHING;
62926297
END LOOP;
6298+
ELSE
6299+
-- No teams specified - add to org's default team (if configured)
6300+
SELECT default_new_user_team_id INTO default_team
6301+
FROM organizations
6302+
WHERE id = pending.org_id;
6303+
6304+
IF default_team IS NOT NULL THEN
6305+
INSERT INTO team_members (team_id, user_id, added_by)
6306+
VALUES (default_team, p_user_id, pending.created_by)
6307+
ON CONFLICT (team_id, user_id) DO NOTHING;
6308+
END IF;
62936309
END IF;
62946310

62956311
-- Apply vault access restrictions (if any vaults specified, restrict to those)
@@ -6311,6 +6327,20 @@ BEGIN
63116327
ON CONFLICT (role_id, user_id) DO NOTHING;
63126328
END LOOP;
63136329
END IF;
6330+
ELSE
6331+
-- No pending invite - user joined via org code
6332+
-- They should already be in default team from join_org_by_slug, but ensure it here as fallback
6333+
IF user_org_id IS NOT NULL THEN
6334+
SELECT default_new_user_team_id INTO default_team
6335+
FROM organizations
6336+
WHERE id = user_org_id;
6337+
6338+
IF default_team IS NOT NULL THEN
6339+
INSERT INTO team_members (team_id, user_id, added_by)
6340+
VALUES (default_team, p_user_id, NULL)
6341+
ON CONFLICT (team_id, user_id) DO NOTHING;
6342+
END IF;
6343+
END IF;
63146344
END IF;
63156345
END;
63166346
$$ LANGUAGE plpgsql SECURITY DEFINER;

0 commit comments

Comments
 (0)