Merged PR 521: API実装(ユーザ名取得API)

## 概要
[Task2924: API実装(ユーザ名取得API)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2924)

- 元PBI or タスクへのリンク(内容・目的などはそちらにあるはず)
- 何をどう変更したか、追加したライブラリなど
- このPull Requestでの対象/対象外
- 影響範囲(他の機能にも影響があるか)
新規のため、なし

## レビューポイント
ユーザー存在チェックを行う必要性はあるか?

## 動作確認状況
- ローカルで確認

## 補足
- 相談、参考資料などがあれば
This commit is contained in:
maruyama.t 2023-10-25 08:28:57 +00:00
parent 911d028073
commit c283df9b0a
3 changed files with 81 additions and 3 deletions

View File

@ -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 };
}
}

View File

@ -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>(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();
}
}
});
});

View File

@ -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<string> {
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}`);
}
}
}