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