-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
75 lines (65 loc) · 2.6 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
require('dotenv').config();
const fs = require('fs');
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const { log } = require("./services/logger");
const { changedTaskFilter } = require('./services/utils');
const { getTaskById, moveTaskToColumn } = require("./services/asana-service");
const { isEstablishingWebHookProcess, handleHandShake } = require('./services/webhook-service');
/* Application */
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
/* Routes */
app.get('/', (req, res) => fs.readFile('/tmp/default.log', 'utf8', (err, logs) => {
res.render('index', {
version: '1.0.1',
logs: logs,
});
}));
app.post('/receive-webhook/core-brands-ndc-7654', (req, res) => {
if (isEstablishingWebHookProcess(req)) {
return handleHandShake(req, res);
}
const events = req.body && req.body.events;
if (events) {
log(`Received ${events.length} webhook events`);
const changedTasksEvents = events.filter(changedTaskFilter);
if (changedTasksEvents) {
log(`Found ${changedTasksEvents.length} events with type: task and action: changed`);
changedTasksEvents.map(event => {
getTaskById(event.resource).then(task => {
if (task && task.completed) {
moveTaskToColumn(task, 'Core Brands NDC', 'Archive');
moveTaskToColumn(task, 'Core Sprint C (NDC)', 'Archive', true);
}
})
});
}
}
res.sendStatus(200);
});
app.post('/receive-webhook/core-sprint-c-ndc-7654', (req, res) => {
if (isEstablishingWebHookProcess(req)) {
return handleHandShake(req, res);
}
const events = req.body && req.body.events;
if (events) {
log(`Received ${events.length} webhook events`);
const changedTasksEvents = events.filter(changedTaskFilter);
if (changedTasksEvents) {
log(`Found ${changedTasksEvents.length} events with type: task and action: changed`);
changedTasksEvents.map(event => {
getTaskById(event.resource).then(task => {
if (task && task.completed) {
moveTaskToColumn(task, 'Core Sprint C (NDC)', 'Archive');
moveTaskToColumn(task, 'Core Brands NDC', 'Archive', true);
}
})
});
}
}
res.sendStatus(200);
});
app.listen(process.env.PORT, () => log(`Example app listening on port ${process.env.PORT}!`));