refactor: migrate shared-link repository to kysely (#15289)

* refactor: migrate shared-link repository to kysely

* fix duplicate individual shared link return in getAll when there are more than 1 asset in the shared link

* using correct order condition

* using eb.table

---------

Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
This commit is contained in:
Daniel Dietzler
2025-01-18 20:25:15 +01:00
committed by GitHub
parent 430d0b86ee
commit 3d13da7f11
7 changed files with 435 additions and 404 deletions

View File

@@ -275,7 +275,6 @@ describe('AuthService', () => {
describe('validate - shared key', () => {
it('should not accept a non-existent key', async () => {
sharedLinkMock.getByKey.mockResolvedValue(null);
await expect(
sut.authenticate({
headers: { 'x-immich-share-key': 'key' },

View File

@@ -76,7 +76,6 @@ describe(SharedLinkService.name, () => {
describe('get', () => {
it('should throw an error for an invalid shared link', async () => {
sharedLinkMock.get.mockResolvedValue(null);
await expect(sut.get(authStub.user1, 'missing-id')).rejects.toBeInstanceOf(BadRequestException);
expect(sharedLinkMock.get).toHaveBeenCalledWith(authStub.user1.user.id, 'missing-id');
expect(sharedLinkMock.update).not.toHaveBeenCalled();
@@ -130,7 +129,6 @@ describe(SharedLinkService.name, () => {
albumId: albumStub.oneAsset.id,
allowDownload: true,
allowUpload: true,
assets: [],
description: null,
expiresAt: null,
showExif: true,
@@ -160,7 +158,7 @@ describe(SharedLinkService.name, () => {
albumId: null,
allowDownload: true,
allowUpload: true,
assets: [{ id: assetStub.image.id }],
assetIds: [assetStub.image.id],
description: null,
expiresAt: null,
showExif: true,
@@ -190,7 +188,7 @@ describe(SharedLinkService.name, () => {
albumId: null,
allowDownload: false,
allowUpload: true,
assets: [{ id: assetStub.image.id }],
assetIds: [assetStub.image.id],
description: null,
expiresAt: null,
showExif: false,
@@ -201,7 +199,6 @@ describe(SharedLinkService.name, () => {
describe('update', () => {
it('should throw an error for an invalid shared link', async () => {
sharedLinkMock.get.mockResolvedValue(null);
await expect(sut.update(authStub.user1, 'missing-id', {})).rejects.toBeInstanceOf(BadRequestException);
expect(sharedLinkMock.get).toHaveBeenCalledWith(authStub.user1.user.id, 'missing-id');
expect(sharedLinkMock.update).not.toHaveBeenCalled();
@@ -222,7 +219,6 @@ describe(SharedLinkService.name, () => {
describe('remove', () => {
it('should throw an error for an invalid shared link', async () => {
sharedLinkMock.get.mockResolvedValue(null);
await expect(sut.remove(authStub.user1, 'missing-id')).rejects.toBeInstanceOf(BadRequestException);
expect(sharedLinkMock.get).toHaveBeenCalledWith(authStub.user1.user.id, 'missing-id');
expect(sharedLinkMock.update).not.toHaveBeenCalled();
@@ -258,9 +254,10 @@ describe(SharedLinkService.name, () => {
]);
expect(accessMock.asset.checkOwnerAccess).toHaveBeenCalledTimes(1);
expect(sharedLinkMock.update).toHaveBeenCalled();
expect(sharedLinkMock.update).toHaveBeenCalledWith({
...sharedLinkStub.individual,
assets: [assetStub.image, { id: 'asset-3' }],
assetIds: ['asset-3'],
});
});
});

View File

@@ -10,7 +10,6 @@ import {
SharedLinkPasswordDto,
SharedLinkResponseDto,
} from 'src/dtos/shared-link.dto';
import { AssetEntity } from 'src/entities/asset.entity';
import { SharedLinkEntity } from 'src/entities/shared-link.entity';
import { Permission, SharedLinkType } from 'src/enum';
import { BaseService } from 'src/services/base.service';
@@ -67,7 +66,7 @@ export class SharedLinkService extends BaseService {
userId: auth.user.id,
type: dto.type,
albumId: dto.albumId || null,
assets: (dto.assetIds || []).map((id) => ({ id }) as AssetEntity),
assetIds: dto.assetIds,
description: dto.description || null,
password: dto.password,
expiresAt: dto.expiresAt || null,
@@ -138,10 +137,12 @@ export class SharedLinkService extends BaseService {
}
results.push({ assetId, success: true });
sharedLink.assets.push({ id: assetId } as AssetEntity);
}
await this.sharedLinkRepository.update(sharedLink);
await this.sharedLinkRepository.update({
...sharedLink,
assetIds: results.filter(({ success }) => success).map(({ assetId }) => assetId),
});
return results;
}