Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite SSE endpoint to return map of reservations per event ID #787

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading