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 MAX_ADDITIONAL_PARTNER_LINKS

Steven Teya066e6c79d25
diff --git a/apps/web/lib/zod/schemas/groups.ts b/apps/web/lib/zod/schemas/groups.ts
index 4708e3dc56..c6673b0b38 100644
--- a/apps/web/lib/zod/schemas/groups.ts
+++ b/apps/web/lib/zod/schemas/groups.ts
@@ -16,8 +16,7 @@ export const DEFAULT_PARTNER_GROUP = {
} as const;
export const MAX_DEFAULT_PARTNER_LINKS = 5;
-
-export const MAX_ADDITIONAL_PARTNER_LINKS = 5;
+export const MAX_ADDITIONAL_PARTNER_LINKS = 10;
export const GROUPS_MAX_PAGE_SIZE = 100;
diff --git a/apps/web/scripts/migrations/backfill-group-links-pgdl-acme.ts b/apps/web/scripts/migrations/backfill-group-links-pgdl-acme.ts
new file mode 100644
index 0000000000..f5180b24ab
--- /dev/null
+++ b/apps/web/scripts/migrations/backfill-group-links-pgdl-acme.ts
@@ -0,0 +1,95 @@
+import { prisma } from "@dub/prisma";
+import { normalizeUrl } from "@dub/utils";
+import "dotenv-flow/config";
+
+// special script for checking if acme default links are set up properly
+async function main() {
+ const program = await prisma.program.findUniqueOrThrow({
+ where: {
+ slug: "acme",
+ },
+ include: {
+ groups: {
+ include: {
+ partnerGroupDefaultLinks: true,
+ },
+ },
+ },
+ });
+
+ for (const group of program.groups) {
+ const defaultLink = group.partnerGroupDefaultLinks[0];
+
+ const programEnrollments = await prisma.programEnrollment.findMany({
+ where: {
+ groupId: group.id,
+ },
+ include: {
+ links: {
+ orderBy: {
+ createdAt: "asc",
+ },
+ },
+ },
+ });
+
+ const firstPartnerLinks = await Promise.all(
+ programEnrollments.map(async (programEnrollment) => {
+ const { programId, partnerId, links } = programEnrollment;
+ const foundDefaultLink = links.find(
+ (link) => link.partnerGroupDefaultLinkId,
+ );
+ const firstPartnerLink = links.find(
+ (link) => normalizeUrl(link.url) === normalizeUrl(defaultLink.url),
+ );
+
+ const {
+ groupDefaultLinkId,
+ partnerDefaultLinkId,
+ firstPartnerLinkDefaultLinkId,
+ } = {
+ groupDefaultLinkId: defaultLink.id,
+ partnerDefaultLinkId: foundDefaultLink?.partnerGroupDefaultLinkId,
+ firstPartnerLinkDefaultLinkId:
+ firstPartnerLink?.partnerGroupDefaultLinkId,
+ };
+
+ const matchingDefaultLink = [
+ partnerDefaultLinkId,
+ firstPartnerLinkDefaultLinkId,
+ ].every((id) => id === groupDefaultLinkId);
+
+ if (!matchingDefaultLink) {
+ await prisma.link.update({
+ where: {
+ id: firstPartnerLink?.id,
+ },
+ data: {
+ partnerGroupDefaultLinkId: groupDefaultLinkId,
+ },
+ });
+
+ console.log(
+ `Updated link ${firstPartnerLink?.id} to have default link ${groupDefaultLinkId}`,
+ );
+ }
+
+ return {
+ id: firstPartnerLink?.id,
+ shortLink: firstPartnerLink?.shortLink,
+ url: firstPartnerLink?.url,
+ programId,
+ partnerId,
+ groupDefaultLinkId,
+ partnerDefaultLinkId,
+ firstPartnerLinkDefaultLinkId,
+ matchingDefaultLink,
+ };
+ }),
+ );
+
+ console.table(firstPartnerLinks);
+ }
+}
+
+main();
diff --git a/apps/web/scripts/migrations/backfill-group-links-pgdl.ts b/apps/web/scripts/migrations/backfill-group-links-pgdl.ts
index 99a799bc80..7f191f67f5 100644
--- a/apps/web/scripts/migrations/backfill-group-links-pgdl.ts
+++ b/apps/web/scripts/migrations/backfill-group-links-pgdl.ts
@@ -1,21 +1,12 @@
import { prisma } from "@dub/prisma";
+import { normalizeUrl } from "@dub/utils";
import "dotenv-flow/config";
-// Helper function to normalize URL by removing UTM parameters
-function normalizeUrl(url: string): string {
- try {
- const urlObj = new URL(url);
- return `https://${urlObj.host}${urlObj.pathname}`;
- } catch {
- return url;
- }
-}
-
// Step 2 of 2: Backfill partner links with partnerGroupDefaultLinkId
async function main() {
const program = await prisma.program.findUniqueOrThrow({
where: {
- slug: "acme",
+ slug: "xxx",
},
include: {
groups: {
@@ -27,13 +18,25 @@ async function main() {
});
for (const group of program.groups) {
+ if (group.partnerGroupDefaultLinks.length === 0) {
+ // should never happen, but just in case
+ console.log(
+ `WARNING: No default links found for group ${group.id}. Skipping...`,
+ );
+ continue;
+ }
+
const defaultLink = group.partnerGroupDefaultLinks[0];
const programEnrollments = await prisma.programEnrollment.findMany({
where: {
groupId: group.id,
+ links: {
+ every: {
+ partnerGroupDefaultLinkId: null,
+ },
+ },
},
- take: 100,
include: {
links: {
orderBy: {
@@ -41,34 +44,31 @@ async function main() {
},
},
},
+ take: 100,
});
- const firstPartnerLinks = programEnrollments
- .map(({ programId, partnerId, links }) => {
+ const firstPartnerLinks = programEnrollments.map(
+ async (programEnrollment) => {
+ const { links } = programEnrollment;
const firstPartnerLink = links.find(
(link) => normalizeUrl(link.url) === normalizeUrl(defaultLink.url),
);
+
if (!firstPartnerLink) {
console.log(
- `No partner link found for ${programId} ${partnerId} that matches ${defaultLink.url}`,
+ `WARNING: No matching partner link found for partner ${programEnrollment.partnerId} in group ${group.id}. Skipping...`,
);
return null;
}
+
return {
- id: firstPartnerLink?.id,
- shortLink: firstPartnerLink?.shortLink,
- url: firstPartnerLink?.url,
- defaultLinkId: firstPartnerLink?.partnerGroupDefaultLinkId,
+ id: firstPartnerLink.id,
+ partnerGroupDefaultLinkId: defaultLink.id,
};
- })
- .filter((link) => link !== null);
+ },
+ );
- console.table(firstPartnerLinks, [
- "id",
- "shortLink",
- "url",
- "defaultLinkId",
- ]);
+ console.table(firstPartnerLinks);
}
}