Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[G2M] Bday modal #46

Merged
merged 5 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 70 additions & 30 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ const axios = require('axios')

const modules = require('./modules')
const { verifySlack } = require('./modules/middleware')
const { bdayInteractive } = require('./modules/bday-interactive')
const { lambda, getFuncName } = require('./modules/util')

const { DEPLOYED } = process.env
const app = express()

const { updateTask } = require('@eqworks/avail-bot')

const rawBodyBuffer = (req, _, buf, encoding) => {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8')
}
}

app.use(bodyParser.urlencoded({verify: rawBodyBuffer, extended: true }))
app.use(bodyParser.urlencoded({ verify: rawBodyBuffer, extended: true }))
app.use(bodyParser.json({ verify: rawBodyBuffer }))

app.get('/', (_, res, next) => {
Expand Down Expand Up @@ -83,37 +84,76 @@ e.g.
*/
app.use('/interactive', (req, res) => {
const parsed = JSON.parse(req.body.payload)
const {
// type,
response_url,
// user,
// channel: { id, name },
// TODO when would this be .length > 1
actions: [{
value: action_value,
// style,
// type: action_type,
// text: { text },
}],

const {
type,
view: {
id,
hash,
callback_id,
private_metadata,
state: { values }, // only available from submission
blocks,
},
actions = [],
user: { id: sender },
// much more avb inside, check documentation
} = parsed
// value standard: '[module] // param-1 // ...param-N'
const [module, taskId, customFieldId, value] = action_value.split(' // ')
if (module === 'vacay') {
updateTask(taskId, { custom_fields: { [customFieldId]: value }})
.then(res => {
const { assignee: { name }, start_on, due_on, custom_fields } = res
const status = custom_fields.find(o => o.name === 'Status').enum_value.name
const date = start_on ? `${start_on} - ${due_on}` : due_on
axios.post(response_url, {
text: `Updated *${name}'s* vacation on *${date}* to *${status}*`,
mrkdwn: true,
response_type: 'ephemeral',
replace_original: false,
})
Comment on lines -100 to -113
Copy link
Contributor Author

@tlowande tlowande Oct 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find any command that was using this block and the /vacay is not even working for me, lemme know if I missed something

Copy link
Contributor Author

@tlowande tlowande Oct 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I just realized that there's a problem at worker. From lambda logs:

    "errorType": "TypeError",
    "errorMessage": "Cannot read property 'gid' of null",
    "stack": [
        "TypeError: Cannot read property 'gid' of null",
        "    at vacation.reduce.true (/var/task/modules/vacation.js:87:24)",
        "    at Array.reduce (<anonymous>)",
        "    at worker (/var/task/modules/vacation.js:84:16)",
        "    at async Runtime.module.exports.handler (/var/task/slack-worker.js:6:3)"
    ]
}

so I'm not sure if it was even using the interactive endpoint in the first place


if (callback_id === 'bday') {
// manipulate data received from submission
const { data = {}, errors = {} } = bdayInteractive({ type, values })
if (Object.values(errors).length) {
return res.status(200).json({ response_action: 'errors', errors })
}
const {
ref,
response_url,
command,
channel_id,
} = JSON.parse(private_metadata)

const payload = {
ref,
command,
type,
view_id: id,
hash,
response_url,
data,
blocks,
action: actions[0] || [],
channel_id,
sender
}

const { worker } = modules['bday']

if (DEPLOYED) {
lambda.invoke({
FunctionName: getFuncName('slack'),
InvocationType: 'Event',
Payload: JSON.stringify({ type: 'bday', payload }),
}, (err) => {
if (err) {
console.error(err)
return res.status(200).json({ response_type: 'ephemeral', text: 'Failed to process bday command' })
}
if (type === 'view_submission') {
return res.status(200).json({ 'response_action': 'clear' })
}
return res.sendStatus(200)
})
} else {
worker(payload).catch(console.error)
if (type === 'view_submission') {
// needs to have only <response_action> for modals submission
return res.status(200).json({ 'response_action': 'clear' })
}
// modal interactions need to have acknowledgement
return res.sendStatus(200)
}
}
return res.status(200) // mandatory response
})

// catch-all error handler
Expand Down
Loading