정민우

Merge branch 'backend' into 'master'

User Upsert 구현

User Upsert 구현

See merge request !2
......@@ -5,3 +5,6 @@ MYSQL_DATABASE=root
MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306
CORS_ALLOWED_URL=
INITIAL_ADMIN_ID=
INITIAL_ADMIN_PW=
JWT_KEY=
......
......@@ -5,6 +5,7 @@ var corsConfig = require('./config/cors')
const indexRouter = require('./routes/index')
const authRouter = require('./routes/authRouter')
const userRouter = require('./routes/userRouter')
let app = express()
app.use(cors(corsConfig))
......@@ -15,6 +16,7 @@ app.use(cookieParser())
app.use('/', indexRouter)
app.use('/', authRouter)
app.use('/', userRouter)
app.use(function(req, res) {
res.status(400)
......
......@@ -83,10 +83,6 @@ exports.userInfo = async (req, res) => {
}
}
exports.adminTest = async (req, res) => {
return sendResponse(res, "Just Test", 200, "Test OK")
}
exports.isAdmin = async (userId) => {
const user = await User.findByPk(userId)
return (user && await user.isAdmin)
......
const bcrypt = require('bcrypt')
const { sequelize, Sequelize, User, Group} = require('../models')
const { sendResponse, sendError } = require('../utils/response')
const { logging } = require('../utils/log')
const { checkRequiredExist, setValues } = require('../utils/validation')
const Op = Sequelize.Op
exports.userRegister = async (req, res) => {
try {
let registerData = setValues(req.body, ['userId', 'password', 'name', 'phone', 'email'])
if (typeof registerData.password !== 'undefined' && registerData.password !== null) {
registerData.password = await bcrypt.hash(registerData.password, 10)
}
if (typeof registerData.userId !== 'undefined' && registerData.userId !== null) {
let userIdDuplicated = await User.count({
where: {
userId: registerData.userId
},
})
if (userIdDuplicated > 0) {
logging('user', 'error', { code: 400, message: 'duplicatedUserId' }, req)
return sendError(res, 400, 'duplicatedUserId')
}
}
let user = await User.create(registerData)
logging('user', 'register', user, req)
return sendResponse(res, user, 200)
} catch (error) {
logging('user', 'error', { code: 500, message: error.message }, req)
return sendError(res, 500, error.message)
}
}
exports.userUpdate = async (req, res) => {
const id = req.decoded.id
if (!id) {
return sendError(res, 401, 'InvalidToken')
}
try {
let updateData = setValues(req.body, ['password', 'name', 'phone', 'email'])
if (typeof updateData.password !== 'undefined' && updateData.password !== null) {
updateData.password = await bcrypt.hash(updateData.password, 10)
}
let logdata = { }
let user = await User.findByPk(id)
if (!user) {
logging('user', 'error', {code: 404, message: 'NoUserFound'}, req)
return sendError(res, 404, 'NoUserFound')
}
logdata.before = { user: user }
await user.update(updateData)
logdata.after = { user: user }
logging('user', 'update', user, req)
return sendResponse(res, user, 200)
} catch (error) {
logging('user', 'error', { code: 500, message: error.message }, req)
return sendError(res, 500, error.message)
}
}
exports.adminUserList = async (req, res) => {
try{
let users = await User.findAndCountAll({
where: {
id: { [Op.ne]: 1 }
},
order: [
['createdAt', 'desc']
]
})
const result = {
count: users.count,
data: users.rows
}
return sendResponse(res, result, 200)
} catch(error) {
logging('user', 'error', { code: 500, message: error.message }, req)
return sendError(res, 500, error.message)
}
}
exports.adminUserInfo = async (req, res) => {
const requiredKey = ['id']
const required = checkRequiredExist(req.body, requiredKey)
if (required) {
logging('user', 'error', { code: 400, message: `missingKey:${required}` }, req)
return sendError(res, 400, `missingKey:${required}`)
}
const userId = req.body.id
try {
const user = await User.findByPk(userId)
if (!user) {
logging('user', 'error', { code: 404, message: 'NoUserFound' }, req)
return sendError(res, 404, 'NoUserFound')
}
return sendResponse(res, user, 200)
} catch (error) {
return sendError(res, 500, error.message)
}
}
exports.adminUserUpsert = async (req, res) => {
const id = req.body.id
try {
let updatedData = setValues(req.body, ['userId', 'password', 'name', 'phone', 'email', 'isAdmin'])
if (typeof updatedData.password !== 'undefined' && updatedData.password !== null) {
updatedData.password = await bcrypt.hash(updatedData.password, 10)
}
if (typeof updatedData.userId !== 'undefined' && updatedData.userId !== null) {
let userIdDuplicated = await User.count({
where: {
id: { [Op.ne]: id },
userId: updatedData.userId
},
})
if (userIdDuplicated > 0) {
logging('user', 'error', { code: 400, message: 'duplicatedUserId' }, req)
return sendError(res, 400, 'duplicatedUserId')
}
}
let logdata = { }
let user = null
if (id) {
user = await User.findByPk(id)
if (!user) {
logging('user', 'error', {code: 404, message: 'NoUserFound'}, req)
return sendError(res, 404, 'NoUserFound')
}
logdata.before = { user: user }
await user.update(updatedData)
} else {
user = await User.create(updatedData)
logdata.before = null
}
logdata.after = { user: user }
logging('user', 'update', logdata, req)
return sendResponse(res, user, 200)
} catch (error) {
logging('user', 'error', { code: 500, message: error.message }, req)
return sendError(res, 500, error.message)
}
}
......@@ -6,6 +6,5 @@ const { guestOnly, memberOnly, adminOnly } = require ('../middlewares/auth')
router.post('/login', guestOnly, authController.login)
router.post('/user/info', memberOnly, authController.userInfo)
router.post('/admin_test', adminOnly, authController.adminTest)
module.exports = router
......
const express = require ('express');
const router = express.Router();
const { guestOnly, memberOnly, adminOnly } = require ('../middlewares/auth')
const userController = require ('../controllers/userController')
router.post('/user/register', guestOnly, userController.userRegister)
router.post('/user/update', memberOnly, userController.userUpdate)
router.post('/admin/user/list', adminOnly, userController.adminUserList)
router.post('/admin/user/info', adminOnly, userController.adminUserInfo)
router.post('/admin/user/upsert', adminOnly, userController.adminUserUpsert)
module.exports = router