Skip to content

Conversation

@6xtvo
Copy link
Member

@6xtvo 6xtvo commented Jul 20, 2025

Description

Delete GameSession and Booking documents upon deletion of GameSessionSchedule

Fixes #545

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • Manual testing (requires screenshots or videos)
  • Integration tests written (requires checks to pass)

Checklist before requesting a review

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added thorough tests that prove my fix is effective and that my feature works
  • I've requested a review from another user

@github-actions
Copy link
Contributor

github-actions bot commented Jul 20, 2025

Preview Deployment Status

✅ Storybook Preview: https://691d21e5.uabc-storybook.pages.dev

🚫 Frontend Preview: Failed

@github-actions
Copy link
Contributor

github-actions bot commented Jul 25, 2025

Coverage Report

Status Category Percentage Covered / Total
🟢 Lines 78.87% (🎯 70%)
⬇️ -5.40%
1713 / 2172
🟢 Statements 78.85% (🎯 70%)
⬇️ -5.28%
1767 / 2241
🔴 Functions 75.12% (🎯 80%)
⬇️ -4.41%
468 / 623
🟢 Branches 75.03% (🎯 60%)
⬇️ -3.12%
1136 / 1514
File CoverageNo changed files found.
Generated in workflow #3818 for commit 29cac12 by the Vitest Coverage Report Action

@jeffplays2005 jeffplays2005 self-requested a review August 23, 2025 09:00
@6xtvo 6xtvo added this to the MVP milestone Sep 24, 2025
Copy link
Member

@jeffplays2005 jeffplays2005 left a 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 👍

Comment on lines 14 to 19

export const gameSessionWithScheduleCreateMock: CreateGameSessionData = {
...gameSessionCreateMock,
gameSessionSchedule: gameSessionScheduleMock,
}

Copy link
Member

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

Copy link
Member Author

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

Comment on lines 235 to 237
* @param transactionID an optional transaction ID for the request, useful for tracing
* @private
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private?

Copy link
Member

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

Comment on lines 264 to 266

return bulkDeletionResult.docs
}
Copy link
Member

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

Comment on lines 242 to 263
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,
},
})
Copy link
Member

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

Copy link
Member Author

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

Comment on lines 94 to 96
const { id } = await params
const cascade = req.nextUrl.searchParams.get("cascade") === "true"
const gameSessionDataService = new GameSessionDataService()
Copy link
Member

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

Comment on lines 101 to 121
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]
Copy link
Member

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

@jeffplays2005 jeffplays2005 changed the title refactor(game-sessions): cascade deletion refactor(game-session-schedule): add support for cascade deletions Oct 16, 2025
@jeffplays2005 jeffplays2005 changed the title refactor(game-session-schedule): add support for cascade deletions refactor(game-session-schedule): add support for cascade deletion Oct 16, 2025
@jeffplays2005
Copy link
Member

@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!

Copy link
Member

@jeffplays2005 jeffplays2005 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there!

Comment on lines 11 to 12
import type { PaginatedDocs, Sort, Where } from "payload"
import { NotFound, type PaginatedDocs, type Sort, type Where } from "payload"
import { payload } from "@/data-layer/adapters/Payload"
Copy link
Member

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?

Copy link
Member Author

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

Comment on lines 313 to 315
*/
public async deleteRelatedBookingsForSchedule(
scheduleId: string,
Copy link
Member

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...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also should be ...ScheduleId

Comment on lines 326 to 328
*/
public async deleteAllGameSessionsByScheduleId(scheduleId: string): Promise<GameSession[]> {
return (
Copy link
Member

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?

Copy link
Member

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)

Copy link
Member Author

@6xtvo 6xtvo Oct 20, 2025

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

Copy link
Member

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).

Comment on lines 101 to 103
await gameSessionDataService.deleteGameSessionSchedule(id)
await gameSessionDataService.deleteAllGameSessionsByScheduleId(id)
await bookingDataService.deleteRelatedBookingsForSchedule(id, transactionID)
Copy link
Member

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!

@6xtvo
Copy link
Member Author

6xtvo commented Oct 20, 2025

@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!

Sorry this PR was created when the issue had the wrong name, I forgot to update it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BACKEND] Cascade GameSessionSchedule deletion to GameSession and Booking docs

3 participants