Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save eashman/99d4d49eea252349d2e461a88f6873c9 to your computer and use it in GitHub Desktop.

Select an option

Save eashman/99d4d49eea252349d2e461a88f6873c9 to your computer and use it in GitHub Desktop.

Revisions

  1. @zthomas zthomas revised this gist Oct 14, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion intercom-delete-old-users.js
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ const async = require('async-q')
    //WARNING: you can only have one scroll working at once. you need to wait for that scroll to clear to try again
    //NOTE: list API will only page up to 10k results. It won't list all the users
    //NOTE: scroll API has no way to control the throttle it's easy to hit the rate limit, use a controlled queue.
    const maxAge = 30 // days
    const maxAge = 21 // days

    // Bulk delete option
    client.users.scroll.each({}, function(d) {
  2. @zthomas zthomas revised this gist Oct 14, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions intercom-delete-old-users.js
    Original file line number Diff line number Diff line change
    @@ -31,10 +31,14 @@ client.users.scroll.each({}, function(d) {
    if (deleteUsers.length) {
    client.users.bulk(deleteUsers).then(resp => {
    console.log('[deleted users]', deleteUsers.length)
    // NOTE: if any one operation fails it will return an error but will continue to process the other jobs
    // ref: https://developers.intercom.com/reference#bulk-apis
    }).catch(err => console.error('[bulk delet failed]', err))
    }
    });

    // --- Example of a async queue based operation --- //

    // const async = require('async-q')
    //NOTE: Rate limit is at 500 requests per minute
    // const delay = 120 //ms
  3. @zthomas zthomas revised this gist Oct 14, 2016. 1 changed file with 32 additions and 5 deletions.
    37 changes: 32 additions & 5 deletions intercom-delete-old-users.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    // License: MIT, feel free to use it!

    const Intercom = require('intercom-client');
    const appId = 'APP_ID'
    @@ -21,14 +22,17 @@ client.users.scroll.each({}, function(d) {
    // Example of custom attributes checking
    let tier = user.custom_attributes.tier
    // Delete use if last seen is more than maxAge
    let deprecate = Boolean(age >= maxAge && tier != 'professional' && tier != 'print')
    let deprecate = Boolean(age >= maxAge && (!tier || tier === 'starter'))
    if (deprecate) console.log('[deleting user]', user.email, age, tier || '')
    else console.log('[skipped user]', user.email, age, tier || '')
    return deprecate
    }).map(user => ({delete: user}))

    client.users.bulk(deleteUsers).then(resp => {
    console.log('[deleted users]', deleteUsers.length)
    }).catch(err => console.error('[bulk delet failed]', err))
    if (deleteUsers.length) {
    client.users.bulk(deleteUsers).then(resp => {
    console.log('[deleted users]', deleteUsers.length)
    }).catch(err => console.error('[bulk delet failed]', err))
    }
    });

    // const async = require('async-q')
    @@ -68,4 +72,27 @@ client.users.scroll.each({}, function(d) {
    // // console.log('[user age]', age)
    // }
    // })
    // });
    // });

    /**
    Copyright (c) 2010-2016 Thomas Zhou
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    **/
  4. @zthomas zthomas revised this gist Oct 14, 2016. 1 changed file with 26 additions and 0 deletions.
    26 changes: 26 additions & 0 deletions intercom-delete-old-users.js
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,32 @@ const apiKey = 'APP_KEY'
    const client = new Intercom.Client(appId, apiKey);
    const async = require('async-q')

    //REF: https://developers.intercom.com/reference#iterating-over-all-users
    //WARNING: you can only have one scroll working at once. you need to wait for that scroll to clear to try again
    //NOTE: list API will only page up to 10k results. It won't list all the users
    //NOTE: scroll API has no way to control the throttle it's easy to hit the rate limit, use a controlled queue.
    const maxAge = 30 // days

    // Bulk delete option
    client.users.scroll.each({}, function(d) {
    const users = d.body.users

    let deleteUsers = users.filter(user => {
    let diff = Date.now()/1000 - user.updated_at
    let age = Math.floor(diff/60/60/24)
    // Example of custom attributes checking
    let tier = user.custom_attributes.tier
    // Delete use if last seen is more than maxAge
    let deprecate = Boolean(age >= maxAge && tier != 'professional' && tier != 'print')
    if (deprecate) console.log('[deleting user]', user.email, age, tier || '')
    return deprecate
    }).map(user => ({delete: user}))

    client.users.bulk(deleteUsers).then(resp => {
    console.log('[deleted users]', deleteUsers.length)
    }).catch(err => console.error('[bulk delet failed]', err))
    });

    // const async = require('async-q')
    //NOTE: Rate limit is at 500 requests per minute
    // const delay = 120 //ms
  5. @zthomas zthomas revised this gist Oct 14, 2016. 1 changed file with 36 additions and 38 deletions.
    74 changes: 36 additions & 38 deletions intercom-delete-old-users.js
    Original file line number Diff line number Diff line change
    @@ -5,43 +5,41 @@ const apiKey = 'APP_KEY'
    const client = new Intercom.Client(appId, apiKey);
    const async = require('async-q')

    //REF: https://developers.intercom.com/reference#iterating-over-all-users
    //WARNING: you can only have one scroll working at once. you need to wait for that scroll to clear to try again
    //NOTE: list API will only page up to 10k results. It won't list all the users
    //NOTE: scroll API has no way to control the throttle it's easy to hit the rate limit, use a controlled queue.
    const maxAge = 30 // days
    // const async = require('async-q')
    //NOTE: Rate limit is at 500 requests per minute
    const delay = 120 //ms
    const concurrency = 1
    // const delay = 120 //ms
    // const concurrency = 1
    //
    // const queue = async.queue((user) => {
    // return client.users.delete(user).then(resp => {
    // console.log('[deleted user]', user.email, resp.status,
    // ` ${(new Date(user.updated_at*1000)).toLocaleDateString()}`)
    // }).then(() => {
    // // add a delay between each request
    // return new Promise(resolve => setTimeout(resolve, delay))
    // }).catch(err => {
    // // Ignore errors and continue
    // // NOTE: remove this catch if you want delete errors to stop the entire script
    // console.error(err)
    // })
    // }, concurrency)

    const queue = async.queue((user) => {
    return client.users.delete(user).then(resp => {
    console.log('[deleted user]', user.email, resp.status,
    ` ${(new Date(user.updated_at*1000)).toLocaleDateString()}`)
    }).then(() => {
    // add a delay between each request
    return new Promise(resolve => setTimeout(resolve, delay))
    }).catch(err => {
    // Ignore errors and continue
    // NOTE: remove this catch if you want delete errors to stop the entire script
    console.error(err)
    })
    }, concurrency)

    client.users.scroll.each({}, function(d) {
    const users = d.body.users

    console.log('[scrolling... queue length]', queue.length(), '[adding users]', users.length);
    users.forEach(user => {
    let diff = Date.now()/1000 - user.updated_at
    let age = Math.floor(diff/60/60/24)
    // Example of custom attributes checking
    let tier = user.custom_attributes.tier
    // Delete use if last seen is more than maxAge
    if (age >= maxAge && tier != 'professional' && tier != 'print') {
    queue.push({id: user.id, updated_at: user.updated_at, email: user.email})
    } else {
    // console.log('[user age]', age)
    }
    })
    });
    // Using queue and deleting users one by one.
    // client.users.scroll.each({}, function(d) {
    // const users = d.body.users
    //
    // console.log('[scrolling... queue length]', queue.length(), '[adding users]', users.length);
    // users.forEach(user => {
    // let diff = Date.now()/1000 - user.updated_at
    // let age = Math.floor(diff/60/60/24)
    // // Example of custom attributes checking
    // let tier = user.custom_attributes.tier
    // // Delete use if last seen is more than maxAge
    // if (age >= maxAge && tier != 'professional' && tier != 'print') {
    // queue.push({id: user.id, updated_at: user.updated_at, email: user.email})
    // } else {
    // if (tier) console.log('[user tier]', user.email, tier)
    // // console.log('[user age]', age)
    // }
    // })
    // });
  6. @zthomas zthomas revised this gist Oct 14, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion intercom-delete-old-users.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,8 @@ const async = require('async-q')

    //REF: https://developers.intercom.com/reference#iterating-over-all-users
    //WARNING: you can only have one scroll working at once. you need to wait for that scroll to clear to try again
    //NOTE: scroll API will not work well for this usecase, there's no way to throttle the request
    //NOTE: list API will only page up to 10k results. It won't list all the users
    //NOTE: scroll API has no way to control the throttle it's easy to hit the rate limit, use a controlled queue.
    const maxAge = 30 // days
    //NOTE: Rate limit is at 500 requests per minute
    const delay = 120 //ms
  7. @zthomas zthomas revised this gist Oct 14, 2016. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions intercom-delete-old-users.js
    Original file line number Diff line number Diff line change
    @@ -34,12 +34,13 @@ client.users.scroll.each({}, function(d) {
    users.forEach(user => {
    let diff = Date.now()/1000 - user.updated_at
    let age = Math.floor(diff/60/60/24)
    // Example of custom attributes checking
    let tier = user.custom_attributes.tier
    // Delete use if last seen is more than maxAge
    if (age >= maxAge) {
    console.log(user)
    if (age >= maxAge && tier != 'professional' && tier != 'print') {
    queue.push({id: user.id, updated_at: user.updated_at, email: user.email})
    } else {
    // console.log('[user age]', age)
    }
    })
    });
    });
  8. @zthomas zthomas revised this gist Oct 14, 2016. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions intercom-delete-old-users.js
    Original file line number Diff line number Diff line change
    @@ -33,11 +33,13 @@ client.users.scroll.each({}, function(d) {
    console.log('[scrolling... queue length]', queue.length(), '[adding users]', users.length);
    users.forEach(user => {
    let diff = Date.now()/1000 - user.updated_at
    let age = Math.floor(diff/60/60/24)
    // Delete use if last seen is more than maxAge
    if (diff/60/60/24 >= maxAge) {
    if (age >= maxAge) {
    console.log(user)
    queue.push({id: user.id, updated_at: user.updated_at, email: user.email})
    } else {
    console.log('[user age]', Math.floor(diff/60/60/24))
    // console.log('[user age]', age)
    }
    })
    });
  9. @zthomas zthomas created this gist Oct 14, 2016.
    43 changes: 43 additions & 0 deletions intercom-delete-old-users.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@

    const Intercom = require('intercom-client');
    const appId = 'APP_ID'
    const apiKey = 'APP_KEY'
    const client = new Intercom.Client(appId, apiKey);
    const async = require('async-q')

    //REF: https://developers.intercom.com/reference#iterating-over-all-users
    //WARNING: you can only have one scroll working at once. you need to wait for that scroll to clear to try again
    //NOTE: scroll API will not work well for this usecase, there's no way to throttle the request
    const maxAge = 30 // days
    //NOTE: Rate limit is at 500 requests per minute
    const delay = 120 //ms
    const concurrency = 1

    const queue = async.queue((user) => {
    return client.users.delete(user).then(resp => {
    console.log('[deleted user]', user.email, resp.status,
    ` ${(new Date(user.updated_at*1000)).toLocaleDateString()}`)
    }).then(() => {
    // add a delay between each request
    return new Promise(resolve => setTimeout(resolve, delay))
    }).catch(err => {
    // Ignore errors and continue
    // NOTE: remove this catch if you want delete errors to stop the entire script
    console.error(err)
    })
    }, concurrency)

    client.users.scroll.each({}, function(d) {
    const users = d.body.users

    console.log('[scrolling... queue length]', queue.length(), '[adding users]', users.length);
    users.forEach(user => {
    let diff = Date.now()/1000 - user.updated_at
    // Delete use if last seen is more than maxAge
    if (diff/60/60/24 >= maxAge) {
    queue.push({id: user.id, updated_at: user.updated_at, email: user.email})
    } else {
    console.log('[user age]', Math.floor(diff/60/60/24))
    }
    })
    });