refactor(auth): hand a deleted user's trip tags over through the maintenance repository

The account deletion no longer reaches into the tags domain, which closed an import cycle between the two.
This commit is contained in:
Maurice
2026-11-23 16:34:51 +01:00
parent 0fe536bbd2
commit 6e0c2b894d
18 changed files with 23 additions and 70 deletions
@@ -54,4 +54,21 @@ 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(),
);
}
}
@@ -49,8 +49,6 @@ type PlaceTagsKyselyDB = Pick<DB, 'place_tags'>;
/** `tags` alone, for the reads that name their columns. */
type TagsKyselyDB = Pick<DB, 'tags'>;
type TagsWithTripsKyselyDB = Pick<DB, 'tags' | 'trips'>;
/**
* `place_tags` joined to `places` for TP46 (`trips.service.ts::copy`'s
* place-tags read). The `trip_id` the caller passes stays `number | string`
@@ -241,22 +239,6 @@ export class TagsRepository extends TrekRepository<Tags> {
return tag ? (toRow(tag) as TagRow) : null;
}
/**
* Hands the trip tags a user created to each trip's owner, so deleting the
* user does not take a tag the whole trip uses (and its place_tags) with it.
* The user's personal tags are left alone.
*/
async handTripTagsToTripOwners(userId: number): Promise<void> {
await this.kysely<TagsWithTripsKyselyDB>()
.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();
}
// ---------------------------------------------------------------------------
// Plan 3c Task 4 (`PlacesService.create`/`applyUpdate`, PL5/PL12/PL13) —
// `place_tags` writes. `PlaceTagsKyselyDB` above documents why Kysely.