From 4ec3d7c51f0880c5bcd84a685ed57ef02cb86d8f Mon Sep 17 00:00:00 2001 From: "oura.a" Date: Thu, 25 May 2023 06:54:50 +0000 Subject: [PATCH] =?UTF-8?q?Merged=20PR=20114:=20[MISO=E3=83=81=E3=83=BC?= =?UTF-8?q?=E3=83=A0]=20=E3=83=A6=E3=83=BC=E3=82=B6=E8=BF=BD=E5=8A=A0?= =?UTF-8?q?=E6=99=82=E3=81=AEAuthorID=E3=81=AE=E9=87=8D=E8=A4=87=E3=83=81?= =?UTF-8?q?=E3=82=A7=E3=83=83=E3=82=AF=E3=81=8C=E3=82=A8=E3=83=A9=E3=83=BC?= =?UTF-8?q?=E3=81=AE=E5=A0=B4=E5=90=88=E3=81=ABAzure=E3=81=AB=E3=82=A2?= =?UTF-8?q?=E3=82=AB=E3=82=A6=E3=83=B3=E3=83=88=E8=BF=BD=E5=8A=A0=E3=81=97?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=82=88=E3=81=86=E4=BF=AE=E6=AD=A3=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 概要 [Task1765: [MISOチーム] ユーザ追加時のAuthorIDの重複チェックがエラーの場合にAzureにアカウント追加しないよう修正する](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/1765) タスク 1765: [MISOチーム] ユーザ追加時のAuthorIDの重複チェックがエラーの場合にAzureにアカウント追加しないよう修正する ・ユーザ追加時、Azureへのユーザ登録前に同一アカウント内のAuthorIDの重複チェックを追加。 ## レビューポイント 処理に問題がないか。 ## UIの変更 なし ## 動作確認状況 ローカルでユーザ追加APIを実行し、同一AuthorIDの際にエラーが出ることを確認。 ## 補足 なし --- .../features/users/test/users.service.mock.ts | 7 +++ .../src/features/users/users.service.spec.ts | 45 +++++++++++++++++-- .../src/features/users/users.service.ts | 22 +++++++++ .../users/users.repository.service.ts | 24 ++++++++++ 4 files changed, 95 insertions(+), 3 deletions(-) diff --git a/dictation_server/src/features/users/test/users.service.mock.ts b/dictation_server/src/features/users/test/users.service.mock.ts index 803014e..53c590a 100644 --- a/dictation_server/src/features/users/test/users.service.mock.ts +++ b/dictation_server/src/features/users/test/users.service.mock.ts @@ -21,6 +21,7 @@ export type UsersRepositoryMockValue = { createNormalUser: User | Error; findSameAccountUsers: User[] | Error; findUserByExternalId: User | Error; + existsAuthorId: boolean | Error; }; export type AdB2cMockValue = { @@ -188,6 +189,7 @@ export const makeUsersRepositoryMock = (value: UsersRepositoryMockValue) => { createNormalUser, findSameAccountUsers, findUserByExternalId, + existsAuthorId, } = value; const aIdError = new authorIdError('ER_DUP_ENTRY'); @@ -219,6 +221,10 @@ export const makeUsersRepositoryMock = (value: UsersRepositoryMockValue) => { findUserByExternalId instanceof Error ? jest.fn, []>().mockRejectedValue(findUserByExternalId) : jest.fn, []>().mockResolvedValue(findUserByExternalId), + existsAuthorId: + existsAuthorId instanceof Error + ? jest.fn, []>().mockRejectedValue(existsAuthorId) + : jest.fn, []>().mockResolvedValue(existsAuthorId), }; }; @@ -332,5 +338,6 @@ export const makeDefaultUsersRepositoryMockValue = createNormalUser: newUser, findSameAccountUsers: [user1, user2], findUserByExternalId: newUser, + existsAuthorId: false, }; }; diff --git a/dictation_server/src/features/users/users.service.spec.ts b/dictation_server/src/features/users/users.service.spec.ts index 69543b8..bc1a693 100644 --- a/dictation_server/src/features/users/users.service.spec.ts +++ b/dictation_server/src/features/users/users.service.spec.ts @@ -460,7 +460,46 @@ it('メールアドレスが重複している場合、エラーとなる。', a new HttpException(makeErrorResponse('E010301'), HttpStatus.BAD_REQUEST), ); }); -it('AuthorIDが重複している場合、エラーとなる。', async () => { +it('AuthorIDが重複している場合、エラーとなる。(AuthorID重複チェックでエラー)', async () => { + const cryptoMockValue = makeDefaultCryptoMockValue(); + const usersRepositoryMockValue = makeDefaultUsersRepositoryMockValue(); + const adb2cParam = makeDefaultAdB2cMockValue(); + const sendgridMockValue = makeDefaultSendGridlValue(); + const configMockValue = makeDefaultConfigValue(); + usersRepositoryMockValue.createNormalUser = new Error(); + usersRepositoryMockValue.existsAuthorId = true; + + const service = await makeUsersServiceMock( + cryptoMockValue, + usersRepositoryMockValue, + adb2cParam, + sendgridMockValue, + configMockValue, + ); + const name = 'test_user8'; + const role = 'Author'; + const email = 'test8@example.co.jp'; + const autoRenew = true; + const licenseAlert = true; + const notification = true; + const authorId = 'testID'; + const token: AccessToken = { userId: '0001', role: '' }; + await expect( + service.createUser( + token, + name, + role, + email, + autoRenew, + licenseAlert, + notification, + authorId, + ), + ).rejects.toEqual( + new HttpException(makeErrorResponse('E010302'), HttpStatus.BAD_REQUEST), + ); +}); +it('AuthorIDが重複している場合、エラーとなる。(insert失敗)', async () => { const cryptoMockValue = makeDefaultCryptoMockValue(); const usersRepositoryMockValue = makeDefaultUsersRepositoryMockValue(); const adb2cParam = makeDefaultAdB2cMockValue(); @@ -476,9 +515,9 @@ it('AuthorIDが重複している場合、エラーとなる。', async () => { sendgridMockValue, configMockValue, ); - const name = 'test_user8'; + const name = 'test_user9'; const role = 'Author'; - const email = 'test8@example.co.jp'; + const email = 'test9@example.co.jp'; const autoRenew = true; const licenseAlert = true; const notification = true; diff --git a/dictation_server/src/features/users/users.service.ts b/dictation_server/src/features/users/users.service.ts index d56e0e8..a80a9df 100644 --- a/dictation_server/src/features/users/users.service.ts +++ b/dictation_server/src/features/users/users.service.ts @@ -114,6 +114,28 @@ export class UsersService { const accountId = adminUser.account_id; + //authorIdが重複していないかチェックする + if (authorId) { + let isAuthorIdDuplicated = false; + try { + isAuthorIdDuplicated = await this.usersRepository.existsAuthorId( + accountId, + authorId, + ); + } catch (e) { + throw new HttpException( + makeErrorResponse('E009999'), + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + if (isAuthorIdDuplicated) { + throw new HttpException( + makeErrorResponse('E010302'), + HttpStatus.BAD_REQUEST, + ); + } + } + // ランダムなパスワードを生成する const ramdomPassword = makePassword(); diff --git a/dictation_server/src/repositories/users/users.repository.service.ts b/dictation_server/src/repositories/users/users.repository.service.ts index 6318a9a..da1ad6d 100644 --- a/dictation_server/src/repositories/users/users.repository.service.ts +++ b/dictation_server/src/repositories/users/users.repository.service.ts @@ -122,6 +122,30 @@ export class UsersRepositoryService { return user; } + /** + * AuthorIdが既に存在するか確認する + * @param user + * @returns 存在する:true 存在しない:false + */ + async existsAuthorId( + accountId: number, + authorId: string, + ): Promise { + const user = await this.dataSource.getRepository(User).findOne({ + where: [ + { + account_id: accountId, + author_id: authorId, + }, + ], + }); + + if (user) { + return true; + } + return false; + } + /** * 特定の情報でユーザーを更新する * @param user