refactor: migrate album repo to kysely (#15474)

This commit is contained in:
Alex
2025-01-21 11:24:48 -06:00
committed by GitHub
parent 58d5cc1e4b
commit c35fd6cbdb
7 changed files with 782 additions and 661 deletions

View File

@@ -15,7 +15,6 @@ import { BulkIdResponseDto, BulkIdsDto } from 'src/dtos/asset-ids.response.dto';
import { AuthDto } from 'src/dtos/auth.dto';
import { AlbumUserEntity } from 'src/entities/album-user.entity';
import { AlbumEntity } from 'src/entities/album.entity';
import { AssetEntity } from 'src/entities/asset.entity';
import { Permission } from 'src/enum';
import { AlbumAssetCount, AlbumInfoOptions } from 'src/interfaces/album.interface';
import { BaseService } from 'src/services/base.service';
@@ -112,16 +111,18 @@ export class AlbumService extends BaseService {
permission: Permission.ASSET_SHARE,
ids: dto.assetIds || [],
});
const assets = [...allowedAssetIdsSet].map((id) => ({ id }) as AssetEntity);
const assetIds = [...allowedAssetIdsSet].map((id) => id);
const album = await this.albumRepository.create({
ownerId: auth.user.id,
albumName: dto.albumName,
description: dto.description,
albumUsers: albumUsers.map((albumUser) => albumUser as AlbumUserEntity) ?? [],
assets,
albumThumbnailAssetId: assets[0]?.id || null,
});
const album = await this.albumRepository.create(
{
ownerId: auth.user.id,
albumName: dto.albumName,
description: dto.description,
albumThumbnailAssetId: assetIds[0] || null,
},
assetIds,
albumUsers,
);
for (const { userId } of albumUsers) {
await this.eventRepository.emit('album.invite', { id: album.id, userId });
@@ -141,7 +142,7 @@ export class AlbumService extends BaseService {
throw new BadRequestException('Invalid album thumbnail');
}
}
const updatedAlbum = await this.albumRepository.update({
const updatedAlbum = await this.albumRepository.update(album.id, {
id: album.id,
albumName: dto.albumName,
description: dto.description,
@@ -170,7 +171,7 @@ export class AlbumService extends BaseService {
const { id: firstNewAssetId } = results.find(({ success }) => success) || {};
if (firstNewAssetId) {
await this.albumRepository.update({
await this.albumRepository.update(id, {
id,
updatedAt: new Date(),
albumThumbnailAssetId: album.albumThumbnailAssetId ?? firstNewAssetId,
@@ -199,11 +200,8 @@ export class AlbumService extends BaseService {
);
const removedIds = results.filter(({ success }) => success).map(({ id }) => id);
if (removedIds.length > 0) {
await this.albumRepository.update({ id, updatedAt: new Date() });
if (album.albumThumbnailAssetId && removedIds.includes(album.albumThumbnailAssetId)) {
await this.albumRepository.updateThumbnails();
}
if (removedIds.length > 0 && album.albumThumbnailAssetId && removedIds.includes(album.albumThumbnailAssetId)) {
await this.albumRepository.updateThumbnails();
}
return results;