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.

use referralLinkId

Steven Teyd39dd162597d
diff --git a/apps/web/app/api/stripe/connect/webhook/customer-created.ts b/apps/web/app/api/stripe/connect/webhook/customer-created.ts
index 844759c32e..84287b98d0 100644
--- a/apps/web/app/api/stripe/connect/webhook/customer-created.ts
+++ b/apps/web/app/api/stripe/connect/webhook/customer-created.ts
@@ -58,18 +58,6 @@ export async function customerCreated(event: Stripe.Event) {
.parse(clickEvent.data[0]);
await Promise.all([
- // update link leads count
- prisma.link.update({
- where: {
- id: clickData.link_id,
- },
- data: {
- leads: {
- increment: 1,
- },
- },
- }),
-
// Record customer
recordCustomer({
workspace_id: customer.projectId,
@@ -86,6 +74,18 @@ export async function customerCreated(event: Stripe.Event) {
event_name: "New customer",
customer_id: customer.id,
}),
+
+ // update link leads count
+ prisma.link.update({
+ where: {
+ id: clickData.link_id,
+ },
+ data: {
+ leads: {
+ increment: 1,
+ },
+ },
+ }),
]);
return `Customer created: ${customer.id}`;
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
index 6dbda24845..7ee757364e 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
@@ -72,20 +72,16 @@ export const PATCH = withWorkspace(
defaultWorkspace: slug,
},
});
-
// Update the workspace's referral link to use the new slug
- waitUntil(
- (async () => {
- try {
- await dub.links.update(`ext_ws_${workspace.id}`, {
- key: slug,
- });
- } catch (error) {
- if (error.error.code !== "not_found") throw error;
- }
- })(),
- );
+ if (workspace.referralLinkId) {
+ waitUntil(
+ dub.links.update(workspace.referralLinkId, {
+ key: slug,
+ }),
+ );
+ }
}
+
return NextResponse.json(
WorkspaceSchema.parse({
...response,
diff --git a/apps/web/lib/actions/get-referral-link.ts b/apps/web/lib/actions/get-referral-link.ts
index fd8bd1fa6e..e3874bfcb4 100644
--- a/apps/web/lib/actions/get-referral-link.ts
+++ b/apps/web/lib/actions/get-referral-link.ts
@@ -8,16 +8,10 @@ export const getReferralLink = unstable_cache(async (slug: string) => {
slug,
},
});
- if (!workspace) {
- return null;
- }
- const workspaceId = `ws_${workspace.id}`;
-
- try {
- return await dub.links.get({
- externalId: `ext_${workspaceId}`,
- });
- } catch (error) {
+ if (!workspace || !workspace.referralLinkId) {
return null;
}
+ return await dub.links.get({
+ linkId: workspace.referralLinkId,
+ });
});
diff --git a/apps/web/lib/api/workspaces.ts b/apps/web/lib/api/workspaces.ts
index 5a2fd303b0..623ed40b86 100644
--- a/apps/web/lib/api/workspaces.ts
+++ b/apps/web/lib/api/workspaces.ts
@@ -1,4 +1,5 @@
import { deleteDomainAndLinks } from "@/lib/api/domains";
+import { dub } from "@/lib/dub";
import { prisma } from "@/lib/prisma";
import { storage } from "@/lib/storage";
import { cancelSubscription } from "@/lib/stripe";
@@ -14,7 +15,10 @@ import {
import { waitUntil } from "@vercel/functions";
export async function deleteWorkspace(
- workspace: Pick<WorkspaceProps, "id" | "slug" | "stripeId" | "logo">,
+ workspace: Pick<
+ WorkspaceProps,
+ "id" | "slug" | "logo" | "stripeId" | "referralLinkId"
+ >,
) {
const [customDomains, defaultDomainLinks] = await Promise.all([
prisma.domain.findMany({
@@ -107,11 +111,12 @@ export async function deleteWorkspace(
storage.delete(workspace.logo.replace(`${R2_URL}/`, "")),
// if they have a Stripe subscription, cancel it
workspace.stripeId && cancelSubscription(workspace.stripeId),
- // SOON: set the referral link to `/deleted/[slug]`
- // dub.links.update(`ext_ws_${workspace.id}`, {
- // key: `/deleted/${workspace.slug}`,
- // archived: true,
- // }),
+ // set the referral link to `/deleted/[slug]`
+ workspace.referralLinkId &&
+ dub.links.update(workspace.referralLinkId, {
+ key: `/deleted/${workspace.slug}-${workspace.id}`,
+ archived: true,
+ }),
// delete the workspace
prisma.project.delete({
where: {
@@ -134,7 +139,10 @@ export async function deleteWorkspace(
}
export async function deleteWorkspaceAdmin(
- workspace: Pick<WorkspaceProps, "id" | "slug" | "stripeId" | "logo">,
+ workspace: Pick<
+ WorkspaceProps,
+ "id" | "slug" | "logo" | "stripeId" | "referralLinkId"
+ >,
) {
const [customDomains, defaultDomainLinks] = await Promise.all([
prisma.domain.findMany({
@@ -194,9 +202,13 @@ export async function deleteWorkspaceAdmin(
storage.delete(workspace.logo.replace(`${R2_URL}/`, "")),
// if they have a Stripe subscription, cancel it
workspace.stripeId && cancelSubscription(workspace.stripeId),
- // SOON: set the referral link to `/deleted/[slug]`
- // dub.links.update(`ext_ws_${workspace.id}`, {
- // key: `/deleted/${workspace.slug}`,
+ // set the referral link to `/deleted/[slug]`
+ workspace.referralLinkId &&
+ dub.links.update(workspace.referralLinkId, {
+ key: `/deleted/${workspace.slug}-${workspace.id}`,
+ archived: true,
+ }),
+ // delete the workspace
// archived: true,
// }),
// delete the workspace
diff --git a/apps/web/lib/fetchers.ts b/apps/web/lib/fetchers.ts
index 34facc391b..4aaab80a14 100644
--- a/apps/web/lib/fetchers.ts
+++ b/apps/web/lib/fetchers.ts
@@ -37,6 +37,7 @@ export const getWorkspace = cache(async ({ slug }: { slug: string }) => {
logo: true,
usage: true,
usageLimit: true,
+ referralLinkId: true,
referredSignups: true,
plan: true,
stripeId: true,
diff --git a/apps/web/lib/zod/schemas/workspaces.ts b/apps/web/lib/zod/schemas/workspaces.ts
index 6cfcdddad2..2fcf44448c 100644
--- a/apps/web/lib/zod/schemas/workspaces.ts
+++ b/apps/web/lib/zod/schemas/workspaces.ts
@@ -26,6 +26,23 @@ export const WorkspaceSchema = z
.nullable()
.default(null)
.describe("The logo of the workspace."),
+
+ plan: planSchema,
+ stripeId: z.string().nullable().describe("The Stripe ID of the workspace."),
+ billingCycleStart: z
+ .number()
+ .describe(
+ "The date and time when the billing cycle starts for the workspace.",
+ ),
+ stripeConnectId: z
+ .string()
+ .nullable()
+ .describe("[BETA]: The Stripe Connect ID of the workspace."),
+ inviteCode: z
+ .string()
+ .nullable()
+ .describe("The invite code of the workspace."),
+
usage: z.number().describe("The usage of the workspace."),
usageLimit: z.number().describe("The usage limit of the workspace."),
linksUsage: z.number().describe("The links usage of the workspace."),
@@ -43,20 +60,22 @@ export const WorkspaceSchema = z
domainsLimit: z.number().describe("The domains limit of the workspace."),
tagsLimit: z.number().describe("The tags limit of the workspace."),
usersLimit: z.number().describe("The users limit of the workspace."),
+ aiUsage: z.number().describe("The AI usage of the workspace."),
+ aiLimit: z.number().describe("The AI limit of the workspace."),
+
+ referralLinkId: z
+ .string()
+ .nullable()
+ .describe("The ID of the referral link of the workspace."),
referredSignups: z
.number()
.describe("The number of signups referred by the workspace."),
- plan: planSchema,
- stripeId: z.string().nullable().describe("The Stripe ID of the workspace."),
- billingCycleStart: z
- .number()
+
+ conversionEnabled: z
+ .boolean()
.describe(
- "The date and time when the billing cycle starts for the workspace.",
+ "Whether the workspace has conversion tracking enabled (d.to/conversions).",
),
- stripeConnectId: z
- .string()
- .nullable()
- .describe("[BETA]: The Stripe Connect ID of the workspace."),
createdAt: z
.date()
.describe("The date and time when the workspace was created."),
@@ -76,15 +95,6 @@ export const WorkspaceSchema = z
}),
)
.describe("The domains of the workspace."),
- inviteCode: z
- .string()
- .nullable()
- .describe("The invite code of the workspace."),
- conversionEnabled: z
- .boolean()
- .describe(
- "Whether the workspace has conversion tracking enabled (d.to/conversions).",
- ),
flags: z
.record(z.boolean())
.optional()
diff --git a/apps/web/prisma/schema/workspace.prisma b/apps/web/prisma/schema/workspace.prisma
index 0a5e67d847..fae80a348e 100644
--- a/apps/web/prisma/schema/workspace.prisma
+++ b/apps/web/prisma/schema/workspace.prisma
@@ -22,6 +22,7 @@ model Project {
aiUsage Int @default(0)
aiLimit Int @default(10)
+ referralLinkId String?
referredSignups Int @default(0)
webhookEnabled Boolean @default(false)
diff --git a/apps/web/tests/utils/schema.ts b/apps/web/tests/utils/schema.ts
index bfb4271719..c56d751dd8 100644
--- a/apps/web/tests/utils/schema.ts
+++ b/apps/web/tests/utils/schema.ts
@@ -49,9 +49,14 @@ export const expectedWorkspace: Partial<Project> = {
name: expect.any(String),
slug: expect.any(String),
logo: expect.any(String),
+
+ plan: expect.any(String),
+ stripeId: expect.any(String),
+ billingCycleStart: expect.any(Number),
+ inviteCode: expect.any(String),
+
usage: expect.any(Number),
usageLimit: expect.any(Number),
- referredSignups: expect.any(Number),
linksUsage: expect.any(Number),
linksLimit: expect.any(Number),
salesUsage: expect.any(Number),
@@ -59,9 +64,9 @@ export const expectedWorkspace: Partial<Project> = {
domainsLimit: expect.any(Number),
tagsLimit: expect.any(Number),
usersLimit: expect.any(Number),
- plan: expect.any(String),
- stripeId: expect.any(String),
- billingCycleStart: expect.any(Number),
- inviteCode: expect.any(String),
+
+ referralLinkId: expect.any(String),
+ referredSignups: expect.any(Number),
+
createdAt: expect.any(String),
};