From 98bba3d3a086069876324f6a94f849cada399b0e Mon Sep 17 00:00:00 2001 From: Ahmed Bouhuolia Date: Mon, 7 Sep 2020 17:43:07 +0200 Subject: [PATCH] fix: account name unique validation. --- server/src/http/controllers/Accounts.ts | 9 ++++-- .../src/services/Accounts/AccountsService.ts | 29 ++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/server/src/http/controllers/Accounts.ts b/server/src/http/controllers/Accounts.ts index 31d7212a8..a90d31971 100644 --- a/server/src/http/controllers/Accounts.ts +++ b/server/src/http/controllers/Accounts.ts @@ -204,9 +204,7 @@ export default class AccountsController extends BaseController{ try { const account = await this.accountsService.editAccount(tenantId, accountId, accountDTO); return res.status(200).send({ id: account.id }); - } catch (error) { - console.log(error); if (error instanceof ServiceError) { this.transformServiceErrorToResponse(res, error); } @@ -251,7 +249,6 @@ export default class AccountsController extends BaseController{ return res.status(200).send({ id: accountId }); } catch (error) { - console.log(error); if (error instanceof ServiceError) { this.transformServiceErrorToResponse(res, error); @@ -366,6 +363,12 @@ export default class AccountsController extends BaseController{ errors: [{ type: 'ACCOUNT.NOT.FOUND', code: 100 }] } ); } + if (error.errorType === 'account_name_not_unqiue') { + return res.boom.badRequest( + 'The given account not unique.', + { errors: [{ type: 'ACCOUNT.NAME.NOT.UNIQUE', code: 150 }], } + ); + } if (error.errorType === 'account_type_not_found') { return res.boom.badRequest( 'The given account type not found.', { diff --git a/server/src/services/Accounts/AccountsService.ts b/server/src/services/Accounts/AccountsService.ts index 2fbe7e341..688b23e6a 100644 --- a/server/src/services/Accounts/AccountsService.ts +++ b/server/src/services/Accounts/AccountsService.ts @@ -3,6 +3,7 @@ import TenancyService from '@/services/Tenancy/TenancyService'; import { ServiceError } from '@/exceptions'; import { IAccountDTO, IAccount } from '@/interfaces'; import { difference } from 'lodash'; +import { Account } from 'src/models'; @Service() export default class AccountsService { @@ -127,14 +128,37 @@ export default class AccountsService { } } + /** + * Validates the account name uniquiness. + * @param {number} tenantId + * @param {string} accountName + * @param {number} notAccountId - Ignore the account id. + */ + private async validateAccountNameUniquiness(tenantId: number, accountName: string, notAccountId?: number) { + const { Account } = this.tenancy.models(tenantId); + + this.logger.info('[accounts] validating account name uniquiness.', { tenantId, accountName, notAccountId }); + const foundAccount = await Account.query().findOne('name', accountName).onBuild((query) => { + if (notAccountId) { + query.whereNot('id', notAccountId); + } + }); + if (foundAccount) { + throw new ServiceError('account_name_not_unqiue'); + } + } + /** * Creates a new account on the storage. * @param {number} tenantId * @param {IAccount} accountDTO - * @returns {IAccount} + * @returns {IAccount} */ public async newAccount(tenantId: number, accountDTO: IAccountDTO) { const { Account } = this.tenancy.models(tenantId); + + // Validate account name uniquiness. + await this.validateAccountNameUniquiness(tenantId, accountDTO.name); // Validate the account code uniquiness. if (accountDTO.code) { @@ -165,6 +189,9 @@ export default class AccountsService { const { Account } = this.tenancy.models(tenantId); const oldAccount = await this.getAccountOrThrowError(tenantId, accountId); + // Validate account name uniquiness. + await this.validateAccountNameUniquiness(tenantId, accountDTO.name, accountId); + await this.isAccountTypeChangedOrThrowError(oldAccount, accountDTO); // Validate the account code not exists on the storage.