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.

2.4 KB
Raw Blame
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { stripe } from "@/lib/stripe";
import { InvoiceSchema } from "@/lib/zod/schemas/invoices";
import { APP_DOMAIN } from "@dub/utils";
import { NextResponse } from "next/server";
import * as z from "zod/v4";

const querySchema = z.object({
  type: z
    .enum(["subscription", "domainRenewal"])
    .optional()
    .default("subscription"),
  stripeStatus: z.enum(["open"]).optional(), // only for Stripe subscription invoices
});

// TODO: move to GET /invoices
export const GET = withWorkspace(
  async ({ workspace, searchParams }) => {
    const { type, stripeStatus } = querySchema.parse(searchParams);

    if (type === "subscription") {
      if (!workspace.stripeId) {
        return NextResponse.json([]);
      }

      const invoices = await getSubscriptionInvoices(workspace.stripeId, {
        status: stripeStatus,
      });

      return NextResponse.json(z.array(InvoiceSchema).parse(invoices));
    }

    const invoices = await getOtherInvoices({
      workspaceId: workspace.id,
      type,
    });

    return NextResponse.json(z.array(InvoiceSchema).parse(invoices));
  },
  {
    requiredPermissions: ["workspaces.read"],
  },
);

const getSubscriptionInvoices = async (
  stripeId: string,
  { status }: { status?: "open" } = {},
) => {
  try {
    const invoices = await stripe.invoices.list({
      customer: stripeId,
      limit: 100,
      ...(status && { status }),
    });

    return invoices.data.map((invoice) => ({
      id: invoice.id,
      total: invoice.total ?? invoice.amount_due,
      stripeStatus: invoice.status,
      createdAt: new Date(invoice.created * 1000),
      description: "Dub subscription",
      pdfUrl: invoice.invoice_pdf,
    }));
  } catch (error) {
    console.log(error);
    return [];
  }
};

const getOtherInvoices = async ({
  workspaceId,
  type,
}: {
  workspaceId: string;
  type: "domainRenewal";
}) => {
  const invoices = await prisma.invoice.findMany({
    where: {
      workspaceId,
      type,
    },
    select: {
      id: true,
      total: true,
      createdAt: true,
      status: true,
      paymentMethod: true,
      failedReason: true,
      refundedAmount: true,
      refundReason: true,
    },
    orderBy: {
      createdAt: "desc",
    },
  });

  return invoices.map((invoice) => {
    return {
      ...invoice,
      description: "Dub domain renewal",
      pdfUrl: `${APP_DOMAIN}/invoices/${invoice.id}`,
    };
  });
};