박권수

feat. api user implement

1 -const Router = require('koa-router'); 1 +const Router = require('koa-router')
2 -const auth = require('./auth'); 2 +const auth = require('./auth')
3 -const bottle = require('./bottle'); 3 +const user = require('./user')
4 -const hub = require('./hub'); 4 +const bottle = require('./bottle')
5 -const medicine = require('./medicine'); 5 +const hub = require('./hub')
6 +const medicine = require('./medicine')
6 7
7 -const api = new Router(); 8 +const api = new Router()
8 9
9 -api.use('/auth', auth.routes()); 10 +api.use('/auth', auth.routes())
10 -api.use('/bottle', bottle.routes()); 11 +api.user('/user', user.routes())
11 -api.use('/hub', hub.routes()); 12 +api.use('/bottle', bottle.routes())
12 -api.use('/medicine', medicine.routes()); 13 +api.use('/hub', hub.routes())
14 +api.use('/medicine', medicine.routes())
13 15
14 -module.exports = api;
...\ No newline at end of file ...\ No newline at end of file
16 +module.exports = api
...\ No newline at end of file ...\ No newline at end of file
......
1 -const Router = require('koa-router'); 1 +const Router = require('koa-router')
2 -const userCtrl = require('./user.ctrl'); 2 +const userCtrl = require('./user.ctrl')
3 3
4 -const user = new Router(); 4 +const user = new Router()
5 5
6 /** 6 /**
7 * 현재 유저 정보 조회 7 * 현재 유저 정보 조회
...@@ -9,7 +9,7 @@ const user = new Router(); ...@@ -9,7 +9,7 @@ const user = new Router();
9 * url : http://localhost:4000/api/user 9 * url : http://localhost:4000/api/user
10 * return : Object User 10 * return : Object User
11 */ 11 */
12 -user.get('/', userCtrl.myInfo); 12 +user.get('/', userCtrl.myInfo)
13 13
14 /** 14 /**
15 * 현재 유저의 타입에 따라 요청 유저 정보 조회(의사 : 환자, 관리자 : 모든 유저) 15 * 현재 유저의 타입에 따라 요청 유저 정보 조회(의사 : 환자, 관리자 : 모든 유저)
...@@ -17,7 +17,7 @@ user.get('/', userCtrl.myInfo); ...@@ -17,7 +17,7 @@ user.get('/', userCtrl.myInfo);
17 * url : http://localhost:4000/api/user/:reqUserId 17 * url : http://localhost:4000/api/user/:reqUserId
18 * return : status 18 * return : status
19 */ 19 */
20 - user.get('/:reqUserId', userCtrl.getUserDetail); 20 + user.get('/:reqUserId', userCtrl.getUserDetail)
21 21
22 /** 22 /**
23 * 현재 유저의 타입에 따라 요청 유저 정보 수정(의사 : 환자, 관리자 : 모든 유저) 23 * 현재 유저의 타입에 따라 요청 유저 정보 수정(의사 : 환자, 관리자 : 모든 유저)
...@@ -25,5 +25,5 @@ user.get('/', userCtrl.myInfo); ...@@ -25,5 +25,5 @@ user.get('/', userCtrl.myInfo);
25 * url : http://localhost:4000/api/user/:reqUserId 25 * url : http://localhost:4000/api/user/:reqUserId
26 * return : status 26 * return : status
27 */ 27 */
28 -user.patch('/:reqUserId', userCtrl.updateReqUser); 28 +user.patch('/:reqUserId', userCtrl.updateReqUser)
29 29
......
...@@ -13,10 +13,10 @@ exports.myInfo = async ctx => { ...@@ -13,10 +13,10 @@ exports.myInfo = async ctx => {
13 } 13 }
14 14
15 const { userId } = jwt.verify(token, process.env.JWT_SECRET) 15 const { userId } = jwt.verify(token, process.env.JWT_SECRET)
16 - const user = await User.findById(userId); 16 + const user = await User.findById(userId)
17 if(!user || !user.userTypeCd) { 17 if(!user || !user.userTypeCd) {
18 - ctx.status = 403; 18 + ctx.status = 403
19 - return; 19 + return
20 } 20 }
21 21
22 let result = { 22 let result = {
...@@ -24,108 +24,108 @@ exports.myInfo = async ctx => { ...@@ -24,108 +24,108 @@ exports.myInfo = async ctx => {
24 myDoctor : null, 24 myDoctor : null,
25 patientList : [], 25 patientList : [],
26 userList : [], 26 userList : [],
27 - }; 27 + }
28 28
29 if (user.userTypeCd === 'NORMAL') { 29 if (user.userTypeCd === 'NORMAL') {
30 - const doctor = await User.findById(user.doctorId); 30 + const doctor = await User.findById(user.doctorId)
31 - result.myDoctor = doctor; 31 + result.myDoctor = doctor
32 32
33 } else if (user.userTypeCd === 'DOCTOR') { 33 } else if (user.userTypeCd === 'DOCTOR') {
34 - const patientList = await User.findAllByDoctorId(user.userId); 34 + const patientList = await User.findAllByDoctorId(user.userId)
35 - result.patientList = patientList; 35 + result.patientList = patientList
36 36
37 } else if (user.userTypeCd === 'MANAGER') { 37 } else if (user.userTypeCd === 'MANAGER') {
38 - const userList = await User.find(); 38 + const userList = await User.find()
39 - result.userList = userList; 39 + result.userList = userList
40 } 40 }
41 41
42 - ctx.status = 200; 42 + ctx.status = 200
43 - ctx.body = result; 43 + ctx.body = result
44 } 44 }
45 45
46 exports.getUserDetail = async ctx => { 46 exports.getUserDetail = async ctx => {
47 - const token = ctx.req.headers.authorization; 47 + const token = ctx.req.headers.authorization
48 if (!token || !token.length) { 48 if (!token || !token.length) {
49 - ctx.status = 401; 49 + ctx.status = 401
50 - return; 50 + return
51 } 51 }
52 52
53 - const { userId } = jwt.verify(token, process.env.JWT_SECRET); 53 + const { userId } = jwt.verify(token, process.env.JWT_SECRET)
54 - const user = await User.findById(userId); 54 + const user = await User.findById(userId)
55 if(!user) { 55 if(!user) {
56 - ctx.status = 403; 56 + ctx.status = 403
57 - return; 57 + return
58 } else if (user.userTypeCd === 'NORMAL') { 58 } else if (user.userTypeCd === 'NORMAL') {
59 - ctx.status = 403; 59 + ctx.status = 403
60 - return; 60 + return
61 } 61 }
62 62
63 let result = { 63 let result = {
64 reqUser : user, 64 reqUser : user,
65 reqUserBottleList : [], 65 reqUserBottleList : [],
66 - }; 66 + }
67 67
68 if (user.userTypeCd === 'DOCTOR') { 68 if (user.userTypeCd === 'DOCTOR') {
69 - const { reqUserId } = ctx.params; 69 + const { reqUserId } = ctx.params
70 - const reqUser = await User.findById(reqUserId); 70 + const reqUser = await User.findById(reqUserId)
71 if (!reqUser) { 71 if (!reqUser) {
72 - ctx.status = 404; 72 + ctx.status = 404
73 - return; 73 + return
74 } 74 }
75 if(reqUser.doctorId !== user.userId) { 75 if(reqUser.doctorId !== user.userId) {
76 - ctx.status = 403; 76 + ctx.status = 403
77 - return; 77 + return
78 } 78 }
79 79
80 - const reqUserHubList = await Hub.findAllByUserId(reqUserId); 80 + const reqUserHubList = await Hub.findAllByUserId(reqUserId)
81 if(reqUserHubList && reqUserHubList.length) { 81 if(reqUserHubList && reqUserHubList.length) {
82 - const reqUserBottleList = []; 82 + const reqUserBottleList = []
83 83
84 await Promise.all(reqUserHubList.forEach(async hub => { 84 await Promise.all(reqUserHubList.forEach(async hub => {
85 - const bottle = await Bottle.findAllByHubId(hub.hubId); 85 + const bottle = await Bottle.findAllByHubId(hub.hubId)
86 - reqUserBottleList.push(...bottle); 86 + reqUserBottleList.push(...bottle)
87 - })); 87 + }))
88 88
89 - result.reqUserBottleList = reqUserBottleList; 89 + result.reqUserBottleList = reqUserBottleList
90 } 90 }
91 91
92 } 92 }
93 93
94 - ctx.status = 200; 94 + ctx.status = 200
95 - ctx.body = result; 95 + ctx.body = result
96 96
97 -}; 97 +}
98 98
99 exports.updateReqUser = async ctx => { 99 exports.updateReqUser = async ctx => {
100 - const token = ctx.req.headers.authorization; 100 + const token = ctx.req.headers.authorization
101 if (!token || !token.length) { 101 if (!token || !token.length) {
102 - ctx.status = 401; 102 + ctx.status = 401
103 - return; 103 + return
104 } 104 }
105 105
106 - const { userId } = jwt.verify(token, process.env.JWT_SECRET); 106 + const { userId } = jwt.verify(token, process.env.JWT_SECRET)
107 - const user = await User.findById(userId); 107 + const user = await User.findById(userId)
108 if(!user) { 108 if(!user) {
109 - ctx.status = 403; 109 + ctx.status = 403
110 - return; 110 + return
111 } 111 }
112 112
113 if (user.userTypeCd === 'MANAGER') { 113 if (user.userTypeCd === 'MANAGER') {
114 - const { useYn } = ctx.request.body; 114 + const { useYn } = ctx.request.body
115 - const { reqUserId } = ctx.params; 115 + const { reqUserId } = ctx.params
116 116
117 - const reqUser = await User.findById(reqUserId); 117 + const reqUser = await User.findById(reqUserId)
118 if(!reqUser) { 118 if(!reqUser) {
119 - ctx.status = 404; 119 + ctx.status = 404
120 - return; 120 + return
121 } 121 }
122 122
123 - await reqUser.setUseYn(useYn); 123 + await reqUser.setUseYn(useYn)
124 - await reqUser.save(); 124 + await reqUser.save()
125 125
126 - return; 126 + return
127 } 127 }
128 128
129 - ctx.status = 200; 129 + ctx.status = 200
130 130
131 } 131 }
...\ No newline at end of file ...\ No newline at end of file
......