Skip to content
Open
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
2 changes: 2 additions & 0 deletions modules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const pipeline = require('./pipeline')
const avail = require('./availability')
const vacay = require('./vacation')
const slack = require('./slack')
const journal = require('./journal')


module.exports = {
Expand All @@ -13,4 +14,5 @@ module.exports = {
avail,
vacay,
slack,
journal,
}
96 changes: 96 additions & 0 deletions modules/journal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
const axios = require('axios')

// mock modules
const mockjournals = []
const mocklwd = []
const mockdes = []

const timeout = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const getJournals = async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

you could try to use https://github.com/EQWorks/avail-bot/blob/8541e4ec3f16c1982f8e6c9d51e58c8f1a1b338a/src/availability.js#L47, providing projectName: 'Dev Journal and whatever fields you want in rawParams.opt_fields

await timeout(3000)
return mockjournals
}
const getFieldInfo = async (field) => {
await timeout(3000)
if (field === 'lwd') return mocklwd
if (field === 'des') return mockdes
}
// end of mock modules

const COMMANDS = ['last workday', 'description']

const worker = async ({ command, response_url }) => {
let journalBlocks
const journals = await getJournals()
const lwd = await getFieldInfo('lwd')
const des = await getFieldInfo('des')

if (command === 'last workday') {
journalBlocks = lwd.map((j) => (
{
type: 'section',
text: {
type: 'mrkdwn',
text: [
`> *${j.name}:*`,
j.lastWorkday,
].join('\n'),
},
}
))
} else if (command === 'description') {
journalBlocks = des.map((j) => (
{
type: 'section',
text: {
type: 'mrkdwn',
text: [
`> *${j.name}:*`,
j.description,
].join('\n'),
},
}
))
} else {
journalBlocks = journals.map((j) => (
{
type: 'section',
text: {
type: 'mrkdwn',
text: [
`> *${j.name}:*`,
j.incompleteSubTasks.map((t) => `* ${t.name}`).join('\n'),
j.completedSubTasks.map((t) => `* ${t.name}`).join('\n'),
].join('\n'),
},
}
))
}

return axios.post(response_url, {
response_type: 'in_channel',
blocks: [
{ type: 'divider' },
...journalBlocks
]
})
}

const route = (req, res) => {
const { text = '', response_url } = req.body
if (text !== '' && !COMMANDS.includes(text)) {
return res.status(200).json({
response_type: 'ephemeral',
text: `"${text}" not supported.`
})
}

const payload = { command: text, response_url }
worker(payload).catch(console.error)
return res.status(200).json({
response_type: 'ephemeral',
text: 'Got it! Asking asana for journals now...'
})
}

module.exports = { worker, route }