-
Notifications
You must be signed in to change notification settings - Fork 1
/
push-notification.service.ts
183 lines (163 loc) · 5.58 KB
/
push-notification.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {Injectable, OnApplicationBootstrap} from '@nestjs/common';
import {Cron, SchedulerRegistry} from '@nestjs/schedule';
import * as admin from 'firebase-admin';
import {ConfigService} from '@nestjs/config';
import {PubSub} from '@google-cloud/pubsub';
import {InjectRepository} from '@nestjs/typeorm';
import {User} from 'src/user/entities/user.entity';
import {Repository} from 'typeorm';
import {gmail} from '@googleapis/gmail';
import sentryConfig from 'src/common/config/sentry.config';
import {IncomingWebhook} from '@slack/client';
@Injectable()
export class PushNotificationService implements OnApplicationBootstrap {
constructor(
private readonly schedulerRegistry: SchedulerRegistry,
private readonly configService: ConfigService,
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {}
onApplicationBootstrap() {
// 메일 삭제 푸시 알림 작동
this.sendDeleteMailPushNotification();
}
async sendDeleteMailPushNotification() {
// google auth 인증
const auth = this.configService.get('googleAuth');
const oAuth2Client = this.configService.get('googleOAuth2Client');
// google PubSub 인증
const pubSubClient = new PubSub({
auth,
projectId: this.configService.get('googlePubSub').projectId,
});
const subscription = pubSubClient.subscription(
this.configService.get('googlePubSub').subscriptionName,
);
// aws dynamoDB client 인증
const dynamoDB = this.configService.get('dynamoDB');
// 메일 삭제 푸시 알림 동의 사용자 푸시 알림 보내는 로직
const messageHandler = async message => {
// 메일 삭제 푸시알림 동의 사용자 이메일 확인
const result = `Received message: id ${message.id}, data ${
message.data
}, attributes: ${JSON.stringify(message.attributes)}`;
const SentryConfig = sentryConfig();
const webhook = new IncomingWebhook(SentryConfig.webhook);
webhook.send({
attachments: [
{
color: 'danger',
text: '💚ah-ha-api-server pubsub 응답 정상💚',
fields: [
{
title: `Request Message: google pubsub`,
value: result,
short: false,
},
],
ts: Math.floor(new Date().getTime() / 1000).toString(),
},
],
});
message.ack();
const {emailAddress} = JSON.parse(`${message.data}`);
const consentedUserEmail = emailAddress;
const {Item} = await dynamoDB
.get({
TableName: 'DeleteMailNotificationUser',
Key: {gmail: consentedUserEmail},
})
.promise();
const consentedUser = Item;
// 사용자 접근 권한 확인
oAuth2Client.setCredentials({refresh_token: consentedUser.googleRefreshToken});
const {token} = await oAuth2Client.getAccessToken();
oAuth2Client.setCredentials({access_token: token});
// 구글 메일 개수 확인
const googleMail = gmail({
version: 'v1',
auth: oAuth2Client,
});
const labels = ['INBOX', 'SPAM'];
let totalCount = 0;
await Promise.all(
labels.map(async label => {
const {data} = await googleMail.users.labels.get({
userId: consentedUserEmail,
id: label,
});
totalCount += data.threadsTotal;
}),
);
if (totalCount == consentedUser.notificationLimit) {
// 사용자한테 보내는 푸시 알림 내용
const token = consentedUser.pushToken;
const message = {
data: {
title: `Please check your account ${consentedUserEmail}.`,
body: `There are ${consentedUser.notificationLimit} emails piled up.`,
},
token,
};
// firebase에 알람 보내는 로직
await admin
.messaging()
.send(message)
.then(response => {
console.log('Successfully sent push notification:', response);
})
.catch(error => {
console.log('Error sending push notification:', error);
});
}
};
subscription.on('message', messageHandler);
return;
}
@Cron('00 00 15 * * 1-5', {
name: 'info_notification',
timeZone: 'Asia/Seoul',
})
async sendInformationPushNotification() {
// aws dynamoDB client 인증
const dynamoDB = this.configService.get('dynamoDB');
const {Items} = await dynamoDB.scan({TableName: 'InfoNotificationUser'}).promise();
console.log(Items);
Items.filter(async consentedUser => {
const token = consentedUser.pushToken;
const message = {
data: {
title: `Environmental information notification`,
body: `During Google Zoom meetings, you can reduce carbon dioxide by turning off the screen.`,
},
token,
};
// firebase에 알람 보내는 로직
await admin
.messaging()
.send(message)
.then(response => {
console.log('Successfully sent push notification:', response);
})
.catch(error => {
console.log('Error sending push notification:', error);
});
});
}
// cron 확인하기
getCrons() {
const jobs = this.schedulerRegistry.getCronJobs();
jobs.forEach((value, key, map) => {
let next;
try {
next = value.nextDates().toDate();
} catch (e) {
next = 'error: next fire date is in the past!';
}
});
}
// cron 삭제
deleteCron(name: string) {
this.schedulerRegistry.deleteCronJob(name);
}
}