The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
moved functions back to lib/api/links
Steven Tey4428abb4c949
diff --git a/lib/api/links.ts b/lib/api/links.tsindex 663fd7a019..21cbca88c0 100644--- a/lib/api/links.ts+++ b/lib/api/links.ts@@ -321,3 +321,51 @@ export async function archiveLink(},});}++/* Change the domain for every link and its respective stats when the project domain is changed */+export async function changeDomainForLinks(+ projectId: string,+ domain: string,+ newDomain: string,+) {+ const links = await prisma.link.findMany({+ where: {+ project: {+ id: projectId,+ },+ },+ });+ const pipeline = redis.pipeline();+ pipeline.rename(`${domain}:root:clicks`, `${newDomain}:root:clicks`);+ links.forEach(({ key }) => {+ pipeline.rename(`${domain}:${key}`, `${newDomain}:${key}`);+ pipeline.rename(`${domain}:clicks:${key}`, `${newDomain}:clicks:${key}`);+ });+ try {+ return await pipeline.exec();+ } catch (e) {+ return null;+ }+}++/* Delete all links & stats associated with a project when it's deleted */+export async function deleteProjectLinks(domain: string) {+ const links = await prisma.link.findMany({+ where: {+ project: {+ domain,+ },+ },+ });+ const pipeline = redis.pipeline();+ pipeline.del(`${domain}:root:clicks`);+ links.forEach(({ key }) => {+ pipeline.del(`${domain}:${key}`);+ pipeline.del(`${domain}:clicks:${key}`);+ });+ try {+ return await pipeline.exec();+ } catch (e) {+ return null;+ }+}diff --git a/lib/api/projects.ts b/lib/api/projects.tsdeleted file mode 100644index 8f19da569e..0000000000--- a/lib/api/projects.ts+++ /dev/null@@ -1,47 +0,0 @@-import { redis } from "@/lib/upstash";--export async function changeDomainForLinks(- projectId: string,- domain: string,- newDomain: string,-) {- const links = await prisma.link.findMany({- where: {- project: {- id: projectId,- },- },- });- const pipeline = redis.pipeline();- pipeline.rename(`${domain}:root:clicks`, `${newDomain}:root:clicks`);- links.forEach(({ key }) => {- pipeline.rename(`${domain}:${key}`, `${newDomain}:${key}`);- pipeline.rename(`${domain}:clicks:${key}`, `${newDomain}:clicks:${key}`);- });- try {- return await pipeline.exec();- } catch (e) {- return null;- }-}--export async function deleteProjectLinks(domain: string) {- const links = await prisma.link.findMany({- where: {- project: {- domain,- },- },- });- const pipeline = redis.pipeline();- pipeline.del(`${domain}:root:clicks`);- links.forEach(({ key }) => {- pipeline.del(`${domain}:${key}`);- pipeline.del(`${domain}:clicks:${key}`);- });- try {- return await pipeline.exec();- } catch (e) {- return null;- }-}diff --git a/lib/cron/domains.tsx b/lib/cron/domains.tsxindex b830ba677d..d8b48160e0 100644--- a/lib/cron/domains.tsx+++ b/lib/cron/domains.tsx@@ -4,7 +4,7 @@ import ProjectDeleted from "emails/ProjectDeleted";import { log } from "@/lib/utils";import { removeDomain } from "@/lib/domains";import prisma from "@/lib/prisma";-import { deleteProjectLinks } from "@/lib/api/projects";+import { deleteProjectLinks } from "@/lib/api/links";import { redis } from "@/lib/upstash";export const handleDomainUpdates = async (diff --git a/pages/api/projects/[slug]/domains/[domain]/index.ts b/pages/api/projects/[slug]/domains/[domain]/index.tsindex b4a768848c..d7374ccb50 100644--- a/pages/api/projects/[slug]/domains/[domain]/index.ts+++ b/pages/api/projects/[slug]/domains/[domain]/index.ts@@ -3,7 +3,7 @@ import { withProjectAuth } from "@/lib/auth";import { addDomain, removeDomain } from "@/lib/domains";import prisma from "@/lib/prisma";import { validDomainRegex } from "@/lib/utils";-import { changeDomainForLinks } from "@/lib/api/projects";+import { changeDomainForLinks } from "@/lib/api/links";import { ProjectProps } from "@/lib/types";export default withProjectAuth(diff --git a/pages/api/projects/[slug]/index.ts b/pages/api/projects/[slug]/index.tsindex cbcbfc4ed3..9b855e8b58 100644--- a/pages/api/projects/[slug]/index.ts+++ b/pages/api/projects/[slug]/index.ts@@ -3,7 +3,7 @@ import { withProjectAuth } from "@/lib/auth";import { removeDomain } from "@/lib/domains";import prisma from "@/lib/prisma";import { ProjectProps } from "@/lib/types";-import { deleteProjectLinks } from "@/lib/api/projects";+import { deleteProjectLinks } from "@/lib/api/links";export default withProjectAuth(async (req: NextApiRequest, res: NextApiResponse, project: ProjectProps) => {