박권수

feat. 회원탈퇴 api 구현, 관리자 승인 api 필요

/* eslint-disable no-undef */
//회원가입, 로그인 및 로그아웃에 관한 api
const User = require('../../models/user');
const Profile = require('../../models/profile');
const DoctorInfo = require('../../models/doctorInfo');
const Hub = require('../../models/hub');
const Bottle = require('../../models/bottle');
const { uploadDoctorLicense } = require('../../util/GoogleCloudStorage');
const Joi = require('joi');
const jwt = require('jsonwebtoken');
......@@ -80,6 +81,7 @@ exports.searchHospital = async ctx => {
const pageSlice = 5;
const url = 'http://apis.data.go.kr/B551182/hospInfoService1/getHospBasisList1';
// eslint-disable-next-line no-undef
let queryParams = '?' + encodeURIComponent('ServiceKey') + '=' + process.env.SERVICE_KEY;
queryParams += '&' + encodeURIComponent('pageNo') + '=' + encodeURIComponent(page);
queryParams += '&' + encodeURIComponent('numOfRows') + '=' + encodeURIComponent(pageSlice);
......@@ -195,7 +197,12 @@ exports.doctorRegister = async ctx => {
}
//로컬 로그인
/**
* 로컬 로그인
* @param {*} ctx
* @returns token
* http methods : POST
*/
exports.login = async(ctx) => {
const { userId, password, deviceToken } = ctx.request.body;
......@@ -437,6 +444,11 @@ exports.socialLogin = async ctx => {
};
/**
* 로그아웃
* @param {*} ctx
* httm methods : POST
*/
exports.logout = async(ctx) => {
ctx.cookies.set('access_token', null, {
httpOnly : true,
......@@ -446,6 +458,67 @@ exports.logout = async(ctx) => {
ctx.status = 204;
};
/**
* 회원 탈퇴
* @param {*} ctx
* http methods : delete
*/
exports.secession = async ctx => {
const token = ctx.req.headers.authorization;
if(!token || !token.length) {
ctx.status = 401;
return;
}
// eslint-disable-next-line no-undef
const { userId } = jwt.verify(token, process.env.JWT_SECRET);
const user = await User.findByUserId(userId);
if(!user || user.useYn !== 'Y') {
ctx.status = 403;
return;
}
const { password } = ctx.query;
const isPasswordTrue = await user.checkPassword(password);
if(!isPasswordTrue) {
ctx.status = 401;
ctx.body = {
error : '비밀번호가 틀렸습니다.',
};
return;
}
if(user.userTypeCd === 'NORMAL') {
const profile = await Profile.findOne({ userId });
profile.setUseYn('N');
profile.save();
const hubList = await Hub.find({ userId });
await Promise.all(hubList.forEach(async hub => {
await Bottle.deleteMany({ hubId : hub.hubId });
}));
await Hub.deleteMany({ userId });
user.setUseYn('N');
user.save();
} else if (user.userTypeCd === 'DOCTOR') {
const doctorInfo = await DoctorInfo.findOne({ doctorId : userId });
doctorInfo.setUseYn('WS');
doctorInfo.save();
user.setUseYn('WS');
user.save();
}
ctx.status = 200;
};
exports.verifyToken = async(ctx) => {
const token = ctx.req.headers.authorization;
if(!token || !token.length) {
......@@ -458,6 +531,7 @@ exports.verifyToken = async(ctx) => {
return;
}
// eslint-disable-next-line no-undef
jwt.verify(token, process.env.JWT_SECRET, (err, decoded) => {
if (err) {
ctx.status = 400;
......
......@@ -4,6 +4,7 @@ const Bottle = require('../../models/bottle');
const Hub = require('../../models/hub');
const Medicine = require('../../models/medicine');
const User = require('../../models/user');
const DoctorInfo = require('../../models/doctorInfo');
const PatientInfo = require('../../models/patientInfo');
const TakeMedicineHist = require('../../models/takeMedicineHistory');
const BottleMedicine = require('../../models/bottleMedicine');
......@@ -146,14 +147,19 @@ exports.getBottleInfo = async(ctx) => {
const bottleMedicine = await BottleMedicine.findOne({ bottleId, useYn : 'Y' });
if(bottleMedicine) {
const medicine = await Medicine.findOne({ medicineId : bottleMedicine.medicineId });
const doctorInfo = await DoctorInfo.findOne({ doctorId : bottleMedicine.doctorId });
const takeMedicineHist = await TakeMedicineHist
.find({ bmId : bottleMedicine._id })
.sort({ takeDate : 'desc' })
.populate('bmId');
.sort({ takeDate : 'desc' });
ctx.status = 200;
ctx.body = {
bottle,
medicine,
doctorInfo,
dosage : bottleMedicine.dosage,
takeMedicineHist,
};
......@@ -161,6 +167,9 @@ exports.getBottleInfo = async(ctx) => {
ctx.status = 200;
ctx.body = {
bottle,
medicine : null,
doctorInfo : null,
dosage : null,
takeMedicineHist : [],
}
}
......
//허브(Mqtt Broker)등록 및 삭제
const Hub = require('../../models/hub');
const Bottle = require('../../models/bottle');
const User = require('../../models/user');
const Mqtt = require('../../util/MqttModule');
const DataProcess = require('../../util/DataProcess');
......@@ -104,6 +105,7 @@ exports.hubDisconnect = async(ctx) => {
const hosting = await hub.getHubHost();
Mqtt.mqttOff(hosting);
await Bottle.deleteMany({ hubId });
await Hub.deleteOne({ hubId });
ctx.status = 204;
......
......@@ -7,6 +7,7 @@ const ProfileSchema = new Schema({
userNm : { type : String, required : true, },
birth : { type : String, required : true, },
contact : { type : String, required : true, },
useYn : { type : String, default : 'Y', },
deviceToken : { type : String, default : null, },
});
......@@ -14,6 +15,10 @@ ProfileSchema.statics.findByUserId = function(userId) {
return this.findOne({ userId });
};
ProfileSchema.statics.setUseYn = function(useYn) {
this.useYn = useYn;
};
ProfileSchema.methods.updateUserContact = function(contact) {
this.contact = contact;
};
......