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..2e70003 100644 --- a/src/controller/alarmController.ts +++ b/src/controller/alarmController.ts @@ -7,8 +7,48 @@ 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 userIds = [25] + + 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 +222,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 +293,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..4d48934 100644 --- a/src/modules/scheduler.ts +++ b/src/modules/scheduler.ts @@ -13,8 +13,13 @@ const deadline_alarm_2 = cron.schedule('00 20 * * *', alarmController.setNoDeadl // 데드라인 당일 알람 : 낮 12시 const deadline_alarm_3 = cron.schedule('00 12 * * *', alarmController.setOnDeadlineAlarm, option); +// 2024-11-1 낮12시에 종료 알림 +const service_end_alarm = cron.schedule('25 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);