diff --git a/common/models/geolocation.js b/common/models/geolocation.js index bbb00b4cad772140648f5f1cd8de933cca3c8356..f2249bc51ea153ad64806a605f174f060ee4c254 100644 --- a/common/models/geolocation.js +++ b/common/models/geolocation.js @@ -22,8 +22,7 @@ along with SMPPIR-CheckIn. If not, see <https://www.gnu.org/licenses/>. module.exports = function(Geolocation) { Geolocation.csvexport = function(type, res, callback ) { - - if (type == "padr" || type == "andr"){ + if (type == "padrao" || type == "android"){ var datetime = new Date(); var datetimeFuture = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate()+7) res.set('Expires', datetimeFuture+''); @@ -35,14 +34,11 @@ module.exports = function(Geolocation) { res.set('Content-Disposition','attachment;filename=geolocation.csv'); res.set('Content-Transfer-Encoding','binary'); - Geolocation.find(function(err, geolocations) { - + Geolocation.find(function(err, geolocations) { // conversão JSON para CSV var itemsFormatted = []; - - if (type == "padr"){ - // cabeçalho do csv - var headers = { + // cabeçalho do csv + var headers = { category_id: "category_id", geolocation_name: "geolocation_name", latitude: "latitude", @@ -51,71 +47,45 @@ module.exports = function(Geolocation) { description_location: "description_location", user_id: "user_id", id: "id" - }; - - geolocations.forEach((item) => { - itemsFormatted.push({ - category_id: item.category_id, - geolocation_name: item.geolocation_name, - latitude: item.latitude, - longitude: item.longitude, - when_sent: item.when_sent, - description_location: item.description_location, - user_id: item.user_id, - id: item.id - }); - }); + }; + + // cabeçalho do csv - Adiciona android_id + if (type == "android"){ + headers['android_id'] = "android_id" } - if (type == "andr"){ - // cabeçalho do csv - var headers = { - category_id: "category_id", - geolocation_name: "geolocation_name", - latitude: "latitude", - longitude: "longitude", - android_id: "android_id", - when_sent: "when_sent", - description_location: "description_location", - user_id: "user_id", - id: "id" - }; - - geolocations.forEach((item) => { - itemsFormatted.push({ - category_id: item.category_id, - geolocation_name: item.geolocation_name, - latitude: item.latitude, - longitude: item.longitude, - android_id: item.android_id, - when_sent: item.when_sent, - description_location: item.description_location, - user_id: item.user_id, - id: item.id - }); - }); - } - if (headers) { - itemsFormatted.unshift(headers); - } + //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); - var jsonObject = JSON.stringify(itemsFormatted); + // 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 (var i = 0; i < array.length; i++) { + for (let i = 0; i < array.length; i++) { var line = ''; - for (var index in array[i]) { + 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 + '\r\n'; + csv += line + '\n'; } res.send(csv); }); } - else - console.log('Use as rotas /padr ou /andr') }; Geolocation.remoteMethod('csvexport',