From 1a2381d563cb3a3a1d1d6ac841b24705aa1e69e6 Mon Sep 17 00:00:00 2001 From: AzizP786 <60585478+AzizPatel786@users.noreply.github.com> Date: Thu, 17 Oct 2024 05:55:22 +1300 Subject: [PATCH] 796 backend events endpoint only include forms url at given time (#799) --- .../business-layer/utils/EventConstants.ts | 4 ++ .../middleware/tests/EventController.test.ts | 42 +++++++++++++++++-- .../controllers/EventController.ts | 15 +++++++ 3 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 server/src/business-layer/utils/EventConstants.ts diff --git a/server/src/business-layer/utils/EventConstants.ts b/server/src/business-layer/utils/EventConstants.ts new file mode 100644 index 000000000..158d34450 --- /dev/null +++ b/server/src/business-layer/utils/EventConstants.ts @@ -0,0 +1,4 @@ +/** + * A constant that defines the duration of one minute in milliseconds. + */ +export const ONE_MINUTE_IN_MS = 60000 diff --git a/server/src/middleware/tests/EventController.test.ts b/server/src/middleware/tests/EventController.test.ts index 663c3a8e3..8510a2c52 100644 --- a/server/src/middleware/tests/EventController.test.ts +++ b/server/src/middleware/tests/EventController.test.ts @@ -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 @@ -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", () => { @@ -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", () => { diff --git a/server/src/service-layer/controllers/EventController.ts b/server/src/service-layer/controllers/EventController.ts index cd5ea777c..696174770 100644 --- a/server/src/service-layer/controllers/EventController.ts +++ b/server/src/service-layer/controllers/EventController.ts @@ -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 { @@ -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 {