Skip to content

Commit

Permalink
chore(backend): validate the routes of application
Browse files Browse the repository at this point in the history
  • Loading branch information
João Paulo committed Aug 18, 2020
1 parent c0bc6bd commit c6a4a05
Show file tree
Hide file tree
Showing 10 changed files with 6,211 additions and 7 deletions.
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "MIT",
"dependencies": {
"bcryptjs": "^2.4.3",
"celebrate": "^12.2.0",
"cors": "^2.8.5",
"date-fns": "^2.12.0",
"express": "^4.17.1",
Expand All @@ -34,6 +35,7 @@
"@types/bcryptjs": "^2.4.2",
"@types/cors": "^2.8.6",
"@types/express": "^4.17.6",
"@types/hapi__joi": "^17.1.4",
"@types/jest": "^26.0.3",
"@types/jsonwebtoken": "^8.3.9",
"@types/mongodb": "^3.5.26",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Router } from 'express';
import { celebrate, Segments, Joi } from 'celebrate';

import ensureAuthenticated from '@modules/users/infra/http/middlewares/ensureAuthenticated';
import AppointmentsController from '../controllers/AppointmentsController';
import ProviderAppointmentsController from '../controllers/ProviderAppointmentsController';
Expand All @@ -9,7 +11,16 @@ const providerAppointmentsController = new ProviderAppointmentsController();

appointmentsRouter.use(ensureAuthenticated);

appointmentsRouter.post('/', appointmentsController.create);
appointmentsRouter.post(
'/',
celebrate({
[Segments.BODY]: {
provider_id: Joi.string().uuid().required(),
date: Joi.date(),
},
}),
appointmentsController.create,
);
appointmentsRouter.get('/me', providerAppointmentsController.index);

export default appointmentsRouter;
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { Router } from 'express';
import { celebrate, Segments, Joi } from 'celebrate';

import ensureAuthenticated from '@modules/users/infra/http/middlewares/ensureAuthenticated';
import ProvidersController from '../controllers/ProvidersController';
import ProviderMonthAvailabilityController from '../controllers/ProviderMonthAvailabilityController';
Expand All @@ -13,12 +15,23 @@ const providerDayAvailabilityController = new ProviderDayAvailabilityController(
providersRouter.use(ensureAuthenticated);

providersRouter.get('/', providersController.index);

providersRouter.get(
'/:provider_id/month-availability',
celebrate({
[Segments.PARAMS]: {
provider_id: Joi.string().uuid().required(),
},
}),
providerMonthAvailabilityController.index,
);
providersRouter.get(
'/:provider_id/day-availability',
celebrate({
[Segments.PARAMS]: {
provider_id: Joi.string().uuid().required(),
},
}),
providerDayAvailabilityController.index,
);

Expand Down
23 changes: 21 additions & 2 deletions backend/src/modules/users/infra/http/routes/password.routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Router } from 'express';
import { celebrate, Segments, Joi } from 'celebrate';

import ForgotPasswordController from '../controllers/ForgotPasswordController';
import ResetPasswordController from '../controllers/ResetPasswordController';
Expand All @@ -7,7 +8,25 @@ const passwordRouter = Router();
const forgotPasswordController = new ForgotPasswordController();
const resetPasswordController = new ResetPasswordController();

passwordRouter.post('/forgot', forgotPasswordController.create);
passwordRouter.post('/reset', resetPasswordController.create);
passwordRouter.post(
'/forgot',
celebrate({
[Segments.BODY]: {
email: Joi.string().email().required(),
},
}),
forgotPasswordController.create,
);
passwordRouter.post(
'/reset',
celebrate({
[Segments.BODY]: {
token: Joi.string().uuid().required(),
password: Joi.string().required(),
password_confirmation: Joi.string().required().valid(Joi.ref('password')),
},
}),
resetPasswordController.create,
);

export default passwordRouter;
15 changes: 14 additions & 1 deletion backend/src/modules/users/infra/http/routes/profile.routes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Router } from 'express';
import { celebrate, Segments, Joi } from 'celebrate';

import ProfileController from '../controllers/ProfileController';

Expand All @@ -10,6 +11,18 @@ const profileController = new ProfileController();
profileRouter.use(ensureAuthenticated);

profileRouter.get('/', profileController.show);
profileRouter.put('/', profileController.update);
profileRouter.put(
'/',
celebrate({
[Segments.BODY]: {
name: Joi.string().required(),
email: Joi.string().email().required(),
old_password: Joi.string(),
password: Joi.string(),
password_confirmation: Joi.string().valid(Joi.ref('password')),
},
}),
profileController.update,
);

export default profileRouter;
12 changes: 11 additions & 1 deletion backend/src/modules/users/infra/http/routes/sessions.routes.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { Router } from 'express';
import { celebrate, Segments, Joi } from 'celebrate';

import SessionsController from '../controllers/SessionsController';

const sessionsRouter = Router();
const sessionsController = new SessionsController();

sessionsRouter.post('/', sessionsController.create);
sessionsRouter.post(
'/',
celebrate({
[Segments.BODY]: {
email: Joi.string().email().required(),
password: Joi.string().required(),
},
}),
sessionsController.create,
);

export default sessionsRouter;
14 changes: 13 additions & 1 deletion backend/src/modules/users/infra/http/routes/users.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import multer from 'multer';

import uploadConfig from '@config/upload';

import { celebrate, Segments, Joi } from 'celebrate';

import ensureAuthenticated from '../middlewares/ensureAuthenticated';

import UsersController from '../controllers/UsersController';
Expand All @@ -14,7 +16,17 @@ const userAvatarController = new UserAvatarController();

const upload = multer(uploadConfig);

usersRouter.post('/', usersController.create);
usersRouter.post(
'/',
celebrate({
[Segments.BODY]: {
name: Joi.string().required(),
email: Joi.string().email().required(),
password: Joi.string().required(),
},
}),
usersController.create,
);

usersRouter.patch(
'/avatar',
Expand Down
3 changes: 3 additions & 0 deletions backend/src/shared/infra/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'reflect-metadata';
import express, { Request, Response, NextFunction } from 'express';
import 'express-async-errors';
import cors from 'cors';
import { errors } from 'celebrate';

import uploadConfig from '@config/upload';
import AppError from '@shared/errors/AppError';
Expand All @@ -18,6 +19,8 @@ app.use(express.json());
app.use('/files', express.static(uploadConfig.uploadsFolder));
app.use(routes);

app.use(errors());

app.use((err: Error, req: Request, res: Response, _: NextFunction) => {
if (err instanceof AppError) {
return res.status(err.statusCode).json({
Expand Down
Loading

0 comments on commit c6a4a05

Please sign in to comment.