-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspaceRequestHandler.js
296 lines (244 loc) · 10.2 KB
/
workspaceRequestHandler.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const { app, fs, loadSheet, taskDoc } = require('./app');
module.exports.handleWorkspaceRequest = async ({ command, ack, respond }) => {
// Acknowledge command request
ack();
console.log("/workspace-request command received")
membersCall = await app.client.conversations.members({
channel: process.env.WORKSPACE_CORE_CHANNEL_ID //Channel ID for workspace core
})
//check if user_id is in workspace
if(membersCall.members.includes(command.user_id)){
fs.readFile("messages/request/add_task_modal.json", 'utf8', async (err, data) => {
if (err) throw err;
try {
const modal = JSON.parse(data);
modal.trigger_id = command.trigger_id
modal.view.private_metadata = JSON.stringify({
"direct_request": "true",
"requester_id": command.user_id,
"channel_id": command.channel_id
})
await app.client.views.open(modal);
} catch (e) {
console.log(e)
}
});
return
}
if(command.text.length < 3){
respond("Please provide a description of your request after the command. (e.g. /workspace-request more printer filament)")
return
}
try {
fs.readFile("messages/request/request.json", 'utf8', async (err, data) => {
if (err) throw err;
const messagePayload = JSON.parse(data);
messagePayload.blocks[1].text.text = `*User:* @${command.user_name}`
messagePayload.blocks[2].text.text = `*Request description:* ${command.text}`
messagePayload.metadata.event_payload.requester_username = command.user_name
messagePayload.metadata.event_payload.requester_id = command.user_id
//posts message to workspace core
await app.client.chat.postMessage(messagePayload)
await respond("Your request has been sent to workspace managers. Thanks!")
});
} catch (error) {
console.error(error);
}
};
module.exports.handleAddTask = async ({ body, ack, say}) => {
await ack();
const receivingMetadata = body.message.metadata.event_payload
const requester_username = receivingMetadata.requester_username
const requester_id = receivingMetadata.requester_id
metadata = JSON.stringify({
"requester_username": requester_username,
"requester_id": requester_id,
"approver_username": body.user.name,
"approver_id": body.user.id,
"message_ts": body.container.message_ts,
"message_user_text": body.message.blocks[1].text.text,
"message_description": body.message.blocks[2].text.text,
"channel_id": body.container.channel_id,
"description": body.message.blocks[2].text.text,
"direct_request": "false"
})
fs.readFile("messages/request/add_task_modal.json", 'utf8', async (err, data) => {
if (err) throw err;
try {
const modal = JSON.parse(data);
modal.trigger_id = body.trigger_id
modal.view.private_metadata = metadata
await app.client.views.open(modal);
} catch (e) {
console.log(e)
}
});
}
module.exports.handleResolve = async ({ body, ack, say}) => {
await ack();
const receivingMetadata = body.message.metadata.event_payload
const requester_username = receivingMetadata.requester_username
const requester_id = receivingMetadata.requester_id
metadata = JSON.stringify({
"requester_username": requester_username,
"requester_id": requester_id,
"approver_username": body.user.username,
"approver_id": body.user.id,
"message_ts": body.container.message_ts,
"message_user_text": body.message.blocks[1].text.text,
"message_description": body.message.blocks[2].text.text,
"channel_id": body.container.channel_id,
"description": body.message.blocks[2].text.text,
})
fs.readFile("messages/request/resolve_modal.json", 'utf8', async (err, data) => {
if (err) throw err;
try {
const modal = JSON.parse(data);
modal.trigger_id = body.trigger_id
modal.view.private_metadata = metadata
await app.client.views.open(modal);
} catch (e) {
console.log(error)
}
});
}
module.exports.handleResolveSubmission = async ({ ack, body, view, client, logger }) => {
// Acknowledge the view_submission request
await ack();
console.log("modal submitted")
metadata = JSON.parse(body.view.private_metadata)
console.log(`dropdown options: ${JSON.stringify(body.view.state.values.dropdown.resolve_modal_a)}`)
const dropdown = body.view.state.values.dropdown.resolve_modal_a.selected_option.value
const notifyUser = body.view.state.values.button.resolve_modal_a.selected_options != ""
console.log(`dropdown: ${dropdown}`)
console.log(`button: ${notifyUser}`)
fs.readFile("messages/request/request.json", 'utf8', async (err, data) => { // TODO : some of this can be taken out of the block I think
if (err) throw err;
try {
const message = JSON.parse(data);
message.blocks[1].text.text = metadata.message_user_text
message.blocks[2].text.text = metadata.message_description
message.blocks[4] = {
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": `Marked as "${body.view.state.values.dropdown.resolve_modal_a.selected_option.text.text}" by <@${metadata.approver_id}>`
}
]
};
message.ts = metadata.message_ts
message.channel = metadata.channel_id
await app.client.chat.update(message)
} catch (e) {
console.log(e)
reportError(e, "fetching request.json file to update request text")
}
//notifying requester
if (notifyUser) {
var text = "error"
if (dropdown == "complete") {
text = `Your request has been marked complete by <@${metadata.approver_id}>`
} else if (dropdown == "added_to_order") {
text = `Your request has been added to be ordered by <@${metadata.approver_id}>`
} else if (dropdown == "ordered") {
text = `Your request has been ordered by <@${metadata.approver_id}>`
} else if (dropdown == "notified_facilities") {
text = `<@${metadata.approver_id}> has notified facilities of your request`
} else if (dropdown == "dismissed") {
text = `Your request has been dismissed by <@${metadata.approver_id}>`
}
await app.client.chat.postMessage({
channel: metadata.requester_id,
text: text,
blocks: [{
"type": "section",
"text": {
"type": "mrkdwn",
"text": text
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": metadata.message_description
}
}]
})
}
});
};
module.exports.handleAddTaskSubmission = async ({ ack, body, view, client, logger}) => {
await ack();
metadata = JSON.parse(body.view.private_metadata)
const nameField = body.view.state.values.title.title_input.value
const detailsField = body.view.state.values.details.details_input.value
var independentTask = false
try{ //causes issues if checkbox is not selected
independentTask = body.view.state.values.checkbox.checkboxes.selected_options[0].value == "value-0"
}catch{
independentTask = false
}
console.log(independentTask)
//add to spreadsheet
const taskSheet = await loadSheet(taskDoc, process.env.GOOGLE_SHEET_TASK_TAB) //Workspace Cleaning Todos sheet
var emptyRowIndex = 8
while(taskSheet.getCell(emptyRowIndex, 2).value != null) {
emptyRowIndex++
}
taskId = taskSheet.getCell(emptyRowIndex, 0).value
taskSheet.getCell(emptyRowIndex, 1).value = false
taskSheet.getCell(emptyRowIndex, 2).value = nameField
taskSheet.getCell(emptyRowIndex, 3).value = detailsField
taskSheet.getCell(emptyRowIndex, 4).value = independentTask
await taskSheet.saveUpdatedCells();
if(metadata.direct_request == "false"){ //only if request was asked by normal member
fs.readFile("messages/request/request.json", 'utf8', async (err, data) => {
if (err) throw err;
try {
const modal = JSON.parse(data);
modal.blocks[1].text.text = metadata.message_user_text
modal.blocks[2].text.text = metadata.message_description
modal.blocks[4] = {
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": `Added to tasks by <@${metadata.approver_id}>`
}
]
};
modal.ts = metadata.message_ts
modal.channel = metadata.channel_id
//posts message to workspace core - add this back in to remove buttons
await app.client.chat.update(modal)
} catch (e) {
console.log(e)
}
});
//notify requester
const text = `Your request has been added to the <https://docs.google.com/spreadsheets/d/1H-DUIAoUmsh2QWe0O9Lts67oEdP0FbVtT3xsi7gUWrc/edit#gid=1301847628|todo list> by <@${metadata.approver_id}>`
fs.readFile("messages/request/task_added_notification.json", 'utf8', async (err, data) => {
if (err) throw err;
try {
const modal = JSON.parse(data);
modal.channel = metadata.requester_id
modal.text = text
modal.blocks[0].text.text = text
modal.blocks[1].text.text = metadata.message_description
modal.blocks[2].text.text = `*Task title:* ${nameField}`
modal.blocks[3].text.text = `*Task description:* ${detailsField}`
await app.client.chat.postMessage(modal)
} catch (e) {
console.log(e)
}
});
}else{
await app.client.chat.postEphemeral({
channel: metadata.channel_id,
text: `Task added to todo list.`,
user: metadata.requester_id,
})
}
};