fix(exports): preserve processing state on async export failures

This commit is contained in:
saravanakumardb1 2026-03-22 11:58:54 -07:00
parent 265599d005
commit 67ef6a6068

View File

@ -63,17 +63,18 @@ export async function exportRoutes(app: FastifyInstance) {
// Process async — respond immediately, update job status when done
const log = req.log;
process.nextTick(async () => {
let currentJob = created;
try {
const processingJob = await repo.updateExportJob({
...created,
currentJob = await repo.updateExportJob({
...currentJob,
status: 'processing',
startedAt: new Date().toISOString(),
});
const rows = await fetchExportData(created.type, access.productId, created.filters);
const serialized = created.format === 'json' ? JSON.stringify(rows, null, 2) : toCsv(rows);
const fileName = `${created.type}-${access.productId}-${Date.now()}.${created.format}`;
await repo.updateExportJob({
...processingJob,
currentJob = await repo.updateExportJob({
...currentJob,
status: 'ready',
data: serialized,
rowCount: rows.length,
@ -86,7 +87,7 @@ export async function exportRoutes(app: FastifyInstance) {
log.error({ err, exportId: created.id }, '[exports] Export job failed');
await repo
.updateExportJob({
...created,
...currentJob,
status: 'failed',
error: err instanceof Error ? err.message : 'Unknown error',
completedAt: new Date().toISOString(),