2024-01-26 11:48:37 -05:00
|
|
|
import { Inject } from '@nestjs/common';
|
|
|
|
|
import { DateTime } from 'luxon';
|
2024-03-20 23:53:07 +01:00
|
|
|
import { BulkIdsDto } from 'src/dtos/asset-ids.response.dto';
|
|
|
|
|
import { AuthDto } from 'src/dtos/auth.dto';
|
2024-08-15 06:57:01 -04:00
|
|
|
import { Permission } from 'src/enum';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IAccessRepository } from 'src/interfaces/access.interface';
|
|
|
|
|
import { IAssetRepository } from 'src/interfaces/asset.interface';
|
2024-03-22 18:24:02 -04:00
|
|
|
import { ClientEvent, IEventRepository } from 'src/interfaces/event.interface';
|
2024-03-21 12:59:49 +01:00
|
|
|
import { IJobRepository, JOBS_ASSET_PAGINATION_SIZE, JobName } from 'src/interfaces/job.interface';
|
2024-08-20 07:49:56 -04:00
|
|
|
import { requireAccess } from 'src/utils/access';
|
2024-03-20 22:15:09 -05:00
|
|
|
import { usePagination } from 'src/utils/pagination';
|
2024-01-26 11:48:37 -05:00
|
|
|
|
|
|
|
|
export class TrashService {
|
|
|
|
|
constructor(
|
2024-08-20 07:49:56 -04:00
|
|
|
@Inject(IAccessRepository) private access: IAccessRepository,
|
2024-01-26 11:48:37 -05:00
|
|
|
@Inject(IAssetRepository) private assetRepository: IAssetRepository,
|
|
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
2024-03-22 18:24:02 -04:00
|
|
|
@Inject(IEventRepository) private eventRepository: IEventRepository,
|
2024-08-20 07:49:56 -04:00
|
|
|
) {}
|
2024-01-26 11:48:37 -05:00
|
|
|
|
|
|
|
|
async restoreAssets(auth: AuthDto, dto: BulkIdsDto): Promise<void> {
|
|
|
|
|
const { ids } = dto;
|
2024-08-20 07:49:56 -04:00
|
|
|
await requireAccess(this.access, { auth, permission: Permission.ASSET_RESTORE, ids });
|
2024-01-26 11:48:37 -05:00
|
|
|
await this.restoreAndSend(auth, ids);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async restore(auth: AuthDto): Promise<void> {
|
|
|
|
|
const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) =>
|
2024-02-12 20:50:47 -05:00
|
|
|
this.assetRepository.getByUserId(pagination, auth.user.id, {
|
|
|
|
|
trashedBefore: DateTime.now().toJSDate(),
|
|
|
|
|
}),
|
2024-01-26 11:48:37 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for await (const assets of assetPagination) {
|
|
|
|
|
const ids = assets.map((a) => a.id);
|
|
|
|
|
await this.restoreAndSend(auth, ids);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async empty(auth: AuthDto): Promise<void> {
|
|
|
|
|
const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) =>
|
2024-02-12 20:50:47 -05:00
|
|
|
this.assetRepository.getByUserId(pagination, auth.user.id, {
|
|
|
|
|
trashedBefore: DateTime.now().toJSDate(),
|
|
|
|
|
}),
|
2024-01-26 11:48:37 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
for await (const assets of assetPagination) {
|
|
|
|
|
await this.jobRepository.queueAll(
|
2024-06-10 13:04:34 -04:00
|
|
|
assets.map((asset) => ({
|
|
|
|
|
name: JobName.ASSET_DELETION,
|
|
|
|
|
data: {
|
|
|
|
|
id: asset.id,
|
|
|
|
|
deleteOnDisk: true,
|
|
|
|
|
},
|
|
|
|
|
})),
|
2024-01-26 11:48:37 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async restoreAndSend(auth: AuthDto, ids: string[]) {
|
|
|
|
|
if (ids.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await this.assetRepository.restoreAll(ids);
|
2024-03-22 18:24:02 -04:00
|
|
|
this.eventRepository.clientSend(ClientEvent.ASSET_RESTORE, auth.user.id, ids);
|
2024-01-26 11:48:37 -05:00
|
|
|
}
|
|
|
|
|
}
|