170 lines
6.6 KiB
JavaScript
170 lines
6.6 KiB
JavaScript
"use strict";
|
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
}
|
|
Object.defineProperty(o, k2, desc);
|
|
}) : (function(o, m, k, k2) {
|
|
if (k2 === undefined) k2 = k;
|
|
o[k2] = m[k];
|
|
}));
|
|
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
}) : function(o, v) {
|
|
o["default"] = v;
|
|
});
|
|
var __importStar = (this && this.__importStar) || (function () {
|
|
var ownKeys = function(o) {
|
|
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
var ar = [];
|
|
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
return ar;
|
|
};
|
|
return ownKeys(o);
|
|
};
|
|
return function (mod) {
|
|
if (mod && mod.__esModule) return mod;
|
|
var result = {};
|
|
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
__setModuleDefault(result, mod);
|
|
return result;
|
|
};
|
|
})();
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.enrichReportExportItems = enrichReportExportItems;
|
|
exports.buildReportCsv = buildReportCsv;
|
|
exports.buildReportXlsxBuffer = buildReportXlsxBuffer;
|
|
exports.buildReportPdfBuffer = buildReportPdfBuffer;
|
|
const XLSX = __importStar(require("xlsx"));
|
|
const queryResilience_1 = require("../utils/queryResilience");
|
|
const usdtFormat_1 = require("../utils/usdtFormat");
|
|
const reportDateFormat_1 = require("../utils/reportDateFormat");
|
|
const countryFormat_1 = require("../utils/countryFormat");
|
|
// pdfmake ships Roboto TTF fonts (full Unicode) — required for French text and proper tables.
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
const pdfMake = require('pdfmake');
|
|
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
const robotoFonts = require('pdfmake/fonts/Roboto');
|
|
pdfMake.setFonts(robotoFonts);
|
|
async function enrichReportExportItems(items, columns) {
|
|
const countryColumns = columns.filter((c) => c.type === 'country');
|
|
if (countryColumns.length === 0)
|
|
return items;
|
|
const countryMap = await (0, countryFormat_1.getCountryNameMap)();
|
|
return items.map((row) => {
|
|
const out = { ...row };
|
|
for (const col of countryColumns) {
|
|
if (col.field in out && out[col.field] != null) {
|
|
out[col.field] = (0, countryFormat_1.formatCountryCode)(String(out[col.field]), countryMap);
|
|
}
|
|
}
|
|
return out;
|
|
});
|
|
}
|
|
function cellValue(row, column) {
|
|
const v = row[column.field];
|
|
if (v == null)
|
|
return '';
|
|
if (column.type === 'money')
|
|
return (0, usdtFormat_1.formatUsdtRaw)(v);
|
|
if (column.type === 'date')
|
|
return (0, reportDateFormat_1.formatReportDate)(v);
|
|
if (v instanceof Date)
|
|
return (0, reportDateFormat_1.formatReportDate)(v);
|
|
return String(v);
|
|
}
|
|
function formatExportDate(d) {
|
|
return d.toLocaleString('fr-FR', {
|
|
dateStyle: 'short',
|
|
timeStyle: 'medium',
|
|
});
|
|
}
|
|
function buildReportCsv(data) {
|
|
const header = data.columns.map((c) => c.headerName).join(',');
|
|
const lines = data.items.map((row) => data.columns
|
|
.map((c) => {
|
|
const s = cellValue(row, c);
|
|
return s.includes(',') || s.includes('"') || s.includes('\n')
|
|
? `"${s.replace(/"/g, '""')}"`
|
|
: s;
|
|
})
|
|
.join(','));
|
|
return '\uFEFF' + [header, ...lines].join('\n');
|
|
}
|
|
function buildReportXlsxBuffer(data) {
|
|
const rows = data.items.map((row) => {
|
|
const out = {};
|
|
for (const c of data.columns)
|
|
out[c.headerName] = cellValue(row, c);
|
|
return out;
|
|
});
|
|
const ws = XLSX.utils.json_to_sheet(rows);
|
|
const wb = XLSX.utils.book_new();
|
|
XLSX.utils.book_append_sheet(wb, ws, 'Report');
|
|
return Buffer.from(XLSX.write(wb, { type: 'buffer', bookType: 'xlsx' }));
|
|
}
|
|
function columnWidths(columnCount) {
|
|
if (columnCount <= 6)
|
|
return Array(columnCount).fill('*');
|
|
if (columnCount <= 10)
|
|
return Array(columnCount).fill('auto');
|
|
return Array(columnCount).fill(42);
|
|
}
|
|
async function buildReportPdfBuffer(data) {
|
|
const exportItems = data.items.length > queryResilience_1.MAX_PDF_EXPORT_ROWS ? data.items.slice(0, queryResilience_1.MAX_PDF_EXPORT_ROWS) : data.items;
|
|
const truncated = exportItems.length < data.items.length;
|
|
const headerRow = data.columns.map((c) => ({
|
|
text: c.headerName,
|
|
style: 'tableHeader',
|
|
}));
|
|
const bodyRows = exportItems.map((row) => data.columns.map((c) => ({
|
|
text: cellValue(row, c),
|
|
style: 'tableCell',
|
|
})));
|
|
const docDefinition = {
|
|
pageOrientation: 'landscape',
|
|
pageMargins: [28, 36, 28, 36],
|
|
defaultStyle: {
|
|
font: 'Roboto',
|
|
fontSize: 8,
|
|
},
|
|
content: [
|
|
{ text: data.title, style: 'title' },
|
|
{
|
|
text: `Rapport : ${data.reportKey} · ${exportItems.length.toLocaleString('fr-FR')} ligne(s)${truncated ? ` (limité à ${queryResilience_1.MAX_PDF_EXPORT_ROWS.toLocaleString('fr-FR')} pour stabilité)` : ''} · ${formatExportDate(new Date())}`,
|
|
style: 'meta',
|
|
margin: [0, 4, 0, 12],
|
|
},
|
|
{
|
|
table: {
|
|
headerRows: 1,
|
|
dontBreakRows: true,
|
|
widths: columnWidths(data.columns.length),
|
|
body: [headerRow, ...bodyRows],
|
|
},
|
|
layout: {
|
|
fillColor: (rowIndex) => (rowIndex === 0 ? '#f0f0f0' : null),
|
|
hLineWidth: () => 0.5,
|
|
vLineWidth: () => 0.25,
|
|
hLineColor: () => '#cccccc',
|
|
vLineColor: () => '#e8e8e8',
|
|
paddingLeft: () => 4,
|
|
paddingRight: () => 4,
|
|
paddingTop: () => 3,
|
|
paddingBottom: () => 3,
|
|
},
|
|
},
|
|
],
|
|
styles: {
|
|
title: { fontSize: 14, bold: true, alignment: 'center' },
|
|
meta: { fontSize: 9, color: '#555555', alignment: 'center' },
|
|
tableHeader: { bold: true, fontSize: 8 },
|
|
tableCell: { fontSize: 7 },
|
|
},
|
|
};
|
|
const pdf = pdfMake.createPdf(docDefinition);
|
|
return pdf.getBuffer();
|
|
}
|
|
//# sourceMappingURL=reportExportService.js.map
|