From a438af4452a60ec158fed926c3d7ed5b786dee79 Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Wed, 18 Sep 2019 10:18:14 -0400 Subject: [PATCH] Add logic for creating new leaders on airtable --- src/interactions/leaderAdd.js | 38 +++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index d0771f9f..63d97730 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -8,20 +8,46 @@ const interactionLeaderAdd = (bot, message) => { return } - getInfoForUser(user).then(({ leader, club }) => { - if (!leader) { - console.log(`${user} isn't a leader, so I told them this was restricted`) + 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 (!club) { - console.log(`${user} doesn't have a club`) + if (!commandUser.club) { + console.log(`${commandUser.user} doesn't have a club`) bot.whisper(message, transcript('leaderAdd.invalidClub')) return } - console.log(message) + const taggedUserID = message.text.match(/\<@(.*)\|/)[1] + getInfoForUser(taggedUserID) + .then(taggedUser => { + // if user doesn't exist + if (!taggedUser.leader) { + 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, + } + 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 + console.log('TODO: validate leader can be assigned to club') + }) + .catch(err => { + throw err + }) }) }