Last active
December 1, 2016 17:28
-
-
Save LeoIannacone/1990b87a87f311977f1cfd5883e7db33 to your computer and use it in GitHub Desktop.
intercom
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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