Skip to content

Commit

Permalink
EW-893 removing some code
Browse files Browse the repository at this point in the history
  • Loading branch information
psachmann committed Jun 7, 2024
1 parent 36bbc96 commit 0101103
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,18 @@ import { AccountConfig } from '../../account-config';
import { AccountRepo } from '../../repo/micro-orm/account.repo';
import { Account } from '../account';
import { AccountSave } from '../account-save';
import { AbstractAccountService } from './account.service.abstract';

@Injectable()
export class AccountServiceDb {
export class AccountServiceDb extends AbstractAccountService {
constructor(
private readonly accountRepo: AccountRepo,
private readonly idmService: IdentityManagementService,
private readonly configService: ConfigService<AccountConfig, true>,
private readonly userRepo: UserRepo
) {}
) {
super();
}

async findById(id: EntityId): Promise<Account> {
const internalId = await this.getInternalId(id);
Expand Down Expand Up @@ -145,18 +148,9 @@ export class AccountServiceDb {
return account;
}

async isUniqueEmail(email: string, userId?: EntityId, accountId?: EntityId, systemId?: EntityId): Promise<boolean> {
public async isUniqueEmail(email: string): Promise<boolean> {
const foundUsers = await this.userRepo.findByEmail(email);
const [accounts] = await this.accountRepo.searchByUsernameExactMatch(email);
const filteredAccounts = accounts.filter((foundAccount) => foundAccount.systemId === systemId);

const multipleUsers = foundUsers.length > 1;
const multipleAccounts = filteredAccounts.length > 1;
// paranoid 'toString': legacy code may call userId or accountId as ObjectID
const oneUserWithoutGivenId = foundUsers.length === 1 && foundUsers[0].id.toString() !== userId?.toString();
const oneAccountWithoutGivenId =
filteredAccounts.length === 1 && filteredAccounts[0].id.toString() !== accountId?.toString();
const isUnique = !(multipleUsers || multipleAccounts || oneUserWithoutGivenId || oneAccountWithoutGivenId);
const isUnique = foundUsers.length === 0;

return isUnique;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ export class AccountServiceIdm extends AbstractAccountService {
throw new EntityNotFoundError(`Account with id ${id.toString()} not found`);
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
async isUniqueEmail(email: string, _userId?: EntityId, accountId?: EntityId, systemId?: EntityId): Promise<boolean> {
const kc = await this.identityManager.findAccountsByUsername(email);
return kc.length > 0;
public async isUniqueEmail(email: string): Promise<boolean> {
const [, count] = await this.identityManager.findAccountsByUsername(email);
const isUniqueEmail = count === 0;

return isUniqueEmail;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ export abstract class AbstractAccountService {

abstract searchByUsernameExactMatch(userName: string): Promise<Counted<Account[]>>;

abstract isUniqueEmail(email: string, userId?: EntityId, accountId?: EntityId, systemId?: EntityId): Promise<boolean>;
abstract isUniqueEmail(email: string): Promise<boolean>;
}
31 changes: 7 additions & 24 deletions apps/server/src/modules/account/domain/services/account.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class AccountService extends AbstractAccountService implements DeletionSe
): Promise<boolean> {
if (updateData.email && user.email !== updateData.email) {
const newMail = updateData.email.toLowerCase();
await this.checkUniqueEmail(account, user, newMail);
await this.checkUniqueEmail(newMail);
user.email = newMail;
accountSave.username = newMail;
return true;
Expand Down Expand Up @@ -166,7 +166,7 @@ export class AccountService extends AbstractAccountService implements DeletionSe
}
if (updateData.username !== undefined) {
const newMail = updateData.username.toLowerCase();
await this.checkUniqueEmail(targetAccount, targetUser, newMail);
await this.checkUniqueEmail(newMail);
targetUser.email = newMail;
targetAccount.username = newMail;
updateUser = true;
Expand Down Expand Up @@ -322,7 +322,7 @@ export class AccountService extends AbstractAccountService implements DeletionSe
// trimPassword hook will be done by class-validator ✔
// local.hooks.hashPassword('password'), will be done by account service ✔
// checkUnique ✔
if (!(await this.isUniqueEmail(accountSave.username, accountSave.userId, accountSave.id, accountSave.systemId))) {
if (!(await this.isUniqueEmail(accountSave.username))) {
throw new ValidationError('Username already exists');
}
// removePassword hook is not implemented
Expand Down Expand Up @@ -426,8 +426,8 @@ export class AccountService extends AbstractAccountService implements DeletionSe
return null;
}

private async checkUniqueEmail(account: Account, user: User, email: string): Promise<void> {
if (!(await this.isUniqueEmail(email, user.id, account.id, account.systemId))) {
private async checkUniqueEmail(email: string): Promise<void> {
if (!(await this.isUniqueEmail(email))) {
throw new ValidationError(`The email address is already in use!`);
}
}
Expand All @@ -438,26 +438,9 @@ export class AccountService extends AbstractAccountService implements DeletionSe
return foundAccounts;
}

public async isUniqueEmail(
email: string,
userId?: string | undefined,
accountId?: string | undefined,
systemId?: string | undefined
): Promise<boolean> {
const isUniqueEmailByKc = await this.accountIdm.isUniqueEmail(email, userId, accountId, systemId);
const isUniqueEmailByDb = await this.accountDb.isUniqueEmail(email, userId, accountId, systemId);
const isUniqueEmail = isUniqueEmailByKc && isUniqueEmailByDb;
public async isUniqueEmail(email: string): Promise<boolean> {
const isUniqueEmail = await this.accountImpl.isUniqueEmail(email);

return isUniqueEmail;
}

public async isUniqueEmailForUser(email: string, userId: EntityId): Promise<boolean> {
const account = await this.accountRepo.findByUserId(userId);
return this.isUniqueEmail(email, userId, account?.id, account?.systemId?.toString());
}

public async isUniqueEmailForAccount(email: string, accountId: EntityId): Promise<boolean> {
const account = await this.accountRepo.findById(accountId);
return this.isUniqueEmail(email, account.userId?.toString(), account.id, account.systemId?.toString());
}
}
2 changes: 1 addition & 1 deletion src/services/user/hooks/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const checkUniqueEmail = async (hook) => {

// get userId of user entry to edit
const editUserId = hook.id ? hook.id.toString() : undefined;
const unique = await hook.app.service('nest-account-service').isUniqueEmailForUser(email, editUserId);
const unique = await hook.app.service('nest-account-service').isUniqueEmail(email, editUserId);

if (unique) {
return hook;
Expand Down

0 comments on commit 0101103

Please sign in to comment.