2024-10-22 04:21:39 -04:00
|
|
|
import RecurlyWrapper from '../../app/src/Features/Subscription/RecurlyWrapper.js'
|
|
|
|
import async from 'async'
|
|
|
|
import { Parser as CSVParser } from 'json2csv'
|
2019-10-21 09:48:27 -04:00
|
|
|
|
|
|
|
const NOW = new Date()
|
|
|
|
|
2019-10-22 07:51:10 -04:00
|
|
|
const slowCallback = (callback, error, data) =>
|
|
|
|
setTimeout(() => callback(error, data), 80)
|
|
|
|
|
|
|
|
const handleAPIError = (type, account, error, callback) => {
|
|
|
|
console.warn(
|
|
|
|
`Errors getting ${type} for account ${account.account_code}`,
|
|
|
|
error
|
|
|
|
)
|
|
|
|
if (typeof error === 'string' && error.match(/429$/)) {
|
|
|
|
return setTimeout(callback, 1000 * 60 * 5)
|
|
|
|
}
|
|
|
|
slowCallback(callback)
|
|
|
|
}
|
|
|
|
|
2019-10-21 09:48:27 -04:00
|
|
|
const getAccountSubscription = (account, callback) =>
|
|
|
|
RecurlyWrapper.getSubscriptions(account.account_code, (error, response) => {
|
|
|
|
if (error) {
|
2019-10-22 07:51:10 -04:00
|
|
|
return handleAPIError('subscriptions', account, error, callback)
|
2019-10-21 09:48:27 -04:00
|
|
|
}
|
2019-10-22 07:51:10 -04:00
|
|
|
slowCallback(callback, null, response.subscriptions[0])
|
2019-10-21 09:48:27 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
const isAccountUsingPaypal = (account, callback) =>
|
|
|
|
RecurlyWrapper.getBillingInfo(account.account_code, (error, response) => {
|
|
|
|
if (error) {
|
2019-10-22 07:51:10 -04:00
|
|
|
return handleAPIError('billing info', account, error, callback)
|
2019-10-21 09:48:27 -04:00
|
|
|
}
|
|
|
|
if (response.billing_info.paypal_billing_agreement_id) {
|
2019-10-22 07:51:10 -04:00
|
|
|
return slowCallback(callback, null, true)
|
2019-10-21 09:48:27 -04:00
|
|
|
}
|
2019-10-22 07:51:10 -04:00
|
|
|
slowCallback(callback, null, false)
|
2019-10-21 09:48:27 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
const printAccountCSV = (account, callback) => {
|
|
|
|
isAccountUsingPaypal(account, (error, isPaypal) => {
|
|
|
|
if (error || !isPaypal) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
getAccountSubscription(account, (error, subscription) => {
|
|
|
|
if (error || !subscription) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
const endAt = new Date(subscription.current_period_ends_at)
|
|
|
|
if (subscription.expires_at) {
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
const csvData = {
|
|
|
|
email: account.email,
|
|
|
|
first_name: account.first_name,
|
|
|
|
last_name: account.last_name,
|
|
|
|
hosted_login_token: account.hosted_login_token,
|
2020-12-15 05:23:54 -05:00
|
|
|
billing_info_url: `https://sharelatex.recurly.com/account/billing_info/edit?ht=${account.hosted_login_token}`,
|
|
|
|
account_management_url: `https://sharelatex.recurly.com/account/${account.hosted_login_token}`,
|
2021-04-14 09:17:21 -04:00
|
|
|
current_period_ends_at: `${endAt.getFullYear()}-${
|
|
|
|
endAt.getMonth() + 1
|
|
|
|
}-${endAt.getDate()}`,
|
2019-10-21 09:48:27 -04:00
|
|
|
current_period_ends_at_segment: parseInt(
|
|
|
|
((endAt - NOW) / 1000 / 3600 / 24 / 365) * 7
|
2021-04-27 03:52:58 -04:00
|
|
|
),
|
2019-10-21 09:48:27 -04:00
|
|
|
}
|
|
|
|
callback(null, csvData)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const printAccountsCSV = callback => {
|
2019-11-12 03:56:08 -05:00
|
|
|
RecurlyWrapper.getPaginatedEndpoint(
|
|
|
|
'accounts',
|
|
|
|
{ state: 'subscriber' },
|
|
|
|
(error, accounts) => {
|
|
|
|
if (error) {
|
|
|
|
return callback(error)
|
|
|
|
}
|
|
|
|
async.mapSeries(accounts, printAccountCSV, (error, csvData) => {
|
|
|
|
csvData = csvData.filter(d => !!d)
|
|
|
|
callback(error, csvData)
|
|
|
|
})
|
2019-10-21 09:48:27 -04:00
|
|
|
}
|
2019-11-12 03:56:08 -05:00
|
|
|
)
|
2019-10-21 09:48:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const csvFields = [
|
|
|
|
'email',
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'hosted_login_token',
|
|
|
|
'billing_info_url',
|
|
|
|
'account_management_url',
|
|
|
|
'current_period_ends_at',
|
2021-04-27 03:52:58 -04:00
|
|
|
'current_period_ends_at_segment',
|
2019-10-21 09:48:27 -04:00
|
|
|
]
|
|
|
|
const csvParser = new CSVParser({ csvFields })
|
|
|
|
|
|
|
|
// print each account
|
|
|
|
printAccountsCSV((error, csvData) => {
|
|
|
|
if (error) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
console.log(csvParser.parse(csvData))
|
|
|
|
process.exit()
|
|
|
|
})
|