Skip to content

Commit

Permalink
Create endpoint for admins to edit an event (#788)
Browse files Browse the repository at this point in the history
* Created new endpoint PATCH events/{id}

* Created tests
  • Loading branch information
jeffplays2005 authored Oct 1, 2024
1 parent 1c08f1e commit d6a093f
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 2 deletions.
65 changes: 65 additions & 0 deletions client/src/models/__generated__/schema.d.ts

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

37 changes: 37 additions & 0 deletions server/src/middleware/__generated__/routes.ts

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

82 changes: 82 additions & 0 deletions server/src/middleware/__generated__/swagger.json

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

14 changes: 13 additions & 1 deletion server/src/middleware/tests/AdminController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,7 @@ describe("AdminController endpoint tests", () => {
start_date: dateToFirestoreTimeStamp(new Date()),
end_date: dateToFirestoreTimeStamp(new Date())
}
const eventService = new EventService()

it("should let admins create an event", async () => {
const res = await request
Expand All @@ -827,7 +828,18 @@ describe("AdminController endpoint tests", () => {
expect(res.status).toEqual(201)

// There should not be more than 1, even if we request more
expect((await new EventService().getAllEvents(69)).events).toHaveLength(1)
expect((await eventService.getAllEvents(69)).events).toHaveLength(1)
})
it("should let admins edit an event", async () => {
const newEvent = await eventService.createEvent(event1)
const res = await request
.patch("/admin/events/" + newEvent.id)
.set("Authorization", `Bearer ${adminToken}`)
.send({ title: "Cool event!", location: "UoA" } as Partial<Event>)
expect(res.status).toEqual(200)
const fetchedEvent = await eventService.getEventById(newEvent.id)
expect(fetchedEvent.title).toEqual("Cool event!")
expect(fetchedEvent.location).toEqual("UoA")
})
})
})
24 changes: 23 additions & 1 deletion server/src/service-layer/controllers/AdminController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
firestoreTimestampToDate,
timestampsInRange
} from "data-layer/adapters/DateUtils"
import { UserAdditionalInfo } from "data-layer/models/firebase"
import { UserAdditionalInfo, Event } from "data-layer/models/firebase"
import BookingDataService from "data-layer/services/BookingDataService"
import BookingSlotService from "data-layer/services/BookingSlotsService"
import UserDataService from "data-layer/services/UserDataService"
Expand Down Expand Up @@ -710,4 +710,26 @@ export class AdminController extends Controller {
this.setStatus(500)
}
}

/**
* Endpoint for admints to edit an event.
*/
@SuccessResponse("200", "Successfully edited the event!")
@Patch("events/{id}")
public async editEvent(
@Path() id: string,
@Body() requestBody: Partial<Event>
) {
const eventService = new EventService()
const fetchedEvent = await eventService.getEventById(id)
if (!fetchedEvent) {
this.setStatus(404)
}
try {
eventService.updateEvent(id, requestBody)
} catch (e) {
this.setStatus(500)
}
this.setStatus(200)
}
}

0 comments on commit d6a093f

Please sign in to comment.