diff --git a/server/src/db/repositories/MaintenanceRepository.ts b/server/src/db/repositories/MaintenanceRepository.ts index e3615db17..311089770 100644 --- a/server/src/db/repositories/MaintenanceRepository.ts +++ b/server/src/db/repositories/MaintenanceRepository.ts @@ -54,21 +54,4 @@ export class MaintenanceRepository { .execute(`DELETE FROM ${table} WHERE user_id = ?`, [userId], 'run', this.em.getTransactionContext()); } } - - /** - * Before a user is deleted: the trip tags they created pass to each trip's - * owner, so a tag the whole trip uses keeps its place_tags. The user's - * personal tags are left to the delete. - */ - async handTripTagsToTripOwners(userId: number): Promise { - this.validateRequestContext(); - await this.em - .getConnection() - .execute( - 'UPDATE tags SET user_id = (SELECT trips.user_id FROM trips WHERE trips.id = tags.trip_id) WHERE user_id = ? AND trip_id IS NOT NULL', - [userId], - 'run', - this.em.getTransactionContext(), - ); - } } diff --git a/server/src/db/repositories/TripMembers.repository.ts b/server/src/db/repositories/TripMembers.repository.ts index 459133f44..87e4eeb7f 100644 --- a/server/src/db/repositories/TripMembers.repository.ts +++ b/server/src/db/repositories/TripMembers.repository.ts @@ -366,4 +366,20 @@ export class TripMembersRepository extends TrekRepository { async clearInvitedBy(userId: number): Promise { await this.nativeUpdate({ invitedByRef: userId }, { invitedByRef: null }); } + + /** + * Before a member's account is deleted: the trip tags they created pass to + * each trip's owner, so a tag the whole trip uses keeps its place_tags. The + * user's personal tags are left to the delete. + */ + async handTripTagsToTripOwners(userId: number): Promise { + await this.kysely>() + .updateTable('tags') + .set((eb) => ({ + user_id: eb.selectFrom('trips').select('trips.user_id').whereRef('trips.id', '=', 'tags.trip_id'), + })) + .where('user_id', '=', userId) + .where('trip_id', 'is not', null) + .execute(); + } } diff --git a/server/src/nest/auth/user-cleanup.service.ts b/server/src/nest/auth/user-cleanup.service.ts index e22b0a79b..f373010fe 100644 --- a/server/src/nest/auth/user-cleanup.service.ts +++ b/server/src/nest/auth/user-cleanup.service.ts @@ -173,7 +173,7 @@ export class UserCleanupService { await this.tripMembersRepo.clearInvitedBy(userId); // UC4 — converted (Plan 4 Task 1) await this.budgetItemsRepo.clearPaidByUser(userId); // UC5 — Plan 3e Task 2, converted. await this.settlementsRepo.clearCreatedBy(userId); - await this.maintenance.handTripTagsToTripOwners(userId); + await this.tripMembersRepo.handTripTagsToTripOwners(userId); await this.budget.removeUserFromBudgetItems(userId); await this.shareTokensRepo.deleteByCreator(userId); // UC6 — converted (Plan 3h Task 6) await this.journeyShareTokensRepo.deleteByCreatedBy(userId); // UC7 — converted (Plan 3g Task 4)