-
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?
Conversation
Preview Deployment Status✅ Storybook Preview: https://691d21e5.uabc-storybook.pages.dev 🚫 Frontend Preview: Failed |
Coverage Report
File CoverageNo changed files found. |
jeffplays2005
left a comment
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.
I haven't reviewed your tests yet as the foundation code logic needs some changes. Please take a look at the comments I've left behind! Thanks 👍
|
|
||
| export const gameSessionWithScheduleCreateMock: CreateGameSessionData = { | ||
| ...gameSessionCreateMock, | ||
| gameSessionSchedule: gameSessionScheduleMock, | ||
| } | ||
|
|
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.
this mock isn't getting used
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.
I didn't create the mock it was already there from a merge
| * @param transactionID an optional transaction ID for the request, useful for tracing | ||
| * @private | ||
| */ |
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.
private?
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.
I also understand that instantiations of data services without needing a constructor is poor practice but should be addressed outside of this ticket
please use public async instead of public static async
|
|
||
| return bulkDeletionResult.docs | ||
| } |
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.
no need to return docs
| 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, | ||
| }, | ||
| }) |
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.
I'd recommend looking into advanced queries again, you could just delete all bookings where the gameSession equals the input id instead of fetching all bookings with target game session, then deleting it again.
tl;dr you're doing double the db operations when you could combine everything together
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.
Oops, don't know what I was thinking there
| const { id } = await params | ||
| const cascade = req.nextUrl.searchParams.get("cascade") === "true" | ||
| const gameSessionDataService = new GameSessionDataService() |
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
| 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] |
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.
use data service methods instead of directly using the db adapter here
this isn't too accurate as there could be multiple game sessions tied to a single schedule object. you should be creating something similar to the deleteRelatedBookingsForSession method but to delete game session's based on a game session schedule
|
@6xtvo Just a reminder to please make sure your PR title and description match the original issue, I've modified them, please take a look to ensure your future work is more precise! |
jeffplays2005
left a comment
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.
Almost there!
| import type { PaginatedDocs, Sort, Where } from "payload" | ||
| import { NotFound, type PaginatedDocs, type Sort, type Where } from "payload" | ||
| import { payload } from "@/data-layer/adapters/Payload" |
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.
question: why was NotFound added?
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.
Accidentally imported it when adding NotFound to JSDoc
| */ | ||
| public async deleteRelatedBookingsForSchedule( | ||
| scheduleId: string, |
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.
for consistency, stick for ...By... rather than ...For...
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.
also should be ...ScheduleId
| */ | ||
| public async deleteAllGameSessionsByScheduleId(scheduleId: string): Promise<GameSession[]> { | ||
| return ( |
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.
any reason why this doesn't support transactions?
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.
also I think it isn't relevant to return the deleted docs, it should be Promise instead (please update tests)
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.
I copied the method from another method where it returned docs for testing
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.
There's no need to return docs for testing here as we can use getById to see if it returns a NotFound error (it's rather inefficient to be parsing the data when it isn't needed).
| await gameSessionDataService.deleteGameSessionSchedule(id) | ||
| await gameSessionDataService.deleteAllGameSessionsByScheduleId(id) | ||
| await bookingDataService.deleteRelatedBookingsForSchedule(id, 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.
all three of these should be using transactions, please modify the first two!
Sorry this PR was created when the issue had the wrong name, I forgot to update it |
Description
Delete
GameSessionandBookingdocuments upon deletion ofGameSessionScheduleFixes #545
Type of change
How Has This Been Tested?
Checklist before requesting a review