Skip to content

Commit

Permalink
Handle leader-add command w/ correctly tagged leader
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwofford committed Sep 18, 2019
1 parent 8042e0d commit b78d2ff
Showing 1 changed file with 62 additions and 58 deletions.
120 changes: 62 additions & 58 deletions src/interactions/leaderAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,72 +13,76 @@ const interactionLeaderAdd = (bot, message) => {
return
}

getInfoForUser(user).then(commandUser => {
if (!commandUser.leader) {
console.log(
`${commandUser.user} isn't a leader, so I told them this was restricted`
)
bot.whisper(message, transcript('leaderAdd.invalidUser'))
return
}
getInfoForUser(user)
.then(commandUser => {
if (!commandUser.leader) {
console.log(
`${commandUser.user} isn't a leader, so I told them this was restricted`
)
bot.whisper(message, transcript('leaderAdd.invalidUser'))
return
}

if (!commandUser.club) {
console.log(`${commandUser.user} doesn't have a club`)
bot.whisper(message, transcript('leaderAdd.invalidClub'))
return
}

if (!commandUser.club) {
console.log(`${commandUser.user} doesn't have a club`)
bot.whisper(message, transcript('leaderAdd.invalidClub'))
return
}
if (commandUser.club.fields['Slack Channel ID'] != channel) {
console.log(`${user} doesn't own channel ${channel}`)
bot.whisper(message, transcript('leaderAdd.invalidChannel'))
return
}

if (commandUser.club.fields['Slack Channel ID'] != channel) {
console.log(`${user} doesn't own channel ${channel}`)
bot.whisper(message, transcript('leaderAdd.invalidChannel'))
return
}
const taggedUserID = (message.text.match(/\<@(.*)\|/) || [])[1]

const taggedUserID = message.text.match(/\<@(.*)\|/)[1]
getInfoForUser(taggedUserID)
.then(taggedUser => {
console.log('found tagged user')
if (taggedUser.slackUser.is_bot) {
throw new Error('bots cannot be leaders')
}
if (!taggedUser.leader) {
// if user doesn't exist
const profile = taggedUser.slackUser.profile
const fields = {
Email: taggedUser.slackUser.profile.email,
'Slack ID': taggedUser.slackUser.id,
'Full Name': profile.real_name || profile.display_name,
getInfoForUser(taggedUserID)
.then(taggedUser => {
console.log('found tagged user')
if (taggedUser.slackUser.is_bot) {
throw new Error('bots cannot be leaders')
}
console.log(fields)
return airCreate('Leaders', fields).catch(err => {
console.error('Ran into issue creating new leader airtable record')
throw err
})
}
return taggedUser
})
.then(taggedUser => {
// ensure we can assign the leader to this club
const clubs = taggedUser.leader.fields['Clubs'] || []
if (clubs.includes(commandUser.club.id)) {
bot.whisper(message, transcript('leaderAdd.alreadyLeader'))
return
}
clubs.push(commandUser.club.id)
return airPatch('Leaders', taggedUser.leader.id, { Clubs: clubs }).then(
() => {
if (!taggedUser.leader) {
// if user doesn't exist
const profile = taggedUser.slackUser.profile
const fields = {
Email: taggedUser.slackUser.profile.email,
'Slack ID': taggedUser.slackUser.id,
'Full Name': profile.real_name || profile.display_name,
}
console.log(fields)
return airCreate('Leaders', fields).catch(err => {
console.error(
'Ran into issue creating new leader airtable record'
)
throw err
})
}
return taggedUser
})
.then(taggedUser => {
// ensure we can assign the leader to this club
const clubs = taggedUser.leader.fields['Clubs'] || []
if (clubs.includes(commandUser.club.id)) {
bot.whisper(message, transcript('leaderAdd.alreadyLeader'))
return
}
clubs.push(commandUser.club.id)
return airPatch('Leaders', taggedUser.leader.id, {
Clubs: clubs,
}).then(() => {
bot.whisper(
message,
transcript('leaderAdd.success', { taggedUserID, channel })
)
}
)
})
.catch(err => {
console.error(err)
bot.whisper(message, transcript('errors.general', { err }))
})
})
})
})
})
.catch(err => {
console.error(err)
bot.whisper(message, transcript('errors.general', { err }))
})
}

export default interactionLeaderAdd

0 comments on commit b78d2ff

Please sign in to comment.