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

796 backend events endpoint only include forms url at given time #799

4 changes: 4 additions & 0 deletions server/src/business-layer/utils/EventConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* A constant that defines the duration of one minute in milliseconds.
*/
export const ONE_MINUTE_IN_MS = 60000
42 changes: 39 additions & 3 deletions server/src/middleware/tests/EventController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Event } from "../../data-layer/models/firebase"
import { Timestamp } from "firebase-admin/firestore"

const earlierStartDate = Timestamp.fromDate(new Date(2023, 1, 1))
const startDate = Timestamp.fromDate(new Date(2024, 1, 1))
const startDate = Timestamp.fromDate(new Date(Date.now() + 60000)) // plus one min
const laterStartDate = Timestamp.fromDate(new Date(Date.now() + 120000)) // plus two min
const endDate = Timestamp.fromDate(new Date(2024, 1, 2))
/**
* Event with the earlier start date
Expand All @@ -18,14 +19,24 @@ const event1: Event = {
}

/**
* Event witht the later start date
* Event with the later start date
*/
const event2: Event = {
title: "Straight Zhao",
location: "UASC",
physical_start_date: startDate,
sign_up_start_date: startDate,
sign_up_end_date: endDate
sign_up_end_date: endDate,
google_forms_link: "https://random.com/event2"
}

const event3: Event = {
title: "Another Event",
location: "Krispy Kreme",
physical_start_date: laterStartDate,
sign_up_start_date: earlierStartDate,
sign_up_end_date: earlierStartDate,
google_forms_link: "https://random.com/event3"
}

describe("EventController endpoint tests", () => {
Expand Down Expand Up @@ -56,6 +67,31 @@ describe("EventController endpoint tests", () => {
expect(res.body.data).not.toContainEqual({ ...event2, id: id2 })
expect(res.status).toEqual(200)
})

it("should include google_forms_link if event is within 1 minute", async () => {
const { id: id2 } = await eventService.createEvent(event2)

const res = await request.get("/events").send()

expect(res.body.data).toContainEqual(
expect.objectContaining({
id: id2,
google_forms_link: "https://random.com/event2"
})
)
})

it("should not include google_forms_link if event is not within 1 minute", async () => {
await eventService.createEvent(event3)

const res = await request.get("/events").send()

expect(res.body.data).toContainEqual(
expect.not.objectContaining({
google_forms_link: expect.any(String)
})
)
})
})

describe("GET /events/:id", () => {
Expand Down
15 changes: 15 additions & 0 deletions server/src/service-layer/controllers/EventController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
GetEventResponse
} from "service-layer/response-models/EventResponse"
import { Controller, Get, Path, Query, Route, SuccessResponse } from "tsoa"
import { ONE_MINUTE_IN_MS } from "../../business-layer/utils/EventConstants"
import { Timestamp } from "firebase-admin/firestore"

@Route("events")
export class EventController extends Controller {
Expand All @@ -26,6 +28,19 @@ export class EventController extends Controller {
}

const res = await eventService.getAllEvents(limit, snapshot)
const currentTime = Timestamp.now()

res.events.forEach((event) => {
const eventStartTime = event.physical_start_date
const timeDifference =
eventStartTime.toMillis() - currentTime.toMillis()

// 1 minute (60000 milliseconds)
if (timeDifference > ONE_MINUTE_IN_MS) {
delete event.google_forms_link
}
})

return { nextCursor: res.nextCursor, data: res.events }
} catch (e) {
return {
Expand Down
Loading