diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index 46ef523d..1ca85ca6 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -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 diff --git a/src/utils/index.js b/src/utils/index.js index 694ae1e9..430bca44 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -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) { diff --git a/src/utils/transcript.yml b/src/utils/transcript.yml index 83f810ff..1570fb54 100644 --- a/src/utils/transcript.yml +++ b/src/utils/transcript.yml @@ -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.