saito.k f1583cf783 Merged PR 611: 操作を特定できる文字列を追跡用のIDに追加する(IPアドレスもログに出力する)
## 概要
[Task3265: IPアドレスを追跡用のIDに追加する](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/3265)

- MiddlewareでUUIDを発行しリクエストのヘッダに追加する
- 各コントローラーではヘッダからUUIDとIPアドレスを取得する
  - 取得したUUIDとADB2Cの外部IDでトラッキングIDを作成する
  - 作成したトラッキングIDとIPアドレスの繋がりをログに出力する。

## レビューポイント
- ADB2Cの外部IDがない場合にUnauthorized Userという文字列を入れているがほかの表現のほうが良いか
  - 外部IDもオプショナルにして入れなくてもよくする?
-

## UIの変更
- Before/Afterのスクショなど
- スクショ置き場

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

## 補足
- 相談、参考資料などがあれば
2023-12-12 04:11:36 +00:00

62 lines
1.7 KiB
TypeScript

import {
Controller,
HttpStatus,
Get,
Logger,
HttpException,
Req,
} from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { TermsService } from '../terms/terms.service';
import { ErrorResponse } from '../../common/error/types/types';
import { makeContext, retrieveRequestId, retrieveIp } from '../../common/log';
import { GetTermsInfoResponse } from './types/types';
import { makeErrorResponse } from '../../common/error/makeErrorResponse';
import { Request } from 'express';
@ApiTags('terms')
@Controller('terms')
export class TermsController {
private readonly logger = new Logger(TermsController.name);
constructor(
private readonly termsService: TermsService, //private readonly cryptoService: CryptoService,
) {}
@Get()
@ApiResponse({
status: HttpStatus.OK,
type: GetTermsInfoResponse,
description: '成功時のレスポンス',
})
@ApiResponse({
status: HttpStatus.INTERNAL_SERVER_ERROR,
description: '想定外のサーバーエラー',
type: ErrorResponse,
})
@ApiOperation({ operationId: 'getTermsInfo' })
async getTermsInfo(@Req() req: Request): Promise<GetTermsInfoResponse> {
const ip = retrieveIp(req);
if (!ip) {
throw new HttpException(
makeErrorResponse('E000401'),
HttpStatus.UNAUTHORIZED,
);
}
const requestId = retrieveRequestId(req);
if (!requestId) {
throw new HttpException(
makeErrorResponse('E000501'),
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
const context = makeContext('anonymous', ip, requestId);
this.logger.log(`[${context.getTrackingId()}] ip : ${ip}`);
const termsInfo = await this.termsService.getTermsInfo(context);
return { termsInfo };
}
}