Skip to content

Commit

Permalink
Merge pull request #96 from TeamHARA/feat/enablePushAlarm
Browse files Browse the repository at this point in the history
[FEAT] 알림 활성화/비활성화 API
  • Loading branch information
leGit-y authored Jan 2, 2024
2 parents 7b33598 + 68dc458 commit f281864
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 6 deletions.
6 changes: 6 additions & 0 deletions src/constants/responseMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,10 @@ export default {
READ_KAKAO_TOKEN_SUCCESS: "카카오 토큰 조회 성공",
READ_KAKAO_TOKEN_FAIL: "카카오 토큰 조회 실패",

//알람
ALARM_ENABLE_SUCCESS: "알림 활성화 성공",
ALARM_ENABLE_FAIL: "알림 활성화 실패",
ALARM_DISABLE_SUCCESS: "알림 비활성화 성공",
ALARM_DISABLE_FAIL: "알림 비활성화 실패",

};
30 changes: 28 additions & 2 deletions src/controller/alarmController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { NextFunction, Request, Response } from "express";
import admin from '../modules/firebaseAdmin';
import { userService } from "../service";
import tokenService from "../service/tokenService";
import { alarm } from "../constants";
import moment from "moment";
import alarmService from "../service/alarmService";
import { alarm, rm, sc } from "../constants";
import { fail, success } from "../constants/response";
import statusCode from "../constants/statusCode"

const setFinishedAlarm = async(req: Request, res: Response, next: NextFunction) => {
try{
Expand Down Expand Up @@ -175,10 +177,34 @@ const pushAlarmToMany = async (data: any) => {

}

const settingAlarm = async (req: Request, res: Response, next: NextFunction) => {
try {
const { isTrue } = req.params
const { userId, deviceToken } = req.body

if(+isTrue == 1){
await tokenService.setDeviceToken(userId, deviceToken)
return res.status(sc.OK).send(success(statusCode.OK, rm.ALARM_ENABLE_SUCCESS));

}
if(+isTrue == 0){
await tokenService.disableDeviceToken(userId, deviceToken)
return res.status(sc.OK).send(success(statusCode.OK, rm.ALARM_DISABLE_SUCCESS));
}


} catch (error) {
next(error);
}
}



export default{
setFinishedAlarm,
setOnDeadlineAlarm,
setBeforeDeadlineAlarm,
setNoDeadlineAlarm,
pushAlarm
pushAlarm,
settingAlarm
}
29 changes: 26 additions & 3 deletions src/repository/tokenRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const findRefreshTokenById = async (userId: number) => {
const findDeviceTokenById = async (userId: number) => {
return await prisma.token.findUnique({
select: {
device_token:true
device_token: true
},
where: {
user_id: userId,
Expand Down Expand Up @@ -78,6 +78,28 @@ const disableRefreshTokenById = async (userId: number) => {
});
};

const enableDeviceToken = async (userId: number, deviceToken: string) => {
return await prisma.token.update({
where: {
user_id: userId
},
data:{
device_token: deviceToken
}
});
};

const disableDeviceToken = async (userId: number) => {
return await prisma.token.update({
where: {
user_id: userId
},
data:{
device_token: ""
}
});
};


export default {
createRefreshToken,
Expand All @@ -86,6 +108,7 @@ export default {
findIdByRefreshToken,
findDeviceTokenListByIds,
updateTokenById,
disableRefreshTokenById

disableRefreshTokenById,
enableDeviceToken,
disableDeviceToken
}
12 changes: 12 additions & 0 deletions src/router/alarmRouter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Router } from "express";
import { auth } from "../middlewares";
import alarmController from "../controller/alarmController";

const router = Router();

router.get("/enable/:isTrue",
auth,
alarmController.settingAlarm
)

export default router;
3 changes: 3 additions & 0 deletions src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import templateRouter from "./templateRouter";
import worryRouter from "./worryRouter";
import reviewRouter from "./reviewRouter";
import authRouter from "./authRouter";
import alarmRouter from "./alarmRouter";

const router: Router = Router();

Expand All @@ -12,6 +13,8 @@ router.use("/template",templateRouter);
router.use("/worry",worryRouter);
router.use("/review",reviewRouter);
router.use("/auth",authRouter);
router.use("/alarm",alarmRouter);




Expand Down
27 changes: 26 additions & 1 deletion src/service/tokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,33 @@ const getDeviceToken =async (userId: number) => {
}


const setDeviceToken =async (userId: number, deviceToken: string) => {


const data = await tokenRepository.enableDeviceToken(userId, deviceToken)


return
}

const disableDeviceToken =async (userId: number, deviceToken: string) => {
const isValidToken = await tokenRepository.findDeviceTokenById(userId)
if(!isValidToken){
throw new ClientException("device token not found in database");
}

if(isValidToken.device_token != deviceToken){
throw new ClientException("wrong device token")
}

return await tokenRepository.disableDeviceToken(userId)

}


export default{
refreshAccessToken,
getDeviceToken
getDeviceToken,
setDeviceToken,
disableDeviceToken
}

0 comments on commit f281864

Please sign in to comment.