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.

Implement default link update for partner groups

Kiran K50c4494a7d9a
diff --git a/apps/web/app/(ee)/api/cron/groups/sync-default-links/route.ts b/apps/web/app/(ee)/api/cron/groups/sync-default-links/route.ts
index d266fbe6c7..9e9b255331 100644
--- a/apps/web/app/(ee)/api/cron/groups/sync-default-links/route.ts
+++ b/apps/web/app/(ee)/api/cron/groups/sync-default-links/route.ts
@@ -1,12 +1,14 @@
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { bulkCreateLinks } from "@/lib/api/links";
+import { linkCache } from "@/lib/api/links/cache";
import { generatePartnerLink } from "@/lib/api/partners/create-partner-link";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { recordLink } from "@/lib/tinybird";
import { DefaultPartnerLink, WorkspaceProps } from "@/lib/types";
import { defaultPartnerLinkSchema } from "@/lib/zod/schemas/groups";
import { prisma } from "@dub/prisma";
-import { isFulfilled, log } from "@dub/utils";
-import { PartnerGroup } from "@prisma/client";
+import { chunk, isFulfilled, linkConstructorSimple, log } from "@dub/utils";
+import { Link, PartnerGroup } from "@prisma/client";
import { z } from "zod";
import { logAndRespond } from "../../utils";
export const dynamic = "force-dynamic";
@@ -35,6 +37,7 @@ export async function POST(req: Request) {
JSON.parse(rawBody),
);
+ // Find the group
const group = await prisma.partnerGroup.findUnique({
where: {
id: groupId,
@@ -59,7 +62,8 @@ export async function POST(req: Request) {
else if (updated) {
await updateDefaultLink({
group,
- defaultLink: updated.new,
+ oldDefaultLink: updated.old,
+ newDefaultLink: updated.new,
userId,
});
} else {
@@ -162,12 +166,95 @@ async function createDefaultLink({
// Update the default link for each partner in the group
async function updateDefaultLink({
group,
- defaultLink,
+ oldDefaultLink,
+ newDefaultLink,
userId,
}: {
group: PartnerGroup;
- defaultLink: DefaultPartnerLink;
+ oldDefaultLink: DefaultPartnerLink;
+ newDefaultLink: DefaultPartnerLink;
userId: string;
}) {
- // Not implemented yet
+ let currentPage = 0;
+
+ while (true) {
+ const programEnrollments = await prisma.programEnrollment.findMany({
+ where: {
+ groupId: group.id,
+ },
+ include: {
+ partner: true,
+ links: {
+ orderBy: {
+ id: "asc",
+ },
+ },
+ },
+ skip: currentPage * PAGE_SIZE,
+ take: PAGE_SIZE,
+ orderBy: {
+ id: "asc",
+ },
+ });
+
+ if (programEnrollments.length === 0) {
+ break;
+ }
+
+ currentPage = currentPage + 1;
+
+ let links = programEnrollments.flatMap(({ links }) => links);
+
+ // Find the links that match the OldDefaultLink
+ const defaultLinks = links.filter(
+ (link) =>
+ link.domain === oldDefaultLink.domain &&
+ link.url === oldDefaultLink.url,
+ );
+
+ const partnerLinkMap = new Map<string, Link>();
+
+ // Take the first matching link from each partner
+ // Because we assume the first matching link is the default link
+ for (const defaultLink of defaultLinks) {
+ if (!defaultLink.partnerId) {
+ continue;
+ }
+
+ if (!partnerLinkMap.has(defaultLink.partnerId)) {
+ partnerLinkMap.set(defaultLink.partnerId, defaultLink);
+ }
+ }
+
+ links = Array.from(partnerLinkMap.values());
+
+ // Split this into chunks of 100 links
+ const linkChunks = chunk(links, 100);
+
+ // Update the UTM for each partner links in the group
+ for (const linkChunk of linkChunks) {
+ const updatedLinks = await Promise.all(
+ linkChunk.map((link) =>
+ prisma.link.update({
+ where: {
+ id: link.id,
+ },
+ data: {
+ url: newDefaultLink.url,
+ domain: newDefaultLink.domain,
+ shortLink: linkConstructorSimple({
+ domain: newDefaultLink.domain,
+ key: link.key,
+ }),
+ },
+ }),
+ ),
+ );
+
+ await Promise.allSettled([
+ recordLink(updatedLinks),
+ linkCache.expireMany(updatedLinks),
+ ]);
+ }
+ }
}
diff --git a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
index b056659b33..1db3eefca4 100644
--- a/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
+++ b/apps/web/app/(ee)/api/groups/[groupIdOrSlug]/route.ts
@@ -91,6 +91,7 @@ export const PATCH = withWorkspace(
}
}
+ // Check if the UTM template exists
if (utmTemplateId) {
await prisma.utmTemplate.findUniqueOrThrow({
where: {
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-utm.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-utm.tsx
index bb6a4a7087..d98162c15c 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-utm.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/links/group-utm.tsx
@@ -71,10 +71,6 @@ export function GroupUTM() {
utm_content ||
ref;
- if (!hasUtmParams) {
- return;
- }
-
const templateData = {
name: `${group.name} UTM Template`,
utm_source,
diff --git a/apps/web/lib/api/groups/utils.ts b/apps/web/lib/api/groups/utils.ts
index a0aceae364..4f4e1405c0 100644
--- a/apps/web/lib/api/groups/utils.ts
+++ b/apps/web/lib/api/groups/utils.ts
@@ -17,7 +17,7 @@ export function diffDefaultPartnerLink(
new: DefaultPartnerLink;
} | null = null;
- const oldMap = new Map(oldLinks.map((link) => [link.id, link]));
+ const oldMap = new Map((oldLinks || []).map((link) => [link.id, link]));
for (const link of newLinks) {
const old = oldMap.get(link.id);