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.

fix: fix updating redis keys when changing domains

Snazzah0f05646895e9
diff --git a/lib/api/links.ts b/lib/api/links.ts
index d06a888d96..6383254992 100644
--- a/lib/api/links.ts
+++ b/lib/api/links.ts
@@ -317,3 +317,22 @@ export async function archiveLink(domain: string, key: string) {
},
});
}
+
+export async function changeDomain(projectId: string, domain: string, newDomain: string) {
+ const links = await prisma.link.findMany({
+ where: {
+ project: {
+ id: projectId
+ }
+ }
+ });
+ const pipeline = redis.pipeline();
+ links.forEach(({ key }) => {
+ pipeline.rename(`${domain}:clicks:${key}`, `${newDomain}:clicks:${key}`);
+ });
+ try {
+ return await pipeline.exec();
+ } catch (e) {
+ return null;
+ }
+}
diff --git a/pages/api/projects/[slug]/domains/[domain]/index.ts b/pages/api/projects/[slug]/domains/[domain]/index.ts
index f04affb220..c8fd167f41 100644
--- a/pages/api/projects/[slug]/domains/[domain]/index.ts
+++ b/pages/api/projects/[slug]/domains/[domain]/index.ts
@@ -2,8 +2,8 @@ import { NextApiRequest, NextApiResponse } from "next";
import { withProjectAuth } from "@/lib/auth";
import { addDomain, removeDomain } from "@/lib/domains";
import prisma from "@/lib/prisma";
-import { changeDomain } from "@/lib/upstash";
import { validDomainRegex } from "@/lib/utils";
+import { changeDomain } from "@/lib/api/links";
export default withProjectAuth(
async (req: NextApiRequest, res: NextApiResponse) => {
@@ -27,7 +27,7 @@ export default withProjectAuth(
where: {
domain: newDomain,
},
- select: { slug: true },
+ select: { id: true, slug: true },
});
if (project && project.slug !== slug) {
return res.status(400).json({ error: "Domain already exists" });
@@ -36,7 +36,7 @@ export default withProjectAuth(
await Promise.all([
removeDomain(domain),
addDomain(newDomain),
- changeDomain(domain, newDomain),
+ changeDomain(project.id, domain, newDomain),
prisma.project.update({
where: {
slug,