Skip to content
Snippets Groups Projects
Commit fcc05d54 authored by Henrique Varella Ehrenfried's avatar Henrique Varella Ehrenfried :speech_balloon:
Browse files

Merge branch 'CSV_download' into 'master'

Csv download

See merge request !1
parents b30f147d 24f16d41
Branches master
No related tags found
1 merge request!1Csv download
...@@ -21,4 +21,53 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>. ...@@ -21,4 +21,53 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>.
'use strict'; 'use strict';
module.exports = function(Category) { module.exports = function(Category) {
Category.csvexport = function(res, callback ) {
var datetime = new Date();
var datetimeFuture = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate()+7)
res.set('Expires', datetimeFuture+'');
res.set('Cache-Control', 'max-age=0, no-cache, must-revalidate, proxy-revalidate');
res.set('Last-Modified', datetime+'');
res.set('Content-Type','application/force-download');
res.set('Content-Type','application/octet-stream');
res.set('Content-Type','application/download');
res.set('Content-Disposition','attachment;filename=category.csv');
res.set('Content-Transfer-Encoding','binary');
Category.find(function(err, categories)
{
// conversão JSON para CSV
// cabeçalho do csv
var headers = {
category_name: "category_name",
category_description: "category_description",
id: "id"
};
//Adiciona os headers no CSV
categories.unshift(headers);
var jsonObject = JSON.stringify(categories);
var array = typeof jsonObject != 'object' ? JSON.parse(jsonObject) : jsonObject;
var csv = '';
for (var i = 0; i < array.length; i++) {
var line = '';
for (var index in array[i]) {
if (line != '') line += ';' // aqui muda o separador do csv
line += array[i][index];
}
csv += line + '\n';
}
res.send(csv);
});
};
Category.remoteMethod('csvexport',
{
accepts: [
{arg: 'res', type: 'object', 'http': {source: 'res'}}
],
returns: {},
http: {path: '/csv', verb: 'get'},
});
}; };
...@@ -21,5 +21,79 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>. ...@@ -21,5 +21,79 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>.
'use strict'; 'use strict';
module.exports = function(Geolocation) { module.exports = function(Geolocation) {
Geolocation.csvexport = function(type, res, callback ) {
if (type == "padrao" || type == "android"){
var datetime = new Date();
var datetimeFuture = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate()+7)
res.set('Expires', datetimeFuture+'');
res.set('Cache-Control', 'max-age=0, no-cache, must-revalidate, proxy-revalidate');
res.set('Last-Modified', datetime +'');
res.set('Content-Type','application/force-download');
res.set('Content-Type','application/octet-stream');
res.set('Content-Type','application/download');
res.set('Content-Disposition','attachment;filename=geolocation.csv');
res.set('Content-Transfer-Encoding','binary');
Geolocation.find(function(err, geolocations) {
// conversão JSON para CSV
// cabeçalho do csv
var headers = {
category_id: "category_id",
geolocation_name: "geolocation_name",
latitude: "latitude",
longitude: "longitude",
when_sent: "when_sent",
description_location: "description_location",
user_id: "user_id",
id: "id"
};
// cabeçalho do csv - Adiciona android_id
if (type == "android"){
headers['android_id'] = "android_id"
}
//Ordena header
var ordered_header = {}
Object.keys(headers).sort().forEach(function(key) {
ordered_header[key] = headers[key];
});
//Adiciona os headers no CSV
geolocations.unshift(headers);
// Posição do Android ID 4 atualmente esta em 8
var jsonObject = JSON.stringify(geolocations);
var array = typeof jsonObject != 'object' ? JSON.parse(jsonObject) : jsonObject;
var csv = '';
for (let i = 0; i < array.length; i++) {
var line = '';
var aux = {}
Object.keys(array[i]).sort().forEach(function(key) {
aux[key] = array[i][key];
});
if (type == "padrao"){
delete aux["android_id"];
}
for (let index in aux) {
if (line != '') line += ';' // aqui muda o separador do csv
line += array[i][index];
}
csv += line + '\n';
}
res.send(csv);
});
}
};
Geolocation.remoteMethod('csvexport',
{
accepts: [
{arg: 'type', type: 'string'},
{arg: 'res', type: 'object', 'http': {source: 'res'}}
],
returns: {},
http: {path: '/csv/:type', verb: 'get'}
});
}; };
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