From 239d4f5a297a8356a869bb309a77abd756d9cd75 Mon Sep 17 00:00:00 2001 From: Pablo Molina Date: Fri, 15 Nov 2024 15:17:48 +0100 Subject: [PATCH] feat: remove tag from assets when it is deleted --- server/src/interfaces/asset.interface.ts | 1 + server/src/repositories/asset.repository.ts | 14 ++++++++++++++ server/src/services/tag.service.ts | 3 ++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/server/src/interfaces/asset.interface.ts b/server/src/interfaces/asset.interface.ts index 1b32c57d41..7659f31467 100644 --- a/server/src/interfaces/asset.interface.ts +++ b/server/src/interfaces/asset.interface.ts @@ -162,6 +162,7 @@ export interface IAssetRepository { getUploadAssetIdByChecksum(ownerId: string, checksum: Buffer): Promise; getByAlbumId(pagination: PaginationOptions, albumId: string): Paginated; getByDeviceIds(ownerId: string, deviceId: string, deviceAssetIds: string[]): Promise; + getByTagId(ownerId: string, tagId: string): Promise; getByUserId(pagination: PaginationOptions, userId: string, options?: AssetSearchOptions): Paginated; getById( id: string, diff --git a/server/src/repositories/asset.repository.ts b/server/src/repositories/asset.repository.ts index ce7d257b40..9d14756931 100644 --- a/server/src/repositories/asset.repository.ts +++ b/server/src/repositories/asset.repository.ts @@ -163,6 +163,20 @@ export class AssetRepository implements IAssetRepository { return assets.map((asset) => asset.deviceAssetId); } + async getByTagId(ownerId: string, tagId: string): Promise { + const assets = await this.repository.find({ + select: { + tags: true, + }, + where: { + ownerId, + tags: [{ id: tagId }], + }, + }); + + return assets.map((asset) => asset.id); + } + getByUserId( pagination: PaginationOptions, userId: string, diff --git a/server/src/services/tag.service.ts b/server/src/services/tag.service.ts index 2aca400cc7..a8bab15746 100644 --- a/server/src/services/tag.service.ts +++ b/server/src/services/tag.service.ts @@ -70,7 +70,8 @@ export class TagService extends BaseService { async remove(auth: AuthDto, id: string): Promise { await this.requireAccess({ auth, permission: Permission.TAG_DELETE, ids: [id] }); - // TODO sync tag changes for affected assets + const assetIdList = await this.assetRepository.getByTagId(auth.user.id, id); + await this.removeAssets(auth, id, { ids: assetIdList }); await this.tagRepository.delete(id); }