Skip to content

Commit

Permalink
Rewrite SSE endpoint to return map of reservations per event ID
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jeffplays2005 committed Sep 30, 2024
1 parent 1c08f1e commit c3aea5f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
4 changes: 2 additions & 2 deletions server/src/data-layer/services/EventService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
10 changes: 5 additions & 5 deletions server/src/data-layer/services/EventService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
public async getActiveReservationsCount(): Promise<Record<string, number>> {
const currentEvents = await this.getActiveEvents()
let total = 0
const output: Record<string, number> = {}
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
}

/**
Expand Down
15 changes: 6 additions & 9 deletions server/src/service-layer/controllers/EventController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, number> =
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<string, number> =
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
Expand Down

0 comments on commit c3aea5f

Please sign in to comment.