## 概要 [Task2218: ルーティング通知登録API実装](https://paruru.nds-tyo.co.jp:8443/tfs/ReciproCollection/fa4924a4-d079-4fab-9fb5-a9a11eb205f0/_workitems/edit/2218) - NotificationHubへの通知登録を実装しました。 ## レビューポイント - UUIDを使ったインストールIDの組み立てに問題はないか - 対象外 - account関連はフォーマット修正によるものなので対象外です。 ## UIの変更 - なし ## 動作確認状況 - ローカルで確認
74 lines
2.0 KiB
TypeScript
74 lines
2.0 KiB
TypeScript
import { Injectable, Logger } from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import {
|
|
NotificationHubsClient,
|
|
createAppleInstallation,
|
|
createFcmLegacyInstallation,
|
|
createWindowsInstallation,
|
|
} from '@azure/notification-hubs';
|
|
import { PNS } from '../../constants';
|
|
@Injectable()
|
|
export class NotificationhubService {
|
|
private readonly logger = new Logger(NotificationhubService.name);
|
|
private readonly client: NotificationHubsClient;
|
|
constructor(private readonly configService: ConfigService) {
|
|
this.client = new NotificationHubsClient(
|
|
this.configService.get<string>('NOTIFICATION_HUB_CONNECT_STRING'),
|
|
this.configService.get<string>('NOTIFICATION_HUB_NAME'),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Registers notificationhub service
|
|
* @param pns
|
|
* @param pnsHandler
|
|
* @returns register
|
|
*/
|
|
async register(
|
|
userId: number,
|
|
pns: string,
|
|
pnsHandler: string,
|
|
installationId: string,
|
|
): Promise<void> {
|
|
this.logger.log(`[IN] ${this.register.name}`);
|
|
|
|
const tag = `user_${userId}`;
|
|
|
|
//登録情報作成
|
|
const installation = {
|
|
// XXX 登録の有効期限を設定したい場合
|
|
//expirationTime: '',
|
|
installationId: installationId,
|
|
pushChannel: pnsHandler,
|
|
tags: [tag],
|
|
};
|
|
|
|
try {
|
|
switch (pns) {
|
|
case PNS.WNS:
|
|
await this.client.createOrUpdateInstallation(
|
|
createWindowsInstallation(installation),
|
|
);
|
|
break;
|
|
case PNS.APNS:
|
|
await this.client.createOrUpdateInstallation(
|
|
createAppleInstallation(installation),
|
|
);
|
|
break;
|
|
case PNS.FCM:
|
|
await this.client.createOrUpdateInstallation(
|
|
createFcmLegacyInstallation(installation),
|
|
);
|
|
break;
|
|
default:
|
|
throw new Error('invalid pns');
|
|
}
|
|
} catch (e) {
|
|
this.logger.error(`error=${e.message}`);
|
|
throw e;
|
|
} finally {
|
|
this.logger.log(`[OUT] ${this.register.name}`);
|
|
}
|
|
}
|
|
}
|