Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🔨 Add script for normalizing stripe subscription metadata
Luke Vella2474888b140f
diff --git a/packages/billing/package.json b/packages/billing/package.jsonindex 1fdcda1d3..6fa32aa15 100644--- a/packages/billing/package.json+++ b/packages/billing/package.json@@ -7,6 +7,9 @@"./next": "./src/next/index.ts",".": "./src/index.ts"},+ "scripts": {+ "normalize-subscription-metadata": "dotenv -e ../../.env -- tsx ./src/scripts/normalize-metadata.ts"+ },"dependencies": {"@rallly/ui": "*","stripe": "^13.2.0",diff --git a/packages/billing/src/scripts/normalize-subscription-metadata.ts b/packages/billing/src/scripts/normalize-subscription-metadata.tsnew file mode 100644index 000000000..8067421c7--- /dev/null+++ b/packages/billing/src/scripts/normalize-subscription-metadata.ts@@ -0,0 +1,65 @@+/**+ * This script will go through all subscriptions and add the userId to the metadata.+ */+import { stripe } from "../lib/stripe";+import { prisma } from "@rallly/database";++async function getSubscriptionsWithMissingMetadata(+ starting_after?: string,+): Promise<string[]> {+ const res: string[] = [];++ const subscriptions = await stripe.subscriptions.list({+ limit: 100,+ starting_after,+ });+ subscriptions.data.forEach((subscription) => {+ if (!subscription.metadata.userId) {+ res.push(subscription.id);+ }+ });+ if (subscriptions.has_more) {+ return [+ ...res,+ ...(await getSubscriptionsWithMissingMetadata(+ subscriptions.data[subscriptions.data.length - 1].id,+ )),+ ];+ } else {+ return res;+ }+}++async function normalizeSubscriptionMetadata() {+ const subscriptions = await getSubscriptionsWithMissingMetadata();++ console.log(+ `Found ${subscriptions.length} subscriptions with missing metadata`,+ );++ for (const subscriptionId of subscriptions) {+ const user = await prisma.user.findFirst({+ select: {+ id: true,+ },+ where: {+ subscriptionId: subscriptionId,+ },+ });++ if (!user) {+ console.log("User not found for subscription", subscriptionId);+ continue;+ }++ await stripe.subscriptions.update(subscriptionId, {+ metadata: {+ userId: user.id,+ },+ });++ console.log("Updated subscription", subscriptionId);+ }+}++normalizeSubscriptionMetadata();