From c283df9b0ac0da4d89422f08a04568f9918e6c2f Mon Sep 17 00:00:00 2001 From: "maruyama.t" Date: Wed, 25 Oct 2023 08:28:57 +0000 Subject: [PATCH] =?UTF-8?q?Merged=20PR=20521:=20API=E5=AE=9F=E8=A3=85?= =?UTF-8?q?=EF=BC=88=E3=83=A6=E3=83=BC=E3=82=B6=E5=90=8D=E5=8F=96=E5=BE=97?= =?UTF-8?q?API=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 概要 [Task2924: API実装(ユーザ名取得API)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2924) - 元PBI or タスクへのリンク(内容・目的などはそちらにあるはず) - 何をどう変更したか、追加したライブラリなど - このPull Requestでの対象/対象外 - 影響範囲(他の機能にも影響があるか) 新規のため、なし ## レビューポイント ユーザー存在チェックを行う必要性はあるか? ## 動作確認状況 - ローカルで確認 ## 補足 - 相談、参考資料などがあれば --- .../src/features/users/users.controller.ts | 3 +- .../src/features/users/users.service.spec.ts | 42 +++++++++++++++++++ .../src/features/users/users.service.ts | 39 ++++++++++++++++- 3 files changed, 81 insertions(+), 3 deletions(-) diff --git a/dictation_server/src/features/users/users.controller.ts b/dictation_server/src/features/users/users.controller.ts index c9d6a56..cee1a78 100644 --- a/dictation_server/src/features/users/users.controller.ts +++ b/dictation_server/src/features/users/users.controller.ts @@ -662,8 +662,7 @@ export class UsersController { } const { userId } = decodedAccessToken as AccessToken; const context = makeContext(userId); - const userName = 'TEST'; - //const userName = await this.usersService.getUserName(context, userId); + const userName = await this.usersService.getUserName(context, userId); return { userName }; } } diff --git a/dictation_server/src/features/users/users.service.spec.ts b/dictation_server/src/features/users/users.service.spec.ts index 6c5c2b4..e749058 100644 --- a/dictation_server/src/features/users/users.service.spec.ts +++ b/dictation_server/src/features/users/users.service.spec.ts @@ -2624,3 +2624,45 @@ describe('UsersService.updateAcceptedVersion', () => { ); }); }); + +describe('UsersService.getUserName', () => { + let source: DataSource | null = null; + + beforeEach(async () => { + source = new DataSource({ + type: 'sqlite', + database: ':memory:', + logging: false, + entities: [__dirname + '/../../**/*.entity{.ts,.js}'], + synchronize: true, + }); + return source.initialize(); + }); + + afterEach(async () => { + if (!source) return; + await source.destroy(); + source = null; + }); + + it('ユーザーが存在しない場合は、ユーザー未存在エラー', async () => { + if (!source) fail(); + + try { + const module = await makeTestingModule(source); + if (!module) fail(); + const context = makeContext(uuidv4()); + + const service = module.get(UsersService); + await service.getUserName(context, 'external_id'); + fail(); + } catch (e) { + if (e instanceof HttpException) { + expect(e.getStatus()).toEqual(HttpStatus.BAD_REQUEST); + expect(e.getResponse()).toEqual(makeErrorResponse('E010204')); + } else { + fail(); + } + } + }); +}); diff --git a/dictation_server/src/features/users/users.service.ts b/dictation_server/src/features/users/users.service.ts index 5ba3c84..66beb3c 100644 --- a/dictation_server/src/features/users/users.service.ts +++ b/dictation_server/src/features/users/users.service.ts @@ -4,7 +4,6 @@ import { makeErrorResponse } from '../../common/error/makeErrorResponse'; import { isVerifyError, verify } from '../../common/jwt'; import { getPublicKey } from '../../common/jwt/jwt'; import { makePassword } from '../../common/password/password'; -import { AccessToken, IDToken } from '../../common/token'; import { SortDirection, TaskListSortableAttribute, @@ -1052,4 +1051,42 @@ export class UsersService { ); } } + /** + * Azure AD B2Cからユーザー名を取得する + * @param context + * @param externalId + */ + async getUserName(context: Context, externalId: string): Promise { + this.logger.log(`[IN] [${context.trackingId}] ${this.getUserName.name}`); + + try { + // extarnalIdの存在チェックを行う + await this.usersRepository.findUserByExternalId(externalId); + // ADB2Cからユーザー名を取得する + const adb2cUser = await this.adB2cService.getUser(externalId); + return adb2cUser.displayName; + } catch (e) { + this.logger.error(`error=${e}`); + if (e instanceof Error) { + switch (e.constructor) { + case UserNotFoundError: + throw new HttpException( + makeErrorResponse('E010204'), + HttpStatus.BAD_REQUEST, + ); + default: + throw new HttpException( + makeErrorResponse('E009999'), + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + throw new HttpException( + makeErrorResponse('E009999'), + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } finally { + this.logger.log(`[OUT] [${context.trackingId}] ${this.getUserName.name}`); + } + } }