refactor(server): move checkExistingAssets(), checkBulkUpdate() remove getAllAssets() (#9715)

* Refactor controller methods, non-breaking change

* Remove getAllAssets

* used imports

* sync:sql

* missing mock

* Removing remaining references

* chore: remove unused code

---------

Co-authored-by: Jason Rasmussen <jrasm91@gmail.com>
This commit is contained in:
Min Idzelis
2024-05-24 21:02:22 -04:00
committed by GitHub
parent 95012dc19b
commit d5cf8e4bfe
27 changed files with 286 additions and 684 deletions

View File

@@ -1,9 +1,7 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { AssetSearchDto, CheckExistingAssetsDto } from 'src/dtos/asset-v1.dto';
import { AssetEntity } from 'src/entities/asset.entity';
import { AssetCheck, AssetOwnerCheck, IAssetRepositoryV1 } from 'src/interfaces/asset-v1.interface';
import { OptionalBetween } from 'src/utils/database';
import { AssetCheck, IAssetRepositoryV1 } from 'src/interfaces/asset-v1.interface';
import { In } from 'typeorm/find-options/operator/In.js';
import { Repository } from 'typeorm/repository/Repository.js';
@@ -11,36 +9,6 @@ import { Repository } from 'typeorm/repository/Repository.js';
export class AssetRepositoryV1 implements IAssetRepositoryV1 {
constructor(@InjectRepository(AssetEntity) private assetRepository: Repository<AssetEntity>) {}
/**
* Retrieves all assets by user ID.
*
* @param ownerId - The ID of the owner.
* @param dto - The AssetSearchDto object containing search criteria.
* @returns A Promise that resolves to an array of AssetEntity objects.
*/
getAllByUserId(ownerId: string, dto: AssetSearchDto): Promise<AssetEntity[]> {
return this.assetRepository.find({
where: {
ownerId,
isVisible: true,
isFavorite: dto.isFavorite,
isArchived: dto.isArchived,
updatedAt: OptionalBetween(dto.updatedAfter, dto.updatedBefore),
},
relations: {
exifInfo: true,
tags: true,
stack: { assets: true },
},
skip: dto.skip || 0,
take: dto.take,
order: {
fileCreatedAt: 'DESC',
},
withDeleted: true,
});
}
get(id: string): Promise<AssetEntity | null> {
return this.assetRepository.findOne({
where: { id },
@@ -73,30 +41,4 @@ export class AssetRepositoryV1 implements IAssetRepositoryV1 {
withDeleted: true,
});
}
async getExistingAssets(ownerId: string, checkDuplicateAssetDto: CheckExistingAssetsDto): Promise<string[]> {
const assets = await this.assetRepository.find({
select: { deviceAssetId: true },
where: {
deviceAssetId: In(checkDuplicateAssetDto.deviceAssetIds),
deviceId: checkDuplicateAssetDto.deviceId,
ownerId,
},
withDeleted: true,
});
return assets.map((asset) => asset.deviceAssetId);
}
getByOriginalPath(originalPath: string): Promise<AssetOwnerCheck | null> {
return this.assetRepository.findOne({
select: {
id: true,
ownerId: true,
checksum: true,
},
where: {
originalPath,
},
});
}
}

View File

@@ -157,6 +157,18 @@ export class AssetRepository implements IAssetRepository {
});
}
getByDeviceIds(ownerId: string, deviceId: string, deviceAssetIds: string[]): Promise<AssetEntity[]> {
return this.repository.find({
select: { deviceAssetId: true },
where: {
deviceAssetId: In(deviceAssetIds),
deviceId,
ownerId,
},
withDeleted: true,
});
}
getByUserId(
pagination: PaginationOptions,
userId: string,
@@ -300,6 +312,21 @@ export class AssetRepository implements IAssetRepository {
});
}
@GenerateSql({ params: [DummyValue.UUID, DummyValue.BUFFER] })
getByChecksums(ownerId: string, checksums: Buffer[]): Promise<AssetEntity[]> {
return this.repository.find({
select: {
id: true,
checksum: true,
},
where: {
ownerId,
checksum: In(checksums),
},
withDeleted: true,
});
}
@GenerateSql({ params: [DummyValue.UUID, DummyValue.BUFFER] })
async getUploadAssetIdByChecksum(ownerId: string, checksum: Buffer): Promise<string | undefined> {
const asset = await this.repository.findOne({

View File

@@ -39,21 +39,6 @@ export class LibraryRepository implements ILibraryRepository {
return this.repository.countBy({ ownerId });
}
@GenerateSql({ params: [DummyValue.UUID] })
getAllByUserId(ownerId: string): Promise<LibraryEntity[]> {
return this.repository.find({
where: {
ownerId,
},
relations: {
owner: true,
},
order: {
createdAt: 'ASC',
},
});
}
@GenerateSql({ params: [] })
getAll(withDeleted = false): Promise<LibraryEntity[]> {
return this.repository.find({