diff --git a/src/apis/subscribe/service.ts b/src/apis/subscribe/service.ts index 16b8133d..00bd63ef 100644 --- a/src/apis/subscribe/service.ts +++ b/src/apis/subscribe/service.ts @@ -67,27 +67,30 @@ export const unsubscribeMajor = async ( }); }; -export const pushNotification = (major: string) => { +export const pushNotification = (major: string): Promise => { const query = `SELECT user FROM ${major}구독`; - db.query(query, (err: Error, res: SubscribeUser[]) => { - if (err) console.error(err); + return new Promise((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); - } + }); }); }; diff --git a/src/hooks/cronNoticeCrawling.ts b/src/hooks/cronNoticeCrawling.ts index 4c9b085f..599d23ab 100644 --- a/src/hooks/cronNoticeCrawling.ts +++ b/src/hooks/cronNoticeCrawling.ts @@ -4,14 +4,15 @@ import cron from 'node-cron'; import notificationToSlack from 'src/hooks/notificateToSlack'; const pushToUsers = async (majors: string[]) => { - return new Promise((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();