Skip to content

Commit

Permalink
Migrate 'leader' -> 'person' airtable column
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwofford committed Dec 5, 2019
1 parent ae07caf commit fbf4768
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/interactions/checkinNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/interactions/dm.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getInfoForUser, transcript, airGet, airFind } from '../utils'
import { getInfoForUser, transcript, airFind } from '../utils'

const substitutions = (text, targetChannel) =>
new Promise((resolve, reject) => {
const pocRegex = /@poc/
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'))
}
Expand Down
24 changes: 12 additions & 12 deletions src/interactions/leaderAdd.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,34 @@ 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,
'Slack ID': taggedUser.slackUser.id,
'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,
Expand All @@ -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(() => {
Expand Down
8 changes: 4 additions & 4 deletions src/interactions/leaderInvite.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/interactions/leaderList.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
11 changes: 8 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fbf4768

Please sign in to comment.