Delete unaccepted users upon merge user op

This commit is contained in:
Tuan Dang
2024-06-11 18:41:35 -07:00
parent 7626dbb96e
commit cb9dabe03f
2 changed files with 18 additions and 12 deletions

View File

@@ -94,14 +94,6 @@ export const userDALFactory = (db: TDbClient) => {
}
};
const findMergeableUsers = async (email: string, tx?: Knex) => {
const users = await (tx || db)(TableName.Users).where((builder) => {
void builder.where({ email, isEmailVerified: true }).orWhere({ email, isAccepted: false });
});
return users;
};
const updateUserEncryptionByUserId = async (userId: string, data: TUserEncryptionKeysUpdate, tx?: Knex) => {
try {
const [userEnc] = await (tx || db)(TableName.UserEncryptionKey)
@@ -162,7 +154,6 @@ export const userDALFactory = (db: TDbClient) => {
findUsersByProjectMembershipIds,
upsertUserEncryptionKey,
createUserEncryption,
findMergeableUsers,
findOneUserAction,
createUserAction
};

View File

@@ -12,7 +12,6 @@ type TUserServiceFactoryDep = {
userDAL: Pick<
TUserDALFactory,
| "find"
| "findMergeableUsers"
| "findOne"
| "findById"
| "transaction"
@@ -22,6 +21,7 @@ type TUserServiceFactoryDep = {
| "findOneUserAction"
| "createUserAction"
| "findUserEncKeyByUserId"
| "delete"
>;
userAliasDAL: Pick<TUserAliasDALFactory, "find" | "insertMany">;
orgMembershipDAL: Pick<TOrgMembershipDALFactory, "find" | "insertMany">;
@@ -86,8 +86,14 @@ export const userServiceFactory = ({
tx
);
// check if there are users with the same email.
const users = await userDAL.findMergeableUsers(email, tx);
// check if there are verified users with the same email.
const users = await userDAL.find(
{
email,
isEmailVerified: true
},
{ tx }
);
if (users.length > 1) {
// merge users
@@ -129,6 +135,15 @@ export const userServiceFactory = ({
);
}
} else {
await userDAL.delete(
{
email,
isAccepted: false,
isEmailVerified: false
},
tx
);
// update current user's username to [email]
await userDAL.updateById(
user.id,