diff --git a/angular/RestClient/src/app/core/services/api/bookings/booking-client.service.ts b/angular/RestClient/src/app/core/services/api/bookings/booking-client.service.ts index 5f206025b7d53cb077a96880de8fe1e1477d9c11..7e88d90b9236a7642cd9f5b95afa232b89186190 100644 --- a/angular/RestClient/src/app/core/services/api/bookings/booking-client.service.ts +++ b/angular/RestClient/src/app/core/services/api/bookings/booking-client.service.ts @@ -1,8 +1,10 @@ import { Injectable } from '@angular/core'; -import { HttpClient, HttpHeaders } from '@angular/common/http'; -import { Observable } from 'rxjs'; +import { HttpClient } from '@angular/common/http'; +import { Observable, throwError } from 'rxjs'; import { environment } from '../../../../../environments/environment'; import { Booking } from '@features/bookings'; +import { HotelClientService } from '../hotels/hotel-client.service'; +import { switchMap, map } from 'rxjs/operators'; @Injectable({ providedIn: 'root', @@ -10,16 +12,36 @@ import { Booking } from '@features/bookings'; export class BookingClientService { private URI = environment.bookingAPI; - constructor(private http: HttpClient) {} + constructor( + private http: HttpClient, + private hotelClientService: HotelClientService // Inyectamos el servicio HotelClientService + ) {} // Método para crear una nueva reserva createBooking(bookingRequest: Booking): Observable<Booking> { - const { start, end } = bookingRequest; - const endDate = end.toISOString(); - console.log({ bookingRequest, end: endDate }); - - return this.http.post<Booking>(this.URI, bookingRequest); + const { hotelId, start, end, userId, roomId } = bookingRequest; + + if (!hotelId) { + console.error("hotelId is undefined"); + return throwError(() => new Error("hotelId is undefined")); + } + + // Llamamos al servicio HotelClientService para obtener el hotel y su managerId + return this.hotelClientService.getHotel(hotelId).pipe( + map((hotel) => { + const managerId = hotel.managerId; + console.log("Manager ID:", managerId); // Verifica que el managerId es correcto + + // Retornamos el objeto bookingRequest con el managerId actualizado + return { ...bookingRequest, managerId }; + }), + switchMap((updatedBookingRequest) => { + console.log("Final bookingRequest with managerId:", updatedBookingRequest); // Verifica el objeto final + return this.http.post<Booking>(this.URI, updatedBookingRequest); + }) + ); } + // Método para obtener todas las reservas getAllBookings(): Observable<Booking[]> { diff --git a/angular/RestClient/src/app/core/services/api/hotels/hotel-client.service.ts b/angular/RestClient/src/app/core/services/api/hotels/hotel-client.service.ts index 671367790bd59dba5699ac31c33d7c507fba30dc..460494edfc8d18a33e3df60ae68a90ce75264d76 100644 --- a/angular/RestClient/src/app/core/services/api/hotels/hotel-client.service.ts +++ b/angular/RestClient/src/app/core/services/api/hotels/hotel-client.service.ts @@ -53,7 +53,7 @@ export class HotelClientService { throw new Error('No session found'); } const { id } = session; - const hotelWithHM = { ...hotel, hotelManager: { id } }; + const hotelWithHM = { ...hotel, managerId: id }; return hotelWithHM; }), switchMap((hotelWithHM) => diff --git a/angular/RestClient/src/app/features/bookings/booking/booking.component.ts b/angular/RestClient/src/app/features/bookings/booking/booking.component.ts index 26a80e2af7168bff9f9761ed84260963d1d3fa39..fba667560908669519a5e45ea91be33309770e90 100644 --- a/angular/RestClient/src/app/features/bookings/booking/booking.component.ts +++ b/angular/RestClient/src/app/features/bookings/booking/booking.component.ts @@ -7,7 +7,7 @@ import { } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; -import { Booking} from '@features/bookings'; +import { Booking } from '@features/bookings'; import { User } from '@features/users'; import { LocalStorageService } from '../../../core/services/storage/local-storage.service'; import { BookingClientService } from '../../../core/services/api/bookings/booking-client.service'; @@ -36,6 +36,7 @@ export class BookingComponent { start: new Date(), }; roomId: number = 0; + hotelId: number = 0; // Agregamos hotelId constructor( private router: Router, @@ -52,26 +53,38 @@ export class BookingComponent { start: [{ value: '', disabled: true }, Validators.required], end: [{ value: '', disabled: true }, Validators.required], }); + const localBooking = storage.read<communication | null>('booking-data'); if (localBooking === null) { this.router.navigate(['/booking', 'search']); return; } this.bookingLocal = localBooking!; + this.route.queryParams.subscribe((params) => { const roomId = Number(params['roomId']); + this.hotelId = Number(params['hotelId']); // Extraemos hotelId de los parámetros de la URL this.roomId = roomId; + if (this.bookingLocal.roomId !== roomId) { this.router.navigate(['/bookings', 'search']); return; } + + if (!this.hotelId) { + console.error('No se ha proporcionado hotelId'); + return; + } + this.bookingLocal = { ...this.bookingLocal, start: new Date(this.bookingLocal.start), end: new Date(this.bookingLocal.end), }; + this.loadBooking(); }); + this.sessionService.getSession().subscribe({ next: (session) => { if (session) this.user = session; @@ -104,6 +117,7 @@ export class BookingComponent { ...this.bookingLocal, userId: id, roomId: this.roomId, + hotelId: this.hotelId, // Aseguramos que hotelId esté incluido }; // Llama al servicio para crear una nueva reserva @@ -115,7 +129,7 @@ export class BookingComponent { // Llama al servicio para actualizar el estado del usuario this.userClient.alterUserStatus(id, 'WITH_ACTIVE_BOOKINGS').subscribe({ next: (response) => { - console.log('Estado de usuario actualizado con exito', response); + console.log('Estado de usuario actualizado con éxito', response); this.storage.remove('booking-data'); this.router.navigate(['/me', 'bookings']); }, diff --git a/angular/RestClient/src/app/features/hotels/hotel-list/hotel-list.component.ts b/angular/RestClient/src/app/features/hotels/hotel-list/hotel-list.component.ts index 69eefe9d0ac4ea8e3fcdbf608e7970390eabedfd..b38638df307105a924dd7129e06c653b6be784bc 100644 --- a/angular/RestClient/src/app/features/hotels/hotel-list/hotel-list.component.ts +++ b/angular/RestClient/src/app/features/hotels/hotel-list/hotel-list.component.ts @@ -1,6 +1,7 @@ import { Component, NgModule } from '@angular/core'; import { RouterModule, Router, ActivatedRoute, Data } from '@angular/router'; -import { Hotel, Room, RoomType, roomTypeArray } from '@features/hotels'; +import { Hotel, Room, RoomType } from '@features/hotels'; +import { roomTypeArray } from '@features/hotels/types/Room.interface'; import { MatAccordion, MatExpansionPanel,