Skip to content

Commit 1b9afac

Browse files
authored
Merge branch 'master' into revjtanton/HOTFIX-temp-user-creation
2 parents 992fe9d + dc8ffab commit 1b9afac

File tree

5 files changed

+111
-3
lines changed

5 files changed

+111
-3
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"express": "4.17.1",
4343
"express-request-id": "1.4.1",
4444
"helmet": "3.22.0",
45+
"json2csv": "^5.0.0",
4546
"jsonwebtoken": "8.5.1",
4647
"lodash": "^4.17.15",
4748
"mocha": "7.1.1",

src/routes/csv.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Router } from 'express';
2+
import validator from 'validator';
3+
import utils from '../utils';
4+
import { parseAsync } from "json2csv";
5+
6+
const router = new Router();
7+
router.use(utils.authMiddleware)
8+
9+
// Gets a data dump from the passed in model (if it exists).
10+
router.get('/:model_type', async (req, res) => {
11+
let code;
12+
let message;
13+
const modelType = req.params.model_type;
14+
try {
15+
if(req.context.models.hasOwnProperty(modelType)){
16+
//todo add filtering
17+
const results = await req.context.models[modelType].findAll({raw:true});
18+
19+
const processedResults = await utils.processResults(results, modelType);
20+
21+
if(results.length !== 0){
22+
message = await parseAsync(JSON.parse(JSON.stringify(processedResults)), Object.keys(results[0]), {});
23+
}
24+
code = 200;
25+
} else {
26+
message = "model type is invalid"
27+
code = 422;
28+
}
29+
} catch (e) {
30+
console.error(e);
31+
code = 500;
32+
}
33+
34+
return utils.response(res, code, message);
35+
});
36+
37+
export default router;

src/routes/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import user from './user';
22
// import userRole from './user-role';
33
import contact from './contact';
44
import entity from './entity';
5+
import csv from './csv';
56
// Exports object of routes we import above. Add to this if you're adding new routes.
67
export default {
78
user,
89
// userRole,
910
contact,
10-
entity
11+
entity,
12+
csv
1113
};

src/utils/index.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,36 @@ const validateEmails = async emails => {
109109
return true;
110110
}
111111

112+
/**
113+
* Processes model results based on type
114+
*
115+
* @param {Array} results
116+
* @param {String} modelType
117+
*
118+
* @return {processedResults}
119+
*/
120+
const processResults = async (results, modelType) => {
121+
switch (modelType){
122+
case "Entity":
123+
let processedResults = [];
124+
for(let result of results){
125+
//todo expand conditional checking as checkin object becomes more mature
126+
if(result["checkIn"] !== null) {
127+
result["checkIn"] = result["checkIn"].checkIns[0];
128+
}
129+
processedResults = [...processedResults, result];
130+
}
131+
return processedResults;
132+
default:
133+
return results;
134+
};
135+
}
136+
112137
export default {
113138
formatTime,
114139
authMiddleware,
115140
response,
116141
encryptPassword,
117-
validateEmails
142+
validateEmails,
143+
processResults
118144
};

swagger.json

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
}, {
2929
"name" : "contact",
3030
"description" : "Operations related to contacts"
31-
} ],
31+
}, {
32+
"name": "csv",
33+
"description": "Operations related to CSV data dumps"
34+
}],
3235
"paths" : {
3336
"/health" : {
3437
"get" : {
@@ -702,6 +705,45 @@
702705
}
703706
}
704707
}
708+
},
709+
"/csv/{model_type}": {
710+
"get": {
711+
"tags" : [ "csv" ],
712+
"summary" : "returns a full csv data dump based on the model type",
713+
"description" : "By passing the model type, you can dump all of the current data in csv format.",
714+
"parameters" : [ {
715+
"name" : "model_type",
716+
"in" : "path",
717+
"description" : "type of model you want a csv data dump for",
718+
"required" : true,
719+
"style" : "simple",
720+
"explode" : false,
721+
"schema" : {
722+
"type" : "string"
723+
}
724+
} ],
725+
"responses" : {
726+
"200" : {
727+
"description" : "the csv data dump",
728+
"content" : {
729+
"text/plain": {
730+
"schema": {
731+
"type": "string"
732+
}
733+
}
734+
}
735+
},
736+
"401" : {
737+
"description" : "Unauthorized"
738+
},
739+
"422" : {
740+
"description" : "Invalid input"
741+
},
742+
"500" : {
743+
"description" : "Server error"
744+
}
745+
}
746+
}
705747
}
706748
},
707749
"components" : {

0 commit comments

Comments
 (0)