Skip to content

Commit

Permalink
Rewrite SSE endpoint to return map of reservations per event ID (#787)
Browse files Browse the repository at this point in the history
* 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

* Modify POST /admin/events to parse dates
  • Loading branch information
jeffplays2005 authored Oct 2, 2024
1 parent d6a093f commit f6a3545
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 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
23 changes: 21 additions & 2 deletions server/src/service-layer/controllers/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,27 @@ export class AdminController extends Controller {
public async createNewEvent(@Body() body: CreateEventBody) {
try {
const eventService = new EventService()
await eventService.createEvent(body.data)

await eventService.createEvent({
...body.data,
start_date: new Timestamp(
body.data.start_date.seconds,
body.data.start_date.nanoseconds
),
end_date: new Timestamp(
body.data.end_date.seconds,
body.data.end_date.nanoseconds
),
physical_start_date: new Timestamp(
body.data.physical_start_date.seconds,
body.data.physical_start_date.nanoseconds
),
...(body.data.physical_end_date && {
physical_end_date: new Timestamp(
body.data.physical_end_date.seconds,
body.data.physical_end_date.nanoseconds
)
})
})
this.setStatus(201)
} catch {
this.setStatus(500)
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 f6a3545

Please sign in to comment.