makabe.t 54db2e8ab5 Merged PR 259: ルーティング通知登録API実装
## 概要
[Task2218: ルーティング通知登録API実装](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2218)

- NotificationHubへの通知登録を実装しました。

## レビューポイント
- UUIDを使ったインストールIDの組み立てに問題はないか

- 対象外
  - account関連はフォーマット修正によるものなので対象外です。

## UIの変更
- なし

## 動作確認状況
- ローカルで確認
2023-07-26 00:40:06 +00:00

71 lines
2.2 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';
@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(
externalId: string,
pns: string,
pnsHandler: string,
): Promise<void> {
this.logger.log(`[IN] ${this.register.name}`);
// ユーザ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(
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] ${this.register.name}`);
}
}
}