From 827dc78fbf867c1e0b30e97bd77d37df3e490010 Mon Sep 17 00:00:00 2001 From: bcho892 Date: Tue, 26 Mar 2024 20:56:12 +1300 Subject: [PATCH] expose method to create booking --- .../adapters/FirestoreCollections.ts | 5 ++-- .../services/BookingDataService.test.ts | 24 +++++++++++++++++++ .../data-layer/services/BookingDataService.ts | 12 ++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 server/src/data-layer/services/BookingDataService.test.ts create mode 100644 server/src/data-layer/services/BookingDataService.ts diff --git a/server/src/data-layer/adapters/FirestoreCollections.ts b/server/src/data-layer/adapters/FirestoreCollections.ts index d6be2a161..5ff835250 100644 --- a/server/src/data-layer/adapters/FirestoreCollections.ts +++ b/server/src/data-layer/adapters/FirestoreCollections.ts @@ -1,6 +1,6 @@ // credit https://plainenglish.io/blog/using-firestore-with-typescript-in-the-v9-sdk-cf36851bb099 import "dotenv/config" -import { UserAdditionalInfo } from "data-layer/models/firebase" +import { Booking, UserAdditionalInfo } from "data-layer/models/firebase" import { admin } from "business-layer/security/Firebase" const converter = () => ({ @@ -23,7 +23,8 @@ const firestore = Object.assign( ) const db = { - users: firestore.collection("users") + users: firestore.collection("users"), + bookings: firestore.collection("bookings") } as const export default db diff --git a/server/src/data-layer/services/BookingDataService.test.ts b/server/src/data-layer/services/BookingDataService.test.ts new file mode 100644 index 000000000..3cd2ba687 --- /dev/null +++ b/server/src/data-layer/services/BookingDataService.test.ts @@ -0,0 +1,24 @@ +import { cleanFirestore } from "test-config/TestUtils" +import BookingDataService from "./BookingDataService" +import FirestoreCollections from "data-layer/adapters/FirestoreCollections" + +describe("BookingDataService", () => { + afterEach(async () => { + await cleanFirestore() + }) + + it("Should create a booking in firestore", async () => { + const { id } = await new BookingDataService().createBooking({ + user_id: "asds", + booking_slot_id: "asdds" + }) + const doc = await FirestoreCollections.bookings.doc(id).get() + const data = doc.data() + + expect(data).not.toBe(undefined) + expect(data).toEqual({ + user_id: "asds", + booking_slot_id: "asdds" + }) + }) +}) diff --git a/server/src/data-layer/services/BookingDataService.ts b/server/src/data-layer/services/BookingDataService.ts new file mode 100644 index 000000000..08f7f76ae --- /dev/null +++ b/server/src/data-layer/services/BookingDataService.ts @@ -0,0 +1,12 @@ +import { Booking } from "data-layer/models/firebase" +import FirestoreCollections from "data-layer/adapters/FirestoreCollections" + +export default class BookingDataService { + // Create + public async createBooking(bookingData: Booking) { + return await FirestoreCollections.bookings.add(bookingData) + } + // Read + // Update + // Delete +}