Skip to content

Commit

Permalink
Refactor on swagger documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
basshamut committed Jun 29, 2024
1 parent c50fec3 commit 8ba4eac
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 12 deletions.
16 changes: 15 additions & 1 deletion backend/config/swagger/swagger.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,22 @@ const options = {
{
url: process.env.API_DOMAIN,
description: 'Servidor'
},

],
components: {
securitySchemes: {
basicAuth: {
type: 'http',
scheme: 'basic',
},
},
},
security: [
{
basicAuth: []
}
]
],
},
apis: ['./controller/*.js'] // Archivos donde se definen las rutas
};
Expand Down
6 changes: 6 additions & 0 deletions backend/controller/MeetController.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const router = express.Router()
* application/json:
* schema:
* $ref: '#/components/schemas/Meet'
* security:
* - basicAuth: []
*/
router.post('/', (request, response) => {
response.setHeader('Content-Type', 'application/json')
Expand Down Expand Up @@ -104,6 +106,8 @@ router.post('/', (request, response) => {
* message:
* type: string
* example: Meet not found
* security:
* - basicAuth: []
*/
router.get('/', async (request, response) => {
const url = request.query.url
Expand Down Expand Up @@ -136,6 +140,8 @@ router.get('/', async (request, response) => {
* type: array
* items:
* $ref: '#/components/schemas/Meet'
* security:
* - basicAuth: []
*/
router.get('/all', async (request, response) => {
const meets = await meetService.getAll()
Expand Down
2 changes: 2 additions & 0 deletions backend/controller/PurchaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const PurchaseService = require("../service/PurchaseService");
* error:
* type: string
* example: Error message
* security:
* - basicAuth: []
*/
router.post('/', async (req, res) => {
try {
Expand Down
2 changes: 2 additions & 0 deletions backend/controller/StripeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ const domain = process.env.FRONTEND_URL;
* error:
* type: string
* example: "Error message"
* security:
* - basicAuth: []
*/
router.post('/create-checkout-session', async (req, res) => {
try {
Expand Down
130 changes: 119 additions & 11 deletions backend/controller/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,106 @@ function validatePassword(password) {
}

/**
* @swagger
* components:
* schemas:
* User:
* type: object
* required:
* - email
* - birthDate
* - password
* properties:
* email:
* type: string
* format: email
* description: El correo electrónico del usuario
* birthDate:
* type: string
* format: date
* description: La fecha de nacimiento del usuario
* password:
* type: string
* format: password
* description: La contraseña del usuario
* example:
* email: "usuario@example.com"
* birthDate: "2000-01-01"
* password: "password1234"
*
* UserLogin:
* type: object
* required:
* - user
* - password
* properties:
* user:
* type: string
* format: email
* description: El correo electrónico del usuario
* password:
* type: string
* format: password
* description: La contraseña del usuario (Base64 encoded)
* example:
* user: "usuario@example.com"
* password: "cGFzc3dvcmQxMjM="
*
* UserRole:
* type: object
* properties:
* userMail:
* type: string
* format: email
* description: El correo electrónico del usuario
* role:
* type: string
* description: El rol del usuario
* example:
* userMail: "usuario@example.com"
* role: "admin"
*
* UserValidation:
* type: object
* required:
* - userMail
* properties:
* userMail:
* type: string
* format: email
* description: El correo electrónico del usuario
* example:
* userMail: "usuario@example.com"
*
* Error:
* type: object
* properties:
* message:
* type: string
* description: El mensaje de error
* example:
* message: "El correo electrónico ya está registrado"
*
* UserResponse:
* type: object
* properties:
* email:
* type: string
* format: email
* description: El correo electrónico del usuario
* birthDate:
* type: string
* format: date
* description: La fecha de nacimiento del usuario
* role:
* type: string
* description: El rol del usuario
* example:
* email: "usuario@example.com"
* birthDate: "2000-01-01"
* role: "user"
/**
* @swagger
* /register:
* post:
Expand Down Expand Up @@ -43,9 +143,11 @@ function validatePassword(password) {
* description: Error en la validación de los datos
* 409:
* description: El email ya está registrado
* security:
* - basicAuth: []
*/
router.post('/register', async (req, res) => {
const { user, date, password } = req.body;
const {user, date, password} = req.body;

const errors = {};

Expand Down Expand Up @@ -74,16 +176,16 @@ router.post('/register', async (req, res) => {
}

if (Object.keys(errors).length > 0) {
return res.status(400).json({ errors });
return res.status(400).json({errors});
}

const userFound = await userService.save({ email: user, birthDate: date, password });
const userFound = await userService.save({email: user, birthDate: date, password});
if (userFound === null) {
const errorResponse = { message: 'El email ya está registrado' };
const errorResponse = {message: 'El email ya está registrado'};
return res.status(409).json(errorResponse);
}

res.status(201).json({ message: 'Registro exitoso' });
res.status(201).json({message: 'Registro exitoso'});
});

/**
Expand Down Expand Up @@ -114,6 +216,8 @@ router.post('/register', async (req, res) => {
* description: Error en la validación de los datos
* 404:
* description: Usuario o contraseña incorrectos
* security:
* - basicAuth: []
*/
router.post('/login', async (req, res) => {
const user = req.body.user;
Expand All @@ -132,13 +236,13 @@ router.post('/login', async (req, res) => {
}

if (Object.keys(errors).length > 0) {
return res.status(400).json({ errors });
return res.status(400).json({errors});
}

const userFound = await userService.login(user, password);

if (!userFound) {
return res.status(404).json({ message: 'Usuario o contraseña incorrectos' });
return res.status(404).json({message: 'Usuario o contraseña incorrectos'});
}

userFound.password = undefined;
Expand All @@ -161,6 +265,8 @@ router.post('/login', async (req, res) => {
* responses:
* 200:
* description: Roles del usuario
* security:
* - basicAuth: []
*/
router.get('/roles', async (req, res) => {
const userMail = req.query.userMail;
Expand Down Expand Up @@ -192,24 +298,26 @@ router.get('/roles', async (req, res) => {
* description: El email ya está validado o no fue proporcionado
* 404:
* description: El email no está registrado
* security:
* - basicAuth: []
*/
router.patch('/validate', async (req, res) => {
const userMail = req.body.userMail;
if (!userMail) {
return res.status(400).json({ message: 'El email es requerido' });
return res.status(400).json({message: 'El email es requerido'});
}

const userExist = await userService.findByEmail(userMail);
if (!userExist) {
return res.status(404).json({ message: 'El email no está registrado' });
return res.status(404).json({message: 'El email no está registrado'});
}

if (userExist.validated) {
return res.status(400).json({ message: 'El email ya está validado' });
return res.status(400).json({message: 'El email ya está validado'});
}

await userService.validate(userMail);
res.status(200).json({ message: 'Validación exitosa' });
res.status(200).json({message: 'Validación exitosa'});
});

module.exports = router;

0 comments on commit 8ba4eac

Please sign in to comment.