Skip to content

Commit

Permalink
Merge pull request #98 from GDSC-PKNU-21-22/fix/#97
Browse files Browse the repository at this point in the history
Fix/#97: 크롤링 시간 변경
  • Loading branch information
pp449 authored Sep 9, 2023
2 parents 839e571 + 4f8278e commit 4b23259
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 24 deletions.
39 changes: 21 additions & 18 deletions src/apis/subscribe/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,30 @@ export const unsubscribeMajor = async (
});
};

export const pushNotification = (major: string) => {
export const pushNotification = (major: string): Promise<number> => {
const query = `SELECT user FROM ${major}구독`;
db.query(query, (err: Error, res: SubscribeUser[]) => {
if (err) console.error(err);
return new Promise<number>((resolve) => {
db.query(query, async (err: Error, res: SubscribeUser[]) => {
if (err) console.error(err);

try {
const message: PushMessage = {
title: `${major} 알림`,
body: '새로운 공지가 추가됐어요',
icon: './icons/icon-192x192.png',
};
try {
const message: PushMessage = {
title: `${major} 알림`,
body: '새로운 공지가 추가됐어요',
icon: './icons/icon-192x192.png',
};

for (const userInfo of res) {
webpush.sendNotification(
JSON.parse(userInfo.user),
JSON.stringify(message),
);
for (const userInfo of res) {
await webpush.sendNotification(
JSON.parse(userInfo.user),
JSON.stringify(message),
);
}
resolve(res.length);
} catch (error) {
notificationToSlack(error);
resolve(0);
}
notificationToSlack(`${major} ${res.length}명 알림 완료`);
} catch (error) {
console.error(error);
}
});
});
};
13 changes: 7 additions & 6 deletions src/hooks/cronNoticeCrawling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import cron from 'node-cron';
import notificationToSlack from 'src/hooks/notificateToSlack';

const pushToUsers = async (majors: string[]) => {
return new Promise<void>((resolve) => {
for (const major of majors) {
pushNotification(major);
}
});
let pushedUserCount = ``;
for (const major of majors) {
const count = await pushNotification(major);
pushedUserCount += `${major} ${count}명 알림 완료\n`;
}
notificationToSlack(pushedUserCount);
};

cron.schedule('0 10 * * *', async () => {
cron.schedule('0 3 * * *', async () => {
const majors = await saveNoticeToDB();
await saveNoticeToDB();
await saveSchoolNoticeToDB();
Expand Down

0 comments on commit 4b23259

Please sign in to comment.