summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlberto Duarte Delgado <alberto@albertoduarte.com>2026-04-01 18:01:50 +0100
committerAlberto Duarte Delgado <alberto@albertoduarte.com>2026-04-01 18:01:50 +0100
commitffb065bba517e060f5490913b12bcf858e904d75 (patch)
tree77aacbdb22a2896c7ab9224e703e24e6720ab6f1
parent47538e50cbad770ddeb998904253afcdd51e20f6 (diff)
querys indexadas a proyecto, mejora de consultas y eas.json
-rw-r--r--eas.json18
-rw-r--r--firebase.json3
-rw-r--r--firestore.indexes.json45
-rw-r--r--src/features/reservations/services/reservationsService.ts29
4 files changed, 85 insertions, 10 deletions
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<string, unknown>;
+};
+
export const reservationsService = {
async create(input: CreateReservationInput): Promise<void> {
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<string, unknown>;
+
+ 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()));
},
};