-
Notifications
You must be signed in to change notification settings - Fork 513
project owner team #835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
project owner team #835
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
7211b79
wip
BilalG1 0bda63e
teams fixes
BilalG1 522b532
fix test
BilalG1 24095dc
fix test
BilalG1 43d7cea
single statement migration
BilalG1 1932f50
fixed migration file
BilalG1 f6ef3f8
fix migration
BilalG1 f51e3c2
fix tests
BilalG1 b55453a
fix tests
BilalG1 4e6e45f
fix migration
BilalG1 6ea4ab7
Merge branch 'dev' into project-owner-team
BilalG1 b3470bc
Merge dev into project-owner-team
N2D4 613ef5b
Merge dev into project-owner-team
N2D4 e457b13
fix migration script
BilalG1 867819e
pr fixes
BilalG1 c7611c1
merge
BilalG1 be54aff
fix type
BilalG1 ddb9696
fix tests
BilalG1 f838606
fix tests
BilalG1 3f264c5
fix switcher
BilalG1 aa40673
Merge branch 'dev' into project-owner-team
BilalG1 21c8bcf
Merge dev into project-owner-team
N2D4 97b8988
Merge dev into project-owner-team
N2D4 31cab05
fix test and team-switcher
BilalG1 14cbfd1
merge dev
BilalG1 7a16c41
Merge branch 'dev' into project-owner-team
BilalG1 0f6f12b
fix tests and lint
BilalG1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
202 changes: 202 additions & 0 deletions
202
apps/backend/prisma/migrations/20250806171211_add_team_based_project_ownership/migration.sql
This file contains hidden or 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,202 @@ | ||
| -- Add team-based project ownership | ||
| -- Step 1: Add ownerTeamId column to Project table | ||
| ALTER TABLE "Project" ADD COLUMN "ownerTeamId" UUID; | ||
|
|
||
| --SPLIT_STATEMENT_SENTINEL | ||
|
|
||
| -- SINGLE_STATEMENT_SENTINEL | ||
| -- Step 2: For each existing user with managed projects, create a personal team and assign their projects to it | ||
|
BilalG1 marked this conversation as resolved.
|
||
| DO $$ | ||
| DECLARE | ||
| user_record RECORD; | ||
| project_id_text TEXT; | ||
| team_uuid UUID; | ||
| managed_project_ids JSONB; | ||
| owners_count INTEGER; | ||
| existing_owner_team_uuid UUID; | ||
| group_team_uuid UUID; | ||
| group_project_display_name TEXT; | ||
| BEGIN | ||
| -- Loop through all users in the 'internal' project who have managed projects | ||
| FOR user_record IN | ||
| SELECT | ||
| pu."tenancyId", | ||
| pu."projectUserId", | ||
| pu."displayName", | ||
| pu."mirroredProjectId", | ||
| pu."mirroredBranchId", | ||
| pu."serverMetadata", | ||
| cc."value" as contact_value | ||
| FROM "ProjectUser" pu | ||
| LEFT JOIN "ContactChannel" cc | ||
| ON cc."projectUserId" = pu."projectUserId" | ||
| AND cc."type" = 'EMAIL' | ||
| AND cc."isPrimary" = 'TRUE' | ||
|
N2D4 marked this conversation as resolved.
BilalG1 marked this conversation as resolved.
|
||
| WHERE pu."mirroredProjectId" = 'internal' | ||
|
BilalG1 marked this conversation as resolved.
|
||
| AND pu."serverMetadata" IS NOT NULL | ||
| AND pu."serverMetadata"::jsonb ? 'managedProjectIds' | ||
| LOOP | ||
| -- Extract managedProjectIds from serverMetadata | ||
| managed_project_ids := user_record."serverMetadata"::jsonb -> 'managedProjectIds'; | ||
|
|
||
| -- Skip if managedProjectIds is not an array or is empty | ||
| IF managed_project_ids IS NULL OR jsonb_array_length(managed_project_ids) = 0 THEN | ||
| CONTINUE; | ||
| END IF; | ||
|
BilalG1 marked this conversation as resolved.
|
||
|
|
||
| -- Create a personal team for this user | ||
| team_uuid := gen_random_uuid(); | ||
|
|
||
| INSERT INTO "Team" ( | ||
| "tenancyId", | ||
| "teamId", | ||
| "mirroredProjectId", | ||
| "mirroredBranchId", | ||
| "displayName", | ||
| "createdAt", | ||
| "updatedAt" | ||
| ) VALUES ( | ||
| user_record."tenancyId", | ||
| team_uuid, | ||
| user_record."mirroredProjectId", | ||
| user_record."mirroredBranchId", | ||
| COALESCE(user_record."displayName", user_record.contact_value, 'User') || '''s Team', | ||
| NOW(), | ||
| NOW() | ||
| ); | ||
|
|
||
| -- Add the user as a team member | ||
| INSERT INTO "TeamMember" ( | ||
| "tenancyId", | ||
| "projectUserId", | ||
| "teamId", | ||
| "isSelected", | ||
| "createdAt", | ||
| "updatedAt" | ||
| ) VALUES ( | ||
| user_record."tenancyId", | ||
| user_record."projectUserId", | ||
| team_uuid, | ||
| NULL, | ||
| NOW(), | ||
| NOW() | ||
| ); | ||
|
|
||
| -- Assign all managed projects to this team | ||
| FOR i IN 0..jsonb_array_length(managed_project_ids) - 1 | ||
| LOOP | ||
| project_id_text := managed_project_ids ->> i; | ||
| -- Determine how many users own/manage this project | ||
| SELECT COUNT(*) INTO owners_count | ||
| FROM "ProjectUser" pu | ||
| WHERE pu."mirroredProjectId" = 'internal' | ||
| AND pu."serverMetadata" IS NOT NULL | ||
| AND (pu."serverMetadata"::jsonb ? 'managedProjectIds') | ||
| AND EXISTS ( | ||
| SELECT 1 | ||
| FROM jsonb_array_elements_text(pu."serverMetadata"::jsonb -> 'managedProjectIds') AS elem | ||
| WHERE elem = project_id_text | ||
| ); | ||
|
|
||
| IF owners_count = 1 THEN | ||
| -- Single owner: assign to the personal team | ||
| UPDATE "Project" | ||
| SET "ownerTeamId" = team_uuid | ||
| WHERE "id" = project_id_text; | ||
| ELSE | ||
|
BilalG1 marked this conversation as resolved.
|
||
| -- Multiple owners: ensure there is a shared team for all owners and assign the project to it | ||
| SELECT "ownerTeamId" INTO existing_owner_team_uuid | ||
| FROM "Project" | ||
| WHERE "id" = project_id_text; | ||
|
|
||
| IF existing_owner_team_uuid IS NULL THEN | ||
| -- Create a shared team for this project's owners (only once) | ||
| group_team_uuid := gen_random_uuid(); | ||
|
|
||
| -- Use project display name if available for a nicer team name | ||
| SELECT COALESCE(p."displayName", 'Project') INTO group_project_display_name | ||
| FROM "Project" p | ||
| WHERE p."id" = project_id_text; | ||
|
|
||
| INSERT INTO "Team" ( | ||
| "tenancyId", | ||
| "teamId", | ||
| "mirroredProjectId", | ||
| "mirroredBranchId", | ||
| "displayName", | ||
| "createdAt", | ||
| "updatedAt" | ||
| ) VALUES ( | ||
| user_record."tenancyId", | ||
| group_team_uuid, | ||
| user_record."mirroredProjectId", | ||
| user_record."mirroredBranchId", | ||
| group_project_display_name || ' Owners', | ||
| NOW(), | ||
| NOW() | ||
| ); | ||
|
|
||
| -- Add all owners as members of the shared team with isSelected unset (NULL) | ||
| INSERT INTO "TeamMember" ( | ||
| "tenancyId", | ||
| "projectUserId", | ||
| "teamId", | ||
| "createdAt", | ||
| "updatedAt" | ||
| ) | ||
| SELECT | ||
| user_record."tenancyId", | ||
| pu."projectUserId", | ||
| group_team_uuid, | ||
| NOW(), | ||
| NOW() | ||
| FROM "ProjectUser" pu | ||
| WHERE pu."mirroredProjectId" = 'internal' | ||
| AND pu."serverMetadata" IS NOT NULL | ||
| AND (pu."serverMetadata"::jsonb ? 'managedProjectIds') | ||
| AND EXISTS ( | ||
| SELECT 1 | ||
| FROM jsonb_array_elements_text(pu."serverMetadata"::jsonb -> 'managedProjectIds') AS elem | ||
| WHERE elem = project_id_text | ||
| ) | ||
| ON CONFLICT ("tenancyId", "projectUserId", "teamId") DO NOTHING; | ||
|
|
||
| -- Point the project to the shared team | ||
| UPDATE "Project" | ||
| SET "ownerTeamId" = group_team_uuid | ||
| WHERE "id" = project_id_text; | ||
| ELSE | ||
| -- Shared team already exists: ensure current and all owners are members; then ensure project points to it | ||
| INSERT INTO "TeamMember" ( | ||
| "tenancyId", | ||
| "projectUserId", | ||
| "teamId", | ||
| "createdAt", | ||
| "updatedAt" | ||
| ) | ||
| SELECT | ||
| user_record."tenancyId", | ||
| pu."projectUserId", | ||
| existing_owner_team_uuid, | ||
| NOW(), | ||
| NOW() | ||
| FROM "ProjectUser" pu | ||
| WHERE pu."mirroredProjectId" = 'internal' | ||
| AND pu."serverMetadata" IS NOT NULL | ||
| AND (pu."serverMetadata"::jsonb ? 'managedProjectIds') | ||
| AND EXISTS ( | ||
| SELECT 1 | ||
| FROM jsonb_array_elements_text(pu."serverMetadata"::jsonb -> 'managedProjectIds') AS elem | ||
| WHERE elem = project_id_text | ||
| ) | ||
| ON CONFLICT ("tenancyId", "projectUserId", "teamId") DO NOTHING; | ||
|
|
||
| UPDATE "Project" | ||
| SET "ownerTeamId" = existing_owner_team_uuid | ||
| WHERE "id" = project_id_text; | ||
| END IF; | ||
| END IF; | ||
| END LOOP; | ||
|
|
||
| END LOOP; | ||
| END $$; | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.