박권수

feat. api user implement

const Router = require('koa-router');
const auth = require('./auth');
const bottle = require('./bottle');
const hub = require('./hub');
const medicine = require('./medicine');
const Router = require('koa-router')
const auth = require('./auth')
const user = require('./user')
const bottle = require('./bottle')
const hub = require('./hub')
const medicine = require('./medicine')
const api = new Router();
const api = new Router()
api.use('/auth', auth.routes());
api.use('/bottle', bottle.routes());
api.use('/hub', hub.routes());
api.use('/medicine', medicine.routes());
api.use('/auth', auth.routes())
api.user('/user', user.routes())
api.use('/bottle', bottle.routes())
api.use('/hub', hub.routes())
api.use('/medicine', medicine.routes())
module.exports = api;
\ No newline at end of file
module.exports = api
\ No newline at end of file
......
const Router = require('koa-router');
const userCtrl = require('./user.ctrl');
const Router = require('koa-router')
const userCtrl = require('./user.ctrl')
const user = new Router();
const user = new Router()
/**
* 현재 유저 정보 조회
......@@ -9,7 +9,7 @@ const user = new Router();
* url : http://localhost:4000/api/user
* return : Object User
*/
user.get('/', userCtrl.myInfo);
user.get('/', userCtrl.myInfo)
/**
* 현재 유저의 타입에 따라 요청 유저 정보 조회(의사 : 환자, 관리자 : 모든 유저)
......@@ -17,7 +17,7 @@ user.get('/', userCtrl.myInfo);
* url : http://localhost:4000/api/user/:reqUserId
* return : status
*/
user.get('/:reqUserId', userCtrl.getUserDetail);
user.get('/:reqUserId', userCtrl.getUserDetail)
/**
* 현재 유저의 타입에 따라 요청 유저 정보 수정(의사 : 환자, 관리자 : 모든 유저)
......@@ -25,5 +25,5 @@ user.get('/', userCtrl.myInfo);
* url : http://localhost:4000/api/user/:reqUserId
* return : status
*/
user.patch('/:reqUserId', userCtrl.updateReqUser);
user.patch('/:reqUserId', userCtrl.updateReqUser)
......
......@@ -13,10 +13,10 @@ exports.myInfo = async ctx => {
}
const { userId } = jwt.verify(token, process.env.JWT_SECRET)
const user = await User.findById(userId);
const user = await User.findById(userId)
if(!user || !user.userTypeCd) {
ctx.status = 403;
return;
ctx.status = 403
return
}
let result = {
......@@ -24,108 +24,108 @@ exports.myInfo = async ctx => {
myDoctor : null,
patientList : [],
userList : [],
};
}
if (user.userTypeCd === 'NORMAL') {
const doctor = await User.findById(user.doctorId);
result.myDoctor = doctor;
const doctor = await User.findById(user.doctorId)
result.myDoctor = doctor
} else if (user.userTypeCd === 'DOCTOR') {
const patientList = await User.findAllByDoctorId(user.userId);
result.patientList = patientList;
const patientList = await User.findAllByDoctorId(user.userId)
result.patientList = patientList
} else if (user.userTypeCd === 'MANAGER') {
const userList = await User.find();
result.userList = userList;
const userList = await User.find()
result.userList = userList
}
ctx.status = 200;
ctx.body = result;
ctx.status = 200
ctx.body = result
}
exports.getUserDetail = async ctx => {
const token = ctx.req.headers.authorization;
const token = ctx.req.headers.authorization
if (!token || !token.length) {
ctx.status = 401;
return;
ctx.status = 401
return
}
const { userId } = jwt.verify(token, process.env.JWT_SECRET);
const user = await User.findById(userId);
const { userId } = jwt.verify(token, process.env.JWT_SECRET)
const user = await User.findById(userId)
if(!user) {
ctx.status = 403;
return;
ctx.status = 403
return
} else if (user.userTypeCd === 'NORMAL') {
ctx.status = 403;
return;
ctx.status = 403
return
}
let result = {
reqUser : user,
reqUserBottleList : [],
};
}
if (user.userTypeCd === 'DOCTOR') {
const { reqUserId } = ctx.params;
const reqUser = await User.findById(reqUserId);
const { reqUserId } = ctx.params
const reqUser = await User.findById(reqUserId)
if (!reqUser) {
ctx.status = 404;
return;
ctx.status = 404
return
}
if(reqUser.doctorId !== user.userId) {
ctx.status = 403;
return;
ctx.status = 403
return
}
const reqUserHubList = await Hub.findAllByUserId(reqUserId);
const reqUserHubList = await Hub.findAllByUserId(reqUserId)
if(reqUserHubList && reqUserHubList.length) {
const reqUserBottleList = [];
const reqUserBottleList = []
await Promise.all(reqUserHubList.forEach(async hub => {
const bottle = await Bottle.findAllByHubId(hub.hubId);
reqUserBottleList.push(...bottle);
}));
const bottle = await Bottle.findAllByHubId(hub.hubId)
reqUserBottleList.push(...bottle)
}))
result.reqUserBottleList = reqUserBottleList;
result.reqUserBottleList = reqUserBottleList
}
}
ctx.status = 200;
ctx.body = result;
ctx.status = 200
ctx.body = result
};
}
exports.updateReqUser = async ctx => {
const token = ctx.req.headers.authorization;
const token = ctx.req.headers.authorization
if (!token || !token.length) {
ctx.status = 401;
return;
ctx.status = 401
return
}
const { userId } = jwt.verify(token, process.env.JWT_SECRET);
const user = await User.findById(userId);
const { userId } = jwt.verify(token, process.env.JWT_SECRET)
const user = await User.findById(userId)
if(!user) {
ctx.status = 403;
return;
ctx.status = 403
return
}
if (user.userTypeCd === 'MANAGER') {
const { useYn } = ctx.request.body;
const { reqUserId } = ctx.params;
const { useYn } = ctx.request.body
const { reqUserId } = ctx.params
const reqUser = await User.findById(reqUserId);
const reqUser = await User.findById(reqUserId)
if(!reqUser) {
ctx.status = 404;
return;
ctx.status = 404
return
}
await reqUser.setUseYn(useYn);
await reqUser.save();
await reqUser.setUseYn(useYn)
await reqUser.save()
return;
return
}
ctx.status = 200;
ctx.status = 200
}
\ No newline at end of file
......