games_app/backend/src/services/reportExportJobService.ts
2026-06-29 15:01:23 +02:00

199 lines
6.2 KiB
TypeScript

/**
* Async admin report export jobs — shares PostgreSQL queue + worker with user exports.
*/
import fs from 'fs';
import path from 'path';
import { v4 as uuidv4 } from 'uuid';
import * as adminExportJobRepository from '../repositories/adminExportJobRepository';
import type { AdminExportJobRow } from '../repositories/adminExportJobRepository';
import * as reportsService from './reportsService';
import {
buildReportCsv,
buildReportPdfBuffer,
buildReportXlsxBuffer,
enrichReportExportItems,
} from './reportExportService';
import type { ExportJobView } from './adminUserExportService';
import { kickExportWorker } from './adminUserExportService';
const EXPORT_DIR = path.join(process.cwd(), 'data', 'exports');
const JOB_TTL_MS = 60 * 60 * 1000;
const PAGE_SIZE = 500;
const DEFAULT_MAX_ROWS = 100_000;
const ABSOLUTE_MAX_ROWS = 500_000;
export type ReportExportRequest = Record<string, unknown> & {
format?: 'csv' | 'xlsx' | 'pdf';
maxRows?: number;
};
function ensureExportDir(): void {
fs.mkdirSync(EXPORT_DIR, { recursive: true });
}
function clampMaxRows(maxRows?: number): number {
if (maxRows === undefined || maxRows === null || !Number.isFinite(maxRows) || maxRows <= 0) {
return DEFAULT_MAX_ROWS;
}
return Math.min(Math.floor(maxRows), ABSOLUTE_MAX_ROWS);
}
function rowToView(row: AdminExportJobRow): ExportJobView {
return {
id: row.id,
status: row.status,
progress: row.progress,
rowCount: row.row_count,
totalEstimate: row.total_estimate,
error: row.error,
fileName: row.file_name,
createdAt: row.created_at.toISOString(),
completedAt: row.completed_at?.toISOString() ?? null,
reportKey: row.report_key,
jobKind: row.job_kind,
};
}
export async function startReportExport(
requestedBy: string,
reportKey: string,
request: ReportExportRequest
): Promise<ExportJobView> {
ensureExportDir();
const def = reportsService.listReportCatalog();
const exists = def.categories.some((c) => c.reports.some((r) => r.key === reportKey));
if (!exists) throw new Error('Report not found');
const id = uuidv4();
const format =
request.format === 'pdf' ? 'pdf' : request.format === 'csv' ? 'csv' : 'xlsx';
const maxRows = clampMaxRows(
typeof request.maxRows === 'number' ? request.maxRows : parseInt(String(request.maxRows ?? ''), 10)
);
const stamp = new Date().toISOString().slice(0, 10);
const suffix = request.ids || request.selectedIds ? '_selection' : '';
const fileName = `${reportKey}${suffix}_${stamp}.${format}`;
const { format: _f, maxRows: _m, ...requestPayload } = request;
const row = await adminExportJobRepository.createExportJob({
id,
requestedBy,
fileName,
requestPayload: { ...requestPayload, reportKey },
format,
maxRows,
columns: [],
expiresAt: new Date(Date.now() + JOB_TTL_MS),
jobKind: 'reports',
reportKey,
});
kickExportWorker();
return rowToView(row);
}
export async function getReportExportJob(
jobId: string,
requestedBy: string
): Promise<ExportJobView | null> {
const row = await adminExportJobRepository.getExportJobForUser(jobId, requestedBy);
if (!row || row.job_kind !== 'reports') return null;
return rowToView(row);
}
export async function getReportExportFilePath(
jobId: string,
requestedBy: string
): Promise<{ filePath: string; fileName: string } | null> {
const row = await adminExportJobRepository.getExportJobForUser(jobId, requestedBy);
if (!row || row.job_kind !== 'reports') return null;
if (row.status !== 'completed' || !row.file_path || !row.file_name) return null;
if (!fs.existsSync(row.file_path)) return null;
return { filePath: row.file_path, fileName: row.file_name };
}
export async function runReportExportJob(job: AdminExportJobRow): Promise<void> {
const reportKey = job.report_key;
if (!reportKey) throw new Error('Missing report_key on export job');
const payload = job.request_payload as ReportExportRequest;
const selectedIds = reportsService.parseSelectedIds(payload);
const allItems: Record<string, unknown>[] = [];
let columns: { field: string; headerName: string; type?: string }[] = [];
let cursor: string | undefined;
let exported = 0;
let includeTotal = true;
const reportProgress = async (totalEstimate?: number | null) => {
await adminExportJobRepository.updateExportJobProgress(
job.id,
exported,
exported,
totalEstimate
);
};
if (selectedIds?.length) {
for (let i = 0; i < selectedIds.length && exported < job.max_rows; i += PAGE_SIZE) {
const batchIds = selectedIds.slice(i, i + PAGE_SIZE);
const page = await reportsService.queryReport(reportKey, {
...payload,
ids: batchIds,
limit: batchIds.length,
offset: 0,
});
columns = page.columns;
allItems.push(...page.items);
exported += page.items.length;
await reportProgress();
}
} else {
while (exported < job.max_rows) {
const remaining = job.max_rows - exported;
const limit = Math.min(PAGE_SIZE, remaining);
const page = await reportsService.queryReport(reportKey, {
...payload,
limit,
offset: cursor ? undefined : 0,
cursor,
includeTotal,
});
columns = page.columns;
if (includeTotal && page.total != null) {
await reportProgress(Math.min(page.total, job.max_rows));
}
includeTotal = false;
if (page.items.length === 0) break;
allItems.push(...page.items);
exported += page.items.length;
await reportProgress();
if (!page.hasMore || !page.nextCursor) break;
cursor = page.nextCursor ?? undefined;
}
}
const exportPayload = {
title: reportKey,
reportKey,
columns,
items: await enrichReportExportItems(allItems, columns),
};
const outPath = path.join(EXPORT_DIR, `${job.id}.${job.format}`);
if (job.format === 'csv') {
fs.writeFileSync(outPath, buildReportCsv(exportPayload), 'utf8');
} else if (job.format === 'pdf') {
const buffer = await buildReportPdfBuffer(exportPayload);
fs.writeFileSync(outPath, buffer);
} else {
const buffer = buildReportXlsxBuffer(exportPayload);
fs.writeFileSync(outPath, buffer);
}
await adminExportJobRepository.completeExportJob(job.id, outPath, exported);
}