From 68d2566522ba3c1b990cfb180c1c18fdede06a4b Mon Sep 17 00:00:00 2001
From: "Henrique V. Ehrenfried" <hvehrenfried@inf.ufpr.br>
Date: Tue, 5 Jun 2018 11:45:21 -0300
Subject: [PATCH 1/5] Change default port

---
 Dockerfile                       | 11 +++++++++++
 README.md                        | 28 +++++++++++++++++++++++++++-
 enviroment.js                    |  2 +-
 server/config.json               |  2 +-
 server/datasources.json.homologa | 18 ++++++++++++++++++
 5 files changed, 58 insertions(+), 3 deletions(-)
 create mode 100644 Dockerfile
 create mode 100644 server/datasources.json.homologa

diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..52aa1dd
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,11 @@
+FROM node:latest
+
+RUN apt-get -y update -qq && apt-get install -y -qq apt-utils tree
+
+COPY . . 
+
+RUN npm install
+
+EXPOSE 4001
+
+CMD npm start
\ No newline at end of file
diff --git a/README.md b/README.md
index 31d4f1d..ae82e7f 100644
--- a/README.md
+++ b/README.md
@@ -71,4 +71,30 @@ The result will be something similar to:
 {
   "category_name": "Categoria teste",
   "category_description": "Categoria usada para testar funcionalidades"
-}
\ No newline at end of file
+}
+```
+
+## Deploy on SMPPIRHOMOLOGA
+
+Whenever you are ready to deploy, please change the `datasources.json` with the `datasources.json.homologa`.
+
+After that, you can use the Docker image:
+
+Para criar a imagem nova
+
+`sudo docker build --tag mapping-api .` 
+
+Para exportar a imagem
+
+`sudo docker save -o ~/mapping-api.tar mapping-api`
+
+Para importar a image
+
+`sudo docker load -i ~/mapping-api.tar ` 
+
+Para rodar a imagem
+
+`sudo docker run -d --rm -p 4001:4001 --net="host" --name mapping-api mapping-api`
+
+
+:warning: O `-d` significa daemon, não quiser que o comando de rodar imagem rode em segundo plano, remova este argumento :warning:
\ No newline at end of file
diff --git a/enviroment.js b/enviroment.js
index e8faa90..d72fa3f 100644
--- a/enviroment.js
+++ b/enviroment.js
@@ -1,4 +1,4 @@
-const URL = 'http://localhost:3000/api/';
+const URL = 'http://localhost:4001/api/';
 
 const randomLowerCase = function() {
   let possible = 'abcdefghijklmnopqrstuvwxyz';
diff --git a/server/config.json b/server/config.json
index ff5c9c9..440a939 100644
--- a/server/config.json
+++ b/server/config.json
@@ -1,7 +1,7 @@
 {
   "restApiRoot": "/api",
   "host": "0.0.0.0",
-  "port": 3000,
+  "port": 4001,
   "remoting": {
     "context": false,
     "rest": {
diff --git a/server/datasources.json.homologa b/server/datasources.json.homologa
new file mode 100644
index 0000000..fec5524
--- /dev/null
+++ b/server/datasources.json.homologa
@@ -0,0 +1,18 @@
+{
+    "db": {
+      "name": "db",
+      "connector": "memory"
+    },
+    "SMPPIR_CheckIn2": {
+      "host": "localhost",
+      "port": 5432,
+      "url": "postgres://smppir:123mudar@localhost:5432/smppir_checkin?ssl=false",
+      "database": "smppir_checkin",
+      "password": "123mudar",
+      "name": "SMPPIR_CheckIn2",
+      "connector": "postgresql",
+      "user": "postgres",
+      "ssl": false
+    }
+  }
+  
\ No newline at end of file
-- 
GitLab


From b30f147df2c4108a4ec369f9d4c1e9adf540bccb Mon Sep 17 00:00:00 2001
From: Rafael S Castliho <rsc15@inf.ufpr.br>
Date: Wed, 27 Feb 2019 09:59:43 -0300
Subject: [PATCH 2/5] Add license to files

---
 common/models/category.js            | 20 ++++++++++++++++++++
 common/models/end_user.js            | 20 ++++++++++++++++++++
 common/models/geolocation.js         | 20 ++++++++++++++++++++
 enviroment.js                        | 20 ++++++++++++++++++++
 server/boot/access-control.js        | 20 ++++++++++++++++++++
 server/boot/authentication.js        | 20 ++++++++++++++++++++
 server/boot/hook.js                  | 20 ++++++++++++++++++++
 server/boot/models-automigratioin.js | 20 ++++++++++++++++++++
 server/boot/root.js                  | 20 ++++++++++++++++++++
 server/server.js                     | 20 ++++++++++++++++++++
 test/account.js                      | 20 ++++++++++++++++++++
 test/category.js                     | 20 ++++++++++++++++++++
 test/geolocation.js                  | 20 ++++++++++++++++++++
 13 files changed, 260 insertions(+)

diff --git a/common/models/category.js b/common/models/category.js
index 0500368..101e682 100644
--- a/common/models/category.js
+++ b/common/models/category.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 'use strict';
 
 module.exports = function(Category) {
diff --git a/common/models/end_user.js b/common/models/end_user.js
index 216d337..c3aa7e6 100644
--- a/common/models/end_user.js
+++ b/common/models/end_user.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 'use strict';
 
 module.exports = function(EndUser) {
diff --git a/common/models/geolocation.js b/common/models/geolocation.js
index 77d4c05..4dff1e0 100644
--- a/common/models/geolocation.js
+++ b/common/models/geolocation.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 'use strict';
 
 module.exports = function(Geolocation) {
diff --git a/enviroment.js b/enviroment.js
index d72fa3f..5f2483c 100644
--- a/enviroment.js
+++ b/enviroment.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 const URL = 'http://localhost:4001/api/';
 
 const randomLowerCase = function() {
diff --git a/server/boot/access-control.js b/server/boot/access-control.js
index 38f66df..3c52e38 100644
--- a/server/boot/access-control.js
+++ b/server/boot/access-control.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 /*
 2 Permissions : admin, user
 
diff --git a/server/boot/authentication.js b/server/boot/authentication.js
index 8e88d4b..e6a21eb 100644
--- a/server/boot/authentication.js
+++ b/server/boot/authentication.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 'use strict';
 
 module.exports = function enableAuthentication(server) {
diff --git a/server/boot/hook.js b/server/boot/hook.js
index 35fea51..f32cef5 100644
--- a/server/boot/hook.js
+++ b/server/boot/hook.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 module.exports = function(server) {
     var remotes = server.remotes();
     // modify all returned values
diff --git a/server/boot/models-automigratioin.js b/server/boot/models-automigratioin.js
index 56f146a..7966110 100644
--- a/server/boot/models-automigratioin.js
+++ b/server/boot/models-automigratioin.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 // module.exports = function(app) {
 //   'use strict';
 //   var db = app.dataSources.SMPPIR_CheckIn2;
diff --git a/server/boot/root.js b/server/boot/root.js
index 6adce90..46ca315 100644
--- a/server/boot/root.js
+++ b/server/boot/root.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 'use strict';
 
 module.exports = function(server) {
diff --git a/server/server.js b/server/server.js
index ef738ab..b3c308d 100644
--- a/server/server.js
+++ b/server/server.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 'use strict';
 
 var loopback = require('loopback');
diff --git a/test/account.js b/test/account.js
index e70a592..c9f58f8 100644
--- a/test/account.js
+++ b/test/account.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 var chakram = require('chakram'), expect = chakram.expect;
 var env = require('../enviroment');
 const URL = env.URL;
diff --git a/test/category.js b/test/category.js
index ac84fac..5194689 100644
--- a/test/category.js
+++ b/test/category.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 var chakram = require('chakram'), expect = chakram.expect;
 var env = require('../enviroment');
 const URL = env.URL;
diff --git a/test/geolocation.js b/test/geolocation.js
index 3808bac..66ee565 100644
--- a/test/geolocation.js
+++ b/test/geolocation.js
@@ -1,3 +1,23 @@
+/*
+Copyright (C) 2016 Centro de Computacao Cientifica e Software Livre
+Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
+
+This file is part of SMPPIR-CheckIn.
+
+SMPPIR-CheckIn is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+SMPPIR-CheckIn is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
+*/
+
 var chakram = require('chakram'), expect = chakram.expect;
 var env = require('../enviroment');
 const URL = env.URL;
-- 
GitLab


From 8115d8b6601e070f50d337c6ed9436f55a798271 Mon Sep 17 00:00:00 2001
From: ns17 <ns17@inf.ufpr.br>
Date: Wed, 17 Jun 2020 12:02:26 -0300
Subject: [PATCH 3/5] Adding csv download of geolocation data and category

---
 common/models/category.js    |  61 ++++++++++++++++++++
 common/models/geolocation.js | 105 +++++++++++++++++++++++++++++++++++
 2 files changed, 166 insertions(+)

diff --git a/common/models/category.js b/common/models/category.js
index 101e682..4425169 100644
--- a/common/models/category.js
+++ b/common/models/category.js
@@ -21,4 +21,65 @@ along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
 'use strict';
 
 module.exports = function(Category) {
+    Category.csvexport = function(type, 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
+              var itemsFormatted = [];
+
+              // cabeçalho do csv
+              var headers = {
+                category_name: "category_name",
+                category_description: "category_description",
+                id: "id"
+              };
+
+              categories.forEach((item) => {
+                itemsFormatted.push({
+                  category_name: item.category_name,
+                  category_description: item.category_description,
+                  id: item.id
+                });
+              });
+
+              if (headers) {
+                itemsFormatted.unshift(headers);
+              }
+            
+              var jsonObject = JSON.stringify(itemsFormatted);
+              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 + '\r\n';
+              }   
+            res.send(csv);
+        });
+    };
+
+    Category.remoteMethod('csvexport',
+      {
+        accepts: [
+          {arg: 'type', type: 'string', required: false},
+          {arg: 'res', type: 'object', 'http': {source: 'res'}}
+        ],
+        returns: {},
+        // Para usar o 'type' -> http: {path: '/csv/:type', verb: 'get'}
+        http: {path: '/csv', verb: 'get'},
+      });
 };
diff --git a/common/models/geolocation.js b/common/models/geolocation.js
index 4dff1e0..bbb00b4 100644
--- a/common/models/geolocation.js
+++ b/common/models/geolocation.js
@@ -21,5 +21,110 @@ along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
 'use strict';
 
 module.exports = function(Geolocation) {
+    Geolocation.csvexport = function(type, res, callback ) {
 
+        if (type == "padr" || type == "andr"){
+          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
+                var itemsFormatted = [];
+
+                if (type == "padr"){
+                  // 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"
+                  };
+
+                  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
+                    });
+                  });
+                }
+                
+                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);
+                }
+              
+                var jsonObject = JSON.stringify(itemsFormatted);
+                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 + '\r\n';
+                }   
+              res.send(csv);
+          });
+        }
+        else
+          console.log('Use as rotas /padr ou /andr')
+    };
+
+    Geolocation.remoteMethod('csvexport',
+      {
+        accepts: [
+          {arg: 'type', type: 'string'},
+          {arg: 'res', type: 'object', 'http': {source: 'res'}}
+        ],
+        returns: {},
+        http: {path: '/csv/:type', verb: 'get'}
+      });
 };
-- 
GitLab


From 066cd2c6d3e93208626dc7b83c1c2807a450f834 Mon Sep 17 00:00:00 2001
From: Henrique Varella Ehrenfried <hvehrenfried@inf.ufpr.br>
Date: Thu, 18 Jun 2020 10:02:58 -0300
Subject: [PATCH 4/5] Refactor code

Signed-off-by: Henrique Varella Ehrenfried <hvehrenfried@inf.ufpr.br>
---
 common/models/geolocation.js | 88 ++++++++++++------------------------
 1 file changed, 29 insertions(+), 59 deletions(-)

diff --git a/common/models/geolocation.js b/common/models/geolocation.js
index bbb00b4..f2249bc 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',
-- 
GitLab


From 24f16d41e3e57f249e666ed552c6d7a4a84b9a5c Mon Sep 17 00:00:00 2001
From: ns17 <ns17@inf.ufpr.br>
Date: Thu, 18 Jun 2020 11:43:13 -0300
Subject: [PATCH 5/5] code cleaning

---
 common/models/category.js    | 26 +++++++-------------------
 common/models/geolocation.js |  1 -
 2 files changed, 7 insertions(+), 20 deletions(-)

diff --git a/common/models/category.js b/common/models/category.js
index 4425169..86cbfd6 100644
--- a/common/models/category.js
+++ b/common/models/category.js
@@ -21,7 +21,7 @@ along with SMPPIR-CheckIn.  If not, see <https://www.gnu.org/licenses/>.
 'use strict';
 
 module.exports = function(Category) {
-    Category.csvexport = function(type, res, callback ) {
+    Category.csvexport = function(res, callback ) {
         var datetime = new Date();
         var datetimeFuture = new Date(datetime.getFullYear(), datetime.getMonth(), datetime.getDate()+7)
         res.set('Expires', datetimeFuture+'');
@@ -33,10 +33,9 @@ module.exports = function(Category) {
         res.set('Content-Disposition','attachment;filename=category.csv');
         res.set('Content-Transfer-Encoding','binary');
 
-        Category.find(function(err, categories) {
+        Category.find(function(err, categories)
+        {
               // conversão JSON para CSV
-              var itemsFormatted = [];
-
               // cabeçalho do csv
               var headers = {
                 category_name: "category_name",
@@ -44,19 +43,10 @@ module.exports = function(Category) {
                 id: "id"
               };
 
-              categories.forEach((item) => {
-                itemsFormatted.push({
-                  category_name: item.category_name,
-                  category_description: item.category_description,
-                  id: item.id
-                });
-              });
-
-              if (headers) {
-                itemsFormatted.unshift(headers);
-              }
+              //Adiciona os headers no CSV
+              categories.unshift(headers);
             
-              var jsonObject = JSON.stringify(itemsFormatted);
+              var jsonObject = JSON.stringify(categories);
               var array = typeof jsonObject != 'object' ? JSON.parse(jsonObject) : jsonObject;
               var csv = '';
           
@@ -66,7 +56,7 @@ module.exports = function(Category) {
                   if (line != '') line += ';' // aqui muda o separador do csv
                     line += array[i][index];
                 }
-                csv += line + '\r\n';
+                csv += line + '\n';
               }   
             res.send(csv);
         });
@@ -75,11 +65,9 @@ module.exports = function(Category) {
     Category.remoteMethod('csvexport',
       {
         accepts: [
-          {arg: 'type', type: 'string', required: false},
           {arg: 'res', type: 'object', 'http': {source: 'res'}}
         ],
         returns: {},
-        // Para usar o 'type' -> http: {path: '/csv/:type', verb: 'get'}
         http: {path: '/csv', verb: 'get'},
       });
 };
diff --git a/common/models/geolocation.js b/common/models/geolocation.js
index f2249bc..cbf8f0f 100644
--- a/common/models/geolocation.js
+++ b/common/models/geolocation.js
@@ -36,7 +36,6 @@ module.exports = function(Geolocation) {
 
           Geolocation.find(function(err, geolocations) {            
                 // conversão JSON para CSV
-                var itemsFormatted = [];
                 // cabeçalho do csv  
                 var headers = {
                     category_id: "category_id",
-- 
GitLab