Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

1.4 KB
Raw Blame
import type { Prisma } from "@rallly/database";
import { prisma } from "@rallly/database";

import { createStripeClient } from "../lib/stripe";

const secretKey = process.env.STRIPE_SECRET_KEY;

if (!secretKey) {
  throw new Error("STRIPE_SECRET_KEY is not set");
}

const stripe = createStripeClient({ secretKey });

(async function syncPaymentMethods() {
  let processed = 0;
  let failed = 0;

  const users = await prisma.user.findMany({
    select: {
      id: true,
      customerId: true,
      email: true,
    },
    where: {
      customerId: {
        not: null,
      },
    },
  });

  console.info(`πŸš€ Syncing ${users.length} users...`);

  for (const user of users) {
    if (!user.customerId) continue;
    try {
      const paymentMethods = await stripe.customers.listPaymentMethods(
        user.customerId,
      );

      await prisma.paymentMethod.createMany({
        data: paymentMethods.data.map((paymentMethod) => ({
          id: paymentMethod.id,
          userId: user.id,
          type: paymentMethod.type,
          data: paymentMethod[paymentMethod.type] as Prisma.JsonObject,
        })),
      });

      console.info(`βœ… Payment methods synced for user ${user.email}`);
      processed++;
    } catch (error) {
      console.error(
        `❌ Failed to sync payment methods for user ${user.email}:`,
        error,
      );
      failed++;
    }
  }

  console.info(`πŸ“Š Sync complete: ${processed} processed, ${failed} failed`);
})();