Skip to content
Snippets Groups Projects
Commit a4bda2dc authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Merge branch 'issue/442' into development

parents 0014d69a 3f1db752
No related branches found
No related tags found
No related merge requests found
Pipeline #16195 failed
...@@ -4,9 +4,10 @@ All notable changes to this project will be documented in this file. ...@@ -4,9 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/) The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/). and this project adheres to [Semantic Versioning](http://semver.org/).
## Unreleased ## UNRELEASED
## Added ### Changed
- Add school building filter/dimension to school count route - Add school building filter/dimension to school count route
- Fixed CSV output when result objects have nested arrays and/or objects
## 1.3.2 - 2018-06-20 ## 1.3.2 - 2018-06-20
### Changed ### Changed
......
...@@ -3,13 +3,51 @@ const log = require(`${libs}/log`)(module); ...@@ -3,13 +3,51 @@ const log = require(`${libs}/log`)(module);
const xml = require('js2xmlparser'); const xml = require('js2xmlparser');
const csv = require('csv-express'); const csv = require('csv-express');
// Custom generic middleware used respond requests. // Função para transformar um resultado que contém objetos com arrays aninhados em vários objetos
// The function reads the req.query.format param and respond in json, xml or csv // sem arrays aninhandos
function flatObj(obj) {
let flatList = [];
let tempObj = {};
Object.keys(obj).forEach((key) => {
if(obj[key] instanceof Array) {
obj[key].forEach((i) => {
let flatten = flatObj(i);
if(flatten instanceof Array) {
flatten.forEach((j) => {
flatList.push(Object.assign({}, tempObj, j));
});
} else {
flatList.push(Object.assign({}, tempObj, flatten));
}
});
} else if(obj[key] instanceof Object) {
tempObj = Object.assign({}, tempObj, obj[key]);
} else {
tempObj[key] = obj[key];
}
});
if(flatList.length > 0) return flatList;
return tempObj;
}
// Custom generic middleware used to answer requests.
// The function reads the req.query.format param and answers in json, xml or csv
function response(value) { function response(value) {
return (req, res, next) => { return (req, res, next) => {
if (req.query.format === 'csv') { if (req.query.format === 'csv') {
let result = [];
req.result.forEach((i) => {
let flatten = flatObj(i);
if(flatten instanceof Array) {
result = [...result, ...flatten];
} else {
result.push(flatten);
}
});
res.attachment(`${value}.csv`); res.attachment(`${value}.csv`);
res.csv(req.result, true); res.csv(result, true);
} else if (req.query.format === 'xml') { } else if (req.query.format === 'xml') {
res.send(xml.parse('result', { [value]: req.result })); res.send(xml.parse('result', { [value]: req.result }));
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment