diff --git a/server/libs/domain/src/search/search.service.ts b/server/libs/domain/src/search/search.service.ts index a16d568465..08b78127f0 100644 --- a/server/libs/domain/src/search/search.service.ts +++ b/server/libs/domain/src/search/search.service.ts @@ -106,9 +106,22 @@ export class SearchService { async getExploreData(authUser: AuthUserDto): Promise[]> { this.assertEnabled(); const results = await this.searchRepository.explore(authUser.id); + const lookup = await this.getLookupMap( + results.reduce( + (ids: string[], result: SearchExploreItem) => [ + ...ids, + ...result.items.map((item) => item.data.id), + ], + [], + ), + ); + return results.map(({ fieldName, items }) => ({ fieldName, - items: items.map(({ value, data }) => ({ value, data: mapAsset(data) })), + items: items + .map(({ value, data }) => ({ value, data: lookup[data.id] })) + .filter(({ data }) => !!data) + .map(({ value, data }) => ({ value, data: mapAsset(data) })), })); } @@ -132,10 +145,17 @@ export class SearchService { } const albums = await this.searchRepository.searchAlbums(query, filters); + const lookup = await this.getLookupMap(assets.items.map((asset) => asset.id)); return { albums: { ...albums, items: albums.items.map(mapAlbum) }, - assets: { ...assets, items: assets.items.map(mapAsset) }, + assets: { + ...assets, + items: assets.items + .map((item) => lookup[item.id]) + .filter((item) => !!item) + .map(mapAsset), + }, }; } @@ -358,4 +378,13 @@ export class SearchService { const [assetId, personId] = key.split('|'); return { assetId, personId }; } + + private async getLookupMap(assetIds: string[]) { + const assets = await this.assetRepository.getByIds(assetIds); + const lookup: Record = {}; + for (const asset of assets) { + lookup[asset.id] = asset; + } + return lookup; + } } diff --git a/server/libs/domain/test/asset.repository.mock.ts b/server/libs/domain/test/asset.repository.mock.ts index 71d31ab5ee..993b987ef0 100644 --- a/server/libs/domain/test/asset.repository.mock.ts +++ b/server/libs/domain/test/asset.repository.mock.ts @@ -2,7 +2,7 @@ import { IAssetRepository } from '../src'; export const newAssetRepositoryMock = (): jest.Mocked => { return { - getByIds: jest.fn(), + getByIds: jest.fn().mockResolvedValue([]), getWithout: jest.fn(), getWith: jest.fn(), getFirstAssetForAlbumId: jest.fn(), diff --git a/server/package.json b/server/package.json index 942e5a19d1..dca288248e 100644 --- a/server/package.json +++ b/server/package.json @@ -140,8 +140,8 @@ "coverageThreshold": { "./libs/domain/": { "branches": 80, - "functions": 87, - "lines": 93.7, + "functions": 85, + "lines": 93, "statements": 93 } },