Skip to content

Commit b7fda31

Browse files
fix: Issues observed when inviting members to organization either through teams migration during onboarding or explicit user invite (#22901)
* fix: make team migrations sequential to avoid race conditions Instead of processing team migrations in parallel using Promise.all, process them sequentially to avoid race conditions when creating profiles and memberships for team members. This simplifies the implementation and avoids the need for complex retry logic or database-level concurrency controls. * Promise.all accepts an array of promises and not an array with array in it as the first item. This fixes the issue where these promises were unhandled * Fix the issue where an existing pending membership with team stays pending even though theorganization membership gets created as accepted
1 parent 94a04f0 commit b7fda31

File tree

5 files changed

+533
-22
lines changed

5 files changed

+533
-22
lines changed

packages/trpc/server/routers/viewer/organizations/createTeams.handler.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,18 @@ export const createTeamsHandler = async ({ ctx, input }: CreateTeamsOptions) =>
9595
teamNames.map((item) => slugify(item)).includes(slug)
9696
);
9797

98-
await Promise.all(
99-
moveTeams
100-
.filter((team) => team.shouldMove)
101-
.map(async ({ id: teamId, newSlug }) => {
102-
await moveTeam({
103-
teamId,
104-
newSlug,
105-
org: {
106-
...organization,
107-
ownerId: organizationOwner.id,
108-
},
109-
creationSource,
110-
});
111-
})
112-
);
98+
// Process team migrations sequentially to avoid race conditions - Moving a team invites members to the organization again and there are known unique constraints failure in membership and profile creation if done in parallel and a user happens to be part of more than one team
99+
for (const team of moveTeams.filter((team) => team.shouldMove)) {
100+
await moveTeam({
101+
teamId: team.id,
102+
newSlug: team.newSlug,
103+
org: {
104+
...organization,
105+
ownerId: organizationOwner.id,
106+
},
107+
creationSource,
108+
});
109+
}
113110

114111
if (duplicatedSlugs.length === teamNames.length) {
115112
return { duplicatedSlugs };

0 commit comments

Comments
 (0)