Skip to content
Snippets Groups Projects
Commit da95c5fc authored by migudel's avatar migudel :speech_balloon:
Browse files

Manejo de reservas mejorado

parent f98e040d
Branches
Tags
1 merge request!10Add ts types and json mocks, remove poblate scripts and fix the cascade...
......@@ -9,6 +9,7 @@ import {
import { BookingService } from '../../../../shared/booking.service'; // Asegúrate de que el servicio exista
import { ActivatedRoute, Router } from '@angular/router';
import { Booking } from '../../../../../types';
import { ClienteApiRestService } from '../../../../shared/cliente-api-rest.service';
@Component({
standalone: true,
......@@ -25,7 +26,8 @@ export class BookingComponent implements OnInit {
private router: Router,
private route: ActivatedRoute,
private fb: FormBuilder,
private bookingService: BookingService
private bookingService: BookingService,
private client: ClienteApiRestService
) {
// Inicialización del formulario con validaciones
this.bookingForm = this.fb.group({
......@@ -59,15 +61,20 @@ export class BookingComponent implements OnInit {
(response) => {
console.log('Reserva creada con éxito', response);
// Llama al servicio para actualizar el estado del usuario
this.bookingService.createBooking(bookingRequest).subscribe({
this.client
.alterUserStatus(userId, 'WITH_ACTIVE_BOOKINGS')
.subscribe({
next: (response) => {
console.log('Reserva creada con éxito', response);
console.log(
'Estado de usuario actualizado con exito',
response
);
this.router.navigate(['/user', userId, 'bookings']);
},
error: (error) => {
console.error('Error al crear la reserva', error);
console.error('Error al cambiar el estado del usuario', error);
},
});
this.router.navigate(['/user', userId, 'bookings']);
},
(error) => {
console.error('Error al crear la reserva', error);
......
......@@ -59,7 +59,6 @@ export class ClienteApiRestService {
const startStr = start.toISOString().split('T')[0];
const endStr = end.toISOString().split('T')[0];
const url = `${ClienteApiRestService.HOTEL_URI}/${hotelId}/rooms?start=${startStr}&end=${endStr}`;
console.warn(url);
return this.http.get<Room[]>(url);
}
......@@ -75,9 +74,9 @@ export class ClienteApiRestService {
);
}
alterUserStatus(status: UserState) {
alterUserStatus(userId: number, status: UserState) {
return this.http.patch(
`${ClienteApiRestService.BASE_URI}/users`,
`${ClienteApiRestService.BASE_URI}/users/${userId}`,
{
status,
},
......
......@@ -70,31 +70,29 @@ public class UserController {
}
UserStatus userStatus = UserStatus.valueOf(strStatus);
// TODO decicir cual va a ser nuestra politica
boolean activeBookings = target.getBookings().stream()
.anyMatch(booking -> booking.getEndDate().isAfter(LocalDate.now())); // reserva > ahora
// boolean activeBookings = target.getBookings().stream().anyMatch(booking ->
// !booking.getEndDate().isBefore(LocalDate.now())); // reserva >= ahora
.anyMatch(booking -> !booking.getEndDate().isBefore(LocalDate.now())); // reserva >= ahora
boolean inactiveBookings = target.getBookings().stream()
.anyMatch(booking -> booking.getStartDate().isBefore(LocalDate.now())); // reserva < ahora
// boolean inactiveBookings = target.getBookings().stream().anyMatch(booking ->
// !booking.getStartDate().isAfter(LocalDate.now())); // reserva <= ahora
switch (userStatus) {
// TODO Buscar como validar las (in)active bookings
case NO_BOOKINGS:
if (!target.getBookings().isEmpty())
throw new IllegalArgumentException("Invalid State: The user has at least one booking");
break;
case WITH_ACTIVE_BOOKINGS:
if (target.getBookings().isEmpty())
throw new IllegalArgumentException("Invalid State: The user don't has bookings");
if (!activeBookings)
throw new IllegalArgumentException("Invalid State: The user don't has active bookings");
break;
case WITH_INACTIVE_BOOKINGS:
if (target.getBookings().isEmpty())
throw new IllegalArgumentException("Invalid State: The user don't has bookings");
if (!inactiveBookings)
throw new IllegalArgumentException("Invalid State: The user don't has inactive bookings");
break;
default:
break;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment