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 duplicate link creation

Kiran Kb994f8052683
diff --git a/apps/web/lib/api/partners/create-partner-link.ts b/apps/web/lib/api/partners/create-partner-link.ts
index 4eb7da9a4b..8863acf673 100644
--- a/apps/web/lib/api/partners/create-partner-link.ts
+++ b/apps/web/lib/api/partners/create-partner-link.ts
@@ -23,6 +23,7 @@ type PartnerLinkArgs = {
>;
userId: string;
partnerId?: string;
+ key?: string; // current key to use for the link
};
/**
@@ -55,6 +56,7 @@ export const generatePartnerLink = async ({
partner,
userId,
partnerId,
+ key,
}: PartnerLinkArgs) => {
if (!program.domain || !program.url) {
throw new DubApiError({
@@ -71,13 +73,15 @@ export const generatePartnerLink = async ({
let code: ErrorCodes | null;
// generate a key for the link
- let currentKey = "";
- if (username) {
- currentKey = username;
- } else if (name) {
- currentKey = slugify(name);
- } else {
- currentKey = slugify(email.split("@")[0]);
+ let currentKey = key;
+ if (!currentKey) {
+ if (username) {
+ currentKey = username;
+ } else if (name) {
+ currentKey = slugify(name);
+ } else {
+ currentKey = slugify(email.split("@")[0]);
+ }
}
while (true) {
diff --git a/apps/web/lib/tolt/import-links.ts b/apps/web/lib/tolt/import-links.ts
index a6dc2b67fd..b566cb1d7b 100644
--- a/apps/web/lib/tolt/import-links.ts
+++ b/apps/web/lib/tolt/import-links.ts
@@ -1,8 +1,10 @@
import { prisma } from "@dub/prisma";
-import { nanoid } from "@dub/utils";
-import { bulkCreateLinks } from "../api/links";
+import { createLink } from "../api/links";
+import { generatePartnerLink } from "../api/partners/create-partner-link";
+import { ProgramProps, WorkspaceProps } from "../types";
import { ToltApi } from "./api";
import { MAX_BATCHES, toltImporter } from "./importer";
+import { ToltLink } from "./types";
export async function importLinks({
programId,
@@ -15,10 +17,15 @@ export async function importLinks({
where: {
id: programId,
},
+ include: {
+ workspace: true,
+ },
});
+ const { workspace } = program;
+
const { token, toltProgramId, userId } = await toltImporter.getCredentials(
- program.workspaceId,
+ workspace.id,
);
const toltApi = new ToltApi({ token });
@@ -49,26 +56,35 @@ export async function importLinks({
},
});
+ // map partner emails to partner ids
const partnerMap = new Map(partners.map(({ email, id }) => [email, id]));
+ // filter links to only include links with a partner
const partnerLinks = links.filter((link) =>
partnerMap.has(link.partner.email),
);
if (partnerLinks.length > 0) {
- await bulkCreateLinks({
- links: partnerLinks.map((link) => ({
+ for (const link of partnerLinks) {
+ const partnerId = partnerMap.get(link.partner.email);
+
+ if (!partnerId) {
+ console.log("Partner not found", link.partner.email);
+ continue;
+ }
+
+ const linkCreated = await createPartnerLink({
+ workspace: workspace as WorkspaceProps,
+ program,
+ link,
+ partnerId,
userId,
- programId,
- projectId: program.workspaceId,
- partnerId: partnerMap.get(link.partner.email),
- folderId: program.defaultFolderId,
- domain: program.domain!,
- key: link.value || nanoid(),
- url: program.url!,
- trackConversion: true,
- })),
- });
+ });
+
+ if (linkCreated?.key !== link.value) {
+ console.log("linkCreated", linkCreated?.key, link.value);
+ }
+ }
}
await new Promise((resolve) => setTimeout(resolve, 2000));
@@ -83,3 +99,50 @@ export async function importLinks({
...(hasMore && { startingAfter }),
});
}
+
+async function createPartnerLink({
+ workspace,
+ program,
+ link,
+ partnerId,
+ userId,
+}: {
+ workspace: WorkspaceProps;
+ program: ProgramProps;
+ link: ToltLink;
+ partnerId: string;
+ userId: string;
+}) {
+ const linkFound = await prisma.link.findFirst({
+ where: {
+ domain: program.domain!,
+ key: link.value,
+ },
+ select: {
+ partnerId: true,
+ },
+ });
+
+ if (linkFound?.partnerId === partnerId) {
+ console.error(
+ `Partner ${link.partner.email} already has a link with key ${link.value}`,
+ );
+ return null;
+ }
+
+ try {
+ const partnerLink = await generatePartnerLink({
+ workspace,
+ program,
+ partner: link.partner,
+ key: link.value,
+ partnerId,
+ userId,
+ });
+
+ return createLink(partnerLink);
+ } catch (error) {
+ console.error("Error creating partner link", error, link);
+ return null;
+ }
+}
diff --git a/apps/web/lib/tolt/import-referrals.ts b/apps/web/lib/tolt/import-referrals.ts
index 4fe5957a7a..90461779f2 100644
--- a/apps/web/lib/tolt/import-referrals.ts
+++ b/apps/web/lib/tolt/import-referrals.ts
@@ -6,7 +6,7 @@ import { recordClick, recordLeadWithTimestamp } from "../tinybird";
import { clickEventSchemaTB } from "../zod/schemas/clicks";
import { ToltApi } from "./api";
import { MAX_BATCHES, toltImporter } from "./importer";
-import { ToltCustomer } from "./types";
+import { ToltAffiliate, ToltCustomer } from "./types";
export async function importReferrals({
programId,
@@ -98,6 +98,7 @@ export async function importReferrals({
createReferral({
workspace,
customer,
+ partner,
links: partnerEmailToLinks.get(partner.email) ?? [],
}),
),
@@ -121,13 +122,18 @@ async function createReferral({
customer,
workspace,
links,
+ partner,
}: {
customer: Omit<ToltCustomer, "partner">;
+ partner: ToltAffiliate;
workspace: Pick<Project, "id" | "stripeConnectId">;
links: Pick<Link, "id" | "key" | "domain" | "url">[];
}) {
if (links.length === 0) {
- console.log(`Link not found for referral ${customer.id}, skipping...`);
+ console.log("Link not found for referral, skipping...", {
+ customerEmail: customer.email,
+ partnerEmail: partner.email,
+ });
return;
}
@@ -221,6 +227,6 @@ async function createReferral({
}),
]);
} catch (error) {
- console.error("error creating customer", customer, error);
+ console.error("Error creating customer", customer, error);
}
}