Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/admin/dto/pagination.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { IsOptional, IsInt, Min, Max, IsString, IsIn } from 'class-validator';
import { Type } from 'class-transformer';
import { ApiPropertyOptional } from '@nestjs/swagger';

export class PaginationDto {
@ApiPropertyOptional({ default: 1 })
@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
page?: number = 1;

@ApiPropertyOptional({ default: 20 })
@IsOptional()
@Type(() => Number)
@IsInt()
@Min(1)
@Max(100)
limit?: number = 20;

@ApiPropertyOptional()
@IsOptional()
@IsString()
sortBy?: string;

@ApiPropertyOptional({ enum: ['ASC', 'DESC'], default: 'ASC' })
@IsOptional()
@IsIn(['ASC', 'DESC'])
sortOrder?: 'ASC' | 'DESC' = 'ASC';
}
41 changes: 41 additions & 0 deletions src/audit/audit-export.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { AuditLog } from './audit-log.entity';
import { Transform } from 'stream';

@Injectable()
export class AuditExportService {
constructor(
@InjectRepository(AuditLog)
private readonly repo: Repository<AuditLog>,
) {}

async streamExport(res: any, startDate: Date, endDate: Date): Promise<void> {
const diff = endDate.getTime() - startDate.getTime();
const maxSyncMs = 90 * 24 * 60 * 60 * 1000;

if (diff > maxSyncMs) {
res.status(400).json({ message: 'Use async export for ranges > 90 days' });
return;
}

const query = this.repo
.createQueryBuilder('log')
.where('log.createdAt BETWEEN :start AND :end', { start: startDate, end: endDate })
.orderBy('log.createdAt', 'ASC')
.stream();

res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-Disposition', 'attachment; filename="audit-export.csv"');

const csvTransform = new Transform({
objectMode: true,
transform(row: Record<string, unknown>, _encoding, callback) {
callback(null, JSON.stringify(row) + '\n');
},
});

query.pipe(csvTransform).pipe(res);
}
}
Loading