refactor(auth): hand the trip tags over through the trip members repository

Kept out of MaintenanceRepository, whose raw SQL strings are pinned.
This commit is contained in:
Maurice
2026-11-23 16:44:45 +01:00
parent 3cef8eae5e
commit 20c86f3a44
3 changed files with 17 additions and 18 deletions
@@ -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<void> {
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(),
);
}
}
@@ -366,4 +366,20 @@ export class TripMembersRepository extends TrekRepository<TripMembers> {
async clearInvitedBy(userId: number): Promise<void> {
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<void> {
await this.kysely<Pick<DB, 'tags' | 'trips'>>()
.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();
}
}
+1 -1
View File
@@ -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)