Showing
4 changed files
with
94 additions
and
4 deletions
1 | -/* eslint-disable no-undef */ | ||
2 | //회원가입, 로그인 및 로그아웃에 관한 api | 1 | //회원가입, 로그인 및 로그아웃에 관한 api |
3 | const User = require('../../models/user'); | 2 | const User = require('../../models/user'); |
4 | const Profile = require('../../models/profile'); | 3 | const Profile = require('../../models/profile'); |
5 | const DoctorInfo = require('../../models/doctorInfo'); | 4 | const DoctorInfo = require('../../models/doctorInfo'); |
5 | +const Hub = require('../../models/hub'); | ||
6 | +const Bottle = require('../../models/bottle'); | ||
6 | const { uploadDoctorLicense } = require('../../util/GoogleCloudStorage'); | 7 | const { uploadDoctorLicense } = require('../../util/GoogleCloudStorage'); |
7 | const Joi = require('joi'); | 8 | const Joi = require('joi'); |
8 | const jwt = require('jsonwebtoken'); | 9 | const jwt = require('jsonwebtoken'); |
... | @@ -80,6 +81,7 @@ exports.searchHospital = async ctx => { | ... | @@ -80,6 +81,7 @@ exports.searchHospital = async ctx => { |
80 | const pageSlice = 5; | 81 | const pageSlice = 5; |
81 | 82 | ||
82 | const url = 'http://apis.data.go.kr/B551182/hospInfoService1/getHospBasisList1'; | 83 | const url = 'http://apis.data.go.kr/B551182/hospInfoService1/getHospBasisList1'; |
84 | + // eslint-disable-next-line no-undef | ||
83 | let queryParams = '?' + encodeURIComponent('ServiceKey') + '=' + process.env.SERVICE_KEY; | 85 | let queryParams = '?' + encodeURIComponent('ServiceKey') + '=' + process.env.SERVICE_KEY; |
84 | queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent(page); | 86 | queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent(page); |
85 | queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent(pageSlice); | 87 | queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent(pageSlice); |
... | @@ -195,7 +197,12 @@ exports.doctorRegister = async ctx => { | ... | @@ -195,7 +197,12 @@ exports.doctorRegister = async ctx => { |
195 | 197 | ||
196 | } | 198 | } |
197 | 199 | ||
198 | -//로컬 로그인 | 200 | +/** |
201 | + * 로컬 로그인 | ||
202 | + * @param {*} ctx | ||
203 | + * @returns token | ||
204 | + * http methods : POST | ||
205 | + */ | ||
199 | exports.login = async(ctx) => { | 206 | exports.login = async(ctx) => { |
200 | const { userId, password, deviceToken } = ctx.request.body; | 207 | const { userId, password, deviceToken } = ctx.request.body; |
201 | 208 | ||
... | @@ -437,6 +444,11 @@ exports.socialLogin = async ctx => { | ... | @@ -437,6 +444,11 @@ exports.socialLogin = async ctx => { |
437 | 444 | ||
438 | }; | 445 | }; |
439 | 446 | ||
447 | +/** | ||
448 | + * 로그아웃 | ||
449 | + * @param {*} ctx | ||
450 | + * httm methods : POST | ||
451 | + */ | ||
440 | exports.logout = async(ctx) => { | 452 | exports.logout = async(ctx) => { |
441 | ctx.cookies.set('access_token', null, { | 453 | ctx.cookies.set('access_token', null, { |
442 | httpOnly : true, | 454 | httpOnly : true, |
... | @@ -446,6 +458,67 @@ exports.logout = async(ctx) => { | ... | @@ -446,6 +458,67 @@ exports.logout = async(ctx) => { |
446 | ctx.status = 204; | 458 | ctx.status = 204; |
447 | }; | 459 | }; |
448 | 460 | ||
461 | +/** | ||
462 | + * 회원 탈퇴 | ||
463 | + * @param {*} ctx | ||
464 | + * http methods : delete | ||
465 | + */ | ||
466 | +exports.secession = async ctx => { | ||
467 | + const token = ctx.req.headers.authorization; | ||
468 | + if(!token || !token.length) { | ||
469 | + ctx.status = 401; | ||
470 | + return; | ||
471 | + } | ||
472 | + | ||
473 | + // eslint-disable-next-line no-undef | ||
474 | + const { userId } = jwt.verify(token, process.env.JWT_SECRET); | ||
475 | + const user = await User.findByUserId(userId); | ||
476 | + if(!user || user.useYn !== 'Y') { | ||
477 | + ctx.status = 403; | ||
478 | + return; | ||
479 | + } | ||
480 | + | ||
481 | + | ||
482 | + const { password } = ctx.query; | ||
483 | + const isPasswordTrue = await user.checkPassword(password); | ||
484 | + if(!isPasswordTrue) { | ||
485 | + ctx.status = 401; | ||
486 | + ctx.body = { | ||
487 | + error : '비밀번호가 틀렸습니다.', | ||
488 | + }; | ||
489 | + return; | ||
490 | + } | ||
491 | + | ||
492 | + if(user.userTypeCd === 'NORMAL') { | ||
493 | + const profile = await Profile.findOne({ userId }); | ||
494 | + | ||
495 | + profile.setUseYn('N'); | ||
496 | + profile.save(); | ||
497 | + | ||
498 | + const hubList = await Hub.find({ userId }); | ||
499 | + await Promise.all(hubList.forEach(async hub => { | ||
500 | + await Bottle.deleteMany({ hubId : hub.hubId }); | ||
501 | + })); | ||
502 | + | ||
503 | + await Hub.deleteMany({ userId }); | ||
504 | + | ||
505 | + user.setUseYn('N'); | ||
506 | + user.save(); | ||
507 | + | ||
508 | + } else if (user.userTypeCd === 'DOCTOR') { | ||
509 | + const doctorInfo = await DoctorInfo.findOne({ doctorId : userId }); | ||
510 | + | ||
511 | + doctorInfo.setUseYn('WS'); | ||
512 | + doctorInfo.save(); | ||
513 | + | ||
514 | + user.setUseYn('WS'); | ||
515 | + user.save(); | ||
516 | + } | ||
517 | + | ||
518 | + ctx.status = 200; | ||
519 | + | ||
520 | +}; | ||
521 | + | ||
449 | exports.verifyToken = async(ctx) => { | 522 | exports.verifyToken = async(ctx) => { |
450 | const token = ctx.req.headers.authorization; | 523 | const token = ctx.req.headers.authorization; |
451 | if(!token || !token.length) { | 524 | if(!token || !token.length) { |
... | @@ -458,6 +531,7 @@ exports.verifyToken = async(ctx) => { | ... | @@ -458,6 +531,7 @@ exports.verifyToken = async(ctx) => { |
458 | return; | 531 | return; |
459 | } | 532 | } |
460 | 533 | ||
534 | + // eslint-disable-next-line no-undef | ||
461 | jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => { | 535 | jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => { |
462 | if (err) { | 536 | if (err) { |
463 | ctx.status = 400; | 537 | ctx.status = 400; | ... | ... |
... | @@ -4,6 +4,7 @@ const Bottle = require('../../models/bottle'); | ... | @@ -4,6 +4,7 @@ const Bottle = require('../../models/bottle'); |
4 | const Hub = require('../../models/hub'); | 4 | const Hub = require('../../models/hub'); |
5 | const Medicine = require('../../models/medicine'); | 5 | const Medicine = require('../../models/medicine'); |
6 | const User = require('../../models/user'); | 6 | const User = require('../../models/user'); |
7 | +const DoctorInfo = require('../../models/doctorInfo'); | ||
7 | const PatientInfo = require('../../models/patientInfo'); | 8 | const PatientInfo = require('../../models/patientInfo'); |
8 | const TakeMedicineHist = require('../../models/takeMedicineHistory'); | 9 | const TakeMedicineHist = require('../../models/takeMedicineHistory'); |
9 | const BottleMedicine = require('../../models/bottleMedicine'); | 10 | const BottleMedicine = require('../../models/bottleMedicine'); |
... | @@ -146,14 +147,19 @@ exports.getBottleInfo = async(ctx) => { | ... | @@ -146,14 +147,19 @@ exports.getBottleInfo = async(ctx) => { |
146 | const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' }); | 147 | const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' }); |
147 | 148 | ||
148 | if(bottleMedicine) { | 149 | if(bottleMedicine) { |
150 | + const medicine = await Medicine.findOne({ medicineId : bottleMedicine.medicineId }); | ||
151 | + const doctorInfo = await DoctorInfo.findOne({ doctorId : bottleMedicine.doctorId }); | ||
152 | + | ||
149 | const takeMedicineHist = await TakeMedicineHist | 153 | const takeMedicineHist = await TakeMedicineHist |
150 | .find({ bmId : bottleMedicine._id }) | 154 | .find({ bmId : bottleMedicine._id }) |
151 | - .sort({ takeDate : 'desc' }) | 155 | + .sort({ takeDate : 'desc' }); |
152 | - .populate('bmId'); | ||
153 | 156 | ||
154 | ctx.status = 200; | 157 | ctx.status = 200; |
155 | ctx.body = { | 158 | ctx.body = { |
156 | bottle, | 159 | bottle, |
160 | + medicine, | ||
161 | + doctorInfo, | ||
162 | + dosage : bottleMedicine.dosage, | ||
157 | takeMedicineHist, | 163 | takeMedicineHist, |
158 | }; | 164 | }; |
159 | 165 | ||
... | @@ -161,6 +167,9 @@ exports.getBottleInfo = async(ctx) => { | ... | @@ -161,6 +167,9 @@ exports.getBottleInfo = async(ctx) => { |
161 | ctx.status = 200; | 167 | ctx.status = 200; |
162 | ctx.body = { | 168 | ctx.body = { |
163 | bottle, | 169 | bottle, |
170 | + medicine : null, | ||
171 | + doctorInfo : null, | ||
172 | + dosage : null, | ||
164 | takeMedicineHist : [], | 173 | takeMedicineHist : [], |
165 | } | 174 | } |
166 | } | 175 | } | ... | ... |
1 | //허브(Mqtt Broker)등록 및 삭제 | 1 | //허브(Mqtt Broker)등록 및 삭제 |
2 | const Hub = require('../../models/hub'); | 2 | const Hub = require('../../models/hub'); |
3 | +const Bottle = require('../../models/bottle'); | ||
3 | const User = require('../../models/user'); | 4 | const User = require('../../models/user'); |
4 | const Mqtt = require('../../util/MqttModule'); | 5 | const Mqtt = require('../../util/MqttModule'); |
5 | const DataProcess = require('../../util/DataProcess'); | 6 | const DataProcess = require('../../util/DataProcess'); |
... | @@ -104,6 +105,7 @@ exports.hubDisconnect = async(ctx) => { | ... | @@ -104,6 +105,7 @@ exports.hubDisconnect = async(ctx) => { |
104 | const hosting = await hub.getHubHost(); | 105 | const hosting = await hub.getHubHost(); |
105 | Mqtt.mqttOff(hosting); | 106 | Mqtt.mqttOff(hosting); |
106 | 107 | ||
108 | + await Bottle.deleteMany({ hubId }); | ||
107 | await Hub.deleteOne({ hubId }); | 109 | await Hub.deleteOne({ hubId }); |
108 | 110 | ||
109 | ctx.status = 204; | 111 | ctx.status = 204; | ... | ... |
... | @@ -7,6 +7,7 @@ const ProfileSchema = new Schema({ | ... | @@ -7,6 +7,7 @@ const ProfileSchema = new Schema({ |
7 | userNm : { type : String, required : true, }, | 7 | userNm : { type : String, required : true, }, |
8 | birth : { type : String, required : true, }, | 8 | birth : { type : String, required : true, }, |
9 | contact : { type : String, required : true, }, | 9 | contact : { type : String, required : true, }, |
10 | + useYn : { type : String, default : 'Y', }, | ||
10 | deviceToken : { type : String, default : null, }, | 11 | deviceToken : { type : String, default : null, }, |
11 | }); | 12 | }); |
12 | 13 | ||
... | @@ -14,6 +15,10 @@ ProfileSchema.statics.findByUserId = function(userId) { | ... | @@ -14,6 +15,10 @@ ProfileSchema.statics.findByUserId = function(userId) { |
14 | return this.findOne({ userId }); | 15 | return this.findOne({ userId }); |
15 | }; | 16 | }; |
16 | 17 | ||
18 | +ProfileSchema.statics.setUseYn = function(useYn) { | ||
19 | + this.useYn = useYn; | ||
20 | +}; | ||
21 | + | ||
17 | ProfileSchema.methods.updateUserContact = function(contact) { | 22 | ProfileSchema.methods.updateUserContact = function(contact) { |
18 | this.contact = contact; | 23 | this.contact = contact; |
19 | }; | 24 | }; | ... | ... |
-
Please register or login to post a comment