-
Notifications
You must be signed in to change notification settings - Fork 4
refactor(game-session-schedule): add support for cascade deletion #600
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
base: master
Are you sure you want to change the base?
Changes from 31 commits
4ab183b
e1c546c
9cca008
4901e9b
3b41900
afeb253
7669707
313c08f
8df6a74
adac403
8c8ae03
355406d
5de1088
69668ab
c85c560
715b99f
703d652
9fcae39
40c44fa
9f551d9
60d3815
507ac15
542a9d8
e127832
36dbcbb
3a31302
6486519
2ac4dee
ad4b923
7f0c7d3
e7208af
ec0190c
d09f0f6
f60b2c4
1cfe0db
3820969
8aafd51
2dff7c5
7fae86c
29cac12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,13 @@ import { type NextRequest, NextResponse } from "next/server" | |
| import { NotFound } from "payload" | ||
| import { ZodError } from "zod" | ||
| import { Security } from "@/business-layer/middleware/Security" | ||
| import { payload } from "@/data-layer/adapters/Payload" | ||
| import { | ||
| commitCascadeTransaction, | ||
| createTransactionId, | ||
| rollbackCascadeTransaction, | ||
| } from "@/data-layer/adapters/Transaction" | ||
| import BookingDataService from "@/data-layer/services/BookingDataService" | ||
| import GameSessionDataService from "@/data-layer/services/GameSessionDataService" | ||
|
|
||
| class RouteWrapper { | ||
|
|
@@ -77,16 +84,52 @@ class RouteWrapper { | |
| /** | ||
| * DELETE method to delete a game session schedule. | ||
| * | ||
| * @param _req The request object | ||
| * @param req The request object | ||
| * @param params Route parameters containing the GameSessionSchedule ID | ||
| * @returns No content status code | ||
| */ | ||
| @Security("jwt", ["admin"]) | ||
| static async DELETE(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) { | ||
| static async DELETE(req: NextRequest, { params }: { params: Promise<{ id: string }> }) { | ||
| try { | ||
| const { id } = await params | ||
| const cascade = req.nextUrl.searchParams.get("cascade") === "true" | ||
| const gameSessionDataService = new GameSessionDataService() | ||
| await gameSessionDataService.deleteGameSessionSchedule(id) | ||
| const transactionID = cascade && (await createTransactionId()) | ||
| console.log(transactionID) | ||
|
|
||
| if (transactionID) { | ||
| try { | ||
| const gameSessionSchedule = await gameSessionDataService.getGameSessionScheduleById(id) | ||
|
|
||
| const { docs } = await payload.find({ | ||
| collection: "gameSession", | ||
| where: { | ||
| or: [ | ||
| { | ||
| gameSessionSchedule: { | ||
| equals: id, | ||
| }, | ||
| }, | ||
| { | ||
| gameSessionSchedule: { | ||
| equals: gameSessionSchedule, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }) | ||
| const gameSession = docs[0] | ||
|
||
| await gameSessionDataService.deleteGameSessionSchedule(id) | ||
| await gameSessionDataService.deleteGameSession(gameSession.id) | ||
| await BookingDataService.deleteRelatedBookingsForSession(gameSession.id, transactionID) | ||
| await commitCascadeTransaction(transactionID) | ||
| } catch { | ||
| await rollbackCascadeTransaction(transactionID) | ||
| } | ||
| } else { | ||
| await gameSessionDataService.deleteGameSessionSchedule(id) | ||
| } | ||
|
|
||
| return new NextResponse(null, { status: StatusCodes.NO_CONTENT }) | ||
| } catch (error) { | ||
| if (error instanceof NotFound) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -229,6 +229,42 @@ export default class BookingDataService { | |
| return await payload.find({ collection: "booking", limit, page }) | ||
| } | ||
|
|
||
| /** | ||
| * Deletes all bookings related to a game session. | ||
| * @param sessionId the ID of the game session whose bookings are to be deleted | ||
| * @param transactionID an optional transaction ID for the request, useful for tracing | ||
| * @private | ||
| */ | ||
|
||
| public static async deleteRelatedBookingsForSession( | ||
| sessionId: string, | ||
| transactionID?: string | number, | ||
| ): Promise<Booking[]> { | ||
| const relatedBookings = ( | ||
| await payload.find({ | ||
| collection: "booking", | ||
| where: { | ||
| gameSession: { | ||
| equals: sessionId, | ||
| }, | ||
| }, | ||
| pagination: false, | ||
| }) | ||
| ).docs | ||
| const relatedBookingIds = relatedBookings.map((booking) => booking.id) | ||
|
|
||
| const bulkDeletionResult = await payload.delete({ | ||
| collection: "booking", | ||
| where: { | ||
| id: { in: relatedBookingIds }, | ||
| }, | ||
| req: { | ||
| transactionID, | ||
| }, | ||
| }) | ||
|
||
|
|
||
| return bulkDeletionResult.docs | ||
| } | ||
|
||
|
|
||
| /** | ||
| * Updates a {@link Booking} by ID. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,11 @@ export const gameSessionCreateMock: CreateGameSessionData = { | |
| casualCapacity: 8, | ||
| } | ||
|
|
||
| export const gameSessionWithScheduleCreateMock: CreateGameSessionData = { | ||
| ...gameSessionCreateMock, | ||
| gameSessionSchedule: gameSessionScheduleMock, | ||
| } | ||
|
|
||
|
||
| export const oneOffGameSessionCreateMock: CreateGameSessionData = { | ||
| semester: semesterMock, | ||
| startTime: new Date().toISOString(), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we've shifted to use
deleteRelatedDocs