2023-08-24 21:28:50 +02:00
|
|
|
import { DatabaseAction } from '@app/infra/entities';
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DateTime } from 'luxon';
|
2023-10-09 16:25:03 +02:00
|
|
|
import { AccessCore, Permission } from '../access';
|
2023-08-24 21:28:50 +02:00
|
|
|
import { AuthUserDto } from '../auth';
|
|
|
|
import { AUDIT_LOG_MAX_DURATION } from '../domain.constant';
|
2023-10-09 16:25:03 +02:00
|
|
|
import { IAccessRepository, IAuditRepository } from '../repositories';
|
2023-08-24 21:28:50 +02:00
|
|
|
import { AuditDeletesDto, AuditDeletesResponseDto } from './audit.dto';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class AuditService {
|
|
|
|
private access: AccessCore;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
@Inject(IAccessRepository) accessRepository: IAccessRepository,
|
|
|
|
@Inject(IAuditRepository) private repository: IAuditRepository,
|
|
|
|
) {
|
|
|
|
this.access = new AccessCore(accessRepository);
|
|
|
|
}
|
|
|
|
|
|
|
|
async handleCleanup(): Promise<boolean> {
|
|
|
|
await this.repository.removeBefore(DateTime.now().minus(AUDIT_LOG_MAX_DURATION).toJSDate());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getDeletes(authUser: AuthUserDto, dto: AuditDeletesDto): Promise<AuditDeletesResponseDto> {
|
|
|
|
const userId = dto.userId || authUser.id;
|
2023-09-20 13:16:33 +02:00
|
|
|
await this.access.requirePermission(authUser, Permission.TIMELINE_READ, userId);
|
2023-08-24 21:28:50 +02:00
|
|
|
|
|
|
|
const audits = await this.repository.getAfter(dto.after, {
|
|
|
|
ownerId: userId,
|
|
|
|
entityType: dto.entityType,
|
|
|
|
action: DatabaseAction.DELETE,
|
|
|
|
});
|
|
|
|
|
|
|
|
const duration = DateTime.now().diff(DateTime.fromJSDate(dto.after));
|
|
|
|
|
|
|
|
return {
|
|
|
|
needsFullSync: duration > AUDIT_LOG_MAX_DURATION,
|
|
|
|
ids: audits.map(({ entityId }) => entityId),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|