2024-01-26 11:48:37 -05:00
|
|
|
import { Inject } from '@nestjs/common';
|
2024-09-30 10:35:11 -04:00
|
|
|
import { OnEvent } from 'src/decorators';
|
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-09-18 09:57:52 -04:00
|
|
|
import { TrashResponseDto } from 'src/dtos/trash.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';
|
2024-09-12 14:12:39 -04:00
|
|
|
import { IEventRepository } from 'src/interfaces/event.interface';
|
2024-09-18 09:57:52 -04:00
|
|
|
import { IJobRepository, JOBS_ASSET_PAGINATION_SIZE, JobName, JobStatus } from 'src/interfaces/job.interface';
|
|
|
|
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
|
|
|
|
import { ITrashRepository } from 'src/interfaces/trash.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-03-22 18:24:02 -04:00
|
|
|
@Inject(IEventRepository) private eventRepository: IEventRepository,
|
2024-09-18 09:57:52 -04:00
|
|
|
@Inject(IJobRepository) private jobRepository: IJobRepository,
|
|
|
|
|
@Inject(ITrashRepository) private trashRepository: ITrashRepository,
|
|
|
|
|
@Inject(ILoggerRepository) private logger: ILoggerRepository,
|
|
|
|
|
) {
|
|
|
|
|
this.logger.setContext(TrashService.name);
|
|
|
|
|
}
|
2024-01-26 11:48:37 -05:00
|
|
|
|
2024-09-18 09:57:52 -04:00
|
|
|
async restoreAssets(auth: AuthDto, dto: BulkIdsDto): Promise<TrashResponseDto> {
|
2024-01-26 11:48:37 -05:00
|
|
|
const { ids } = dto;
|
2024-09-18 09:57:52 -04:00
|
|
|
if (ids.length === 0) {
|
|
|
|
|
return { count: 0 };
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-20 08:50:14 -04:00
|
|
|
await requireAccess(this.access, { auth, permission: Permission.ASSET_DELETE, ids });
|
2024-09-18 09:57:52 -04:00
|
|
|
await this.trashRepository.restoreAll(ids);
|
|
|
|
|
await this.eventRepository.emit('assets.restore', { assetIds: ids, userId: auth.user.id });
|
|
|
|
|
|
|
|
|
|
this.logger.log(`Restored ${ids.length} assets from trash`);
|
|
|
|
|
|
|
|
|
|
return { count: ids.length };
|
2024-01-26 11:48:37 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-18 09:57:52 -04:00
|
|
|
async restore(auth: AuthDto): Promise<TrashResponseDto> {
|
|
|
|
|
const count = await this.trashRepository.restore(auth.user.id);
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
this.logger.log(`Restored ${count} assets from trash`);
|
|
|
|
|
}
|
|
|
|
|
return { count };
|
|
|
|
|
}
|
2024-01-26 11:48:37 -05:00
|
|
|
|
2024-09-18 09:57:52 -04:00
|
|
|
async empty(auth: AuthDto): Promise<TrashResponseDto> {
|
|
|
|
|
const count = await this.trashRepository.empty(auth.user.id);
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.QUEUE_TRASH_EMPTY, data: {} });
|
2024-01-26 11:48:37 -05:00
|
|
|
}
|
2024-09-18 09:57:52 -04:00
|
|
|
return { count };
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 10:35:11 -04:00
|
|
|
@OnEvent({ name: 'assets.delete' })
|
2024-09-18 09:57:52 -04:00
|
|
|
async onAssetsDelete() {
|
|
|
|
|
await this.jobRepository.queue({ name: JobName.QUEUE_TRASH_EMPTY, data: {} });
|
2024-01-26 11:48:37 -05:00
|
|
|
}
|
|
|
|
|
|
2024-09-18 09:57:52 -04:00
|
|
|
async handleQueueEmptyTrash() {
|
|
|
|
|
let count = 0;
|
2024-01-26 11:48:37 -05:00
|
|
|
const assetPagination = usePagination(JOBS_ASSET_PAGINATION_SIZE, (pagination) =>
|
2024-09-18 09:57:52 -04:00
|
|
|
this.trashRepository.getDeletedIds(pagination),
|
2024-01-26 11:48:37 -05:00
|
|
|
);
|
|
|
|
|
|
2024-09-18 09:57:52 -04:00
|
|
|
for await (const assetIds of assetPagination) {
|
|
|
|
|
this.logger.debug(`Queueing ${assetIds.length} assets for deletion from the trash`);
|
|
|
|
|
count += assetIds.length;
|
2024-01-26 11:48:37 -05:00
|
|
|
await this.jobRepository.queueAll(
|
2024-09-18 09:57:52 -04:00
|
|
|
assetIds.map((assetId) => ({
|
2024-06-10 13:04:34 -04:00
|
|
|
name: JobName.ASSET_DELETION,
|
|
|
|
|
data: {
|
2024-09-18 09:57:52 -04:00
|
|
|
id: assetId,
|
2024-06-10 13:04:34 -04:00
|
|
|
deleteOnDisk: true,
|
|
|
|
|
},
|
|
|
|
|
})),
|
2024-01-26 11:48:37 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-18 09:57:52 -04:00
|
|
|
this.logger.log(`Queued ${count} assets for deletion from the trash`);
|
2024-01-26 11:48:37 -05:00
|
|
|
|
2024-09-18 09:57:52 -04:00
|
|
|
return JobStatus.SUCCESS;
|
2024-01-26 11:48:37 -05:00
|
|
|
}
|
|
|
|
|
}
|