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.
-3
View File
@@ -31,7 +31,6 @@ import { SessionsModule } from '../sessions/sessions.module';
import { buildStorageUploadOptions } from '../storage/storage-upload.factory';
import { StorageModule } from '../storage/storage.module';
import { StorageService } from '../storage/storage.service';
import { TagsModule } from '../tags/tags.module';
import { TokensModule } from '../tokens/tokens.module';
import { TripMembershipModule } from '../trip-membership/trip-membership.module';
import { AuthPublicController } from './auth-public.controller';
@@ -100,8 +99,6 @@ import { MulterModule } from '@nestjs/platform-express';
AppConfigModule,
TokensModule,
BudgetModule,
// TripTagsService: the trip tags of a deleted user stay with the trip.
TagsModule,
// MaintenanceRepository: UserCleanupService's UC1 plugin-table erasure.
MaintenanceModule,
// AppSettings/Users: AuthService/UserProfileService each pass their own
+1 -3
View File
@@ -29,7 +29,6 @@ import { UnitOfWork } from '../database/unit-of-work';
import { pluginsDataRoot } from '../plugins/paths';
import { enqueueHookUserDataErasures } from '../plugins/user-erasure-enqueue';
import { StorageReleaseService } from '../storage/storage-release.service';
import { TripTagsService } from '../tags/trip-tags.service';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
@@ -76,7 +75,6 @@ export class UserCleanupService {
@InjectRepository(PluginUserErasureQueue)
private readonly pluginUserErasureQueueRepo: PluginUserErasureQueueRepository,
private readonly release: StorageReleaseService,
private readonly tripTags: TripTagsService,
) {}
/**
@@ -175,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.tripTags.handToTripOwners(userId);
await this.maintenance.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)
+2 -2
View File
@@ -41,7 +41,7 @@ import { Module } from '@nestjs/common';
// PROVIDERS only, and a missing entry here would leave tags.* answering
// PERMISSION_DENIED with no other symptom.
providers: [TagsService, TagWritesService, TripTagsService, TagsMcp, TripTagsMcp, TagsRpc],
// For in-container consumers (TagsRpc, and the account deletion for TripTagsService).
exports: [TagsService, TripTagsService],
// For in-container consumers (TagsRpc).
exports: [TagsService],
})
export class TagsModule {}
@@ -47,11 +47,6 @@ export class TripTagsService {
private readonly uow: UnitOfWork,
) {}
/** Before a user is deleted: the trip tags they created stay with each trip, under its owner. */
async handToTripOwners(userId: number): Promise<void> {
await this.tags.handTripTagsToTripOwners(userId);
}
/** The REST caller of a tag write: its event skips the socket that made the request. */
restWriter(tripId: string, user: User, socketId: string | undefined): TripWriter {
return restTripWriter(this.realtime, tripId, user, socketId);