const fs = require('fs'); const fsp = require('fs/promises'); const path = require('path'); const { buildTimesheetPdfLocation, getPdfBaseDir } = require('../helpers/pdf-paths'); async function ensureDirectoryExists(dirPath) { await fsp.mkdir(dirPath, { recursive: true }); } async function fileExists(filePath) { try { await fsp.access(filePath, fs.constants.F_OK); return true; } catch { return false; } } async function savePdfBufferAtomic(targetPath, buffer) { const dir = path.dirname(targetPath); await ensureDirectoryExists(dir); const tmpPath = `${targetPath}.tmp-${process.pid}-${Date.now()}`; await fsp.writeFile(tmpPath, buffer); await fsp.rename(tmpPath, targetPath); } function resolvePdfLocationFromTimesheetRow(timesheetRow) { return buildTimesheetPdfLocation(timesheetRow); } function resolveAbsolutePathFromRelative(relativePath) { const baseDir = getPdfBaseDir(); return path.join(baseDir, relativePath); } module.exports = { ensureDirectoryExists, fileExists, savePdfBufferAtomic, resolvePdfLocationFromTimesheetRow, resolveAbsolutePathFromRelative, };