-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from hackclub/admin-leader-actions
Allow permissioned ambassadors to add leaders to clubs
- Loading branch information
Showing
3 changed files
with
35 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters