Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat]: Topic을 활용한 FCM push 로직 구성 #124

Merged
merged 1 commit into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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