summaryrefslogtreecommitdiffstats
path: root/src/features/reservations/services/reservationsService.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/reservations/services/reservationsService.ts')
-rw-r--r--src/features/reservations/services/reservationsService.ts63
1 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()));
+ },
+};