Skip to content

Commit

Permalink
Add script to sync plans with stripe product ids
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgenwerk committed Nov 21, 2024
1 parent 419ad53 commit 852bb15
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions packages/realm-server/scripts/sync-stripe-products.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import '../setup-logger';
import { PgAdapter } from '@cardstack/postgres';

async function fetchStripeProducts() {
const response = await fetch(
'https://api.stripe.com/v1/products?active=true',
{
method: 'GET',
headers: {
Authorization: `Bearer ${process.env.STRIPE_API_KEY}`,
'Content-Type': 'application/json',
},
},
);
return response.json();
}

async function main() {
let pgAdapter = new PgAdapter();
let products = await fetchStripeProducts();
if (products.error) {
throw new Error(products.error.message);
}
let planQuery = await pgAdapter.execute(`SELECT name FROM plans`);
let planNames = planQuery.map((row) => row.name);

let planStripeIds = products.data.reduce(
(acc: Record<string, string>, product: any) => {
acc[product.name] = product.id;
return acc;
},
{},
) as Record<string, string>;

let updateQueries = planNames.map((planName) => {
let planStripeId = planStripeIds[planName as string];
if (!planStripeId) {
throw new Error(`Plan ${planName} not found in Stripe`);
}
return `UPDATE plans SET stripe_plan_id = '${planStripeId}' WHERE name = '${planName}';`;
});

await pgAdapter.execute(updateQueries.join('\n'));

console.log('Plans were updated using the following queries:');
console.log(updateQueries.join('\n'));
}

main();

0 comments on commit 852bb15

Please sign in to comment.