-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (33 loc) · 929 Bytes
/
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
require('dotenv').config();
const dateformat = require('dateformat');
const Koa = require('koa');
const app = new Koa();
const { Pool, Client } = require('pg')
const connectionString = process.env.POSTGRESQL_MESSAGES;
function addDays(date, days) {
var result = new Date(date);
result.setDate(result.getDate() + days);
return result;
}
async function get(ctx) {
const client = new Client({ connectionString });
client.connect();
var lastWeek = dateformat(addDays(new Date(), -7), 'yyyy-mm-dd');
try {
const messages = `
SELECT data
FROM messages
where data->>'endDate' > '${lastWeek}'
order by data->>'startDate'`;
var res = await client.query(messages);
client.end();
ctx.status = 200;
ctx.body = res.rows;
}
catch (err) {
client.end();
ctx.throw(err);
}
}
app.use(get);
app.listen(process.env.PORT || 5000);