diff --git a/backend/src/services/auth/auth-signup-service.ts b/backend/src/services/auth/auth-signup-service.ts index ae27b113da..0007f24475 100644 --- a/backend/src/services/auth/auth-signup-service.ts +++ b/backend/src/services/auth/auth-signup-service.ts @@ -178,6 +178,27 @@ export const authSignupServiceFactory = ({ const hashedPassword = await crypto.hashing().createHash(password, appCfg.SALT_ROUNDS); const updateduser = await authDAL.transaction(async (tx) => { + const duplicateUsers = await userDAL.find( + { + email: user.email || sanitizedEmail, + isAccepted: false + }, + { tx } + ); + const duplicateUserIds = duplicateUsers + .filter((duplicateUser) => duplicateUser.id !== user.id) + .map((duplicateUser) => duplicateUser.id); + if (duplicateUserIds.length > 0) { + await userDAL.delete( + { + $in: { + id: duplicateUserIds + } + }, + tx + ); + } + const us = await userDAL.updateById(user.id, { firstName, lastName, isAccepted: true }, tx); if (!us) throw new Error("User not found"); @@ -342,6 +363,27 @@ export const authSignupServiceFactory = ({ const appCfg = getConfig(); const hashedPassword = await crypto.hashing().createHash(password, appCfg.SALT_ROUNDS); const updateduser = await authDAL.transaction(async (tx) => { + const duplicateUsers = await userDAL.find( + { + email: user.email || sanitizedEmail, + isAccepted: false + }, + { tx } + ); + const duplicateUserIds = duplicateUsers + .filter((duplicateUser) => duplicateUser.id !== user.id) + .map((duplicateUser) => duplicateUser.id); + if (duplicateUserIds.length > 0) { + await userDAL.delete( + { + $in: { + id: duplicateUserIds + } + }, + tx + ); + } + const us = await userDAL.updateById(user.id, { firstName, lastName, isAccepted: true }, tx); if (!us) throw new Error("User not found"); const userEncKey = await userDAL.upsertUserEncryptionKey( diff --git a/backend/src/services/user/user-dal.ts b/backend/src/services/user/user-dal.ts index 41b615b5df..eecc256299 100644 --- a/backend/src/services/user/user-dal.ts +++ b/backend/src/services/user/user-dal.ts @@ -199,8 +199,6 @@ export const userDALFactory = (db: TDbClient) => { const doc = await db(TableName.Users) .where({ email }) .leftJoin(TableName.Membership, `${TableName.Membership}.actorUserId`, `${TableName.Users}.id`) - .where(`${TableName.Membership}.scope`, AccessScope.Organization) - .whereNotNull(`${TableName.Membership}.actorUserId`) .leftJoin(TableName.Organization, `${TableName.Organization}.id`, `${TableName.Membership}.scopeOrgId`) .select(selectAllTableCols(TableName.Users)) .select(