Showing
6 changed files
with
179 additions
and
5 deletions
... | @@ -5,6 +5,7 @@ var corsConfig = require('./config/cors') | ... | @@ -5,6 +5,7 @@ var corsConfig = require('./config/cors') |
5 | 5 | ||
6 | const indexRouter = require('./routes/index') | 6 | const indexRouter = require('./routes/index') |
7 | const authRouter = require('./routes/authRouter') | 7 | const authRouter = require('./routes/authRouter') |
8 | +const userRouter = require('./routes/userRouter') | ||
8 | 9 | ||
9 | let app = express() | 10 | let app = express() |
10 | app.use(cors(corsConfig)) | 11 | app.use(cors(corsConfig)) |
... | @@ -15,6 +16,7 @@ app.use(cookieParser()) | ... | @@ -15,6 +16,7 @@ app.use(cookieParser()) |
15 | 16 | ||
16 | app.use('/', indexRouter) | 17 | app.use('/', indexRouter) |
17 | app.use('/', authRouter) | 18 | app.use('/', authRouter) |
19 | +app.use('/', userRouter) | ||
18 | 20 | ||
19 | app.use(function(req, res) { | 21 | app.use(function(req, res) { |
20 | res.status(400) | 22 | res.status(400) | ... | ... |
... | @@ -83,10 +83,6 @@ exports.userInfo = async (req, res) => { | ... | @@ -83,10 +83,6 @@ exports.userInfo = async (req, res) => { |
83 | } | 83 | } |
84 | } | 84 | } |
85 | 85 | ||
86 | -exports.adminTest = async (req, res) => { | ||
87 | - return sendResponse(res, "Just Test", 200, "Test OK") | ||
88 | -} | ||
89 | - | ||
90 | exports.isAdmin = async (userId) => { | 86 | exports.isAdmin = async (userId) => { |
91 | const user = await User.findByPk(userId) | 87 | const user = await User.findByPk(userId) |
92 | return (user && await user.isAdmin) | 88 | return (user && await user.isAdmin) | ... | ... |
1 | +const bcrypt = require('bcrypt') | ||
2 | + | ||
3 | +const { sequelize, Sequelize, User, Group} = require('../models') | ||
4 | +const { sendResponse, sendError } = require('../utils/response') | ||
5 | +const { logging } = require('../utils/log') | ||
6 | +const { checkRequiredExist, setValues } = require('../utils/validation') | ||
7 | + | ||
8 | +const Op = Sequelize.Op | ||
9 | + | ||
10 | +exports.userRegister = async (req, res) => { | ||
11 | + try { | ||
12 | + let registerData = setValues(req.body, ['userId', 'password', 'name', 'phone', 'email']) | ||
13 | + | ||
14 | + if (typeof registerData.password !== 'undefined' && registerData.password !== null) { | ||
15 | + registerData.password = await bcrypt.hash(registerData.password, 10) | ||
16 | + } | ||
17 | + | ||
18 | + if (typeof registerData.userId !== 'undefined' && registerData.userId !== null) { | ||
19 | + let userIdDuplicated = await User.count({ | ||
20 | + where: { | ||
21 | + userId: registerData.userId | ||
22 | + }, | ||
23 | + }) | ||
24 | + if (userIdDuplicated > 0) { | ||
25 | + logging('user', 'error', { code: 400, message: 'duplicatedUserId' }, req) | ||
26 | + return sendError(res, 400, 'duplicatedUserId') | ||
27 | + } | ||
28 | + } | ||
29 | + let user = await User.create(registerData) | ||
30 | + logging('user', 'register', user, req) | ||
31 | + return sendResponse(res, user, 200) | ||
32 | + | ||
33 | + } catch (error) { | ||
34 | + logging('user', 'error', { code: 500, message: error.message }, req) | ||
35 | + return sendError(res, 500, error.message) | ||
36 | + } | ||
37 | +} | ||
38 | + | ||
39 | +exports.userUpdate = async (req, res) => { | ||
40 | + const id = req.decoded.id | ||
41 | + if (!id) { | ||
42 | + return sendError(res, 401, 'InvalidToken') | ||
43 | + } | ||
44 | + try { | ||
45 | + let updateData = setValues(req.body, ['password', 'name', 'phone', 'email']) | ||
46 | + | ||
47 | + if (typeof updateData.password !== 'undefined' && updateData.password !== null) { | ||
48 | + updateData.password = await bcrypt.hash(updateData.password, 10) | ||
49 | + } | ||
50 | + let logdata = { } | ||
51 | + let user = await User.findByPk(id) | ||
52 | + if (!user) { | ||
53 | + logging('user', 'error', {code: 404, message: 'NoUserFound'}, req) | ||
54 | + return sendError(res, 404, 'NoUserFound') | ||
55 | + } | ||
56 | + logdata.before = { user: user } | ||
57 | + await user.update(updateData) | ||
58 | + | ||
59 | + logdata.after = { user: user } | ||
60 | + logging('user', 'update', user, req) | ||
61 | + return sendResponse(res, user, 200) | ||
62 | + | ||
63 | + } catch (error) { | ||
64 | + logging('user', 'error', { code: 500, message: error.message }, req) | ||
65 | + return sendError(res, 500, error.message) | ||
66 | + } | ||
67 | +} | ||
68 | + | ||
69 | +exports.adminUserList = async (req, res) => { | ||
70 | + try{ | ||
71 | + let users = await User.findAndCountAll({ | ||
72 | + where: { | ||
73 | + id: { [Op.ne]: 1 } | ||
74 | + }, | ||
75 | + order: [ | ||
76 | + ['createdAt', 'desc'] | ||
77 | + ] | ||
78 | + }) | ||
79 | + | ||
80 | + const result = { | ||
81 | + count: users.count, | ||
82 | + data: users.rows | ||
83 | + } | ||
84 | + return sendResponse(res, result, 200) | ||
85 | + } catch(error) { | ||
86 | + logging('user', 'error', { code: 500, message: error.message }, req) | ||
87 | + return sendError(res, 500, error.message) | ||
88 | + } | ||
89 | +} | ||
90 | + | ||
91 | +exports.adminUserInfo = async (req, res) => { | ||
92 | + const requiredKey = ['id'] | ||
93 | + const required = checkRequiredExist(req.body, requiredKey) | ||
94 | + if (required) { | ||
95 | + logging('user', 'error', { code: 400, message: `missingKey:${required}` }, req) | ||
96 | + return sendError(res, 400, `missingKey:${required}`) | ||
97 | + } | ||
98 | + | ||
99 | + const userId = req.body.id | ||
100 | + | ||
101 | + try { | ||
102 | + const user = await User.findByPk(userId) | ||
103 | + if (!user) { | ||
104 | + logging('user', 'error', { code: 404, message: 'NoUserFound' }, req) | ||
105 | + return sendError(res, 404, 'NoUserFound') | ||
106 | + } | ||
107 | + | ||
108 | + return sendResponse(res, user, 200) | ||
109 | + } catch (error) { | ||
110 | + return sendError(res, 500, error.message) | ||
111 | + } | ||
112 | +} | ||
113 | + | ||
114 | +exports.adminUserUpsert = async (req, res) => { | ||
115 | + const id = req.body.id | ||
116 | + | ||
117 | + try { | ||
118 | + let updatedData = setValues(req.body, ['userId', 'password', 'name', 'phone', 'email', 'isAdmin']) | ||
119 | + | ||
120 | + if (typeof updatedData.password !== 'undefined' && updatedData.password !== null) { | ||
121 | + updatedData.password = await bcrypt.hash(updatedData.password, 10) | ||
122 | + } | ||
123 | + | ||
124 | + if (typeof updatedData.userId !== 'undefined' && updatedData.userId !== null) { | ||
125 | + let userIdDuplicated = await User.count({ | ||
126 | + where: { | ||
127 | + id: { [Op.ne]: id }, | ||
128 | + userId: updatedData.userId | ||
129 | + }, | ||
130 | + }) | ||
131 | + if (userIdDuplicated > 0) { | ||
132 | + logging('user', 'error', { code: 400, message: 'duplicatedUserId' }, req) | ||
133 | + return sendError(res, 400, 'duplicatedUserId') | ||
134 | + } | ||
135 | + } | ||
136 | + | ||
137 | + let logdata = { } | ||
138 | + let user = null | ||
139 | + if (id) { | ||
140 | + user = await User.findByPk(id) | ||
141 | + if (!user) { | ||
142 | + logging('user', 'error', {code: 404, message: 'NoUserFound'}, req) | ||
143 | + return sendError(res, 404, 'NoUserFound') | ||
144 | + } | ||
145 | + logdata.before = { user: user } | ||
146 | + await user.update(updatedData) | ||
147 | + } else { | ||
148 | + user = await User.create(updatedData) | ||
149 | + logdata.before = null | ||
150 | + } | ||
151 | + | ||
152 | + logdata.after = { user: user } | ||
153 | + logging('user', 'update', logdata, req) | ||
154 | + return sendResponse(res, user, 200) | ||
155 | + | ||
156 | + } catch (error) { | ||
157 | + logging('user', 'error', { code: 500, message: error.message }, req) | ||
158 | + return sendError(res, 500, error.message) | ||
159 | + } | ||
160 | +} | ... | ... |
... | @@ -6,6 +6,5 @@ const { guestOnly, memberOnly, adminOnly } = require ('../middlewares/auth') | ... | @@ -6,6 +6,5 @@ const { guestOnly, memberOnly, adminOnly } = require ('../middlewares/auth') |
6 | 6 | ||
7 | router.post('/login', guestOnly, authController.login) | 7 | router.post('/login', guestOnly, authController.login) |
8 | router.post('/user/info', memberOnly, authController.userInfo) | 8 | router.post('/user/info', memberOnly, authController.userInfo) |
9 | -router.post('/admin_test', adminOnly, authController.adminTest) | ||
10 | 9 | ||
11 | module.exports = router | 10 | module.exports = router | ... | ... |
1 | +const express = require ('express'); | ||
2 | +const router = express.Router(); | ||
3 | + | ||
4 | +const { guestOnly, memberOnly, adminOnly } = require ('../middlewares/auth') | ||
5 | +const userController = require ('../controllers/userController') | ||
6 | + | ||
7 | +router.post('/user/register', guestOnly, userController.userRegister) | ||
8 | +router.post('/user/update', memberOnly, userController.userUpdate) | ||
9 | + | ||
10 | +router.post('/admin/user/list', adminOnly, userController.adminUserList) | ||
11 | +router.post('/admin/user/info', adminOnly, userController.adminUserInfo) | ||
12 | +router.post('/admin/user/upsert', adminOnly, userController.adminUserUpsert) | ||
13 | + | ||
14 | +module.exports = router | ... | ... |
-
Please register or login to post a comment