From fbf4768617803ef38f552502fff5323d990dcc8a Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Thu, 5 Dec 2019 13:28:43 -0500 Subject: [PATCH] Migrate 'leader' -> 'person' airtable column --- src/interactions/checkinNotification.js | 6 +++--- src/interactions/dm.js | 4 ++-- src/interactions/leaderAdd.js | 24 ++++++++++++------------ src/interactions/leaderInvite.js | 8 ++++---- src/interactions/leaderList.js | 2 +- src/utils.js | 11 ++++++++--- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/src/interactions/checkinNotification.js b/src/interactions/checkinNotification.js index 330d0761..75283ef0 100644 --- a/src/interactions/checkinNotification.js +++ b/src/interactions/checkinNotification.js @@ -12,9 +12,9 @@ const interactionCheckinNotification = (bot = initBot(), message) => { .then(club => { const pocAirtableID = club.fields.POC if (pocAirtableID) { - airFind('Leaders', `RECORD_ID() = '${pocAirtableID}'`) - .then(leader => { - user = leader.fields['Slack ID'] + airFind('People', `RECORD_ID() = '${pocAirtableID}'`) + .then(poc => { + user = poc.fields['Slack ID'] console.log( transcript('checkinNotification.log.foundPoc', { channel, diff --git a/src/interactions/dm.js b/src/interactions/dm.js index 4953a0df..ab6ac47a 100644 --- a/src/interactions/dm.js +++ b/src/interactions/dm.js @@ -1,4 +1,4 @@ -import { getInfoForUser, transcript, airGet, airFind } from '../utils' +import { getInfoForUser, transcript, airFind } from '../utils' const substitutions = (text, targetChannel) => new Promise((resolve, reject) => { @@ -6,7 +6,7 @@ const substitutions = (text, targetChannel) => if (text.match(pocRegex)) { airFind('Clubs', 'Slack Channel ID', targetChannel) .then(club => - airFind('Leaders', `'${club.fields.POC}' = RECORD_ID()`).then(poc => { + airFind('People', `'${club.fields.POC}' = RECORD_ID()`).then(poc => { if (!poc || !poc.fields) { reject(new Error('No POC for club')) } diff --git a/src/interactions/leaderAdd.js b/src/interactions/leaderAdd.js index ad1bf07c..46ef523d 100644 --- a/src/interactions/leaderAdd.js +++ b/src/interactions/leaderAdd.js @@ -39,10 +39,10 @@ const interactionLeaderAdd = (bot, message) => { .then(taggedUser => { console.log('found tagged user') if (taggedUser.slackUser.is_bot) { - throw new Error('bots cannot be leaders') + throw new Error('bots cannot be club leaders') } - if (!taggedUser.leader) { - // if user doesn't exist + if (!taggedUser.person) { + // if user doesn't exist in Airtable const profile = taggedUser.slackUser.profile const fields = { Email: taggedUser.slackUser.profile.email, @@ -50,23 +50,23 @@ const interactionLeaderAdd = (bot, message) => { 'Full Name': profile.real_name || profile.display_name, } console.log(fields) - return airCreate('Leaders', fields) - .then(taggedLeader => { - return taggedLeader + return airCreate('Person', fields) + .then(taggedPerson => { + return taggedPerson }) .catch(err => { console.error( - 'Ran into issue creating new leader airtable record' + 'Ran into issue creating new Person record in Airtable' ) throw err }) } else { - return taggedUser.leader + return taggedUser.person } }) - .then(taggedLeader => { - // ensure we can assign the leader to this club - const clubs = taggedLeader.fields['Clubs'] || [] + .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, @@ -75,7 +75,7 @@ const interactionLeaderAdd = (bot, message) => { return } clubs.push(commandUser.club.id) - return airPatch('Leaders', taggedLeader.id, { + return airPatch('People', taggedPerson.id, { Clubs: clubs, }) .then(() => { diff --git a/src/interactions/leaderInvite.js b/src/interactions/leaderInvite.js index 5f788f73..5d0124a6 100644 --- a/src/interactions/leaderInvite.js +++ b/src/interactions/leaderInvite.js @@ -1,11 +1,11 @@ import { getInfoForUser, airFind, initBot, transcript } from '../utils' const LEADERS_CHANNEL = 'GAE0FFNFN' -const invitePromise = leaderRecordID => +const invitePromise = personRecordID => new Promise((resolve, reject) => { - airFind('Leaders', `'${leaderRecordID}' = RECORD_ID()`) - .then(leader => { - const slackID = leader['Slack ID'] + airFind('Person', `'${personRecordID}' = RECORD_ID()`) + .then(person => { + const slackID = person['Slack ID'] initBot(true).api.groups.invite( { user: slackID, diff --git a/src/interactions/leaderList.js b/src/interactions/leaderList.js index f706b9a4..a968d217 100644 --- a/src/interactions/leaderList.js +++ b/src/interactions/leaderList.js @@ -29,7 +29,7 @@ const interactionLeaderList = (bot, message) => { console.log(club.fields) airGet( - 'Leaders', + 'People', `OR(${club.fields['Leaders'].map(l => `RECORD_ID()='${l}'`).join(',')})` ) .then(leaders => { diff --git a/src/utils.js b/src/utils.js index ef5d5589..5ea5d3a8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -80,7 +80,7 @@ export const airGet = ( // usage: // for key/value lookup: `airGet('Clubs', 'Slack Channel ID', slackChannelID)` // for formula lookup: `airGet('Clubs', '{Slack Channel ID} = BLANK()')` - // for all records: `airGet('Leaders')` + // for all records: `airGet('People')` const timestamp = Date.now() @@ -142,8 +142,13 @@ export const getInfoForUser = user => userRecord(user).then(userRecord => (results.userRecord = userRecord)), airGet('Badges', `FIND('${user}', {People Slack IDs})`).then(badges => results.badges = badges), // Get the leader from the user - airFind('Leaders', 'Slack ID', user) - .then(leader => (results.leader = leader)) + airFind('People', 'Slack ID', user) + .then(person => (results.person = person)) + .then(() => { + if (results.person.fields['Clubs']) { + results.leader = results.person + } + }) // Then club from leader .then(() => { if (!results.leader) return null