박권수

feat. Batch System

1 const Koa = require('koa'); 1 const Koa = require('koa');
2 -const cors = require('@koa/cors');
3 const Router = require('koa-router'); 2 const Router = require('koa-router');
3 +
4 +const cors = require('@koa/cors');
4 const bodyparser = require('koa-bodyparser'); 5 const bodyparser = require('koa-bodyparser');
5 6
6 const Mongoose = require('mongoose'); 7 const Mongoose = require('mongoose');
7 const api = require('./src/api'); 8 const api = require('./src/api');
8 const updateMedicineInfo = require('./src/lib/UpdatingMedicineInfo'); 9 const updateMedicineInfo = require('./src/lib/UpdatingMedicineInfo');
9 const MqttServer = require('./src/util/MqttServer'); 10 const MqttServer = require('./src/util/MqttServer');
11 +const BatchSystem = require('./src/util/Batch');
10 12
11 require('dotenv').config(); 13 require('dotenv').config();
12 // eslint-disable-next-line no-undef 14 // eslint-disable-next-line no-undef
...@@ -26,7 +28,7 @@ Mongoose.connect(MONGO_URL, { ...@@ -26,7 +28,7 @@ Mongoose.connect(MONGO_URL, {
26 // updateMedicineInfo.updateMedicineInfo(); 28 // updateMedicineInfo.updateMedicineInfo();
27 }).catch(e => { 29 }).catch(e => {
28 console.log(e); 30 console.log(e);
29 -}) 31 +});
30 32
31 app.use(bodyparser()); 33 app.use(bodyparser());
32 router.use('/api', api.routes()); 34 router.use('/api', api.routes());
...@@ -36,4 +38,6 @@ app.use(router.routes()).use(router.allowedMethods()); ...@@ -36,4 +38,6 @@ app.use(router.routes()).use(router.allowedMethods());
36 app.listen(SERVER_PORT, () => { 38 app.listen(SERVER_PORT, () => {
37 console.log('\x1b[1;36mPORT : ', SERVER_PORT, 'is connected\x1b[0m'); 39 console.log('\x1b[1;36mPORT : ', SERVER_PORT, 'is connected\x1b[0m');
38 MqttServer.on(); 40 MqttServer.on();
39 -})
...\ No newline at end of file ...\ No newline at end of file
41 + BatchSystem.CheckNewYear();
42 + BatchSystem.PushNotifyByDosage();
43 +});
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -10,4 +10,92 @@ ...@@ -10,4 +10,92 @@
10 const cron = require('node-cron'); 10 const cron = require('node-cron');
11 11
12 const Profile = require('../models/profile'); 12 const Profile = require('../models/profile');
13 +const User = require('../models/user');
14 +const Hub = require('../models/hub');
15 +const Bottle = require('../models/bottle');
13 const BottleMedicine = require('../models/bottleMedicine'); 16 const BottleMedicine = require('../models/bottleMedicine');
17 +
18 +
19 +//매년 1월 1일 00시 00분에 1살씩 추가
20 +exports.CheckNewYear = () => {
21 + cron.schedule('0 0 0 1 1 *', async () => {
22 + const profileList = await Profile.find();
23 + profileList.forEach(async profile => {
24 + await profile.updateUserAge();
25 + profile.save();
26 + });
27 +
28 + }, {
29 + timezone : 'Asia/Tokyo',
30 + });
31 +};
32 +
33 +//dosage에 따라, Push Notification을 발송한다.
34 +//아침 8시, 점심 12시, 저녁 6시에 한번씩 발송
35 +exports.PushNotifyByDosage = async() => {
36 +
37 + //매일 아침 8시 : 복용량 상관 없이 보냄
38 + cron.schedule('0 0 8 * * *', async () => {
39 + const bottleMedicineList = await BottleMedicine.find({ useYn : 'Y', dosage : { $gte : 1 } });
40 + bottleMedicineList.forEach(async bottleMedicine => {
41 + const bottle = await Bottle.findOne({ bottleId : bottleMedicine.bottleId });
42 + const hub = await Hub.findOne({ hubId : bottle.hubId });
43 + const user = await User.findOne({ userId : hub.userId, useYn : 'Y' });
44 +
45 + if(user) {
46 + const profile = await Profile.findOne({ userId : user.userId });
47 +
48 + const { deviceToken } = profile;
49 + PushNotify(deviceToken);
50 + }
51 + });
52 + }, {
53 + timezone : 'Asia/Tokyo',
54 + });
55 +
56 +
57 + //매일 점심 12시 : 복용량이 3인 환자들만
58 + cron.schedule('0 0 12 * * *', async () => {
59 + const bottleMedicineList = await BottleMedicine.find({ useYn : 'Y', dosage : { $gte : 3 } });
60 + bottleMedicineList.forEach(async bottleMedicine => {
61 + const bottle = await Bottle.findOne({ bottleId : bottleMedicine.bottleId });
62 + const hub = await Hub.findOne({ hubId : bottle.hubId });
63 + const user = await User.findOne({ userId : hub.userId, useYn : 'Y' });
64 +
65 + if(user) {
66 + const profile = await Profile.findOne({ userId : user.userId });
67 +
68 + const { deviceToken } = profile;
69 + PushNotify(deviceToken);
70 + }
71 + });
72 + }, {
73 + timezone : 'Asia/Tokyo',
74 + });
75 +
76 +
77 + //매일 저녁 6시
78 + cron.schedule('0 0 18 * * *', async () => {
79 + const bottleMedicineList = await BottleMedicine.find({ useYn : 'Y', dosage : { $gte : 2 } });
80 + bottleMedicineList.forEach(async bottleMedicine => {
81 + const bottle = await Bottle.findOne({ bottleId : bottleMedicine.bottleId });
82 + const hub = await Hub.findOne({ hubId : bottle.hubId });
83 + const user = await User.findOne({ userId : hub.userId, useYn : 'Y' });
84 +
85 + if(user) {
86 + const profile = await Profile.findOne({ userId : user.userId });
87 +
88 + const { deviceToken } = profile;
89 + PushNotify(deviceToken);
90 + }
91 + });
92 + }, {
93 + timezone : 'Asia/Tokyo',
94 + });
95 +
96 +};
97 +
98 +const PushNotify = async(deviceToken) => {
99 + //toDo : deviceToken을 받아서 push notification을 발송하는 함수
100 + console.log(deviceToken);
101 +};
......