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.

simplify

Steven Tey4bf206a872c3
diff --git a/apps/web/app/api/cron/links/[linkId]/complete-tests/route.ts b/apps/web/app/api/cron/links/[linkId]/complete-tests/route.ts
index 742b40310b..231593803d 100644
--- a/apps/web/app/api/cron/links/[linkId]/complete-tests/route.ts
+++ b/apps/web/app/api/cron/links/[linkId]/complete-tests/route.ts
@@ -21,9 +21,6 @@ export async function POST(
where: {
id: linkId,
},
- include: {
- project: true,
- },
});
if (!link) {
@@ -40,7 +37,7 @@ export async function POST(
link.testCompletedAt < new Date() &&
Date.now() - link.testCompletedAt.getTime() < 60 * 1000 // Limit to 1 minute window
) {
- await completeABTests(link as any);
+ await completeABTests(link);
return new Response(`Tests completed for link ${linkId}.`);
}
diff --git a/apps/web/lib/api/links/complete-ab-tests.ts b/apps/web/lib/api/links/complete-ab-tests.ts
index b74c40bbe7..17d42c306a 100644
--- a/apps/web/lib/api/links/complete-ab-tests.ts
+++ b/apps/web/lib/api/links/complete-ab-tests.ts
@@ -3,12 +3,12 @@ import { recordLink } from "@/lib/tinybird";
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { ABTestVariantsSchema, linkEventSchema } from "@/lib/zod/schemas/links";
import { prisma } from "@dub/prisma";
-import { Link, Project } from "@prisma/client";
+import { Link } from "@prisma/client";
import { waitUntil } from "@vercel/functions";
import { linkCache } from "./cache";
import { includeTags } from "./include-tags";
-export async function completeABTests(link: Link & { project: Project }) {
+export async function completeABTests(link: Link) {
if (!link.testVariants || !link.testCompletedAt || !link.projectId) {
return;
}
@@ -57,9 +57,6 @@ export async function completeABTests(link: Link & { project: Project }) {
return;
}
- // Update the link's URL to the winner
- const { project, ...originalLink } = link;
-
const response = await prisma.link.update({
where: {
id: link.id,
@@ -67,18 +64,25 @@ export async function completeABTests(link: Link & { project: Project }) {
data: {
url: winner.url,
},
- include: includeTags,
+ include: {
+ ...includeTags,
+ project: true,
+ },
});
waitUntil(
Promise.allSettled([
+ // update the link cache
linkCache.set(response),
+ // record the link
recordLink(response),
- sendWorkspaceWebhook({
- trigger: "link.updated",
- workspace: link.project,
- data: linkEventSchema.parse(response),
- }),
+ // send a link.updated webhook to the workspace
+ response.project &&
+ sendWorkspaceWebhook({
+ trigger: "link.updated",
+ workspace: response.project,
+ data: linkEventSchema.parse(response),
+ }),
]),
);
}