-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
122 lines (92 loc) · 3.29 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* Main App Entry Point.
*/
/**
* Module dependencies.
*/
const express = require('express');
const bodyParser = require('body-parser');
const logger = require('morgan');
const chalk = require('chalk');
const errorHandler = require('errorhandler');
const nano = require('nano');
/**
* Create Express Server.
*/
const app = express();
/**
* Express configuration.
*/
app.set('port', 5555);
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(__dirname + '/public'));
/**
* Controllers.
*/
const assessmentController = require('./controllers/assessment');
const resultController = require('./controllers/result');
const workflowController = require('./controllers/workflow');
const csvController = require('./controllers/generate_csv');
const changesController = require('./controllers/changes');
const tripController = require('./controllers/trip');
/**
* Hook data processing function to couchDB changes feed.
*/
const dbConfig = require('./config');
const dbQuery = require('./utils/dbQuery');
const processChangedDocument = require('./controllers/changes').processChangedDocument;
const generateCSV = require('./controllers/generate_csv').generateCSV;
const BASE_DB = nano(dbConfig.base_db);
const feed = BASE_DB.follow({ since: 'now', include_docs: true });
feed.on('change', (resp) => {
feed.pause();
processChangedDocument(resp, dbConfig.base_db, dbConfig.result_db);
setTimeout(function() { feed.resume() }, 500);
});
feed.on('error', (err) => err);
feed.follow();
/**
* App routes.
*/
app.get('/', (req, res) => res.render('index'));
app.post('/', (req, res) => {
const resultDbUrl = dbConfig.result_db;
const resultId = req.body.workflowId;
const resultYear = req.body.year;
let resultMonth = req.body.month;
resultMonth = resultMonth ? resultMonth : false;
let queryId = resultMonth && resultYear ? `${resultId}_${resultYear}_${resultMonth}` : resultId;
dbQuery.retrieveDoc(resultId, resultDbUrl)
.then(async (docHeaders) => {
const result = await dbQuery.getProcessedResults(queryId, resultDbUrl);
const csvFile = await generateCSV(docHeaders, result);
const downloadFile = __dirname + `/${csvFile}`;
res.download(downloadFile);
})
.catch((err) => res.send(err));
});
app.post('/assessment', assessmentController.all);
app.post('/assessment/headers/all', assessmentController.generateAll);
app.post('/assessment/headers/:id', assessmentController.generateHeader);
app.post('/result', resultController.all);
app.post('/assessment/result/:id', resultController.processResult);
app.post('/workflow', workflowController.all);
app.post('/workflow/headers/all', workflowController.generateAll);
app.post('/workflow/headers/:id', workflowController.generateHeader);
app.post('/workflow/result/:id', tripController.processResult);
app.post('/generate_csv/:id/:year?/:month?', csvController.generate);
app.post('/tangerine_changes', changesController.changes);
app.post('/get_processed_results/:id', dbQuery.processedResultsById);
/**
* Error Handler.
*/
app.use(errorHandler());
/**
* Start Express server.
*/
app.listen(app.get('port'), () => {
console.log('%s Tangerine Reporting server listening on port %d.', chalk.green('✓'), app.get('port'));
});
module.exports = app;