Skip to content

Commit

Permalink
Merge pull request #86 from TeamHARA/feat/push-alarm
Browse files Browse the repository at this point in the history
[FEAT] 최종결정 완료시 푸시 알림
  • Loading branch information
leGit-y authored Dec 4, 2023
2 parents 1b4d378 + 45dbc1f commit 9d89498
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 13 deletions.
18 changes: 18 additions & 0 deletions src/constants/alarmMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export default{
FINISHED_ALARM_TITLE: "캐라 보석 획득",
DEADLINE_ALARM_TITLE: "고민 보석 캐기",
NO_DEADLINE_ALARM_TITLE: "고민 원석 캐기",

FINISHED_ALARM_1: "자유의 보석을 캐냈어요!",
FINISHED_ALARM_2: "가능성의 보석을 캐냈어요!",
FINISHED_ALARM_3: "호기심의 보석을 캐냈어요!",
FINISHED_ALARM_4: "집중의 보석을 캐냈어요!",
FINISHED_ALARM_5: "긍정의 보석을 캐냈어요!",
FINISHED_ALARM_6: "가치의 보석을 캐냈어요!",

BEFORE_DEADLINE_ALARM: "고민의 여정이 3일 남았어요. 잠시 이 고민에 집중해보세요.",
ON_DEADLINE_ALARM: "오늘은 고민을 마치기로 한 날이에요. 과정을 기록하고, 불안을 덜어내세요.",

NO_DEADLINE_ALARM: "잠시 이 고민에 집중해보세요. 고민 원석을 보석으로 바꿀 수 있어요."

}
2 changes: 2 additions & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export { default as rm } from "./responseMessage";
export { default as sc } from "./statusCode";
export { default as tokenType } from "./tokenType";
export { default as alarm } from "./alarmMessage";

75 changes: 64 additions & 11 deletions src/controller/alarmController.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,71 @@
import { NextFunction, Request, Response } from "express";
import admin from '../modules/firebaseAdmin';
import { userService } from "../service";
import tokenService from "../service/tokenService";
import { alarm } from "../constants";

const admin = require('firebase-admin');
let serviceAccount = require("../../firebase-admin.json");
const setFinishedAlarm = async(req: Request, res: Response, next: NextFunction) => {
try{
const { templateId, userId } = req.body;
const user = await userService.getUserById(userId);
const userName = user.name;
const token = await tokenService.getDeviceToken(userId);

admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
})
const title = alarm.FINISHED_ALARM_TITLE;
let msg = `${userName}님은 `;
if (templateId == 1){
msg += alarm.FINISHED_ALARM_1
}
if (templateId == 2){
msg += alarm.FINISHED_ALARM_2
}
if (templateId == 3){
msg += alarm.FINISHED_ALARM_3
}
if (templateId == 4){
msg += alarm.FINISHED_ALARM_4
}
if (templateId == 5){
msg += alarm.FINISHED_ALARM_5
}
if (templateId == 6){
msg += alarm.FINISHED_ALARM_6
}

const data = {
"title": title,
"contents": msg,
"deviceToken": token
}
// req.body.title = title;
// req.body.contents = msg;
// req.body.deviceToken = token;
// next();

const pushAlarm = (req: Request, res: Response, next: NextFunction) => {
pushAlarm(data,next);

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

const setDeadlineAlarm = async(req: Request, res: Response, next: NextFunction) => {
try{
const { deviceToken } = req.body;



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

const pushAlarm = (data: any, next: NextFunction) => {
try{
const { deviceToken, title, contents } = data;
let message = {
notification: {
title: '테스트 발송',
body: '우와~!!!!!!',
title: title,
body: contents,
},
token: deviceToken,
}
Expand All @@ -24,11 +75,11 @@ const pushAlarm = (req: Request, res: Response, next: NextFunction) => {
.send(message)
.then(function (response:Response) {
console.log('Successfully sent message: : ', response)
return res.status(200).json({success : true})
// return res.status(200).json({success : true})
})
.catch(function (err:Error) {
console.log('Error Sending message!!! : ', err)
return res.status(400).json({success : false})
// return res.status(400).json({success : false})
});
} catch (error) {
next(error);
Expand All @@ -37,5 +88,7 @@ const pushAlarm = (req: Request, res: Response, next: NextFunction) => {
}

export default{
setFinishedAlarm,
setDeadlineAlarm,
pushAlarm
}
5 changes: 4 additions & 1 deletion src/controller/worryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ const patchFinalAnswer = async (req: Request, res: Response, next: NextFunction)

const data = await worryService.patchFinalAnswer(finalAnswerCreateDTO);

return res.status(sc.OK).send(success(statusCode.OK, rm.MAKE_FINAL_ANSWER_SUCCESS,data));
req.body.templateId = data.alarm_data.templateId;
next();

return res.status(sc.OK).send(success(statusCode.OK, rm.MAKE_FINAL_ANSWER_SUCCESS,data.quote_data));

} catch (error) {
next(error);
Expand Down
7 changes: 7 additions & 0 deletions src/modules/firebaseAdmin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const admin = require('firebase-admin');
let serviceAccount = require("../../firebase-admin.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
})

export default admin;
12 changes: 12 additions & 0 deletions src/repository/tokenRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ const findRefreshTokenById = async (userId: number) => {
});
};

const findDeviceTokenById = async (userId: number) => {
return await prisma.token.findUnique({
select: {
device_token:true
},
where: {
user_id: userId,
}
});
};

const findIdByRefreshToken = async (refreshToken: string) => {
return await prisma.token.findUnique({
select:{
Expand Down Expand Up @@ -60,6 +71,7 @@ const disableRefreshTokenById = async (userId: number) => {
export default {
createRefreshToken,
findRefreshTokenById,
findDeviceTokenById,
findIdByRefreshToken,
updateTokenById,
disableRefreshTokenById
Expand Down
2 changes: 2 additions & 0 deletions src/router/worryRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import validate from "../middlewares/validate";
import { body,query } from "express-validator";
import { validateBody } from "../middlewares/validateDTO";
import { worryCreateDTO, worryUpdateDTO,finalAnswerCreateDTO,deadlineUpdateDTO } from "../interfaces/DTO/worryDTO";
import alarmController from "../controller/alarmController";

const router = Router();

Expand Down Expand Up @@ -62,6 +63,7 @@ router.patch("/finalAnswer",
validate,
validateBody(finalAnswerCreateDTO),
worryController.patchFinalAnswer,
alarmController.setFinishedAlarm
);

router.patch("/deadline",
Expand Down
11 changes: 11 additions & 0 deletions src/service/tokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,19 @@ const refreshAccessToken =async (refreshToken:string) => {
return data;
}

const getDeviceToken =async (userId: number) => {

const data = await tokenRepository.findDeviceTokenById(userId);
if(!data){
throw new ClientException("device token not found in database");
}

return data.device_token;
}



export default{
refreshAccessToken,
getDeviceToken
}
11 changes: 10 additions & 1 deletion src/service/worryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,19 @@ const patchFinalAnswer =async (finalAnswerCreateDTO: finalAnswerCreateDTO) => {
const quotes = await quoteRepository.findAllQuote();
const random_quote = quotes[calculate_random_num(quotes.length)].content

const data = {
const quote_data = {
"quote": random_quote
}

const alarm_data = {
"templateId": updatedWorry.template_id
}

const data = {
quote_data,
alarm_data
}

return data;

}
Expand Down

0 comments on commit 9d89498

Please sign in to comment.