Introducci贸n a Node.js y Express
Node.js y Express
Entorno de trabajo
Variables de entorno
Rutas din谩micas
Body parser
Postman
CRUD
驴Que es una API Restful?
Solicitudes GET
Solicitudes POST
CRUD
Soluci贸n del reto de validaci贸n
Solicitudes DELETE
MIddlewares
Middlewares - logger
Middlewares - ErrorHandler
DB
Instalar Postgresql
Instalar Prisma y PostgreSQL Client
Prisma Models
JWT
Autenticaci贸n utilizando JSON Web Tokens (JWT)
Auth - Register
Auth - Login
Expres.js
Arquitectura
Arquitectura parte 2
Creaci贸n y Migraci贸n de Modelos con Prisma para Citas M茅dicas
Admin controllers / services
Admin controllers / services parte 2
Reservations
Reservations parte 2
Appointments
Deploy
PostgreSQL
Deploy
You don't have access to this class
Keep learning! Join and start boosting your career
Appointment and time block management is an essential component of any reservation system. Properly implementing the administrative part of your application not only improves the user experience, but also provides efficient control over availability and reservations. In this content, we will explore how to create administrative services to manage time blocks and list reservations, all with a focus on security and user roles.
To implement the administrative part of our application, we need to create a specific service that handles the business logic related to managing time blocks and reservations. This service will act as an intermediary between our controllers and the database.
First, we create a new file called admin.service.js
where we will implement all the necessary logic. The first thing we need is to import Prisma to communicate with our database:
const { PrismaClient } = require('@prisma/client');constprisma = new PrismaClient();
We need to implement two main services:
const createTimeBlockService = async (startTime, endTime) => { const newTimeBlock = await prisma.timeBlock.create({ data: { startTime: new Date(startTime), endTime: new Date(endTime) } } );
return newTimeBlock;};
const listReservationService = async () => { const reservations = await prisma.appointments.findMany({ include: { user: true, timeBlock: true } } });
return reservations;};
module.exports = { createTimeBlockService, listReservationService};
It is important to note that the reservation listing service includes information on both the user and the associated time block, providing a complete view of each reservation.
Security is a crucial aspect in any application, especially when it comes to administrative functionalities. To protect our routes, we implement an authentication middleware that verifies both the validity of the token and the role of the user.
In our administrative controller, we need to import this middleware:
const authenticateToken = require('../middlewares/authorization');
Then, we apply this middleware to our administrative routes:
router.get('/reservations', authenticateToken, async (req, res) => { // Logic to list reservations});
router.post('/time-blocks', authenticateToken, async (req, res) => { // Logic to create time blocks});
The authentication middleware not only verifies that the token is valid, but also checks if the user has the role of administrator. This ensures that only users with appropriate permissions can access these functionalities.
If a user without an administrator role attempts to access these paths, they will receive an "Access denied" message, even if their token is valid. This provides an additional layer of security to protect sensitive information.
To test our administrative services, we can use tools like Postman. Here are some important testing scenarios:
It is critical to verify that the routes are correctly defined. A common mistake is to use incorrect path names, which will result in 404 (Not Found) errors.
Implementing robust administrative services significantly improves the management of your application, allowing efficient control over availability and reservations. Always remember to prioritize security by properly validating tokens and user roles. Have you ever implemented a similar system in your projects? Share your experience in the comments and tell us what challenges you encountered during the process.
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?