feat: add check for existing organization members to prevent duplicates during member creation
This commit is contained in:
@@ -165,6 +165,31 @@ export const checkEmailExistsInOrg = async (orgId: string, email: string): Promi
|
||||
return result.length > 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if an org has a member matching the given profileId or email.
|
||||
* Used to avoid creating duplicate members (e.g. when user has a pending invite).
|
||||
*/
|
||||
export const hasOrgMemberByProfileIdOrEmail = async (
|
||||
orgId: string,
|
||||
profileId: string,
|
||||
email: string | null | undefined
|
||||
): Promise<boolean> => {
|
||||
const conditions = [eq(schema.organizationmember.organizationId, orgId)];
|
||||
|
||||
const profileOrEmailMatch = or(
|
||||
eq(schema.organizationmember.profileId, profileId),
|
||||
...(email ? [eq(schema.organizationmember.email, email.toLowerCase().trim())] : [])
|
||||
);
|
||||
|
||||
const result = await db
|
||||
.select({ id: schema.organizationmember.id })
|
||||
.from(schema.organizationmember)
|
||||
.where(and(...conditions, profileOrEmailMatch))
|
||||
.limit(1);
|
||||
|
||||
return result.length > 0;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks which emails already exist as team members in an organization (bulk check)
|
||||
* Checks both organizationmember.email and profile.email (via organizationmember.profileId)
|
||||
|
||||
Reference in New Issue
Block a user