Skip to content

Commit

Permalink
Merge pull request #8 from hackclub/admin-leader-actions
Browse files Browse the repository at this point in the history
Allow permissioned ambassadors to add leaders to clubs
  • Loading branch information
maxwofford authored Jan 18, 2020
2 parents 1a4f927 + b2d9594 commit e2276bc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 91 deletions.
115 changes: 27 additions & 88 deletions src/interactions/leaderAdd.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,40 @@
import { getInfoForUser, airCreate, airPatch, transcript } from '../utils'
import { getInfoForUser, airPatch, transcript, airFind } from '../utils'

const interactionLeaderAdd = (bot, message) => {
const interactionLeaderAdd = async (bot, message) => {
const { user, text, channel } = message

if (text === '' || text === 'help') {
const taggedUserID = (text.match(/\<@(.*)\|/) || [])[1]
if (!taggedUserID) {
bot.replyPrivateDelayed(message, transcript('leaderAdd.help'))
return
}

return getInfoForUser(user)
.then(commandUser => {
if (!commandUser.leader) {
console.log(
`${commandUser.user} isn't a leader, so I told them this was restricted`
)
bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidUser'))
return
}
const taggedUser = await getInfoForUser(taggedUserID)
const commandUser = await getInfoForUser(user)
const recipientClub = await airFind('Clubs', 'Slack Channel ID', channel)

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

if (commandUser.club && commandUser.club.id != recipientClub.id) {
// A leader is trying to permission someone to a channel that's not their
// club channel
throw transcript('leaderAdd.invalidChannel')
}

if (commandUser.club.fields['Slack Channel ID'] != channel) {
console.log(`${user} doesn't own channel ${channel}`)
bot.replyPrivateDelayed(message, transcript('leaderAdd.invalidChannel'))
return
}
const taggedUserClubs = taggedUser.fields['Clubs'] || []
if (taggedUserClubs.includes(recipientClub)) {
throw transcript('leaderAdd.alreadyLeader')
}

const taggedUserID = (message.text.match(/\<@(.*)\|/) || [])[1]
if (!taggedUserID) {
throw new Error('Invalid Slack user')
}
await airPatch('People', taggedUser.id, {
Clubs: [...taggedUserClubs, recipientClub],
})

return getInfoForUser(taggedUserID)
.then(taggedUser => {
console.log('found tagged user')
if (taggedUser.slackUser.is_bot) {
throw new Error('bots cannot be club leaders')
}
if (!taggedUser.person) {
// if user doesn't exist in Airtable
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('Person', fields)
.then(taggedPerson => {
return taggedPerson
})
.catch(err => {
console.error(
'Ran into issue creating new Person record in Airtable'
)
throw err
})
} else {
return taggedUser.person
}
})
.then(taggedPerson => {
// ensure we can assign the person as a leader to this club
const clubs = taggedPerson.fields['Clubs'] || []
if (clubs.includes(commandUser.club.id)) {
bot.replyPrivateDelayed(
message,
transcript('leaderAdd.alreadyLeader')
)
return
}
clubs.push(commandUser.club.id)
return airPatch('People', taggedPerson.id, {
Clubs: clubs,
})
.then(() => {
bot.replyPrivateDelayed(
message,
transcript('leaderAdd.success', { taggedUserID, channel })
)
})
.catch(err => {
throw err
})
})
.catch(err => {
throw err
})
})
.catch(err => {
console.error(err)
bot.replyPrivateDelayed(message, transcript('errors.general', { err }))
})
bot.replyPrivateDelayed(
message,
transcript('leaderAdd.success', { taggedUserID, channel })
)
}

export default interactionLeaderAdd
6 changes: 6 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ export const getInfoForUser = user =>
airFind('People', 'Slack ID', user).then(
person => (results.person = person)
),
airFind('Ambassadors', 'Slack ID', user).then(ambassador => {
results.ambassador = ambassador
if (ambassador) {
results.permissionedAmbassador = ambassador.fields['Permissioned']
}
}),
])
.then(async () => {
if (!results.person && results.slackUser) {
Expand Down
5 changes: 2 additions & 3 deletions src/utils/transcript.yml
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,10 @@ leaderAdd:
Registers a new leader to your club. Ex. \`/leader-add @orpheus\`
_Needs to be run by a club leader in their own club channel_
Additionally, you can see a list of who is already a leader with \`/leader-list\`
invalidUser: Only club leaders can use this command.
invalidClub: I couldn't find your club.
invalidAuth: I couldn't find your club. Only registered leaders or operations team members can run this command.
invalidChannel: This isn't your club channel. Please run this in a channel you lead.
success: And... done! I've added <@${this.taggedUserID}> to the club in <#${this.channel}>.
alreadyLeader: They're already a leader in your club!
success: And... done! I've added <@${this.taggedUserID}> to the club in <#${this.channel}>.
leaderList:
invalidUser: Only club leaders can use this command.
invalidClub: I couldn't find your club.
Expand Down

0 comments on commit e2276bc

Please sign in to comment.