mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +00:00
fix(memories): scope album link cleanup to its owner
Confirm trip access and the scoped album link inside the transaction before handling related photos. Treat a missing link as a completed removal.
This commit is contained in:
@@ -146,7 +146,7 @@ export class TripPhotosRepository extends TrekRepository<TripPhotos> {
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** UM8 (`UnifiedMemoriesService.removeAlbumLink`'s pre-tx orphan-candidate list) — `SELECT photo_id FROM trip_photos WHERE trip_id = ? AND album_link_id = ?`. */
|
||||
/** UM8 (`UnifiedMemoriesService.removeAlbumLink`, inside its transaction). */
|
||||
async listPhotoIdsForAlbumLink(trip_id: number | string, album_link_id: number | string): Promise<number[]> {
|
||||
const rows = await this.kysely<TripPhotosKyselyDB>()
|
||||
.selectFrom('trip_photos')
|
||||
|
||||
@@ -304,25 +304,19 @@ export class UnifiedMemoriesService {
|
||||
}
|
||||
|
||||
async removeAlbumLink(tripId: string, linkId: string, userId: number): Promise<ServiceResult<true>> {
|
||||
const access = await this.trips.findAccessible(tripId, userId);
|
||||
if (!access) {
|
||||
return fail('Trip not found or access denied', 404);
|
||||
}
|
||||
// A link id toRowId refuses names no link, so there is nothing to remove,
|
||||
// as for an id no link has; bound raw, Postgres refused it with an error.
|
||||
const link = toRowId(linkId);
|
||||
if (link === null) return success(true);
|
||||
|
||||
try {
|
||||
const linkedPhotoIds = await this.tripPhotos.listPhotoIdsForAlbumLink(tripId, link);
|
||||
return await this.uow.transactional<ServiceResult<true>>(async () => {
|
||||
const trip = await this.trips.findAccessible(tripId, userId);
|
||||
if (!trip) return fail('Trip not found or access denied', 404);
|
||||
if (link === null || !(await this.tripAlbumLinks.findScoped(link, trip.id, userId))) return success(true);
|
||||
|
||||
await this.uow.transactional(async () => {
|
||||
await this.tripPhotos.deleteForAlbumLink(tripId, link);
|
||||
await this.tripAlbumLinks.deleteScoped(link, tripId, userId);
|
||||
const linkedPhotoIds = await this.tripPhotos.listPhotoIdsForAlbumLink(trip.id, link);
|
||||
await this.tripPhotos.deleteForAlbumLink(trip.id, link);
|
||||
await this.tripAlbumLinks.deleteScoped(link, trip.id, userId);
|
||||
for (const photo_id of linkedPhotoIds) await this.photos.deleteIfOrphan(photo_id);
|
||||
return success(true);
|
||||
});
|
||||
|
||||
return success(true);
|
||||
} catch (error) {
|
||||
return mapDbError(error, 'Failed to remove album link');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user