Skip to content

Commit

Permalink
[feat]: Topic을 활용한 FCM push 로직 구성 (#124)
Browse files Browse the repository at this point in the history
1. FCM push 활용을 위한 firebase-admin 설정
2. json도 불러올 수 있게끔 tsconfig.json 변경
3. pushJob을 모듈 단위로 설정할 수 있게끔 설정
4. 푸시 테스트를 위해 필요 로직 주석 처리
  • Loading branch information
epitone authored Apr 5, 2022
1 parent 1814d7d commit f9034f3
Show file tree
Hide file tree
Showing 9 changed files with 1,862 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.env
env_files/*
db_data
pog-adminsdk.json

# compiled output
/dist
Expand Down
8 changes: 6 additions & 2 deletions apps/push/src/push/PushApiConsumer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PushJobService } from './../../../../libs/common-config/src/job/src/PushJobService';
import { Process, Processor } from '@nestjs/bull';
import { Logger } from '@nestjs/common';
import { Job } from 'bull';
Expand All @@ -6,8 +7,11 @@ import { Job } from 'bull';
export class PushApiConsumer {
private readonly logger = new Logger(PushApiConsumer.name);

constructor(private readonly pushJobService: PushJobService) {}

@Process('summonerList')
getSummonerIdQueue(job: Job) {
this.logger.log(`${job.id} 번 작업을 수신했습니다.`);
async getSummonerIdQueue(job: Job) {
await this.pushJobService.send(job.data['summonerId']);
this.logger.log(`${job.data['summonerId']} topic 푸시를 전송했습니다.`);
}
}
2 changes: 2 additions & 0 deletions apps/push/src/push/PushApiModule.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PushJobModule } from './../../../../libs/common-config/src/job/src/PushJobModule';
import { Module } from '@nestjs/common';
import { WinstonModule } from 'nest-winston';
import { getWinstonLogger } from '@app/common-config/getWinstonLogger';
Expand All @@ -11,6 +12,7 @@ import { ScheduleModule } from '@nestjs/schedule';

@Module({
imports: [
PushJobModule,
WinstonModule.forRoot(getWinstonLogger(process.env.NODE_ENV, 'push')),
getBullQueue(),
RedisModule.register(RedisModuleConfig),
Expand Down
65 changes: 42 additions & 23 deletions apps/push/src/push/PushApiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,48 @@ export class PushApiService {
private readonly redisService: RedisService,
) {}

@Interval('pushCronTask', 10000)
// @Interval('pushCronTask', 10000)
async addMessageQueue(): Promise<void> {
const redisClient: Redis = this.redisService.getClient();
const summonerIds = await redisClient.smembers('summonerId');
summonerIds.map(async summonerId => {
const riotApiResponse = plainToInstance(
PushRiotApi,
await RiotApiJobs(summonerId),
);
const redisResponse = await redisClient.mget(
`summonerId:${summonerId}:win`,
`summonerId:${summonerId}:lose`,
`summonerId:${summonerId}:tier`,
);
const isChangeRecord = await this.compareRecord(
riotApiResponse,
redisResponse,
);
if (isChangeRecord) {
await this.addPushQueue();
await this.changeRecord(riotApiResponse, redisClient, summonerId);
}
});
// summonerIds.map(async summonerId => {
// const riotApiResponse = plainToInstance(
// PushRiotApi,
// await RiotApiJobs(summonerId),
// );
// const redisResponse = await redisClient.mget(
// `summonerId:${summonerId}:win`,
// `summonerId:${summonerId}:lose`,
// `summonerId:${summonerId}:tier`,
// );
// const isChangeRecord = await this.compareRecord(
// riotApiResponse,
// redisResponse,
// );
// if (isChangeRecord) {
// await this.addPushQueue(summonerId);
// await this.changeRecord(riotApiResponse, redisClient, summonerId);
// }
// });
const summonerId =
'sqyAJOaCU72G_FT6kahr0RVPCnv6ciDse-xq50DrjgrXpmkGtlwsdFgGkA';
const riotApiResponse = plainToInstance(
PushRiotApi,
await RiotApiJobs(summonerId),
);
// const redisResponse = await redisClient.mget(
// `summonerId:${summonerId}:win`,
// `summonerId:${summonerId}:lose`,
// `summonerId:${summonerId}:tier`,
// );
// const isChangeRecord = await this.compareRecord(
// riotApiResponse,
// redisResponse,
// );
// if (isChangeRecord) {
await this.addPushQueue(summonerId);
// await this.changeRecord(riotApiResponse, redisClient, summonerId);
// }
}

private async compareRecord(
Expand All @@ -51,11 +70,11 @@ export class PushApiService {
if (riotApiResponse.lose !== Number(lose)) return true;
}

private async addPushQueue() {
await this.pushQueue.add(
private async addPushQueue(summonerId: string) {
return await this.pushQueue.add(
'summonerList',
{
dataId: 1,
summonerId,
},
{ delay: 10000, removeOnComplete: true },
);
Expand Down
8 changes: 8 additions & 0 deletions libs/common-config/src/job/src/PushJobModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { PushJobService } from './PushJobService';

@Module({
providers: [PushJobService],
exports: [PushJobService],
})
export class PushJobModule {}
25 changes: 25 additions & 0 deletions libs/common-config/src/job/src/PushJobService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Injectable } from '@nestjs/common';

import * as admin from 'firebase-admin';
import { ServiceAccount } from 'firebase-admin';
import * as serviceAccount from '../../../../../pog-adminsdk.json';

@Injectable()
export class PushJobService {
constructor() {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount as ServiceAccount),
});
}

async send(summonerId: string) {
const message = {
notification: {
title: 'test',
body: 'test',
},
topic: summonerId,
};
return Promise.all([await admin.messaging().send(message)]);
}
}
Loading

0 comments on commit f9034f3

Please sign in to comment.