박권수

feat. FCM start

......@@ -8,6 +8,7 @@ const Mongoose = require('mongoose');
const api = require('./src/api');
const MqttServer = require('./src/util/MqttServer');
const BatchSystem = require('./src/util/Batch');
const FCM = require('./src/util/FCM');
require('dotenv').config();
// eslint-disable-next-line no-undef
......@@ -37,5 +38,7 @@ app.use(router.routes()).use(router.allowedMethods());
app.listen(SERVER_PORT, () => {
console.log('\x1b[1;36mPORT : ', SERVER_PORT, 'is connected\x1b[0m');
MqttServer.on();
BatchSystem.PushNotifyByDosage();
FCM.initializeFCM();
BatchSystem.removeQrCode();
BatchSystem.pushNotifyByDosage();
});
\ No newline at end of file
......
This diff could not be displayed because it is too large.
......@@ -19,6 +19,7 @@
"dependencies": {
"@google-cloud/storage": "^5.14.2",
"@koa/cors": "^3.1.0",
"firebase-admin": "^9.11.1",
"koa-body": "^4.2.0",
"moment": "^2.29.1",
"moment-timezone": "^0.5.33",
......
......@@ -17,6 +17,7 @@ exports.register = async(ctx) => {
userNm,
birth,
contact,
deviceToken,
} = ctx.request.body;
const schema = Joi.object().keys({
......@@ -59,6 +60,7 @@ exports.register = async(ctx) => {
userNm,
birth,
contact,
deviceToken,
});
await user.save();
......@@ -192,7 +194,7 @@ exports.doctorRegister = async ctx => {
}
exports.login = async(ctx) => {
const { userId, password } = ctx.request.body;
const { userId, password, deviceToken } = ctx.request.body;
const schema = Joi.object().keys({
userId : Joi.string().email().max(50).required(),
......@@ -225,7 +227,6 @@ exports.login = async(ctx) => {
};
return;
}
if(user.useYn !== 'Y') {
ctx.status = 403;
ctx.body = {
......@@ -234,6 +235,16 @@ exports.login = async(ctx) => {
return;
}
//일반 유저의 deviceToken값이 바뀌면 업데이트한다 = 기기가 바뀌면
if(user.userTypeCd === 'NORMAL') {
const profile = await Profile.findByUserId(user.userId);
if(deviceToken && profile.deviceToken !== deviceToken) {
profile.updateDeviceToken(deviceToken);
await profile.save();
}
}
const token = await user.generateToken();
ctx.cookies.set('access_token', token, {
httpOnly : true,
......@@ -243,7 +254,7 @@ exports.login = async(ctx) => {
ctx.status = 200;
ctx.body = {
userTypeCd : user.userTypeCd,
token
token,
};
};
......
......@@ -6,15 +6,18 @@
* 1) Dosage에 따라, Push Notification 발송
*/
const cron = require('node-cron');
const cron = require('node-cron');
const fs = require('fs');
const Profile = require('../models/profile');
const User = require('../models/user');
const Hub = require('../models/hub');
const Bottle = require('../models/bottle');
const BottleMedicine = require('../models/bottleMedicine');
const Profile = require('../models/profile');
const User = require('../models/user');
const Hub = require('../models/hub');
const Bottle = require('../models/bottle');
const BottleMedicine = require('../models/bottleMedicine');
const Medicine = require('../models/medicine');
const updateMedicineInfo = require('../lib/UpdatingMedicineInfo');
const { sendPushMessage } = require('./FCM');
// //매년 1월 1일 00시 00분에 1살씩 추가
......@@ -35,12 +38,27 @@ const updateMedicineInfo = require('../lib/UpdatingMedicineInfo');
exports.updateMedicineData = async () => {
cron.schedule('0 0 0 1 * *', () => {
updateMedicineInfo.updateMedicineInfo();
}, {
timezone : 'Asia/Tokyo',
});
};
//매주 일요일마다 불필요한 qrcode 제거
exports.removeQrCode = () => {
cron.schedule('0 0 0 * * 0', () => {
// eslint-disable-next-line no-undef
const qrDir = process.env.QR_DIR;
fs.rm(qrDir, { recursive : true, force : true, }, () => {
fs.mkdir(qrDir, (err) => { if(err) console.log(err) });
});
}, {
timezone : 'Asia/Tokyo',
});
};
//dosage에 따라, Push Notification을 발송한다.
//아침 8시, 점심 12시, 저녁 6시에 한번씩 발송
exports.PushNotifyByDosage = async() => {
exports.pushNotifyByDosage = async() => {
//매일 아침 8시 : 복용량 상관 없이 보냄
cron.schedule('0 0 8 * * *', async () => {
......@@ -52,9 +70,15 @@ exports.updateMedicineData = async () => {
if(user) {
const profile = await Profile.findOne({ userId : user.userId });
const { deviceToken } = profile;
PushNotify(deviceToken);
if(deviceToken) {
const medicine = await Medicine.findOne({ medicineId : bottleMedicine.medicineId });
pushNotify({
deviceToken,
message : medicine.name + '을 복용하셔야 합니다.',
});
}
}
});
}, {
......@@ -72,9 +96,15 @@ exports.updateMedicineData = async () => {
if(user) {
const profile = await Profile.findOne({ userId : user.userId });
const { deviceToken } = profile;
PushNotify(deviceToken);
if(deviceToken) {
const medicine = await Medicine.findOne({ medicineId : bottleMedicine.medicineId });
pushNotify({
deviceToken,
message : medicine.name + '을 복용하셔야 합니다.',
});
}
}
});
}, {
......@@ -92,21 +122,27 @@ exports.updateMedicineData = async () => {
if(user) {
const profile = await Profile.findOne({ userId : user.userId });
const { deviceToken } = profile;
PushNotify(deviceToken);
if(deviceToken) {
const medicine = await Medicine.findOne({ medicineId : bottleMedicine.medicineId });
pushNotify({
deviceToken,
message : medicine.name + '을 복용하셔야 합니다.',
});
}
}
});
}, {
timezone : 'Asia/Tokyo',
});
};
};
const PushNotify = async(deviceToken) => {
const pushNotify = ({ deviceToken, message }) => {
//toDo : deviceToken을 받아서 push notification을 발송하는 함수
if(deviceToken) {
console.log(deviceToken);
}
};
\ No newline at end of file
sendPushMessage({
deviceToken,
message,
});
};
......
const fcm = require('firebase-admin');
exports.initializeFCM = () => {
fcm.initializeApp({
credential : fcm.credential.applicationDefault(),
});
};
exports.sendPushMessage = async ({ deviceToken, message }) => {
const notifyMessage = {
notification : {
title : '약 먹을 시간입니다',
body : message,
},
token : deviceToken,
};
fcm.messaging().send(notifyMessage);
};
\ No newline at end of file
......@@ -3,14 +3,15 @@ const moment = require('moment');
exports.generateQrCode_prescribe = async ({ medicine, dosage, patientId, doctorId }) => {
const directory = "/Users/parkkwonsoo/Desktop/Project/Capstone_Design_1/server/data/";
// eslint-disable-next-line no-undef
const directory = process.env.QR_DIR;
const now = moment().format('YYYY-MM-DD_HH:mm');
const qrCodeFileName = `${now}_${doctorId}_${patientId}_${medicine.medicineId}_${dosage}.png`;
try {
await QrCode.toFile(
directory + qrCodeFileName,
directory + '/' + qrCodeFileName,
`${medicine.name}/${medicine.medicineId}/${dosage}/${patientId}/${doctorId}`,
{
color : {
......
This diff could not be displayed because it is too large.