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.

be smarter at generating links

Steven Tey5a99d6e19e7a
diff --git a/apps/web/app/api/user/referrals-token/route.ts b/apps/web/app/api/user/referrals-token/route.ts
index 37230392c2..1fc168d442 100644
--- a/apps/web/app/api/user/referrals-token/route.ts
+++ b/apps/web/app/api/user/referrals-token/route.ts
@@ -17,8 +17,6 @@ export const GET = withSession(async ({ session }) => {
name: session.user.name,
email: session.user.email,
image: session.user.image || null,
- // here we don't have to worry about duplicates since createReferralsEmbedToken takes care of this for us
- username: session.user.email.split("@")[0],
tenantId: session.user.id,
},
};
diff --git a/apps/web/lib/api/partners/create-partner-link.ts b/apps/web/lib/api/partners/create-partner-link.ts
index dd9779c7bf..8945907c4e 100644
--- a/apps/web/lib/api/partners/create-partner-link.ts
+++ b/apps/web/lib/api/partners/create-partner-link.ts
@@ -8,6 +8,7 @@ import {
import { sendWorkspaceWebhook } from "@/lib/webhook/publish";
import { linkEventSchema } from "@/lib/zod/schemas/links";
import { nanoid } from "@dub/utils";
+import slugify from "@sindresorhus/slugify";
import { waitUntil } from "@vercel/functions";
import { DubApiError } from "../errors";
import { createLink } from "../links/create-link";
@@ -23,7 +24,10 @@ export const createPartnerLink = async ({
}: {
workspace: Pick<WorkspaceProps, "id" | "plan" | "webhookEnabled">;
program: Pick<ProgramProps, "defaultFolderId" | "domain" | "url" | "id">;
- partner: Pick<CreatePartnerProps, "tenantId" | "linkProps" | "username">;
+ partner: Pick<
+ CreatePartnerProps,
+ "tenantId" | "linkProps" | "name" | "email" | "username"
+ >;
userId: string;
partnerId?: string;
}) => {
@@ -35,12 +39,21 @@ export const createPartnerLink = async ({
});
}
- const { username } = partner;
+ const { name, email, username } = partner;
let link: ProcessedLinkProps;
let error: string | null;
let code: ErrorCodes | null;
- let currentKey = username ?? nanoid();
+
+ // generate a key for the link
+ let currentKey = "";
+ if (username) {
+ currentKey = username;
+ } else if (name) {
+ currentKey = slugify(name);
+ } else {
+ currentKey = slugify(email.split("@")[0]);
+ }
while (true) {
const result = await processLink({
@@ -63,7 +76,7 @@ export const createPartnerLink = async ({
result.code === "conflict" &&
result.error.startsWith("Duplicate key")
) {
- currentKey = username + nanoid(4).toLowerCase();
+ currentKey = `${username}-${nanoid(4).toLowerCase()}`;
continue;
}
diff --git a/apps/web/lib/zod/schemas/partners.ts b/apps/web/lib/zod/schemas/partners.ts
index 0b2ed4396d..5ea4238d4f 100644
--- a/apps/web/lib/zod/schemas/partners.ts
+++ b/apps/web/lib/zod/schemas/partners.ts
@@ -140,7 +140,7 @@ export const createPartnerSchema = z.object({
.max(100)
.nullish()
.describe(
- "A unique username for the partner in your system (max 100 characters). This will be used to create a short link for the partner using your program's default domain. If not provided, a random 7-character slug will be generated.",
+ "A unique username for the partner in your system (max 100 characters). This will be used to create a short link for the partner using your program's default domain. If not provided, Dub will try to generate a username from the partner's name or email.",
),
image: z
.string()