7to Commit

This commit is contained in:
DillusDev001 2026-10-23 17:07:13 -04:00
parent d910d65b61
commit f38f464f83
2 changed files with 48 additions and 8 deletions

View file

@ -5,7 +5,13 @@ import {
} from '@nestjs/common'; } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { randomUUID } from 'node:crypto'; 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 { join } from 'node:path';
import PDFDocument from 'pdfkit'; import PDFDocument from 'pdfkit';
import { Repository } from 'typeorm'; import { Repository } from 'typeorm';
@ -596,9 +602,24 @@ export class ReportesService {
const dir = join(process.cwd(), 'uploads', 'reportes'); const dir = join(process.cwd(), 'uploads', 'reportes');
mkdirSync(dir, { recursive: true }); 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-<uuid>.pdf`). Firma:
// - `asistencia-servicio` → `<tipo>-<idServicio>.pdf`
// - los 4 por rango → `<tipo>.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 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); await this.renderPdf(dir, archivo, datos, nombreGenerado);
@ -800,9 +821,29 @@ export class ReportesService {
size: 'LETTER', size: 'LETTER',
bufferPages: true, bufferPages: true,
}); });
const stream = createWriteStream(join(dir, archivo)); // Escritura atómica (tanda 2026-10-23): se renderiza a un `.tmp` único
stream.on('finish', () => resolve()); // y se renombra al finalizar, para que (a) una exportación simultánea
stream.on('error', reject); // 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); doc.pipe(stream);
const cols = datos.columnas; const cols = datos.columnas;

View file

@ -1,11 +1,10 @@
import { Test, TestingModule } from '@nestjs/testing'; import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common'; import { INestApplication } from '@nestjs/common';
import request from 'supertest'; import request from 'supertest';
import { App } from 'supertest/types';
import { AppModule } from './../src/app.module.js'; import { AppModule } from './../src/app.module.js';
describe('AppController (e2e)', () => { describe('AppController (e2e)', () => {
let app: INestApplication<App>; let app: INestApplication;
beforeEach(async () => { beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({ const moduleFixture: TestingModule = await Test.createTestingModule({