diff --git a/src/controller/alarmController.ts b/src/controller/alarmController.ts index 199c24e..2074aae 100644 --- a/src/controller/alarmController.ts +++ b/src/controller/alarmController.ts @@ -54,22 +54,23 @@ const setFinishedAlarm = async(req: Request, res: Response, next: NextFunction) const setOnDeadlineAlarm = async() => { try{ const today = moment().format('YYYY-MM-DD'); - const deviceTokens = await alarmService.getUserListByDeadline(new Date(today)); - if(!deviceTokens){ - console.log("no device tokens"); + const user = await alarmService.getUserListByDeadline(new Date(today)); + if(!user){ return; } - const data = { - "title": alarm.DEADLINE_ALARM_TITLE, - "contents": alarm.ON_DEADLINE_ALARM, - "deviceTokens": deviceTokens + for(var i =0;i { const setBeforeDeadlineAlarm = async() => { try{ const deadline = moment().add(3,"days").format('YYYY-MM-DD'); - const deviceTokens = await alarmService.getUserListByDeadline(new Date(deadline)); - if(!deviceTokens){ - console.log("no device tokens"); + const user = await alarmService.getUserListByDeadline(new Date(deadline)); + if(!user){ return; } - const data = { - "title": alarm.DEADLINE_ALARM_TITLE, - "contents": alarm.BEFORE_DEADLINE_ALARM, - "deviceTokens": deviceTokens + for(var i =0;i { await pushAlarmToMany(data); }catch (error) { - + console.log(error) } } @@ -122,13 +124,48 @@ const setNoDeadlineAlarm = async() => { const pushAlarm = (data: any, next: NextFunction) => { try{ const { deviceToken, title, contents } = data; + let message = { + token: deviceToken, notification: { - title: title, - body: contents, - }, + title: title, + body: contents, + } + } + + admin + .messaging() + .send(message) + .then(function (response:Response) { + console.log('Successfully sent message: : ', response) + // return res.status(200).json({success : true}) + }) + .catch(function (err:Error) { + console.log('Error Sending message!!! : ', err) + // return res.status(400).json({success : false}) + }); + } catch (error) { + next(error) + } + +} + +const pushAlarmWithPayload = (data: any) => { + try{ + const { payload, deviceToken, title, contents } = data; + + // data must only contain string values + let message = { token: deviceToken, + notification: { + title: title, + body: contents, + }, + data: { + worryId: String(payload) + } } + console.log(message) admin .messaging() @@ -142,7 +179,7 @@ const pushAlarm = (data: any, next: NextFunction) => { // return res.status(400).json({success : false}) }); } catch (error) { - next(error); + console.log(error) } } @@ -182,6 +219,16 @@ const settingAlarm = async (req: Request, res: Response, next: NextFunction) => const { isTrue } = req.params const { userId, deviceToken } = req.body + const data = await tokenService.getDeviceToken(userId) + // 알람 비활성화 상태에서 알람 비활성화하려 할 때 + if(data == "" && +isTrue == 0){ + return res.status(sc.OK).send(success(statusCode.OK, rm.ALARM_DISABLE_SUCCESS)) + } + // 알람 활성화 상태에서 알람 활성화하려 할 때 + if(data != "" && +isTrue == 1){ + return res.status(sc.OK).send(success(statusCode.OK, rm.ALARM_ENABLE_SUCCESS)); + } + if(+isTrue == 1){ await tokenService.setDeviceToken(userId, deviceToken) return res.status(sc.OK).send(success(statusCode.OK, rm.ALARM_ENABLE_SUCCESS)); @@ -206,5 +253,6 @@ export default{ setBeforeDeadlineAlarm, setNoDeadlineAlarm, pushAlarm, + pushAlarmWithPayload, settingAlarm } \ No newline at end of file diff --git a/src/repository/worryRepository.ts b/src/repository/worryRepository.ts index b008766..491d815 100644 --- a/src/repository/worryRepository.ts +++ b/src/repository/worryRepository.ts @@ -228,8 +228,12 @@ const findWorryListByTemplate = async(templateId: number,userId: number) => { const findUserListByDeadline = async(date: Date) => { return await prisma.worry.findMany({ - select:{ - user_id:true, + include:{ + user:{ + include:{ + token:true + } + } }, where: { deadline: date, diff --git a/src/service/alarmService.ts b/src/service/alarmService.ts index 93f0755..ba095b5 100644 --- a/src/service/alarmService.ts +++ b/src/service/alarmService.ts @@ -8,18 +8,16 @@ const getUserListByDeadline =async (date: Date) => { return null; } - const user_ids :Array = []; + const result :Array = []; for (var i =0;i = []; - for (var i =0;i {