From 30f9e33951de6fef1cf0abf3867dc559af6f91d0 Mon Sep 17 00:00:00 2001 From: thejsj Date: Tue, 20 Sep 2016 08:11:07 -0700 Subject: [PATCH 1/2] Add script for fixing subscriptions --- lib/util/runnable-api-client.js | 1 + scripts/fix-subscriptions.js | 72 +++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 scripts/fix-subscriptions.js diff --git a/lib/util/runnable-api-client.js b/lib/util/runnable-api-client.js index 2329bad..0b22a73 100644 --- a/lib/util/runnable-api-client.js +++ b/lib/util/runnable-api-client.js @@ -1,4 +1,5 @@ 'use strict' +require('loadenv')() const Promise = require('bluebird') const RunnableClient = require('@runnable/api-client') diff --git a/scripts/fix-subscriptions.js b/scripts/fix-subscriptions.js new file mode 100644 index 0000000..a1769bc --- /dev/null +++ b/scripts/fix-subscriptions.js @@ -0,0 +1,72 @@ + +'use strict' +require('loadenv')() + +const Promise = require('bluebird') +const moment = require('moment') + +const bigPoppa = require('util/big-poppa') +const stripe = require('util/stripe') +const runnableAPI = require('util/runnable-api-client') + +// DRY RUN BY DEFAULT +const isDryRun = process.env.DRY_RUN !== 'false' +const log = require('util/logger').child({ module: 'scripts/fix-subscription', isDryRun }) + +let orgIds = [] +let orgsSuccsefullyUpdated = [] + +const logOrgs = (message) => { + return (orgs) => log.trace({ orgIds: orgs.map(x => x.id), numberOfOrgs: orgs.length }, message) +} + +Promise.resolve() + .then(() => runnableAPI.login()) + .then(function getAllOrgs () { + log.info('getAllOrgs') + return bigPoppa.getOrganizations({}) + }) + .tap(logOrgs('All orgs')) + .filter(function getSubscription (org) { + orgIds.push(org.name) + log.trace({ orgId: org.id, stripeCustomerId: org.stripeCustomerId }, 'Fetch subscription') + return stripe.getSubscriptionForOrganization(org.stripeCustomerId) + .then(sub => org.subscription = sub) + .return(false) + .catch(err => { + log.trace({ err, orgId: org.id, stripeCustomerId: org.stripeCustomerId }, 'No subscription found') + return true + }) + }) + .tap(logOrgs('Orgs with no subscriptions')) + .map(function createNewSubscription (org) { + return stripe.getPlanIdForOrganizationBasedOnCurrentUsage(org.githubId) + .then(planId => { org.planId = planId }) + .return(org) + }) + .tap(logOrgs('Orgs with plans')) + .map(function createNewSubscription (org) { + let trialEnd = moment().add(1, 'minute') + let createObject = Object.assign({ + customer: org.stripeCustomerId, + plan: org.planId, + trial_end: +trialEnd.format('X') + }, stripe._getUpdateObjectForUsers(org.users)) + createObject.metadata.subscriptionCreatedAfterDeleted = trialEnd.toISOString() + log.trace({ createObject, org }, 'Creating subscription') + orgsSuccsefullyUpdated.push(org.name) + if (isDryRun) { + return true + } + log.trace('Actually updating subscription') + return stripe.stripeClient.subscriptions.create(createObject) + }) + .then(() => runnableAPI.logout()) + .then(() => { + log.info({ + numberOfOrgs: orgIds.length, + numberOfUpdatedOrgs: orgsSuccsefullyUpdated.length, + orgIds, + orgsSuccsefullyUpdated + }, 'Finished') + }) From c756d91dcc8401a72cdae2e536dc4c8610054f00 Mon Sep 17 00:00:00 2001 From: thejsj Date: Tue, 20 Sep 2016 14:57:11 -0700 Subject: [PATCH 2/2] Fix script --- scripts/fix-subscriptions.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/scripts/fix-subscriptions.js b/scripts/fix-subscriptions.js index a1769bc..2ef1084 100644 --- a/scripts/fix-subscriptions.js +++ b/scripts/fix-subscriptions.js @@ -31,7 +31,7 @@ Promise.resolve() orgIds.push(org.name) log.trace({ orgId: org.id, stripeCustomerId: org.stripeCustomerId }, 'Fetch subscription') return stripe.getSubscriptionForOrganization(org.stripeCustomerId) - .then(sub => org.subscription = sub) + .then(sub => { org.subscription = sub }) .return(false) .catch(err => { log.trace({ err, orgId: org.id, stripeCustomerId: org.stripeCustomerId }, 'No subscription found') @@ -39,10 +39,14 @@ Promise.resolve() }) }) .tap(logOrgs('Orgs with no subscriptions')) - .map(function createNewSubscription (org) { + .filter(function createNewSubscription (org) { return stripe.getPlanIdForOrganizationBasedOnCurrentUsage(org.githubId) .then(planId => { org.planId = planId }) - .return(org) + .return(true) + .catch(() => { + log.error({ org }, 'No plan found for organization') + return false + }) }) .tap(logOrgs('Orgs with plans')) .map(function createNewSubscription (org) {