Skip to content

Commit

Permalink
Merge pull request #101 from TeamHARA/feat/endAlarm
Browse files Browse the repository at this point in the history
[FEAT] 서비스 종료 알림
  • Loading branch information
leGit-y authored Nov 1, 2024
2 parents 8e6e282 + 0babe2c commit 122bc6a
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
54 changes: 49 additions & 5 deletions src/controller/alarmController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -250,5 +293,6 @@ export default {
setNoDeadlineAlarm,
pushAlarm,
pushAlarmWithPayload,
settingAlarm
settingAlarm,
serviceEndAlarm
}
7 changes: 6 additions & 1 deletion src/modules/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 11 additions & 1 deletion src/repository/tokenRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ const disableDeviceToken = async (userId: number) => {
});
};

const findAllDeviceTokens = async () => {
return await prisma.token.findMany({
select:{
device_token: true
}
});
};



export default {
createRefreshToken,
Expand All @@ -99,5 +108,6 @@ export default {
updateTokenById,
disableRefreshTokenById,
enableDeviceToken,
disableDeviceToken
disableDeviceToken,
findAllDeviceTokens
}
29 changes: 10 additions & 19 deletions src/repository/userRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
5 changes: 5 additions & 0 deletions src/router/alarmRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ router.post("/enable/:isTrue",
alarmController.settingAlarm
)

router.post("/notify",
auth,
alarmController.serviceEndAlarm
)

export default router;
17 changes: 16 additions & 1 deletion src/service/tokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 14 additions & 1 deletion src/service/userService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/service/worryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 122bc6a

Please sign in to comment.