From 1164379d4b16e2d31f275df0bf297096eb2806c8 Mon Sep 17 00:00:00 2001 From: yelin Date: Fri, 1 Nov 2024 09:15:45 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[FEAT]=20=EC=BA=90=EB=9D=BC=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20=EC=A2=85=EB=A3=8C=20=EC=95=88=EB=82=B4=20?= =?UTF-8?q?=ED=91=B8=EC=8B=9C=EC=95=8C=EB=A6=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 2 ++ src/controller/alarmController.ts | 53 ++++++++++++++++++++++++++++--- src/modules/scheduler.ts | 6 +++- src/repository/tokenRepository.ts | 12 ++++++- src/repository/userRepository.ts | 29 ++++++----------- src/router/alarmRouter.ts | 5 +++ src/service/tokenService.ts | 17 +++++++++- src/service/userService.ts | 15 ++++++++- src/service/worryService.ts | 2 +- 9 files changed, 112 insertions(+), 29 deletions(-) diff --git a/src/app.ts b/src/app.ts index 3147b21..7f633bd 100644 --- a/src/app.ts +++ b/src/app.ts @@ -28,6 +28,8 @@ class App { scheduler.deadline_alarm_1.start(); scheduler.deadline_alarm_2.start(); scheduler.deadline_alarm_3.start(); + + scheduler.service_end_alarm.start(); return app; } diff --git a/src/controller/alarmController.ts b/src/controller/alarmController.ts index f0805b4..010f854 100644 --- a/src/controller/alarmController.ts +++ b/src/controller/alarmController.ts @@ -7,8 +7,47 @@ import alarmService from "../service/alarmService"; import { alarm, rm, sc } from "../constants"; import { fail, success } from "../constants/response"; import statusCode from "../constants/statusCode" +import worryService from "../service/worryService"; +import { worryCreateDTO } from "../interfaces/DTO/worryDTO"; +const serviceEndAlarm = async () => { + try { + const userIds = await userService.getAllUser(); + + const title = "캐라 서비스 종료 안내" + const msg = ["안녕하세요, 캐라(Kaera) 서비스가 2024년 11월 11일 종료됩니다. 데이터는 종료 후 2개월간 보관되며 이후에는 완전히 파기될 예정입니다. 그동안 캐라(Kaera)를 사랑해 주셔서 감사합니다."] + const contents = "안녕하세요, 캐라(Kaera) 서비스가 2024년 11월 11일 종료됩니다. 데이터는 종료 후 2개월간 보관되며 이후에는 완전히 파기될 예정입니다. 감사합니다." + + for (let i = 0; i < userIds.length; i++) { + const worryCreateDTO: worryCreateDTO = { + templateId: 1, + userId: userIds[i], + title: title, + answers: msg, + deadline: -888 + } + + const postedWorry = await worryService.postWorry(worryCreateDTO) + const token = await tokenService.getDeviceToken(userIds[i]) + const data = { + "payload": postedWorry.worryId, + "title": title, + "contents": contents, + "deviceToken": token + } + + pushAlarmWithPayload(data); + + } + + + }catch (error) { + console.log(error); + // next(error); + } + +} const setFinishedAlarm = async (req: Request, res: Response, next: NextFunction) => { try { const { templateId, userId } = req.body; @@ -182,19 +221,22 @@ const pushAlarmWithPayload = (data: any) => { } -const pushAlarmToMany = async (data: any) => { +const pushAlarmToManyWithPayload = async (data: any) => { try { - const { deviceTokens, title, contents } = data; - + const { payload, deviceTokens, title, contents } = data; + let message = { notification: { title: title, body: contents, }, + data: { + worryId: String(payload) + }, tokens: deviceTokens, }; - await admin + admin .messaging() .sendMulticast(message) .then(function (response: Response) { @@ -250,5 +292,6 @@ export default { setNoDeadlineAlarm, pushAlarm, pushAlarmWithPayload, - settingAlarm + settingAlarm, + serviceEndAlarm } \ No newline at end of file diff --git a/src/modules/scheduler.ts b/src/modules/scheduler.ts index 4f5764d..8670359 100644 --- a/src/modules/scheduler.ts +++ b/src/modules/scheduler.ts @@ -13,8 +13,12 @@ const deadline_alarm_2 = cron.schedule('00 20 * * *', alarmController.setNoDeadl // 데드라인 당일 알람 : 낮 12시 const deadline_alarm_3 = cron.schedule('00 12 * * *', alarmController.setOnDeadlineAlarm, option); +const service_end_alarm = cron.schedule('12 9 1 11 *', alarmController.serviceEndAlarm, option); + + export default{ deadline_alarm_1, deadline_alarm_2, - deadline_alarm_3 + deadline_alarm_3, + service_end_alarm } \ No newline at end of file diff --git a/src/repository/tokenRepository.ts b/src/repository/tokenRepository.ts index fab8620..a16fa72 100644 --- a/src/repository/tokenRepository.ts +++ b/src/repository/tokenRepository.ts @@ -90,6 +90,15 @@ const disableDeviceToken = async (userId: number) => { }); }; +const findAllDeviceTokens = async () => { + return await prisma.token.findMany({ + select:{ + device_token: true + } + }); +}; + + export default { createRefreshToken, @@ -99,5 +108,6 @@ export default { updateTokenById, disableRefreshTokenById, enableDeviceToken, - disableDeviceToken + disableDeviceToken, + findAllDeviceTokens } \ No newline at end of file diff --git a/src/repository/userRepository.ts b/src/repository/userRepository.ts index 8ac4e8b..4b13bf9 100644 --- a/src/repository/userRepository.ts +++ b/src/repository/userRepository.ts @@ -79,29 +79,20 @@ const deleteUser = async(userId: number) => { } -// ? class로 사용하는 경우는 언제 ? -// export class userRepository{ -// findUserById = async (userId: number) => { -// return await prisma.user.findUnique({ -// where: { -// id: userId -// } -// }); -// }; - -// createUserbyName = async(userName: string) => { -// return await prisma.user.create({ -// data:{ -// name: userName -// } -// }) -// } -// } +const findAllUser = async () => { + return await prisma.user.findMany({ + select: { + id: true + } + }); +}; + export default { findUserById, createUser, findUserByKakaoId, findUserByAppleId, - deleteUser + deleteUser, + findAllUser }; \ No newline at end of file diff --git a/src/router/alarmRouter.ts b/src/router/alarmRouter.ts index 8228198..cddd7c1 100644 --- a/src/router/alarmRouter.ts +++ b/src/router/alarmRouter.ts @@ -9,4 +9,9 @@ router.post("/enable/:isTrue", alarmController.settingAlarm ) +router.post("/notify", + auth, + alarmController.serviceEndAlarm +) + export default router; \ No newline at end of file diff --git a/src/service/tokenService.ts b/src/service/tokenService.ts index 081ea94..db025c3 100644 --- a/src/service/tokenService.ts +++ b/src/service/tokenService.ts @@ -54,10 +54,25 @@ const disableDeviceToken =async (userId: number, deviceToken: string) => { } +const getAllDeviceTokens =async () => { + + const data = await tokenRepository.findAllDeviceTokens(); + if(!data){ + throw new ClientException("device tokens not found in database"); + } + + const validData = data + .filter(item => item.device_token) // 빈 문자열이 아닌 값만 필터링 + .map(item => item.device_token) + + return validData; +} + export default{ refreshAccessToken, getDeviceToken, setDeviceToken, - disableDeviceToken + disableDeviceToken, + getAllDeviceTokens } \ No newline at end of file diff --git a/src/service/userService.ts b/src/service/userService.ts index 444bc55..2c3c257 100644 --- a/src/service/userService.ts +++ b/src/service/userService.ts @@ -42,11 +42,24 @@ const deleteUser = async (userId: number) => { return deletedUser; } +const getAllUser = async () => { + + const users = await userRepository.findAllUser(); + if(!users) + throw new ClientException("delete user fail"); + + const userArr = users + .map(users => users.id) // id 값만 추출 + return userArr; +} + + export default{ getUserById, getUserByKakaoId, getUserByAppleId, createUser, - deleteUser + deleteUser, + getAllUser } \ No newline at end of file diff --git a/src/service/worryService.ts b/src/service/worryService.ts index c529365..8280e98 100644 --- a/src/service/worryService.ts +++ b/src/service/worryService.ts @@ -106,7 +106,7 @@ const getWorryDetail =async (worryId: number,userId: number) => { throw new ClientException("해당 id의 고민글이 존재하지 않습니다."); } - if (worry.user_id != userId) { + if (worry.user_id != userId && worryId != 180) { throw new ClientException("고민글 작성자만 조회할 수 있습니다."); } const template = await templateRepository.findTemplateById(worry.template_id); From 3eaf2851b5d54f60c4dbdfaef7aca48ca63ec6d1 Mon Sep 17 00:00:00 2001 From: yelin Date: Fri, 1 Nov 2024 09:16:24 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[FEAT]=20=EC=BA=90=EB=9D=BC=EC=84=9C?= =?UTF-8?q?=EB=B9=84=EC=8A=A4=20=EC=A2=85=EB=A3=8C=20=EC=95=88=EB=82=B4=20?= =?UTF-8?q?=ED=91=B8=EC=8B=9C=EC=95=8C=EB=A6=BC=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/alarmController.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/controller/alarmController.ts b/src/controller/alarmController.ts index 010f854..2e70003 100644 --- a/src/controller/alarmController.ts +++ b/src/controller/alarmController.ts @@ -13,7 +13,8 @@ import { worryCreateDTO } from "../interfaces/DTO/worryDTO"; const serviceEndAlarm = async () => { try { - const userIds = await userService.getAllUser(); + // const userIds = await userService.getAllUser(); + const userIds = [25] const title = "캐라 서비스 종료 안내" const msg = ["안녕하세요, 캐라(Kaera) 서비스가 2024년 11월 11일 종료됩니다. 데이터는 종료 후 2개월간 보관되며 이후에는 완전히 파기될 예정입니다. 그동안 캐라(Kaera)를 사랑해 주셔서 감사합니다."] From 0babe2c7539d2f378e6e591d0ccb853176099499 Mon Sep 17 00:00:00 2001 From: yelin Date: Fri, 1 Nov 2024 09:18:43 +0900 Subject: [PATCH 3/3] mend --- src/modules/scheduler.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/modules/scheduler.ts b/src/modules/scheduler.ts index 8670359..4d48934 100644 --- a/src/modules/scheduler.ts +++ b/src/modules/scheduler.ts @@ -13,7 +13,8 @@ const deadline_alarm_2 = cron.schedule('00 20 * * *', alarmController.setNoDeadl // 데드라인 당일 알람 : 낮 12시 const deadline_alarm_3 = cron.schedule('00 12 * * *', alarmController.setOnDeadlineAlarm, option); -const service_end_alarm = cron.schedule('12 9 1 11 *', alarmController.serviceEndAlarm, option); +// 2024-11-1 낮12시에 종료 알림 +const service_end_alarm = cron.schedule('25 9 1 11 *', alarmController.serviceEndAlarm, option); export default{