-
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 37 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,12 @@ import { type NextRequest, NextResponse } from "next/server" | |
| import { NotFound } from "payload" | ||
| import { ZodError } from "zod" | ||
| import { Security } from "@/business-layer/middleware/Security" | ||
| 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 +83,32 @@ 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("delateRelatedDocs") === "true" | ||
| const gameSessionDataService = new GameSessionDataService() | ||
| await gameSessionDataService.deleteGameSessionSchedule(id) | ||
| const bookingDataService = new BookingDataService() | ||
| const transactionID = cascade && (await createTransactionId()) | ||
|
|
||
| if (transactionID) { | ||
| try { | ||
| await gameSessionDataService.deleteGameSessionSchedule(id) | ||
| await gameSessionDataService.deleteAllGameSessionsByScheduleId(id) | ||
| await bookingDataService.deleteRelatedBookingsForSchedule(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 |
|---|---|---|
|
|
@@ -286,7 +286,7 @@ export default class BookingDataService { | |
| * | ||
| * @param userId The ID of the user to bulk delete bookings for | ||
| * @param transactionId an optional transaction ID for the request, useful for tracing | ||
| * @returns the deleted {@link Booking} documents if it exists, otherwise returns an empty array | ||
|
|
||
| */ | ||
| public async deleteBookingsByUserId( | ||
| userId: string, | ||
|
|
@@ -304,4 +304,27 @@ export default class BookingDataService { | |
| }) | ||
| ).docs | ||
| } | ||
|
|
||
| /** | ||
| * Deletes all bookings related to a game session schedule. | ||
| * | ||
| * @param scheduleId the ID of the game session schedule whose bookings are to be deleted | ||
| * @param transactionID an optional transaction ID for the request, useful for tracing | ||
| */ | ||
| public async deleteRelatedBookingsForSchedule( | ||
| scheduleId: string, | ||
|
Comment on lines
+313
to
+315
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for consistency, stick for
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also should be |
||
| transactionID?: string | number, | ||
| ): Promise<void> { | ||
| await payload.delete({ | ||
| collection: "booking", | ||
| where: { | ||
| "gameSession.gameSessionSchedule": { | ||
| equals: scheduleId, | ||
| }, | ||
| }, | ||
| req: { | ||
| transactionID, | ||
| }, | ||
| }) | ||
| } | ||
| } | ||
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