Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve export and ensure columns order #255

Merged
merged 5 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/httpInterface.js
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ async function handleGetListLookup(request, reply) {
.stream(),
this.castResultsAsStream(),
streamValidator(),
...responseStringifiers(),
...responseStringifiers(this.allFieldNames),
reply.raw
)
} catch (error) {
Expand Down Expand Up @@ -532,7 +532,7 @@ async function handleGetList(request, reply) {
.stream(),
this.castResultsAsStream(),
streamValidator(),
...responseStringifiers(),
...responseStringifiers(this.allFieldNames),
reply.raw
)
} catch (error) {
Expand Down
20 changes: 9 additions & 11 deletions lib/transformers/excel.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,24 @@

const { Transform } = require('stream')
const XLSXTransformStream = require('xlsx-write-stream')
const { formatDataForColumnExport } = require('./utils')


module.exports = () => ({
stringifier: () => {
stringifier: (fieldNames) => {
let headerProcessed = false
const dataTransformer = new Transform({
transform(chunk, _, callback) {
if (!headerProcessed) {
headerProcessed = true
const columns = Object.keys(chunk)
this.push(columns)
this.push(fieldNames)
}

this.push(
Object.values(chunk)
.map(documentValue => (
typeof documentValue === 'object'
? JSON.stringify(documentValue)
: documentValue
))
)
const data = fieldNames.reduce((acc, field) => {
acc.push(formatDataForColumnExport(chunk[field]))
return acc
}, [])
this.push(data)
return callback()
},
objectMode: true,
Expand Down
13 changes: 13 additions & 0 deletions lib/transformers/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict'

module.exports = {
formatDataForColumnExport: data => {
if (!data) {
return ''
}

return typeof data === 'object'
? JSON.stringify(data)
: data
},
}
56 changes: 44 additions & 12 deletions tests/httpInterface.getExport.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1129,21 +1129,37 @@ 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',
// NOTE: array and objects are stringified in the generated document
'["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()
})
Expand Down Expand Up @@ -1177,21 +1193,37 @@ 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',
// NOTE: array and objects are stringified in the generated document
'["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()
})
Expand Down
Loading