Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add script to backfill invoice payment methods

Kiran K70ad5357b598
diff --git a/apps/web/scripts/migrations/backfill-invoice-payment-method.ts b/apps/web/scripts/migrations/backfill-invoice-payment-method.ts
new file mode 100644
index 0000000000..e9f2ee37d7
--- /dev/null
+++ b/apps/web/scripts/migrations/backfill-invoice-payment-method.ts
@@ -0,0 +1,67 @@
+import { STRIPE_PAYMENT_METHOD_NORMALIZATION } from "@/lib/partners/constants";
+import { prisma } from "@dub/prisma";
+import { Invoice } from "@dub/prisma/client";
+import "dotenv-flow/config";
+
+// migrations/backfill-invoice-payment-method
+async function main() {
+ const invoices = await prisma.invoice.findMany({
+ where: {
+ paymentMethod: null,
+ stripeChargeMetadata: {
+ not: {},
+ },
+ },
+ take: 100,
+ });
+
+ console.log(`Found ${invoices.length} invoices to backfill`);
+
+ const invoicesToUpdate: Pick<Invoice, "id" | "paymentMethod">[] = [];
+
+ for (const invoice of invoices) {
+ const chargeMetadata = invoice.stripeChargeMetadata as any;
+ const paymentMethodType = chargeMetadata?.payment_method_details?.type;
+
+ if (!paymentMethodType) {
+ console.log(`No payment method type found for invoice ${invoice.id}`);
+ continue;
+ }
+
+ const normalizedPaymentMethod =
+ STRIPE_PAYMENT_METHOD_NORMALIZATION[
+ paymentMethodType as keyof typeof STRIPE_PAYMENT_METHOD_NORMALIZATION
+ ];
+
+ if (!normalizedPaymentMethod) {
+ console.log(
+ `Unknown payment method type ${paymentMethodType} for invoice ${invoice.id}`,
+ );
+ continue;
+ }
+
+ invoicesToUpdate.push({
+ id: invoice.id,
+ paymentMethod: normalizedPaymentMethod,
+ });
+ }
+
+ console.table(invoicesToUpdate);
+
+ await Promise.allSettled(
+ invoicesToUpdate.map((invoice) =>
+ prisma.invoice.update({
+ where: {
+ id: invoice.id,
+ },
+ data: {
+ paymentMethod: invoice.paymentMethod,
+ },
+ }),
+ ),
+ );
+
+ console.log("Backfill completed");
+}
+
+main();