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}`); + } + } }