Skip to content

Commit

Permalink
Merge pull request #100 from TeamHARA/chore/prettier
Browse files Browse the repository at this point in the history
[CHORE] 파일 정렬
  • Loading branch information
leGit-y authored Apr 3, 2024
2 parents 1994e80 + 8b5916d commit 0f64e15
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 325 deletions.
119 changes: 58 additions & 61 deletions src/controller/alarmController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ 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{

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

const user = await userService.getUserById(userId);
Expand All @@ -18,22 +19,22 @@ const setFinishedAlarm = async(req: Request, res: Response, next: NextFunction)

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

Expand All @@ -43,22 +44,22 @@ const setFinishedAlarm = async(req: Request, res: Response, next: NextFunction)
"deviceToken": token
}

pushAlarm(data,next);
pushAlarm(data, next);

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

const setOnDeadlineAlarm = async() => {
try{
const setOnDeadlineAlarm = async () => {
try {
const today = moment().format('YYYY-MM-DD');
const user = await alarmService.getUserListByDeadline(new Date(today));
if(!user){
if (!user) {
return;
}

for(var i =0;i<user.length;i++){
for (var i = 0; i < user.length; i++) {
const data = {
"payload": user[i].worryId,
"title": alarm.DEADLINE_ALARM_TITLE,
Expand All @@ -68,21 +69,20 @@ const setOnDeadlineAlarm = async() => {
pushAlarmWithPayload(data);
}

}catch (error) {
} catch (error) {
console.log(error)
}
}


const setBeforeDeadlineAlarm = async() => {
try{
const deadline = moment().add(3,"days").format('YYYY-MM-DD');
const setBeforeDeadlineAlarm = async () => {
try {
const deadline = moment().add(3, "days").format('YYYY-MM-DD');
const user = await alarmService.getUserListByDeadline(new Date(deadline));
if(!user){
if (!user) {
return;
}

for(var i =0;i<user.length;i++){
for (var i = 0; i < user.length; i++) {
const data = {
"payload": user[i].worryId,
"title": alarm.DEADLINE_ALARM_TITLE,
Expand All @@ -92,20 +92,20 @@ const setBeforeDeadlineAlarm = async() => {
pushAlarmWithPayload(data);
}

}catch (error) {
} catch (error) {
console.log(error)
}
}

const setNoDeadlineAlarm = async() => {
try{
const createdAt = moment().subtract(30,"days").format('YYYY-MM-DD');
const user = await alarmService.getUserListWithNoDeadline(createdAt);
if(!user){
const setNoDeadlineAlarm = async () => {
try {
const createdAt = moment().subtract(30, "days").format('YYYY-MM-DD');
const user = await alarmService.getUserListWithNoDeadline(createdAt);
if (!user) {
return;
}

for(var i =0;i<user.length;i++){
for (var i = 0; i < user.length; i++) {
const data = {
"payload": user[i].worryId,
"title": alarm.NO_DEADLINE_ALARM_TITLE,
Expand All @@ -115,14 +115,13 @@ const setNoDeadlineAlarm = async() => {
pushAlarmWithPayload(data);
}

}catch (error) {
} catch (error) {
console.log(error)
}
}


const pushAlarm = (data: any, next: NextFunction) => {
try{
try {
const { deviceToken, title, contents } = data;

let message = {
Expand All @@ -136,24 +135,24 @@ const pushAlarm = (data: any, next: NextFunction) => {
admin
.messaging()
.send(message)
.then(function (response:Response) {
console.log('Successfully sent message: : ', response)
// return res.status(200).json({success : true})
.then(function (response: Response) {
console.log('Successfully sent message: : ', response)
// return res.status(200).json({success : true})
})
.catch(function (err:Error) {
.catch(function (err: Error) {
console.log('Error Sending message!!! : ', err)
// return res.status(400).json({success : false})
});
} catch (error) {
next(error)
next(error)
}

}

const pushAlarmWithPayload = (data: any) => {
try{
try {
const { payload, deviceToken, title, contents } = data;

// data must only contain string values
let message = {
token: deviceToken,
Expand All @@ -169,41 +168,40 @@ const pushAlarmWithPayload = (data: any) => {
admin
.messaging()
.send(message)
.then(function (response:Response) {
console.log('Successfully sent message: : ', response)
// return res.status(200).json({success : true})
.then(function (response: Response) {
console.log('Successfully sent message: : ', response)
// return res.status(200).json({success : true})
})
.catch(function (err:Error) {
.catch(function (err: Error) {
console.log('Error Sending message!!! : ', err)
// return res.status(400).json({success : false})
});
} catch (error) {
console.log(error)
console.log(error)
}

}

const pushAlarmToMany = async (data: any) => {
try{
try {
const { deviceTokens, title, contents } = data;


let message = {
notification: {
title: title,
body: contents,
title: title,
body: contents,
},
tokens: deviceTokens,
};

await admin
.messaging()
.sendMulticast(message)
.then(function (response:Response) {
console.log('Successfully sent messages');
// return res.status(200).json({success : true})
.messaging()
.sendMulticast(message)
.then(function (response: Response) {
console.log('Successfully sent messages');
// return res.status(200).json({success : true})
})
.catch(function (err:Error) {
.catch(function (err: Error) {
console.log('Error Sending message!!! : ', err)
// return res.status(400).json({success : false})
});
Expand All @@ -220,33 +218,32 @@ const settingAlarm = async (req: Request, res: Response, next: NextFunction) =>

const data = await tokenService.getDeviceToken(userId)
// 알람 비활성화 상태에서 알람 비활성화하려 할 때
if(data == "" && +isTrue == 0){
if (data == "" && +isTrue == 0) {
return res.status(sc.OK).send(success(statusCode.OK, rm.ALARM_DISABLE_SUCCESS))
}
// 알람 활성화 상태에서 알람 활성화하려 할 때
if(data != "" && +isTrue == 1){
if (data != "" && +isTrue == 1) {
return res.status(sc.OK).send(success(statusCode.OK, rm.ALARM_ENABLE_SUCCESS));
}

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

}
if(+isTrue == 0){
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{
export default {
setFinishedAlarm,
setOnDeadlineAlarm,
setBeforeDeadlineAlarm,
Expand Down
Loading

0 comments on commit 0f64e15

Please sign in to comment.