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.

FEAT: refer.dub.co links

Steven Tey467e976b2a17
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
index a67e67e65b..6a49c81d34 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
@@ -1,12 +1,14 @@
import { DubApiError } from "@/lib/api/errors";
import { deleteWorkspace } from "@/lib/api/workspaces";
import { withWorkspace } from "@/lib/auth";
+import { dub } from "@/lib/dub";
import { getFeatureFlags } from "@/lib/edge-config";
import { prisma } from "@/lib/prisma";
import {
WorkspaceSchema,
updateWorkspaceSchema,
} from "@/lib/zod/schemas/workspaces";
+import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
// GET /api/workspaces/[idOrSlug] – get a specific workspace by id or slug
@@ -61,16 +63,25 @@ export const PATCH = withWorkspace(
},
});
- if (slug !== workspace.slug) {
- await prisma.user.updateMany({
- where: {
- defaultWorkspace: workspace.slug,
- },
- data: {
- defaultWorkspace: slug,
- },
- });
- }
+ waitUntil(
+ (async () => {
+ if (slug !== workspace.slug) {
+ await prisma.user.updateMany({
+ where: {
+ defaultWorkspace: workspace.slug,
+ },
+ data: {
+ defaultWorkspace: slug,
+ },
+ });
+
+ // update the workspace's referral link to use the new slug
+ await dub.links.update(`ext_ws_${workspace.id}`, {
+ key: slug,
+ });
+ }
+ })(),
+ );
return NextResponse.json(
WorkspaceSchema.parse({
diff --git a/apps/web/app/api/workspaces/route.ts b/apps/web/app/api/workspaces/route.ts
index 200715d2a1..818c4087da 100644
--- a/apps/web/app/api/workspaces/route.ts
+++ b/apps/web/app/api/workspaces/route.ts
@@ -1,5 +1,6 @@
import { DubApiError } from "@/lib/api/errors";
import { withSession } from "@/lib/auth";
+import { dub } from "@/lib/dub";
import { checkIfUserExists } from "@/lib/planetscale";
import { prisma } from "@/lib/prisma";
import {
@@ -118,6 +119,14 @@ export const POST = withSession(async ({ req, session }) => {
waitUntil(
(async () => {
+ // create a referral link for the workspace
+ await dub.links.create({
+ domain: "refer.dub.co",
+ key: slug,
+ url: "https://dub.co",
+ externalId: `ws_${workspaceResponse.id}`,
+ tagIds: ["cm000srqx0004o6ldehod07zc"],
+ });
if (session.user["defaultWorkspace"] === null) {
await prisma.user.update({
where: {
diff --git a/apps/web/lib/api/workspaces.ts b/apps/web/lib/api/workspaces.ts
index c69f38527d..775157739c 100644
--- a/apps/web/lib/api/workspaces.ts
+++ b/apps/web/lib/api/workspaces.ts
@@ -1,7 +1,11 @@
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";
+import { recordLink } from "@/lib/tinybird";
+import { WorkspaceProps } from "@/lib/types";
+import { formatRedisLink, redis } from "@/lib/upstash";
import {
DUB_DOMAINS_ARRAY,
LEGAL_USER_ID,
@@ -9,9 +13,6 @@ import {
R2_URL,
} from "@dub/utils";
import { waitUntil } from "@vercel/functions";
-import { recordLink } from "../tinybird";
-import { WorkspaceProps } from "../types";
-import { formatRedisLink, redis } from "../upstash";
export async function deleteWorkspace(
workspace: Pick<WorkspaceProps, "id" | "slug" | "stripeId" | "logo">,
@@ -107,6 +108,11 @@ export async function deleteWorkspace(
storage.delete(workspace.logo.replace(`${R2_URL}/`, "")),
// if they have a Stripe subscription, cancel it
workspace.stripeId && cancelSubscription(workspace.stripeId),
+ // set the referral link to `/deleted/[slug]`
+ dub.links.update(`ext_ws_${workspace.id}`, {
+ key: `/deleted/${workspace.slug}`,
+ archived: true,
+ }),
// delete the workspace
prisma.project.delete({
where: {
@@ -189,6 +195,11 @@ export async function deleteWorkspaceAdmin(
storage.delete(workspace.logo.replace(`${R2_URL}/`, "")),
// if they have a Stripe subscription, cancel it
workspace.stripeId && cancelSubscription(workspace.stripeId),
+ // set the referral link to `/deleted/[slug]`
+ dub.links.update(`ext_ws_${workspace.id}`, {
+ key: `/deleted/${workspace.slug}`,
+ archived: true,
+ }),
// delete the workspace
prisma.project.delete({
where: {