forked from oleh-polishchuk/asana-subscriber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (98 loc) · 4.3 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
require('dotenv').config();
const { Logger } = require('channelape-logger');
const { LogLevel } = require('channelape-sdk');
const LOGGER = new Logger('subscriberMain', LogLevel.INFO);
const express = require('express');
const app = express();
const schedule = require('node-schedule');
const bodyParser = require('body-parser');
const { deleteAllTasksInProject, getTaskById, addProjectOnSubtask, subscribeToTaskAddedWebhook, createSectionOnProject, getUserById, getProjectMembershipById, getProjectById, subscribeToProjectMembershipWebhook, aggregateProjects } = require("./services/asana-service");
const { addEmailToHarvestProject } = require("./services/harvest-service");
const { isEstablishingWebHookProcess, handleHandShake } = require('./services/webhook-service');
/* Application */
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.post('/receive-webhook/task-added', (req, res) => {
if (isEstablishingWebHookProcess(req)) {
return handleHandShake(req, res);
}
const events = req.body && req.body.events;
if (events) {
LOGGER.info(`Received ${events.length} task-added webhook events`);
events.map(event => {
if(event.resource.resource_type === 'task' && event.action === 'added' && event.parent.resource_type === 'task'){
getTaskById(event.resource.gid).then(task => {
if(task.parent != null){
LOGGER.debug('its a subtask with parent resource_type of task')
getTaskById(task.parent.gid).then(parentTask => {
addProjectOnSubtask(task, parentTask);
});
}
})
}else{
LOGGER.debug('its not a task or task with parent with resource_type of task')
}
});
}
res.sendStatus(200);
});
app.post('/receive-webhook/project-added', (req, res) => {
if (isEstablishingWebHookProcess(req)) {
return handleHandShake(req, res);
}
const events = req.body && req.body.events;
if (events) {
LOGGER.info(`Received ${events.length} project-added webhook events`);
events.map(event => {
if(event.action === 'added' && event.resource.resource_type === 'project' && event.parent.resource_type === 'workspace'){
const projectId = event.resource.gid;
subscribeToTaskAddedWebhook(projectId, event.resource.name);
subscribeToProjectMembershipWebhook(projectId, event.resource.name);
createSectionOnProject(projectId, 'Subtasks');
}
});
}
res.sendStatus(200);
});
app.post('/receive-webhook/project-archive-change', (req, res) => {
if (isEstablishingWebHookProcess(req)) {
return handleHandShake(req, res);
}
const events = req.body && req.body.events;
if (events) {
events.map(event => {
LOGGER.info(event);
});
}
res.sendStatus(200);
//todo archive in harvest
});
app.post('/receive-webhook/project-membership', (req, res) => {
if (isEstablishingWebHookProcess(req)) {
return handleHandShake(req, res);
}
const events = req.body && req.body.events;
if (events) {
LOGGER.info(`Received ${events.length} project-membership webhook event(s)`);
events.map(event => {
if(event.action === 'added' && event.resource.resource_type === 'project_membership'){
const projectMembershipId = event.resource.gid;
getProjectMembershipById(projectMembershipId).then(projectMembership => {
const uid = projectMembership.user.gid;
getUserById(uid).then(user => {
getProjectById(event.parent.gid).then(project => {
addEmailToHarvestProject(user.email, project.name);
});
});
});
}
});
}
res.sendStatus(200);
});
app.listen(process.env.PORT, () => LOGGER.info(`Webhook Asana Subscriber listening on port ${process.env.PORT}!`));
schedule.scheduleJob('12 23 * * *', function(){
LOGGER.info('Running webhook daily scheduler to fix missing webhooks');
aggregateProjects();
});