fix: Errors seen in some cases when organization is created through webhook (#20249)

This commit is contained in:
Hariom Balhara
2025-03-20 13:02:32 +00:00
committed by GitHub
parent 6bb8a7c6a9
commit 207147f5ff
3 changed files with 19 additions and 2 deletions
@@ -25,6 +25,16 @@ export const createAProfileForAnExistingUser = async ({
if (!org) {
throw new Error(`Organization with id ${organizationId} not found`);
}
const existingProfile = await ProfileRepository.findByUserIdAndOrgId({
userId: user.id,
organizationId,
});
if (existingProfile) {
return existingProfile;
}
const usernameInOrg = getOrgUsernameFromEmail(
user.email,
org.organizationSettings?.orgAutoAcceptEmail ?? null
@@ -243,6 +243,9 @@ async function moveTeam({
language: "en",
inviterName: null,
teamId: org.id,
// This is important so that if we re-invite existing users accidentally, we don't endup erroring out.
// Because this is a bulk action that could be taken from organization payment webhook, we could have cases where a user was just invited through another team migration in parallel.
isDirectUserAction: false,
});
}
@@ -141,16 +141,20 @@ export const inviteMembersWithNoInviterPermissionCheck = async (
role: MembershipRole;
}[];
creationSource: CreationSource;
/**
* Whether invitation is a direct user action or not i.e. we need to show them User based errors like inviting existing users or not.
*/
isDirectUserAction?: boolean;
} & TargetTeam
) => {
const { inviterName, orgSlug, invitations, language, creationSource } = data;
const { inviterName, orgSlug, invitations, language, creationSource, isDirectUserAction = true } = data;
const myLog = log.getSubLogger({ prefix: ["inviteMembers"] });
const translation = await getTranslation(language ?? "en", "common");
const team = "team" in data ? data.team : await getTeamOrThrow(data.teamId);
const isTeamAnOrg = team.isOrganization;
const uniqueInvitations = await getUniqueInvitationsOrThrowIfEmpty(invitations);
const beSilentAboutErrors = shouldBeSilentAboutErrors(uniqueInvitations);
const beSilentAboutErrors = shouldBeSilentAboutErrors(uniqueInvitations) || !isDirectUserAction;
const existingUsersToBeInvited = await findUsersWithInviteStatus({
invitations: uniqueInvitations,
team,