The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
move invoices to separate route
Steven Teyaf577c7e231f
diff --git a/apps/web/app/api/stripe/webhook/payment-method-attached.ts b/apps/web/app/api/stripe/webhook/payment-method-attached.tsindex dda356817a..66e2e9888e 100644--- a/apps/web/app/api/stripe/webhook/payment-method-attached.ts+++ b/apps/web/app/api/stripe/webhook/payment-method-attached.ts@@ -1,5 +1,4 @@import { prisma } from "@dub/prisma";-import { generateRandomString } from "@dub/utils";import Stripe from "stripe";export async function paymentMethodAttached(event: Stripe.Event) {@@ -9,7 +8,7 @@ export async function paymentMethodAttached(event: Stripe.Event) {return;}- const workspace = await prisma.project.update({+ await prisma.project.update({where: {stripeId: customer as string,},@@ -21,16 +20,4 @@ export async function paymentMethodAttached(event: Stripe.Event) {invoicePrefix: true,},});-- // Generate invoice prefix if it doesn't exist- if (!workspace.invoicePrefix) {- await prisma.project.update({- where: {- id: workspace.id,- },- data: {- invoicePrefix: generateRandomString(8),- },- });- }}diff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/invoices/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/billing/invoices/route.tsindex 9a92bd1a81..8bfcdcb736 100644--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/invoices/route.ts+++ b/apps/web/app/api/workspaces/[idOrSlug]/billing/invoices/route.ts@@ -67,10 +67,7 @@ const payoutInvoices = async (workspaceId: string) => {return {...invoice,description: "Dub Partner payout",- pdfUrl:- invoice.status === "completed"- ? `${APP_DOMAIN}/api/workspaces/ws_${workspaceId}/billing/invoices/${invoice.id}`- : null,+ pdfUrl: `${APP_DOMAIN}/invoices/${invoice.id}`,};});};diff --git a/apps/web/app/api/workspaces/route.ts b/apps/web/app/api/workspaces/route.tsindex 1cc3204f4d..fd2fb81fe8 100644--- a/apps/web/app/api/workspaces/route.ts+++ b/apps/web/app/api/workspaces/route.ts@@ -6,7 +6,11 @@ import {createWorkspaceSchema,} from "@/lib/zod/schemas/workspaces";import { prisma } from "@dub/prisma";-import { FREE_WORKSPACES_LIMIT, nanoid } from "@dub/utils";+import {+ FREE_WORKSPACES_LIMIT,+ generateRandomString,+ nanoid,+} from "@dub/utils";import { waitUntil } from "@vercel/functions";import { NextResponse } from "next/server";@@ -93,6 +97,7 @@ export const POST = withSession(async ({ req, session }) => {},},billingCycleStart: new Date().getDate(),+ invoicePrefix: generateRandomString(8),inviteCode: nanoid(24),defaultDomains: {create: {}, // by default, we give users all the default domains when they create a projectdiff --git a/apps/web/app/api/workspaces/[idOrSlug]/billing/invoices/[invoiceId]/route.tsx b/apps/web/app/app.dub.co/invoices/[invoiceId]/route.tsxsimilarity index 95%rename from apps/web/app/api/workspaces/[idOrSlug]/billing/invoices/[invoiceId]/route.tsxrename to apps/web/app/app.dub.co/invoices/[invoiceId]/route.tsxindex 5ede5bfa38..12ea8cb424 100644--- a/apps/web/app/api/workspaces/[idOrSlug]/billing/invoices/[invoiceId]/route.tsx+++ b/apps/web/app/app.dub.co/invoices/[invoiceId]/route.tsx@@ -1,5 +1,5 @@import { DubApiError } from "@/lib/api/errors";-import { withWorkspace } from "@/lib/auth";+import { withSession } from "@/lib/auth";import { stripe } from "@/lib/stripe";import { prisma } from "@dub/prisma";import {@@ -30,7 +30,7 @@ const tw = createTw({},});-export const GET = withWorkspace(async ({ workspace, params }) => {+export const GET = withSession(async ({ session, params }) => {const { invoiceId } = params;const invoice = await prisma.invoice.findUniqueOrThrow({@@ -39,7 +39,6 @@ export const GET = withWorkspace(async ({ workspace, params }) => {},select: {id: true,- workspaceId: true,amount: true,fee: true,total: true,@@ -60,25 +59,33 @@ export const GET = withWorkspace(async ({ workspace, params }) => {},},},+ workspace: {+ select: {+ id: true,+ stripeId: true,+ },+ },+ },+ });++ const userInWorkspace = await prisma.projectUsers.findUnique({+ where: {+ userId_projectId: {+ userId: session.user.id,+ projectId: invoice.workspace.id,+ },},});- if (invoice.workspaceId !== workspace.id) {+ if (!userInWorkspace) {throw new DubApiError({code: "unauthorized",message: "You are not authorized to view this invoice",});}- if (invoice.status !== "completed") {- throw new DubApiError({- code: "unprocessable_entity",- message: "You can download the invoice once it is completed.",- });- }-const customer = (await stripe.customers.retrieve(- workspace.stripeId!,+ invoice.workspace.stripeId!,)) as Stripe.Customer;const customerAddress = customer.shipping?.address;diff --git a/packages/utils/src/functions/generate-random-string.ts b/packages/utils/src/functions/generate-random-string.tsnew file mode 100644index 0000000000..7bc7b7195c--- /dev/null+++ b/packages/utils/src/functions/generate-random-string.ts@@ -0,0 +1,14 @@+import { randomBytes } from "crypto";++export function generateRandomString(length: number): string {+ const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";+ const randomBytesArray = randomBytes(length);+ let result = "";++ for (let i = 0; i < length; i++) {+ const randomIndex = randomBytesArray[i] % charset.length;+ result += charset[randomIndex];+ }++ return result;+}diff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.tsindex 046f4f9aca..6ef820f467 100644--- a/packages/utils/src/functions/index.ts+++ b/packages/utils/src/functions/index.ts@@ -10,6 +10,7 @@ export * from "./deep-equal";export * from "./domains";export * from "./fetch-with-timeout";export * from "./fetcher";+export * from "./generate-random-string";export * from "./hash-string";export * from "./is-click-on-interactive-child";export * from "./is-iframeable";diff --git a/packages/utils/src/functions/random-value.ts b/packages/utils/src/functions/random-value.tsindex 3b3cf6ee6f..7ace22e191 100644--- a/packages/utils/src/functions/random-value.ts+++ b/packages/utils/src/functions/random-value.ts@@ -1,18 +1,3 @@-import { randomBytes } from "crypto";-export const randomValue = (values: any[]) => {return values[Math.floor(Math.random() * values.length)];};--export function generateRandomString(length: number): string {- const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";- const randomBytesArray = randomBytes(length);- let result = "";-- for (let i = 0; i < length; i++) {- const randomIndex = randomBytesArray[i] % charset.length;- result += charset[randomIndex];- }-- return result;-}