Skip to content

Instantly share code, notes, and snippets.

@LeoIannacone
Last active December 1, 2016 17:28
Show Gist options
  • Save LeoIannacone/1990b87a87f311977f1cfd5883e7db33 to your computer and use it in GitHub Desktop.
Save LeoIannacone/1990b87a87f311977f1cfd5883e7db33 to your computer and use it in GitHub Desktop.
intercom
const Intercom = require('intercom-client')
const SEGMENT_ID = process.env.INTERCOM_SEGMENT_ID
const TOKEN = process.env.INTERCOM_TOKEN
const client = new Intercom.Client({token: TOKEN})
const oldUsers = []
const bulkLimit = 50
const deleteUsers = () => {
if (oldUsers.length === 0) {
return
}
const bulkDeletion = oldUsers.splice(0, 50).map(id => ({ delete: { id }}))
console.log('Removing', bulkLimit, 'users - still', oldUsers.length, 'to remove')
client.users.bulk(bulkDeletion, (err, res) => {
if (err) {
console.error(err)
}
deleteUsers()
})
}
const getUsers = (err, res) => {
if (err) {
console.error(err)
process.exit(1)
}
const {pages, users} = res.body
if (pages.total_pages === 0) {
console.log('No user found. Exit.')
return
}
console.log('Fetching page', pages.page, 'over', pages.total_pages)
users.forEach(u => oldUsers.push(u.id))
if (pages.next) {
client.nextPage(pages, getUsers)
} else {
deleteUsers()
}
}
client.users.listBy({segment_id: SEGMENT_ID}, getUsers)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment