Skip to content

Commit

Permalink
feat: add script to sync with stripe subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Oct 12, 2024
1 parent 2c12f5d commit a749341
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/lib/storage/jobs/helpers/updateStripeSubscriptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { getStripe } from '../../../integrations/stripe';
import { getDatabase } from '../../../../data_layer';
import Stripe from 'stripe';

const stripe = getStripe();

const updateStripeSubscriptions = async () => {
let hasMore = true;
let startingAfter: string | undefined = undefined;
const database = getDatabase();

while (hasMore) {
const subscriptions: Stripe.ApiList<Stripe.Subscription> =
await stripe.subscriptions.list({
limit: 100,
status: 'active',
starting_after: startingAfter,
});

for (const subscription of subscriptions.data) {
if (typeof subscription.customer === 'string') {
const customer = await stripe.customers.retrieve(subscription.customer);

if ('email' in customer) {
const sub = await database
.table('subscriptions')
.where({ email: customer.email })
.first();

if (sub && !sub.active) {
console.info('Updating customer', customer.id);
await database
.table('customers')
.where({ email: customer.email })
.update({ active: true });
} else {
console.info('Customer already active', customer.id);
}
} else {
console.warn('Customer does not have an email');
}
}
}

hasMore = subscriptions.has_more;
if (hasMore) {
startingAfter = subscriptions.data[subscriptions.data.length - 1].id;
}
}
};

if (require.main === module) {
updateStripeSubscriptions().catch(console.error);
}

0 comments on commit a749341

Please sign in to comment.