From c3aea5f927a566b613931a03f63f0624dc33c504 Mon Sep 17 00:00:00 2001 From: jeffplays2005 Date: Tue, 1 Oct 2024 12:17:35 +1300 Subject: [PATCH] Rewrite SSE endpoint to return map of reservations per event ID I've changed the method in the controller to use the modified method. Also changed EventService to return a Record instead of just a number. Updated tests --- .../src/data-layer/services/EventService.test.ts | 4 ++-- server/src/data-layer/services/EventService.ts | 10 +++++----- .../service-layer/controllers/EventController.ts | 15 ++++++--------- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/server/src/data-layer/services/EventService.test.ts b/server/src/data-layer/services/EventService.test.ts index 7b7129b6..9f62e703 100644 --- a/server/src/data-layer/services/EventService.test.ts +++ b/server/src/data-layer/services/EventService.test.ts @@ -246,8 +246,8 @@ describe("EventService integration tests", () => { await eventService.addReservation(newEvent.id, reservation1) await eventService.addReservation(newEvent.id, reservation2) - const count = await eventService.getActiveReservationsCount() - expect(count).toBe(2) + const eventCounts = await eventService.getActiveReservationsCount() + expect(eventCounts).toStrictEqual({ [newEvent.id]: 2 }) }) it("Should get all event reservations", async () => { diff --git a/server/src/data-layer/services/EventService.ts b/server/src/data-layer/services/EventService.ts index 98109358..71c28a7e 100644 --- a/server/src/data-layer/services/EventService.ts +++ b/server/src/data-layer/services/EventService.ts @@ -116,18 +116,18 @@ class EventService { /** * Used for the SSE feature to display the total number of active event reservations. - * @returns the total number of active event reservations + * @returns a record of all the event ids and their event count */ - public async getActiveReservationsCount(): Promise { + public async getActiveReservationsCount(): Promise> { const currentEvents = await this.getActiveEvents() - let total = 0 + const output: Record = {} await Promise.all( currentEvents.map(async (event) => { const eventReservations = await this.getAllReservations(event.id) - total += eventReservations.length + output[`${event.id}`] = eventReservations.length }) ) - return total + return output } /** diff --git a/server/src/service-layer/controllers/EventController.ts b/server/src/service-layer/controllers/EventController.ts index c927c6b1..a78133a6 100644 --- a/server/src/service-layer/controllers/EventController.ts +++ b/server/src/service-layer/controllers/EventController.ts @@ -119,19 +119,16 @@ export class EventController extends Controller { req.res.flushHeaders() const eventService = new EventService() - const signupCount = await eventService.getActiveReservationsCount() // Fetch the current signup count - req.res.write( - `data: ${JSON.stringify({ reservation_count: signupCount })}\n\n` - ) + const signupRecord: Record = + await eventService.getActiveReservationsCount() // Fetch the current signup count + req.res.write(`data: ${JSON.stringify(signupRecord)}\n\n`) - // Create something that updates every 5 seconds const interValID = setInterval(async () => { - const signupCount = await eventService.getActiveReservationsCount() // Fetch the current signup count + const signupRecord: Record = + await eventService.getActiveReservationsCount() // NOTE: We use double new line because SSE requires this to indicate we're ready for the next event // We also need the data: to indicate data payload - req.res.write( - `data: ${JSON.stringify({ reservation_count: signupCount })}\n\n` - ) // res.write() instead of res.send() + req.res.write(`data: ${JSON.stringify(signupRecord)}\n\n`) // res.write() instead of res.send() }, 5000) // If the connection drops, stop sending events