This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
170 lines (142 loc) · 5.79 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
if (process.env.NODE_ENV !== 'production') require('dotenv').config();
const express = require('express');
const path = require('path');
const fs = require('fs');
const app = express();
const helmet = require('helmet');
const express_enforces_ssl = require('express-enforces-ssl');
const bodyParser = require('body-parser');
const fetch = require('node-fetch');
const PORT = process.env.PORT || 2020;
const Util = require('./bin/lib/utils');
const Sheet = require('./bin/lib/sheets');
const Structure = require('./bin/lib/structure');
const Input = require('./bin/lib/inputManagement');
const DataReq = require('./bin/lib/dataRequest');
if (process.env.NODE_ENV === 'production') {
app.use(helmet());
app.enable('trust proxy');
app.use(express_enforces_ssl());
const googleTokenPath = path.resolve(`${__dirname}/keyfile.json`);
fs.writeFileSync(googleTokenPath, process.env.GOOGLE_CREDS);
}
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// ----------------------------------------------------------------------------
// Endpoint for handling /add slash commands from Slack
// ----------------------------------------------------------------------------
app.post('/add', async (req, res) => {
const private_metadata = {
channel: req.body.channel,
response_url: req.body.response_url
};
const payload = await Structure.build(req.body.trigger_id, private_metadata);
const options = {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json',
Authorization: 'Bearer ' + process.env.SLACK_TOKEN
}
};
return fetch(`https://slack.com/api/views.open`, options)
.then(response => response.json())
.then(data => {
console.log('D::', data);
if (data.ok) {
res.end();
} else {
res.send('We have issues processing the request, contact the app admin');
}
})
.catch(err => console.log(err));
});
// ----------------------------------------------------------------------------
// Endpoint for receiving data submissions/requests from Slack
// ----------------------------------------------------------------------------
app.post('/submit', async (req, res) => {
const response = JSON.parse(req.body.payload);
if (response.hasOwnProperty('view')) {
return modalResponse(req, res);
} else {
return summaryResponse(req, res);
}
});
// ----------------------------------------------------------------------------
// Build and send Slack modal form for user(s) to add Project Status Updates
// ----------------------------------------------------------------------------
async function modalResponse(req, res) {
const response = JSON.parse(req.body.payload);
const user = response.user.name;
const viewID = response.view.id;
const values = response.view.state.values;
const submission = Input.submit(viewID, user, values);
const response_msg_process = Structure.processing(submission.productName);
const private_metadata = JSON.parse(response.view.private_metadata);
const response_url = private_metadata.response_url;
// 1. Send a Slack message to the user confirming reception
// of the add entry request
Util.postJSONData(response_url, response_msg_process);
if (submission) {
Sheet.write(submission, data => {
// Deletes view so future requests can get a fresh version
Input.deleteView(viewID);
// 2. Send a Slack message confirming that the contents
// of the request was added to the data store
const response_msg_confirm = Structure.confirm(data);
Util.postJSONData(response_url, response_msg_confirm);
});
}
// 3. Send a Slack message to clear the Slack modal form window
return res.json(Structure.clear());
}
// ----------------------------------------------------------------------------
// Endpoint for handling /summary slash commands from Slack
// ----------------------------------------------------------------------------
app.post('/summary', async (req, res) => {
const parameter = req.body.text;
const response_url = req.body.response_url;
const titles = await DataReq.getReportTitles();
if (parameter && titles.includes(parameter)) {
// Send specifically requested Report
Sheet.read(parameter, 'value', true, (data, headers, worksheet_id) => {
postSummary(response_url, parameter, headers, data, worksheet_id);
});
return res.json(Structure.processingReport(parameter));
} else if (parameter === '') {
// Send list of current available Reports
return res.json(Structure.summaryList(titles));
} else {
return res.json(Structure.summaryList(titles, 'No summary by that name'));
}
});
// ----------------------------------------------------------------------------
// Build and send Ascii table summaries of data reports
// ----------------------------------------------------------------------------
async function summaryResponse(req, res) {
const parsedPayload = JSON.parse(req.body.payload);
const parameter = parsedPayload.actions[0].selected_option.text.text;
const response_url = parsedPayload.response_url;
Sheet.read(parameter, 'value', true, (data, headers, worksheet_id) => {
postSummary(response_url, parameter, headers, data, worksheet_id);
});
return res.json(Structure.processingReport(parameter));
}
// ----------------------------------------------------------------------------
// Build summary data from specified report to be sent to Slack user
// ----------------------------------------------------------------------------
async function postSummary(url, name, headers, data, worksheet_id) {
const rows = [];
data.forEach(item => {
const row = [];
for (const property in item) {
if (headers.includes(property)) {
row.push(item[property]);
}
}
rows.push(row);
});
const summary = await Structure.summary(name, headers, rows, worksheet_id);
Util.postJSONData(url, summary);
}
app.listen(PORT, () => console.log(`Example app listening on port ${PORT}!`));