Skip to content

Commit

Permalink
Scope get event id endpoint to admins only
Browse files Browse the repository at this point in the history
* Updated endpoint

* Updated interfaces

* Updated tests

* Generate swagger
  • Loading branch information
jeffplays2005 committed Nov 2, 2024
1 parent 13817ab commit 0512b2a
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 132 deletions.
81 changes: 41 additions & 40 deletions server/src/middleware/__generated__/routes.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 49 additions & 43 deletions server/src/middleware/__generated__/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions server/src/middleware/tests/AdminController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,4 +850,29 @@ describe("AdminController endpoint tests", () => {
).toEqual(newDate)
})
})
describe("GET /events/:id", () => {
const event1: Event = {
title: "UASC New event",
physical_start_date: dateToFirestoreTimeStamp(new Date()),
location: "UASC",
sign_up_start_date: dateToFirestoreTimeStamp(new Date()),
sign_up_end_date: dateToFirestoreTimeStamp(new Date())
}
const eventService = new EventService()

it("should return the event details for a valid event ID", async () => {
const { id: id1 } = await eventService.createEvent(event1)
const res = await request.get(`/events/${id1}`).send()
expect(res.status).toEqual(200)
expect(res.body.data).toBeDefined()
expect(res.body.data.title).toEqual("UASC New event")
expect(res.body.data.location).toEqual("UASC")
})

it("should return 404 if the event does not exist", async () => {
const res = await request.get("/events/random-event").send()
expect(res.status).toEqual(404)
expect(res.body.error).toEqual("Event not found.")
})
})
})
17 changes: 0 additions & 17 deletions server/src/middleware/tests/EventController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,4 @@ describe("EventController endpoint tests", () => {
)
})
})

describe("GET /events/:id", () => {
it("should return the event details for a valid event ID", async () => {
const { id: id1 } = await eventService.createEvent(event1)
const res = await request.get(`/events/${id1}`).send()
expect(res.status).toEqual(200)
expect(res.body.data).toBeDefined()
expect(res.body.data.title).toEqual("UASC New event")
expect(res.body.data.location).toEqual("UASC")
})

it("should return 404 if the event does not exist", async () => {
const res = await request.get("/events/random-event").send()
expect(res.status).toEqual(404)
expect(res.body.error).toEqual("Event not found.")
})
})
})
26 changes: 25 additions & 1 deletion server/src/service-layer/controllers/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ import BookingUtils, {
CHECK_OUT_TIME
} from "business-layer/utils/BookingUtils"
import BookingHistoryService from "data-layer/services/BookingHistoryService"
import { FetchLatestBookingHistoryEventResponse } from "service-layer/response-models/AdminResponse"
import {
FetchLatestBookingHistoryEventResponse,
GetEventResponse
} from "service-layer/response-models/AdminResponse"
import { CreateEventBody } from "service-layer/request-models/EventRequests"
import EventService from "data-layer/services/EventService"

Expand Down Expand Up @@ -788,4 +791,25 @@ export class AdminController extends Controller {
}
this.setStatus(200)
}

@Get("{id}")
@SuccessResponse("200", "Successfully fetched the event")
public async getEventById(@Path() id: string): Promise<GetEventResponse> {
try {
const eventService = new EventService()
const event = await eventService.getEventById(id)

if (!event) {
this.setStatus(404)
return { error: "Event not found." }
}

return { data: event }
} catch (e) {
this.setStatus(500)
return {
error: "Something went wrong when fetching the event, please try again"
}
}
}
}
28 changes: 2 additions & 26 deletions server/src/service-layer/controllers/EventController.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import EventService from "data-layer/services/EventService"
import {
GetAllEventsResponse,
GetEventResponse
} from "service-layer/response-models/EventResponse"
import { Controller, Get, Path, Query, Route, SuccessResponse } from "tsoa"
import { GetAllEventsResponse } from "service-layer/response-models/EventResponse"
import { Controller, Get, Query, Route, SuccessResponse } from "tsoa"
import { ONE_MINUTE_IN_MS } from "../../business-layer/utils/EventConstants"
import { Timestamp } from "firebase-admin/firestore"

Expand Down Expand Up @@ -48,25 +45,4 @@ export class EventController extends Controller {
}
}
}

@Get("{id}")
@SuccessResponse("200", "Successfully fetched the event")
public async getEventById(@Path() id: string): Promise<GetEventResponse> {
try {
const eventService = new EventService()
const event = await eventService.getEventById(id)

if (!event) {
this.setStatus(404)
return { error: "Event not found." }
}

return { data: event }
} catch (e) {
this.setStatus(500)
return {
error: "Something went wrong when fetching the event, please try again"
}
}
}
}
6 changes: 5 additions & 1 deletion server/src/service-layer/response-models/AdminResponse.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { BookingHistoryEvent } from "data-layer/models/firebase"
import { BookingHistoryEvent, Event } from "data-layer/models/firebase"
import { CommonResponse, CursorPaginatedResponse } from "./CommonResponse"

export interface FetchLatestBookingHistoryEventResponse
extends CursorPaginatedResponse,
CommonResponse {
historyEvents?: BookingHistoryEvent[]
}

export interface GetEventResponse extends CommonResponse {
data?: Event
}
4 changes: 0 additions & 4 deletions server/src/service-layer/response-models/EventResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,3 @@ export interface GetAllEventsResponse
CursorPaginatedResponse {
data?: DocumentDataWithUid<Event>[]
}

export interface GetEventResponse extends CommonResponse {
data?: Event
}

0 comments on commit 0512b2a

Please sign in to comment.