summaryrefslogtreecommitdiffstats
path: root/src/features/reservations/services/reservationsService.ts
diff options
context:
space:
mode:
authorAlberto Duarte Delgado <alberto@albertoduarte.com>2026-04-01 13:03:25 +0100
committerAlberto Duarte Delgado <alberto@albertoduarte.com>2026-04-01 13:03:25 +0100
commit08ce78dbd97ad792e163482f454f285557c7cdd0 (patch)
tree998ce8d7d48f8344d9ec34691809f2482cde5136 /src/features/reservations/services/reservationsService.ts
parentc927739f8f221feabb6e19af2c2667eda905a2eb (diff)
Mejoras de UX y feedback de errores; y, manejo de status de reserva
Diffstat (limited to 'src/features/reservations/services/reservationsService.ts')
-rw-r--r--src/features/reservations/services/reservationsService.ts61
1 files changed, 56 insertions, 5 deletions
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<Reservation, 'date' | 'endTime'>): 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<void> {
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<Reservation[]> {