From 380fc0697925f69995cc76a499dfd0f16480c5ba Mon Sep 17 00:00:00 2001
From: Pranay Pandey <79053599+Pranay-Pandey@users.noreply.github.com>
Date: Mon, 4 Nov 2024 20:33:03 +0530
Subject: [PATCH] fix: remove duplicateIds on unique assets (#13752)

---
 server/src/services/duplicate.service.spec.ts | 16 +++++++++++++--
 server/src/services/duplicate.service.ts      | 20 +++++++++++++++++--
 2 files changed, 32 insertions(+), 4 deletions(-)

diff --git a/server/src/services/duplicate.service.spec.ts b/server/src/services/duplicate.service.spec.ts
index 095d53dde6..75af1ef6f1 100644
--- a/server/src/services/duplicate.service.spec.ts
+++ b/server/src/services/duplicate.service.spec.ts
@@ -31,11 +31,23 @@ describe(SearchService.name, () => {
 
   describe('getDuplicates', () => {
     it('should get duplicates', async () => {
-      assetMock.getDuplicates.mockResolvedValue([assetStub.hasDupe]);
+      assetMock.getDuplicates.mockResolvedValue([assetStub.hasDupe, assetStub.hasDupe]);
       await expect(sut.getDuplicates(authStub.admin)).resolves.toEqual([
-        { duplicateId: assetStub.hasDupe.duplicateId, assets: [expect.objectContaining({ id: assetStub.hasDupe.id })] },
+        {
+          duplicateId: assetStub.hasDupe.duplicateId,
+          assets: [
+            expect.objectContaining({ id: assetStub.hasDupe.id }),
+            expect.objectContaining({ id: assetStub.hasDupe.id }),
+          ],
+        },
       ]);
     });
+
+    it('should update assets with duplicateId', async () => {
+      assetMock.getDuplicates.mockResolvedValue([assetStub.hasDupe]);
+      await expect(sut.getDuplicates(authStub.admin)).resolves.toEqual([]);
+      expect(assetMock.updateAll).toHaveBeenCalledWith([assetStub.hasDupe.id], { duplicateId: null });
+    });
   });
 
   describe('handleQueueSearchDuplicates', () => {
diff --git a/server/src/services/duplicate.service.ts b/server/src/services/duplicate.service.ts
index 2fac7fcd3e..0d91df5790 100644
--- a/server/src/services/duplicate.service.ts
+++ b/server/src/services/duplicate.service.ts
@@ -16,8 +16,24 @@ import { usePagination } from 'src/utils/pagination';
 export class DuplicateService extends BaseService {
   async getDuplicates(auth: AuthDto): Promise<DuplicateResponseDto[]> {
     const res = await this.assetRepository.getDuplicates({ userIds: [auth.user.id] });
-
-    return mapDuplicateResponse(res.map((a) => mapAsset(a, { auth, withStack: true })));
+    const uniqueAssetIds: string[] = [];
+    const duplicates = mapDuplicateResponse(res.map((a) => mapAsset(a, { auth, withStack: true }))).filter(
+      (duplicate) => {
+        if (duplicate.assets.length === 1) {
+          uniqueAssetIds.push(duplicate.assets[0].id);
+          return false;
+        }
+        return true;
+      },
+    );
+    if (uniqueAssetIds.length > 0) {
+      try {
+        await this.assetRepository.updateAll(uniqueAssetIds, { duplicateId: null });
+      } catch (error: any) {
+        this.logger.error(`Failed to remove duplicateId from assets: ${error.message}`);
+      }
+    }
+    return duplicates;
   }
 
   @OnJob({ name: JobName.QUEUE_DUPLICATE_DETECTION, queue: QueueName.DUPLICATE_DETECTION })