From ffb065bba517e060f5490913b12bcf858e904d75 Mon Sep 17 00:00:00 2001 From: Alberto Duarte Delgado Date: Wed, 1 Apr 2026 18:01:50 +0100 Subject: querys indexadas a proyecto, mejora de consultas y eas.json --- eas.json | 18 +++++++++ firebase.json | 3 +- firestore.indexes.json | 45 ++++++++++++++++++++++ .../reservations/services/reservationsService.ts | 29 +++++++++----- 4 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 eas.json create mode 100644 firestore.indexes.json diff --git a/eas.json b/eas.json new file mode 100644 index 0000000..56af5af --- /dev/null +++ b/eas.json @@ -0,0 +1,18 @@ +{ + "cli": { + "version": ">= 16.0.0" + }, + "build": { + "preview": { + "distribution": "internal", + "android": { + "buildType": "apk" + } + }, + "production": { + "android": { + "buildType": "app-bundle" + } + } + } +} diff --git a/firebase.json b/firebase.json index 0207c3f..d4d918a 100644 --- a/firebase.json +++ b/firebase.json @@ -1,5 +1,6 @@ { "firestore": { - "rules": "firestore.rules" + "rules": "firestore.rules", + "indexes": "firestore.indexes.json" } } diff --git a/firestore.indexes.json b/firestore.indexes.json new file mode 100644 index 0000000..16fdc81 --- /dev/null +++ b/firestore.indexes.json @@ -0,0 +1,45 @@ +{ + "indexes": [ + { + "collectionGroup": "reservations", + "queryScope": "COLLECTION", + "fields": [ + { + "fieldPath": "userId", + "order": "ASCENDING" + }, + { + "fieldPath": "date", + "order": "ASCENDING" + }, + { + "fieldPath": "startTime", + "order": "ASCENDING" + } + ] + }, + { + "collectionGroup": "reservations", + "queryScope": "COLLECTION", + "fields": [ + { + "fieldPath": "officeId", + "order": "ASCENDING" + }, + { + "fieldPath": "date", + "order": "ASCENDING" + }, + { + "fieldPath": "status", + "order": "ASCENDING" + }, + { + "fieldPath": "startTime", + "order": "ASCENDING" + } + ] + } + ], + "fieldOverrides": [] +} diff --git a/src/features/reservations/services/reservationsService.ts b/src/features/reservations/services/reservationsService.ts index cc53782..176bbce 100644 --- a/src/features/reservations/services/reservationsService.ts +++ b/src/features/reservations/services/reservationsService.ts @@ -81,6 +81,11 @@ function isPastActiveReservation(reservation: Reservation, now = new Date()): bo return reservation.status === 'active' && getReservationEndDate(reservation) < now; } +type ReservationRecord = { + reservation: Reservation; + rawData: Record; +}; + export const reservationsService = { async create(input: CreateReservationInput): Promise { const reservationsCollection = collection(db, 'reservations'); @@ -187,18 +192,25 @@ 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) + const reservationRecords: ReservationRecord[] = snapshot.docs.map((snapshotDoc) => { + const rawData = snapshotDoc.data() as Record; + + return { + reservation: mapReservation(snapshotDoc.id, rawData), + rawData, + }; + }); + const reservations = reservationRecords.map((record) => record.reservation); + const reservationsToComplete = reservationRecords.filter((record) => + isPastActiveReservation(record.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; + for (const { reservation, rawData } of reservationsToComplete) { + const storedLockIds = rawData.lockIds; const lockIds = Array.isArray(storedLockIds) && storedLockIds.every((value) => typeof value === 'string') ? storedLockIds @@ -230,14 +242,13 @@ export const reservationsService = { collection(db, 'reservations'), where('officeId', '==', officeId), where('date', '==', date), + where('status', '==', 'active'), orderBy('startTime', 'asc') ); const snapshot = await getDocs(q); - return snapshot.docs - .map((item) => mapReservation(item.id, item.data())) - .filter((reservation) => reservation.status === 'active'); + return snapshot.docs.map((item) => mapReservation(item.id, item.data())); }, }; -- cgit v1.2.3-54-g00ecf