Skip to content

Commit

Permalink
Create user endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Oct 30, 2019
1 parent b934dcb commit 3486b9e
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
module.exports.authController = require('../controllers/auth.controller');
module.exports.userController = require('../controllers/user.controller');
13 changes: 13 additions & 0 deletions src/controllers/user.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const httpStatus = require('http-status');
const catchAsync = require('../utils/catchAsync');
const { userService } = require('../services');

const createUser = catchAsync(async (req, res) => {
const user = await userService.createUser(req.body);
const response = user.transform();
res.status(httpStatus.CREATED).send(response);
});

module.exports = {
createUser,
};
2 changes: 2 additions & 0 deletions src/routes/v1/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const express = require('express');
const authRoute = require('./auth.route');
const userRoute = require('./user.route');

const router = express.Router();

router.use('/auth', authRoute);
router.use('/users', userRoute);

module.exports = router;
10 changes: 10 additions & 0 deletions src/routes/v1/user.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require('express');
const validate = require('../../middlewares/validate');
const userValidation = require('../../validations/user.validation');
const userController = require('../../controllers/user.controller');

const router = express.Router();

router.route('/').post(validate(userValidation.createUser), userController.createUser);

module.exports = router;
13 changes: 13 additions & 0 deletions src/utils/validation.util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const validatePassword = (value, helpers) => {
if (value.length < 8) {
return helpers.message('Password must be at least 8 characters');
}
if (!value.match(/\d/) || !value.match(/[a-zA-Z]/)) {
return helpers.message('Password must contain at least 1 letter and 1 number');
}
return value;
};

module.exports = {
validatePassword,
};
9 changes: 2 additions & 7 deletions src/validations/auth.validation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Joi = require('@hapi/joi');
const validationUtils = require('../utils/validation.util');

const register = {
body: Joi.object().keys({
Expand All @@ -7,13 +8,7 @@ const register = {
.email(),
password: Joi.string()
.required()
.min(8)
.custom((value, helpers) => {
if (!value.match(/\d/) || !value.match(/[a-zA-Z]/)) {
return helpers.message('Password must contain at least one letter and one number');
}
return value;
}),
.custom(validationUtils.validatePassword),
name: Joi.string().required(),
}),
};
Expand Down
1 change: 1 addition & 0 deletions src/validations/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
module.exports.authValidation = require('./auth.validation');
module.exports.userValidation = require('./user.validation');
21 changes: 21 additions & 0 deletions src/validations/user.validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Joi = require('@hapi/joi');
const validationUtils = require('../utils/validation.util');

const createUser = {
body: Joi.object().keys({
email: Joi.string()
.required()
.email(),
password: Joi.string()
.required()
.custom(validationUtils.validatePassword),
name: Joi.string().required(),
role: Joi.string()
.required()
.valid('user', 'admin'),
}),
};

module.exports = {
createUser,
};

0 comments on commit 3486b9e

Please sign in to comment.