makabe.t 794099f37d Merged PR 292: 外部連携APIにログを入れ込む(強化)
## 概要
[Task2294: 外部連携APIにログを入れ込む(強化)](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2294)

- 外部連携APIのログを強化しました。
  - contextオブジェクトで操作者情報を渡すようにしています。
- ログポリシーに従って追加しています。
  - [ログポリシー](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/OMDSDictation/_wiki/wikis/OMDSDictation_wiki/223/%E3%83%AD%E3%82%B0%E3%83%9D%E3%83%AA%E3%82%B7%E3%83%BC)

## レビューポイント
- 出力内容に過不足はないか
- ログ追加対象に過不足はないか。
- contextで操作者情報を渡しているが想定通りか

## UIの変更
- なし

## 動作確認状況
- ローカルで確認
2023-08-02 01:07:02 +00:00

76 lines
2.4 KiB
TypeScript

import { HttpException, HttpStatus, Injectable, Logger } from '@nestjs/common';
import { makeErrorResponse } from '../../common/error/makeErrorResponse';
import { NotificationhubService } from '../../gateways/notificationhub/notificationhub.service';
import { UsersRepositoryService } from '../../repositories/users/users.repository.service';
import { UserNotFoundError } from '../../repositories/users/errors/types';
import { v4 as uuidv4 } from 'uuid';
import { Context } from '../../common/log';
@Injectable()
export class NotificationService {
private readonly logger = new Logger(NotificationService.name);
constructor(
private readonly usersRepository: UsersRepositoryService,
private readonly notificationhubService: NotificationhubService,
) {}
/**
* Registers notification service
* @param pns
* @param pnsHandler
* @returns register
*/
async register(
context: Context,
externalId: string,
pns: string,
pnsHandler: string,
): Promise<void> {
this.logger.log(
`[IN] [${context.trackingId}] ${this.register.name} | params: { externalId: ${externalId}, pns: ${pns}, pnsHandler: ${pnsHandler} }`,
);
// ユーザIDからアカウントIDを取得する
let userId: number;
try {
userId = (await this.usersRepository.findUserByExternalId(externalId)).id;
} catch (e) {
this.logger.error(`error=${e}`);
switch (e.constructor) {
case UserNotFoundError:
throw new HttpException(
makeErrorResponse('E010204'),
HttpStatus.BAD_REQUEST,
);
default:
throw new HttpException(
makeErrorResponse('E009999'),
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
try {
// TODO: 登録毎に新規登録する想定でUUIDを付与している
// もしデバイスごとに登録を上書きするようであればUUID部分にデバイス識別子を設定
const installationId = `${pns}_${userId}_${uuidv4()}`;
this.logger.log(installationId);
await this.notificationhubService.register(
context,
userId,
pns,
pnsHandler,
installationId,
);
} catch (e) {
this.logger.error(`error=${e}`);
throw new HttpException(
makeErrorResponse('E009999'),
HttpStatus.INTERNAL_SERVER_ERROR,
);
} finally {
this.logger.log(`[OUT] [${context.trackingId}] ${this.register.name}`);
}
}
}