From 08ce78dbd97ad792e163482f454f285557c7cdd0 Mon Sep 17 00:00:00 2001 From: Alberto Duarte Delgado Date: Wed, 1 Apr 2026 13:03:25 +0100 Subject: Mejoras de UX y feedback de errores; y, manejo de status de reserva --- .../reservations/services/reservationsService.ts | 61 ++++++++++++++++++++-- 1 file changed, 56 insertions(+), 5 deletions(-) (limited to 'src/features/reservations/services') diff --git a/src/features/reservations/services/reservationsService.ts b/src/features/reservations/services/reservationsService.ts index e58308d..4ddf436 100644 --- a/src/features/reservations/services/reservationsService.ts +++ b/src/features/reservations/services/reservationsService.ts @@ -6,6 +6,7 @@ import { query, runTransaction, serverTimestamp, + writeBatch, where, } from 'firebase/firestore'; import { db } from '@/shared/lib/firebase/config'; @@ -16,6 +17,10 @@ import { } from '@/features/reservations/types'; function mapReservationStatus(value: unknown): ReservationStatus { + if (value === 'completed') { + return 'completed'; + } + return value === 'cancelled' ? 'cancelled' : 'active'; } @@ -67,6 +72,14 @@ function buildReservationLockIds(input: { return lockIds; } +function getReservationEndDate(reservation: Pick): Date { + return new Date(`${reservation.date}T${reservation.endTime}:00`); +} + +function isPastActiveReservation(reservation: Reservation, now = new Date()): boolean { + return reservation.status === 'active' && getReservationEndDate(reservation) < now; +} + export const reservationsService = { async create(input: CreateReservationInput): Promise { const reservationsCollection = collection(db, 'reservations'); @@ -75,20 +88,24 @@ export const reservationsService = { const lockIds = buildReservationLockIds(input); await runTransaction(db, async (transaction) => { - for (const lockId of lockIds) { - const lockRef = doc(reservationLocksCollection, lockId); - const lockSnapshot = await transaction.get(lockRef); + const lockRefs = lockIds.map((lockId) => doc(reservationLocksCollection, lockId)); + const lockSnapshots = await Promise.all( + lockRefs.map((lockRef) => transaction.get(lockRef)) + ); + for (const lockSnapshot of lockSnapshots) { if (lockSnapshot.exists()) { throw new Error(RESERVATION_CONFLICT_ERROR); } + } + for (const [index, lockRef] of lockRefs.entries()) { transaction.set(lockRef, { reservationId: reservationRef.id, userId: input.userId, officeId: input.officeId, date: input.date, - time: lockId.split('__')[2], + time: lockIds[index].split('__')[2], }); } @@ -143,8 +160,42 @@ export const reservationsService = { ); const snapshot = await getDocs(q); + const reservations = snapshot.docs.map((doc) => mapReservation(doc.id, doc.data())); + const reservationsToComplete = reservations.filter((reservation) => + isPastActiveReservation(reservation) + ); + + if (reservationsToComplete.length > 0) { + const batch = writeBatch(db); + const reservationLocksCollection = collection(db, 'reservationLocks'); + + for (const reservation of reservationsToComplete) { + const snapshotData = snapshot.docs.find((doc) => doc.id === reservation.id)?.data(); + const storedLockIds = snapshotData?.lockIds; + const lockIds = + Array.isArray(storedLockIds) && storedLockIds.every((value) => typeof value === 'string') + ? storedLockIds + : buildReservationLockIds(reservation); + + for (const lockId of lockIds) { + batch.delete(doc(reservationLocksCollection, lockId)); + } + + batch.update(doc(db, 'reservations', reservation.id), { + status: 'completed', + }); + } + + await batch.commit(); + + return reservations.map((reservation) => + isPastActiveReservation(reservation) + ? { ...reservation, status: 'completed' } + : reservation + ); + } - return snapshot.docs.map((doc) => mapReservation(doc.id, doc.data())); + return reservations; }, async getByOfficeAndDate(officeId: string, date: string): Promise { -- cgit v1.2.3-54-g00ecf