Skip to content

Commit

Permalink
Attempt to read & update from json in airtable
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwofford committed Sep 6, 2019
1 parent fc08c57 commit 0e14955
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
18 changes: 14 additions & 4 deletions src/interactions/meetingTutorial.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { airRecord } from '../utils'
import { airRecord, airPatch } from '../utils'

const interactionMeetingTutorial = (bot, message) => {
const { user } = message
console.log('Running meeting tutorial')

// bot.whisper(message, `Hey <@${user}>! Welcome to the check-in tutorial. First I'll need to know when your first meeting is. Run this command to let me know: \`/meeting-time next wednesday at 4 PM\``)
// bot.whisper(message, "(If you don't know when your first meeting will be, just set it for a couple weeks for now so we can get through the tutorial, then you can change it later)")
airRecord(user).then(userRecord => {
console.log(userRecord)
const oldRecordData = JSON.parse(userRecord.fields['Data'])
if (oldRecordData['Flag: Initiated tutorial']) {
bot.whisper(message, `Hmmm.... looks like we've already started this tutorial`)
} else {
bot.whisper(message, `Hey <@${user}>! Welcome to the check-in tutorial. First I'll need to know when your first meeting is. Run this command to let me know: \`/meeting-time next wednesday at 4 PM\``)
bot.whisper(message, "(If you don't know when your first meeting will be, just set it for a couple weeks for now so we can get through the tutorial, then you can change it later)")
}
const newRecordData = JSON.parse(userRecord.fields['Data'])
newRecordData['Flag: Initiated tutorial'] = true
airPatch('Orpheus', userRecord.id, JSON.stringify(newRecordFields)).then((newRecord) => {
console.log('patching...')
}).catch(err => { throw err })
userRecord.update({tutorialIntroFlag: true})
}).catch(err => {
console.error(err)
bot.whisper(`Hmmmm... I'm getting \`${err}\` and I'm pretty sure that's not right`)
Expand Down
24 changes: 19 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,28 @@ export const recordMeeting = (club, meeting, cb) => {
)
}

export const airRecord = (user) =>
// const buildUserRecord = (airtableRecord) => ({
// update: (attributes) => new Promise((resolve, reject) => {
// airGet('Orpheus', `RECORD_ID() = ${airtableRecord.id}`).then(initialRecord => {
// initialRecord.
// })
// airPatch('Orpheus', `RECORD_ID() = ${airtableRecord.id}`, newValues).then(

// ).catch(err => reject(err))
// resolve(buildUserRecord(record))
// }),
// id: airtableRecord.id
// })
const buildUserRecord = r => r

export const userRecord = (user) =>
new Promise((resolve, reject) => {
console.log(`*I'm looking up an airRecord for "${user}"*`)
airFind('Orpheus', 'User', user).then(userRecord => {
if (userRecord) {
airFind('Orpheus', 'User', user).then(record => {
if (record) {
console.log(`*I found an airRecord for "${user}"*`)
// if it already exists, return it
resolve(userRecord)
resolve(buildUserRecord(record))
} else {
console.log(`*I didn't find an airRecord for "${user}", so I'm creating a new one*`)
// if it doesn't exist, create one...
Expand All @@ -136,7 +150,7 @@ export const airRecord = (user) =>
if (err) { throw err }
console.log(`*I created a new airRecord for "${user}"*`)
// ... & return it
resolve(record)
resolve(buildUserRecord(record))
})
}
}
Expand Down

0 comments on commit 0e14955

Please sign in to comment.