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.

update domains

Steven Tey1db6a450d71d
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.ts
index cdabd4219d..0779210cdb 100644
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@ -5,10 +5,12 @@ import {
validateDomain,
} from "@/lib/api/domains";
import { getDomain } from "@/lib/api/domains/get-domain";
-import { updateDomain } from "@/lib/api/domains/update-domain";
import { DubApiError } from "@/lib/api/errors";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
+import { prisma } from "@/lib/prisma";
+import { recordLink } from "@/lib/tinybird";
+import { redis } from "@/lib/upstash";
import { updateDomainBodySchema } from "@/lib/zod/schemas/domains";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
@@ -34,9 +36,12 @@ export const PATCH = withWorkspace(
const body = await parseRequestBody(req);
const payload = updateDomainBodySchema.parse(body);
- const { slug: newDomain } = payload;
+ const { slug: newDomain, placeholder, expiredUrl, archived } = payload;
- if (newDomain && newDomain.toLowerCase() !== domain.toLowerCase()) {
+ const domainUpdated =
+ newDomain && newDomain.toLowerCase() !== domain.toLowerCase();
+
+ if (domainUpdated) {
const validDomain = await validateDomain(newDomain);
if (validDomain.error && validDomain.code) {
throw new DubApiError({
@@ -53,20 +58,53 @@ export const PATCH = withWorkspace(
}
}
- const domainRecord = await updateDomain({
- ...payload,
- slug: domain,
- newSlug: newDomain,
- workspace,
+ const domainRecord = await prisma.domain.update({
+ where: {
+ slug: domain,
+ },
+ data: {
+ archived,
+ ...(domainUpdated && { slug: newDomain }),
+ ...(placeholder && { placeholder }),
+ ...(workspace.plan != "free" && {
+ expiredUrl,
+ }),
+ },
});
waitUntil(
- Promise.all([
- // remove old domain from Vercel
- newDomain &&
- newDomain.toLowerCase() !== domain.toLowerCase() &&
- removeDomainFromVercel(domain),
- ]),
+ (async () => {
+ if (domainUpdated) {
+ await Promise.all([
+ // remove old domain from Vercel
+ removeDomainFromVercel(domain),
+ // rename redis key
+ redis.rename(domain, newDomain),
+ ]);
+
+ const allLinks = await prisma.link.findMany({
+ where: {
+ domain: newDomain,
+ },
+ include: {
+ tags: true,
+ },
+ });
+
+ // update all links in Tinybird
+ recordLink([
+ ...allLinks.map((link) => ({
+ link_id: link.id,
+ domain: link.domain,
+ key: link.key,
+ url: link.url,
+ tag_ids: link.tags.map((tag) => tag.tagId),
+ workspace_id: link.projectId,
+ created_at: link.createdAt,
+ })),
+ ]);
+ }
+ })(),
);
return NextResponse.json(domainRecord);
diff --git a/apps/web/lib/api/domains/update-domain.ts b/apps/web/lib/api/domains/update-domain.ts
deleted file mode 100644
index 984d44f039..0000000000
--- a/apps/web/lib/api/domains/update-domain.ts
+++ /dev/null
@@ -1,33 +0,0 @@
-import { prisma } from "@/lib/prisma";
-import { WorkspaceProps } from "@/lib/types";
-import z from "@/lib/zod";
-import { updateDomainBodySchema } from "@/lib/zod/schemas/domains";
-
-type UpdateDomainInput = z.infer<typeof updateDomainBodySchema> & {
- newSlug?: string;
- workspace: Pick<WorkspaceProps, "id" | "plan" | "domains">;
-};
-
-export const updateDomain = async (input: UpdateDomainInput) => {
- const { slug, newSlug, workspace, placeholder, expiredUrl, archived } = input;
-
- // Update domain
- return await prisma.domain.update({
- where: {
- slug,
- },
- data: {
- slug: newSlug,
- archived,
- ...(placeholder && { placeholder }),
- ...(workspace.plan != "free" && {
- expiredUrl,
- }),
- },
- include: {
- links: {
- take: 1,
- },
- },
- });
-};