mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-25 22:27:32 +00:00
fix(tags): keep a trip's tags when the member who created them deletes their account
The trip tags a deleted user created now pass to each trip's owner before the user row goes, so the tag and its place assignments no longer cascade away for everyone else on the trip.
This commit is contained in:
@@ -49,6 +49,8 @@ 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`
|
||||
@@ -239,6 +241,22 @@ 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.
|
||||
|
||||
@@ -31,6 +31,7 @@ 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';
|
||||
@@ -99,6 +100,8 @@ 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
|
||||
|
||||
@@ -29,6 +29,7 @@ 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';
|
||||
|
||||
@@ -75,6 +76,7 @@ export class UserCleanupService {
|
||||
@InjectRepository(PluginUserErasureQueue)
|
||||
private readonly pluginUserErasureQueueRepo: PluginUserErasureQueueRepository,
|
||||
private readonly release: StorageReleaseService,
|
||||
private readonly tripTags: TripTagsService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -173,6 +175,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.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)
|
||||
|
||||
@@ -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).
|
||||
exports: [TagsService],
|
||||
// For in-container consumers (TagsRpc, and the account deletion for TripTagsService).
|
||||
exports: [TagsService, TripTagsService],
|
||||
})
|
||||
export class TagsModule {}
|
||||
|
||||
@@ -47,6 +47,11 @@ 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);
|
||||
|
||||
Reference in New Issue
Block a user