Skip to content

Commit

Permalink
feat: req04
Browse files Browse the repository at this point in the history
  • Loading branch information
dopimentel committed Feb 21, 2024
1 parent 4ac65a5 commit 817d4a4
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const login = async (req, res, next) => {
const create = async (req, res, next) => {
const { displayName, email, password, image } = req.body;
const response = await userService.create({ displayName, email, password, image });
if (response.error) return next(response.error);
if (response.status === 409) {
const err = new Error(response.message);
err.status = response.status;
Expand Down
2 changes: 1 addition & 1 deletion src/migrations/20230914143828-create-users.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module.exports = {
type: Sequelize.STRING,
},
image: {
allowNull: false,
allowNull: true,
type: Sequelize.STRING,
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports = (sequelize, DataTypes) => {
type: DataTypes.STRING,
},
image: {
allowNull: false,
allowNull: true,
type: DataTypes.STRING,
},
},
Expand Down
4 changes: 3 additions & 1 deletion src/services/userService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const jwt = require('jsonwebtoken');
const { User } = require('../models');
const { loginValidation } = require('./validations');
const { loginValidation, createUserValidation } = require('./validations');

const { JWT_SECRET } = process.env;
const jwtConfig = {
Expand Down Expand Up @@ -34,6 +34,8 @@ const login = async ({ email, password }) => {
};

const create = async ({ displayName, email, password, image }) => {
const error = createUserValidation({ displayName, email, password });
if (error) return { error };
const userExists = await User.findOne({ where: { email } });
if (userExists) {
return {
Expand Down
8 changes: 8 additions & 0 deletions src/services/validations/createUserValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const { createUserSchema } = require('../../utils/schemas');

const validateCreateUser = (body) => {
const { error } = createUserSchema.validate(body);
return error;
}

module.exports = validateCreateUser;
3 changes: 2 additions & 1 deletion src/services/validations/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const loginValidation = require('./loginValidation');

const createUserValidation = require('./createUserValidation');

module.exports = {
loginValidation,
createUserValidation,
};
24 changes: 24 additions & 0 deletions src/utils/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,30 @@ const loginSchema = Joi.object({
}),
});

const createUserSchema = Joi.object({
displayName: Joi.string().min(8).required()
.messages({
'string.empty': 'Some required fields are missing',
'any.min': 'displayName must be at least 8 characters long',
'any.required': 'Some required fields are missing',
}),
email: Joi.string().regex(/^[a-z0-9.]+@[a-z0-9]+\.[a-z]+(\.[a-z]+)?$/i).required()
.messages({
'string.empty': 'Some required fields are missing',
'string.pattern.base': '"email" must be a valid email',
'any.required': 'Some required fields are missing',
}),
password: Joi.string().min(6).required()
.messages({
'string.empty': 'Some required fields are missing',
'any.min': 'password must be at least 6 characters long',
'any.required': 'Some required fields are missing',
}),
image: Joi.string()

});

module.exports = {
loginSchema,
createUserSchema,
};

0 comments on commit 817d4a4

Please sign in to comment.