mirror of
https://github.com/immich-app/immich.git
synced 2025-12-27 17:24:58 +03:00
refactor(server): view repository (#12755)
This commit is contained in:
@@ -1,21 +1,20 @@
|
||||
import { mapAsset } from 'src/dtos/asset-response.dto';
|
||||
import { IAssetRepository } from 'src/interfaces/asset.interface';
|
||||
|
||||
import { IViewRepository } from 'src/interfaces/view.interface';
|
||||
import { ViewService } from 'src/services/view.service';
|
||||
import { assetStub } from 'test/fixtures/asset.stub';
|
||||
import { authStub } from 'test/fixtures/auth.stub';
|
||||
import { newAssetRepositoryMock } from 'test/repositories/asset.repository.mock';
|
||||
import { newViewRepositoryMock } from 'test/repositories/view.repository.mock';
|
||||
|
||||
import { Mocked } from 'vitest';
|
||||
|
||||
describe(ViewService.name, () => {
|
||||
let sut: ViewService;
|
||||
let assetMock: Mocked<IAssetRepository>;
|
||||
let viewMock: Mocked<IViewRepository>;
|
||||
|
||||
beforeEach(() => {
|
||||
assetMock = newAssetRepositoryMock();
|
||||
viewMock = newViewRepositoryMock();
|
||||
|
||||
sut = new ViewService(assetMock);
|
||||
sut = new ViewService(viewMock);
|
||||
});
|
||||
|
||||
it('should work', () => {
|
||||
@@ -25,12 +24,12 @@ describe(ViewService.name, () => {
|
||||
describe('getUniqueOriginalPaths', () => {
|
||||
it('should return unique original paths', async () => {
|
||||
const mockPaths = ['path1', 'path2', 'path3'];
|
||||
assetMock.getUniqueOriginalPaths.mockResolvedValue(mockPaths);
|
||||
viewMock.getUniqueOriginalPaths.mockResolvedValue(mockPaths);
|
||||
|
||||
const result = await sut.getUniqueOriginalPaths(authStub.admin);
|
||||
|
||||
expect(result).toEqual(mockPaths);
|
||||
expect(assetMock.getUniqueOriginalPaths).toHaveBeenCalledWith(authStub.admin.user.id);
|
||||
expect(viewMock.getUniqueOriginalPaths).toHaveBeenCalledWith(authStub.admin.user.id);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -45,11 +44,11 @@ describe(ViewService.name, () => {
|
||||
|
||||
const mockAssetReponseDto = mockAssets.map((a) => mapAsset(a, { auth: authStub.admin }));
|
||||
|
||||
assetMock.getAssetsByOriginalPath.mockResolvedValue(mockAssets);
|
||||
viewMock.getAssetsByOriginalPath.mockResolvedValue(mockAssets);
|
||||
|
||||
const result = await sut.getAssetsByOriginalPath(authStub.admin, path);
|
||||
expect(result).toEqual(mockAssetReponseDto);
|
||||
await expect(assetMock.getAssetsByOriginalPath(authStub.admin.user.id, path)).resolves.toEqual(mockAssets);
|
||||
await expect(viewMock.getAssetsByOriginalPath(authStub.admin.user.id, path)).resolves.toEqual(mockAssets);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
import { Inject } from '@nestjs/common';
|
||||
import { AssetResponseDto, mapAsset } from 'src/dtos/asset-response.dto';
|
||||
import { AuthDto } from 'src/dtos/auth.dto';
|
||||
import { IAssetRepository } from 'src/interfaces/asset.interface';
|
||||
import { IViewRepository } from 'src/interfaces/view.interface';
|
||||
|
||||
export class ViewService {
|
||||
constructor(@Inject(IAssetRepository) private assetRepository: IAssetRepository) {}
|
||||
constructor(@Inject(IViewRepository) private viewRepository: IViewRepository) {}
|
||||
|
||||
getUniqueOriginalPaths(auth: AuthDto): Promise<string[]> {
|
||||
return this.assetRepository.getUniqueOriginalPaths(auth.user.id);
|
||||
return this.viewRepository.getUniqueOriginalPaths(auth.user.id);
|
||||
}
|
||||
|
||||
async getAssetsByOriginalPath(auth: AuthDto, path: string): Promise<AssetResponseDto[]> {
|
||||
const assets = await this.assetRepository.getAssetsByOriginalPath(auth.user.id, path);
|
||||
|
||||
const assets = await this.viewRepository.getAssetsByOriginalPath(auth.user.id, path);
|
||||
return assets.map((asset) => mapAsset(asset, { auth }));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user