fix for failing integration tests
All checks were successful
Server Tests / Mocha – Unit & Utility Tests (push) Successful in 1m10s
Server Tests / Jest – Integration Tests (push) Successful in 1m33s

This commit is contained in:
Devin Major 2026-05-08 12:02:19 -04:00
parent ad7db99f07
commit 1e7977cdba

View File

@ -19,6 +19,7 @@
const path = require('path');
const fs = require('fs');
const os = require('os');
const { Transform, pipeline } = require('stream');
const { promisify } = require('util');
const pipelineAsync = promisify(pipeline);
@ -35,6 +36,7 @@ const env = require('../helpers/env');
const EXPORT_TTL_HOURS = env.EXPORT_TTL_HOURS || 24;
const EXPORT_DEDUP_MINS = env.EXPORT_DEDUP_MINS ?? 5;
const TEMP_DIR = env.TEMP_DIR || os.tmpdir();
/**
* On startup: delete orphaned export files whose ExportJob is expired or missing.
@ -43,13 +45,13 @@ const EXPORT_DEDUP_MINS = env.EXPORT_DEDUP_MINS ?? 5;
setImmediate(async () => {
try {
const pattern = /^export_[a-f0-9]+\.(csv|geojson)$/;
const files = await fs.promises.readdir(env.TEMP_DIR).catch(() => []);
const files = await fs.promises.readdir(TEMP_DIR).catch(() => []);
for (const file of files) {
if (!pattern.test(file)) continue;
const id = file.replace(/^export_/, '').replace(/\.(csv|geojson)$/, '');
const exists = ObjectId.isValid(id) && await ExportJob.exists({ _id: id, expiresAt: { $gt: new Date() } });
if (!exists) {
fs.unlink(path.join(env.TEMP_DIR, file), () => {});
fs.unlink(path.join(TEMP_DIR, file), () => {});
}
}
} catch { /* non-fatal */ }
@ -241,12 +243,12 @@ function recordToRow(d, sessionMeta, jobHeader, units, includeFm = false) {
// ─── Async generation ─────────────────────────────────────────────────────────
async function generateExport(exportJobId) {
try {
const exportJob = await ExportJob.findById(exportJobId);
if (!exportJob) return;
try {
exportJob.status = ExportJobStatus.PROCESSING;
await exportJob.save();
await ExportJob.updateOne({ _id: exportJobId }, { $set: { status: ExportJobStatus.PROCESSING } });
const job = await Job.findById(exportJob.jobId, 'name orderNumber client')
.populate('client', '_id name')
@ -273,7 +275,7 @@ async function generateExport(exportJobId) {
const interval = exportJob.interval;
const includeFm = !!exportJob.fm;
const outPath = path.join(env.TEMP_DIR, `export_${exportJobId}.${exportJob.format}`);
const outPath = path.join(TEMP_DIR, `export_${exportJobId}.${exportJob.format}`);
const writeStream = fs.createWriteStream(outPath);
const units = exportJob.units || 'metric';
@ -353,17 +355,19 @@ async function generateExport(exportJobId) {
});
const expiresAt = new Date(Date.now() + EXPORT_TTL_HOURS * 3600 * 1000);
exportJob.status = ExportJobStatus.READY;
exportJob.filePath = outPath;
exportJob.expiresAt = expiresAt;
await exportJob.save();
await ExportJob.updateOne(
{ _id: exportJobId },
{ $set: { status: ExportJobStatus.READY, filePath: outPath, expiresAt } }
);
} catch (err) {
exportJob.status = ExportJobStatus.ERROR;
exportJob.errorMsg = err.message;
await exportJob.save();
await ExportJob.updateOne(
{ _id: exportJobId },
{ $set: { status: ExportJobStatus.ERROR, errorMsg: err.message } }
);
console.error('[export] generation failed', err);
}
} catch (_) { /* document deleted or DB disconnected — nothing to update */ }
}
// ─── Route handlers ───────────────────────────────────────────────────────────