diff --git a/CHANGELOG.md b/CHANGELOG.md index f485d900..ad650a32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Fixed + +- improved excel export to ensure column consistency + ## 6.9.5 - 2024-01-19 ### Added diff --git a/lib/httpInterface.js b/lib/httpInterface.js index 9b98220b..04122450 100644 --- a/lib/httpInterface.js +++ b/lib/httpInterface.js @@ -459,7 +459,7 @@ async function handleGetListLookup(request, reply) { .stream(), this.castResultsAsStream(), streamValidator(), - ...responseStringifiers(), + ...responseStringifiers({ fields: this.allFieldNames }), reply.raw ) } catch (error) { @@ -532,7 +532,7 @@ async function handleGetList(request, reply) { .stream(), this.castResultsAsStream(), streamValidator(), - ...responseStringifiers(), + ...responseStringifiers({ fields: this.allFieldNames }), reply.raw ) } catch (error) { diff --git a/lib/transformers/excel.js b/lib/transformers/excel.js index dafd50fc..132b5f11 100644 --- a/lib/transformers/excel.js +++ b/lib/transformers/excel.js @@ -17,26 +17,24 @@ const { Transform } = require('stream') const XLSXTransformStream = require('xlsx-write-stream') +const { formatDataForColumnExport } = require('./utils') + module.exports = () => ({ - stringifier: () => { + stringifier: ({ fields }) => { let headerProcessed = false const dataTransformer = new Transform({ transform(chunk, _, callback) { if (!headerProcessed) { headerProcessed = true - const columns = Object.keys(chunk) - this.push(columns) + this.push(fields) } - this.push( - Object.values(chunk) - .map(documentValue => ( - typeof documentValue === 'object' - ? JSON.stringify(documentValue) - : documentValue - )) - ) + const data = fields.reduce((acc, field) => { + acc.push(formatDataForColumnExport(chunk[field])) + return acc + }, []) + this.push(data) return callback() }, objectMode: true, diff --git a/lib/transformers/utils.js b/lib/transformers/utils.js new file mode 100644 index 00000000..67598dc2 --- /dev/null +++ b/lib/transformers/utils.js @@ -0,0 +1,13 @@ +'use strict' + +module.exports = { + formatDataForColumnExport: data => { + if (!data) { + return '' + } + + return typeof data === 'object' + ? JSON.stringify(data) + : data + }, +} diff --git a/tests/httpInterface.getExport.test.js b/tests/httpInterface.getExport.test.js index 10a7178e..6328267c 100644 --- a/tests/httpInterface.getExport.test.js +++ b/tests/httpInterface.getExport.test.js @@ -1129,9 +1129,28 @@ tap.test('HTTP GET /export with _id in querystring', async t => { const [{ data }] = xlsx.parse(response.rawPayload) t.strictSame(data.length, 2) const [row1, row2] = data - t.strictSame(row1, Object.keys(HTTP_STATION_DOC)) + t.strictSame(row1, [ + '_id', + 'updaterId', + 'updatedAt', + 'creatorId', + 'createdAt', + '__STATE__', + 'Cap', + 'CodiceMIR', + 'Comune', + 'Direttrici', + 'Indirizzo', + 'country', + 'nonNullableDate', + ]) t.strictSame(row2, [ '002415b0-8d6d-427c-b654-9857183e57a7', + 'my-updated-id', + '2017-11-11T00:00:00.000Z', + 'my-creator-id', + '2017-11-10T00:00:00.000Z', + 'PUBLIC', 25040, 'S01788', 'Borgonato', @@ -1139,11 +1158,8 @@ tap.test('HTTP GET /export with _id in querystring', async t => { '["D028"]', 'Via Stazione, 24', 'it', - '2017-11-10T00:00:00.000Z', - 'my-creator-id', - 'my-updated-id', - '2017-11-11T00:00:00.000Z', - 'PUBLIC', + // Missing column data + '', ]) t.end() }) @@ -1177,9 +1193,28 @@ tap.test('HTTP GET /export with _id in querystring', async t => { const [{ data }] = xlsx.parse(response.rawPayload) t.strictSame(data.length, 2) const [row1, row2] = data - t.strictSame(row1, Object.keys(HTTP_STATION_DOC)) + t.strictSame(row1, [ + '_id', + 'updaterId', + 'updatedAt', + 'creatorId', + 'createdAt', + '__STATE__', + 'Cap', + 'CodiceMIR', + 'Comune', + 'Direttrici', + 'Indirizzo', + 'country', + 'nonNullableDate', + ]) t.strictSame(row2, [ '002415b0-8d6d-427c-b654-9857183e57a7', + 'my-updated-id', + '2017-11-11T00:00:00.000Z', + 'my-creator-id', + '2017-11-10T00:00:00.000Z', + 'PUBLIC', 25040, 'S01788', 'Borgonato', @@ -1187,11 +1222,8 @@ tap.test('HTTP GET /export with _id in querystring', async t => { '["D028"]', 'Via Stazione, 24', 'it', - '2017-11-10T00:00:00.000Z', - 'my-creator-id', - 'my-updated-id', - '2017-11-11T00:00:00.000Z', - 'PUBLIC', + // Missing column data + '', ]) t.end() })