diff options
| author | Alberto Duarte Delgado <alberto@albertoduarte.com> | 2026-03-30 17:37:42 +0100 |
|---|---|---|
| committer | Alberto Duarte Delgado <alberto@albertoduarte.com> | 2026-03-30 17:37:42 +0100 |
| commit | 174f1acd46c4ffbde88c04465e8fa6b6810ce113 (patch) | |
| tree | d0c6705b0130f088e76b7303d394ff2a8d66129c /src/features/reservations/services | |
| parent | 31584b4e5a3a551e2ceb15281a38677c39c46e12 (diff) | |
logica de reservas
Diffstat (limited to 'src/features/reservations/services')
| -rw-r--r-- | src/features/reservations/services/reservationsService.ts | 63 | ||||
| -rw-r--r-- | src/features/reservations/services/reservationsService.ts~ | 0 |
2 files changed, 63 insertions, 0 deletions
diff --git a/src/features/reservations/services/reservationsService.ts b/src/features/reservations/services/reservationsService.ts new file mode 100644 index 0000000..e56a302 --- /dev/null +++ b/src/features/reservations/services/reservationsService.ts @@ -0,0 +1,63 @@ +import { + addDoc, + collection, + getDocs, + orderBy, + query, + serverTimestamp, + where, +} from 'firebase/firestore'; +import { db } from '@/shared/lib/firebase/config'; +import { + CreateReservationInput, + Reservation, +} from '@/features/reservations/types'; + +function mapReservation(id: string, data: any): Reservation { + return { + id, + officeId: data.officeId ?? '', + officeName: data.officeName ?? '', + userId: data.userId ?? '', + userEmail: data.userEmail ?? '', + date: data.date ?? '', + startTime: data.startTime ?? '', + endTime: data.endTime ?? '', + createdAt: data.createdAt, + }; +} + +export const reservationsService = { + async create(input: CreateReservationInput): Promise<void> { + await addDoc(collection(db, 'reservations'), { + ...input, + createdAt: serverTimestamp(), + }); + }, + + async getByUserId(userId: string): Promise<Reservation[]> { + const q = query( + collection(db, 'reservations'), + where('userId', '==', userId), + orderBy('date', 'asc'), + orderBy('startTime', 'asc') + ); + + const snapshot = await getDocs(q); + + return snapshot.docs.map((doc) => mapReservation(doc.id, doc.data())); + }, + + async getByOfficeAndDate(officeId: string, date: string): Promise<Reservation[]> { + const q = query( + collection(db, 'reservations'), + where('officeId', '==', officeId), + where('date', '==', date), + orderBy('startTime', 'asc') + ); + + const snapshot = await getDocs(q); + + return snapshot.docs.map((doc) => mapReservation(doc.id, doc.data())); + }, +}; diff --git a/src/features/reservations/services/reservationsService.ts~ b/src/features/reservations/services/reservationsService.ts~ new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/features/reservations/services/reservationsService.ts~ |