diff options
Diffstat (limited to 'src/features/reservations')
| -rw-r--r-- | src/features/reservations/hooks/useOfficeAvailability.ts | 74 | ||||
| -rw-r--r-- | src/features/reservations/hooks/useReservations.ts | 17 | ||||
| -rw-r--r-- | src/features/reservations/services/reservationsService.ts | 27 | ||||
| -rw-r--r-- | src/features/reservations/types.ts | 1 |
4 files changed, 119 insertions, 0 deletions
diff --git a/src/features/reservations/hooks/useOfficeAvailability.ts b/src/features/reservations/hooks/useOfficeAvailability.ts new file mode 100644 index 0000000..5cdd348 --- /dev/null +++ b/src/features/reservations/hooks/useOfficeAvailability.ts @@ -0,0 +1,74 @@ +import { useEffect, useState } from 'react'; +import { reservationsService } from '@/features/reservations/services/reservationsService'; +import { Reservation } from '@/features/reservations/types'; +import { getErrorMessage } from '@/shared/lib/errors/getErrorMessage'; + +function getToday(): string { + return new Date().toISOString().slice(0, 10); +} + +function normalizeDateInput(value: string): string { + const digits = value.replace(/\D/g, '').slice(0, 8); + + if (digits.length <= 4) { + return digits; + } + + if (digits.length <= 6) { + return `${digits.slice(0, 4)}-${digits.slice(4)}`; + } + + return `${digits.slice(0, 4)}-${digits.slice(4, 6)}-${digits.slice(6)}`; +} + +export function useOfficeAvailability(officeId?: string) { + const [selectedDate, setSelectedDateValue] = useState(getToday); + const [reservations, setReservations] = useState<Reservation[]>([]); + const [loading, setLoading] = useState(true); + const [error, setError] = useState<string | null>(null); + + const loadAvailability = async () => { + if (!officeId) { + setReservations([]); + setLoading(false); + setError('No se indicó una oficina válida'); + return; + } + + if (selectedDate.trim().length !== 10) { + setReservations([]); + setLoading(false); + return; + } + + try { + setError(null); + setLoading(true); + + const data = await reservationsService.getByOfficeAndDate(officeId, selectedDate); + setReservations(data); + } catch (err) { + setError(getErrorMessage(err, 'No se pudo cargar la disponibilidad')); + } finally { + setLoading(false); + } + }; + + useEffect(() => { + loadAvailability(); + }, [officeId, selectedDate]); + + const setSelectedDate = (value: string) => { + setError(null); + setSelectedDateValue(normalizeDateInput(value)); + }; + + return { + selectedDate, + reservations, + loading, + error, + setSelectedDate, + reload: loadAvailability, + }; +} diff --git a/src/features/reservations/hooks/useReservations.ts b/src/features/reservations/hooks/useReservations.ts index cf58811..bb67a84 100644 --- a/src/features/reservations/hooks/useReservations.ts +++ b/src/features/reservations/hooks/useReservations.ts @@ -11,6 +11,7 @@ export function useReservations() { const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); const [cancelingReservationId, setCancelingReservationId] = useState<string | null>(null); + const [checkingInReservationId, setCheckingInReservationId] = useState<string | null>(null); const loadReservations = async () => { if (!user?.uid) { @@ -50,6 +51,20 @@ export function useReservations() { } }; + const checkInReservation = async (reservationId: string) => { + try { + setError(null); + setCheckingInReservationId(reservationId); + + await reservationsService.checkIn(reservationId); + await loadReservations(); + } catch (err) { + setError(getErrorMessage(err, 'No se pudo registrar la llegada')); + } finally { + setCheckingInReservationId(null); + } + }; + return { reservations, activeReservations: reservations.filter((reservation) => reservation.status === 'active'), @@ -57,7 +72,9 @@ export function useReservations() { loading, error, cancelingReservationId, + checkingInReservationId, cancelReservation, + checkInReservation, reload: loadReservations, }; } diff --git a/src/features/reservations/services/reservationsService.ts b/src/features/reservations/services/reservationsService.ts index 4ddf436..cc53782 100644 --- a/src/features/reservations/services/reservationsService.ts +++ b/src/features/reservations/services/reservationsService.ts @@ -35,6 +35,7 @@ function mapReservation(id: string, data: any): Reservation { startTime: data.startTime ?? '', endTime: data.endTime ?? '', status: mapReservationStatus(data.status), + checkInAt: data.checkInAt, createdAt: data.createdAt, }; } @@ -151,6 +152,32 @@ export const reservationsService = { }); }, + async checkIn(reservationId: string): Promise<void> { + const reservationRef = doc(db, 'reservations', reservationId); + + await runTransaction(db, async (transaction) => { + const reservationSnapshot = await transaction.get(reservationRef); + + if (!reservationSnapshot.exists()) { + throw new Error('La reserva no existe'); + } + + const reservation = mapReservation(reservationSnapshot.id, reservationSnapshot.data()); + + if (reservation.status !== 'active') { + throw new Error('Solo puedes hacer check-in en reservas activas'); + } + + if (reservation.checkInAt) { + return; + } + + transaction.update(reservationRef, { + checkInAt: serverTimestamp(), + }); + }); + }, + async getByUserId(userId: string): Promise<Reservation[]> { const q = query( collection(db, 'reservations'), diff --git a/src/features/reservations/types.ts b/src/features/reservations/types.ts index 9a5fe73..5b36351 100644 --- a/src/features/reservations/types.ts +++ b/src/features/reservations/types.ts @@ -10,6 +10,7 @@ export type Reservation = { startTime: string; endTime: string; status: ReservationStatus; + checkInAt?: unknown; createdAt?: unknown; }; |