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.

getProgramByPublishableKey

Steven Tey6768285fc267
diff --git a/apps/web/app/api/track/click/route.ts b/apps/web/app/api/track/click/route.ts
index 2a07d67c86..00823fc21d 100644
--- a/apps/web/app/api/track/click/route.ts
+++ b/apps/web/app/api/track/click/route.ts
@@ -1,9 +1,6 @@
import { DubApiError, handleAndReturnErrorResponse } from "@/lib/api/errors";
import { extractPublishableKey, parseRequestBody } from "@/lib/api/utils";
-import {
- getLinkByIdentifier,
- getWorkspaceByPublishableKey,
-} from "@/lib/planetscale";
+import { getLinkViaEdge, getProgramByPublishableKey } from "@/lib/planetscale";
import { recordClick } from "@/lib/tinybird";
import { redis } from "@/lib/upstash";
import { LOCALHOST_IP, nanoid } from "@dub/utils";
@@ -21,19 +18,18 @@ const CORS_HEADERS = {
// POST /api/track/click – Track a click event from client side
export const POST = async (req: Request) => {
try {
+ const { identifier } = await parseRequestBody(req);
const publishableKey = extractPublishableKey(req);
- const workspace = await getWorkspaceByPublishableKey(publishableKey);
+ const program = await getProgramByPublishableKey(publishableKey);
- if (!workspace) {
+ if (!program?.domain) {
throw new DubApiError({
code: "unauthorized",
- message: `Workspace not found for publishable key: ${publishableKey}`,
+ message: `Program domain not found for publishable key: ${publishableKey}`,
});
}
- const { identifier } = await parseRequestBody(req);
-
- const link = await getLinkByIdentifier(workspace.id, identifier);
+ const link = await getLinkViaEdge(program.domain, identifier);
if (!link) {
return new Response(null, {
diff --git a/apps/web/lib/planetscale/get-link-by-identifier.ts b/apps/web/lib/planetscale/get-link-by-identifier.ts
deleted file mode 100644
index f241af6dcd..0000000000
--- a/apps/web/lib/planetscale/get-link-by-identifier.ts
+++ /dev/null
@@ -1,18 +0,0 @@
-import { Link } from "@prisma/client";
-import { conn } from "./connection";
-
-// Get link by workspaceId and identifier
-export const getLinkByIdentifier = async (
- workspaceId: string,
- identifier: string,
-) => {
- const { rows } =
- (await conn.execute(
- "SELECT * FROM Link WHERE projectId = ? AND identifier = ?",
- [workspaceId, identifier],
- )) || {};
-
- return rows && Array.isArray(rows) && rows.length > 0
- ? (rows[0] as Link)
- : null;
-};
diff --git a/apps/web/lib/planetscale/get-program-by-publishable-key.ts b/apps/web/lib/planetscale/get-program-by-publishable-key.ts
new file mode 100644
index 0000000000..ceb0f18f20
--- /dev/null
+++ b/apps/web/lib/planetscale/get-program-by-publishable-key.ts
@@ -0,0 +1,15 @@
+import { Program } from "@prisma/client";
+import { conn } from "./connection";
+
+// Get program by publishable key
+export const getProgramByPublishableKey = async (publishableKey: string) => {
+ const { rows } =
+ (await conn.execute(
+ "SELECT * FROM Program WHERE publishableKey = ? LIMIT 1",
+ [publishableKey],
+ )) || {};
+
+ return rows && Array.isArray(rows) && rows.length > 0
+ ? (rows[0] as Program)
+ : null;
+};
diff --git a/apps/web/lib/planetscale/get-workspace-by-publishable-key.ts b/apps/web/lib/planetscale/get-workspace-by-publishable-key.ts
deleted file mode 100644
index 1c64ac9595..0000000000
--- a/apps/web/lib/planetscale/get-workspace-by-publishable-key.ts
+++ /dev/null
@@ -1,15 +0,0 @@
-import { Project } from "@prisma/client";
-import { conn } from "./connection";
-
-// Get workspace by publishable key
-export const getWorkspaceByPublishableKey = async (publishableKey: string) => {
- const { rows } =
- (await conn.execute(
- "SELECT * FROM Project WHERE publishableKey = ? LIMIT 1",
- [publishableKey],
- )) || {};
-
- return rows && Array.isArray(rows) && rows.length > 0
- ? (rows[0] as Project)
- : null;
-};
diff --git a/apps/web/lib/planetscale/index.ts b/apps/web/lib/planetscale/index.ts
index 06172b1f90..967296af1c 100644
--- a/apps/web/lib/planetscale/index.ts
+++ b/apps/web/lib/planetscale/index.ts
@@ -1,10 +1,9 @@
export * from "./check-if-key-exists";
export * from "./check-if-user-exists";
export * from "./connection";
-export * from "./get-link-by-identifier";
export * from "./get-link-via-edge";
+export * from "./get-program-by-publishable-key";
export * from "./get-random-key";
export * from "./get-shortlink-via-edge";
-export * from "./get-workspace-by-publishable-key";
export * from "./get-workspace-via-edge";
export * from "./types";
diff --git a/apps/web/prisma/schema/program.prisma b/apps/web/prisma/schema/program.prisma
index 0007313305..3fcecead0f 100644
--- a/apps/web/prisma/schema/program.prisma
+++ b/apps/web/prisma/schema/program.prisma
@@ -34,6 +34,7 @@ model Program {
brandColor String?
domain String?
url String?
+ publishableKey String? @unique
type ProgramType @default(affiliate)
cookieLength Int @default(90)
commissionAmount Int @default(0)
diff --git a/apps/web/prisma/schema/workspace.prisma b/apps/web/prisma/schema/workspace.prisma
index 97a92711bd..b8429f2a03 100644
--- a/apps/web/prisma/schema/workspace.prisma
+++ b/apps/web/prisma/schema/workspace.prisma
@@ -34,6 +34,7 @@ model Project {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
usageLastChecked DateTime @default(now())
+ // TODO: Remove this after we move publishable key to program table
publishableKey String? @unique
dotsAppId String? @unique