diff --git a/src/modules/reportes/reportes.service.ts b/src/modules/reportes/reportes.service.ts index 44bbdae..47d104b 100644 --- a/src/modules/reportes/reportes.service.ts +++ b/src/modules/reportes/reportes.service.ts @@ -5,7 +5,13 @@ import { } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { randomUUID } from 'node:crypto'; -import { existsSync, mkdirSync, createWriteStream } from 'node:fs'; +import { + createWriteStream, + existsSync, + mkdirSync, + renameSync, + unlinkSync, +} from 'node:fs'; import { join } from 'node:path'; import PDFDocument from 'pdfkit'; import { Repository } from 'typeorm'; @@ -596,9 +602,24 @@ export class ReportesService { const dir = join(process.cwd(), 'uploads', 'reportes'); mkdirSync(dir, { recursive: true }); - const archivo = `${new Date().toISOString().slice(0, 10)}-${tipo}-${randomUUID()}.pdf`; + // Nombre estable por la firma del reporte (tanda 2026-10-23): reexportar + // REEMPLAZA el archivo en vez de acumular un PDF nuevo por clic (antes: + // `fecha-tipo-.pdf`). Firma: + // - `asistencia-servicio` → `-.pdf` + // - los 4 por rango → `.pdf` (solo sobrevive el último rango) + const idServicio = + typeof p.idServicio === 'string' && p.idServicio.length > 0 + ? p.idServicio + : null; + const firma = + tipo === 'asistencia-servicio' && idServicio !== null + ? `${tipo}-${idServicio}` + : tipo; + const archivo = `${firma}.pdf`; const rutaRel = `uploads/reportes/${archivo}`; - const url = `/uploads/reportes/${archivo}`; + // `?v=` rompe la caché del navegador: la URL base es siempre la misma y + // sin esto se reabriría el PDF anterior ya reemplazado. + const url = `/uploads/reportes/${archivo}?v=${Date.now()}`; await this.renderPdf(dir, archivo, datos, nombreGenerado); @@ -800,9 +821,29 @@ export class ReportesService { size: 'LETTER', bufferPages: true, }); - const stream = createWriteStream(join(dir, archivo)); - stream.on('finish', () => resolve()); - stream.on('error', reject); + // Escritura atómica (tanda 2026-10-23): se renderiza a un `.tmp` único + // y se renombra al finalizar, para que (a) una exportación simultánea + // no sirva un PDF a medio escribir y (b) el reemplazo del archivo + // anterior no lo deje corrupto si el proceso muere a mitad de escritura. + const destino = join(dir, archivo); + const temporal = `${destino}.${randomUUID()}.tmp`; + const stream = createWriteStream(temporal); + stream.on('finish', () => { + try { + renameSync(temporal, destino); + resolve(); + } catch (error) { + reject(error); + } + }); + stream.on('error', (error) => { + try { + unlinkSync(temporal); + } catch { + // El temporal no llegó a crearse: nada que limpiar. + } + reject(error); + }); doc.pipe(stream); const cols = datos.columnas; diff --git a/test/app.e2e-spec.ts b/test/app.e2e-spec.ts index fd20981..fcf56d7 100644 --- a/test/app.e2e-spec.ts +++ b/test/app.e2e-spec.ts @@ -1,11 +1,10 @@ import { Test, TestingModule } from '@nestjs/testing'; import { INestApplication } from '@nestjs/common'; import request from 'supertest'; -import { App } from 'supertest/types'; import { AppModule } from './../src/app.module.js'; describe('AppController (e2e)', () => { - let app: INestApplication; + let app: INestApplication; beforeEach(async () => { const moduleFixture: TestingModule = await Test.createTestingModule({