This repository has been archived by the owner on Nov 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
130 lines (108 loc) · 3.49 KB
/
index.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
123
124
125
126
127
128
129
130
require('dotenv').config()
require('winston-daily-rotate-file');
const express = require('express')
const app = express()
const port = process.env.PORT;
const bodyParser = require('body-parser');
app.use(bodyParser.json());
const winston = require('winston');
const expressWinston = require('express-winston');
const { json } = require('body-parser');
const knex = require('knex')({
client: 'pg',
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT
}
});
app.get('/health-check', (req, res) => {
res.status(200);
res.json({ status: 'ok' });
})
app.post('/conversa', async (req, res) => {
const { handle_hashed, started_at } = req.body;
if (!handle_hashed) {
res.status(400);
return res.json({
error: 'param_missing',
param_missing: 'handle_hashed'
});
}
if (!started_at) {
res.status(400);
return res.json({
error: 'param_missing',
param_missing: 'started_at'
});
}
const conversa = await knex('conversa')
.insert({
handle_hashed: handle_hashed,
started_at: started_at
}, 'id');
res.status(201);
return res.json({ id: conversa[0] });
});
app.post('/analytics', async (req, res) => {
const { conversa_id, step_code, last_step_code, tag_code, first_msg_tz, finished, questionnaire_id, json_version_code } = req.body;
console.log(req.body);
let step_code_row = await knex('step_code').select('id').where({
code: step_code,
json_version_code: json_version_code
}).first();
let step_code_id;
if (!step_code_row) {
step_code_row = await knex('step_code').insert({
questionnaire_id: questionnaire_id || undefined,
code: step_code,
json_version_code: json_version_code
}, 'id');
step_code_id = step_code_row[0];
}
else {
step_code_id = step_code_row.id;
}
let last_step_code_id;
if (last_step_code) {
let last_step_code_row = await knex('step_code').select('id').where({
code: last_step_code,
json_version_code: json_version_code
}).first();
if (!last_step_code_row) {
last_step_code_row = await knex('step_code').insert({
questionnaire_id: questionnaire_id || undefined,
code: last_step_code,
json_version_code: json_version_code
}, 'id');
last_step_code_id = last_step_code_row[0];
}
else {
last_step_code_id = last_step_code_row.id;
}
}
const analytics = await knex('analytics').insert({
conversa_id: conversa_id,
step_code_id: step_code_id,
previous_step_code_id: last_step_code_id,
first_msg_tz: first_msg_tz,
tag_code: tag_code,
state: finished
}, 'id');
res.status(200);
res.json({ id: analytics[0] })
});
app.post('/timeout', async (req, res) => {
const { analytics_id, timeout_epoch } = req.body;
const analytics = await knex('analytics').where({ id: analytics_id }).update({
state: 'QUESTIONNAIRE_TIMEOUT',
timeout_at: knex.raw('TO_TIMESTAMP(?)', [timeout_epoch])
}, 'id');
res.status(200);
res.json({ id: analytics[0] });
})
app.listen(port, () => {
console.log(`API listening at http://localhost:${port}`)
})