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.

wip

Kiran K8a8d566e6f33
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index f44c5a445b..419bb7093d 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -63,6 +63,7 @@ export const POST = withWorkspace(
.slice(index + 1)
.some((l) => l.domain === link.domain && l.key === link.key),
);
+
if (duplicates.length > 0) {
throw new DubApiError({
code: "bad_request",
@@ -120,17 +121,21 @@ export const POST = withWorkspace(
name: true,
},
});
+
const workspaceTagIds = workspaceTags.map(({ id }) => id);
const workspaceTagNames = workspaceTags.map(({ name }) => name);
+
validLinks.forEach((link, index) => {
const combinedTagIds =
combineTagIds({
tagId: link.tagId,
tagIds: link.tagIds,
}) ?? [];
+
const invalidTagIds = combinedTagIds.filter(
(id) => !workspaceTagIds.includes(id),
);
+
if (invalidTagIds.length > 0) {
// remove link from validLinks and add error to errorLinks
validLinks = validLinks.filter((_, i) => i !== index);
@@ -144,6 +149,7 @@ export const POST = withWorkspace(
const invalidTagNames = link.tagNames?.filter(
(name) => !workspaceTagNames.includes(name),
);
+
if (invalidTagNames?.length) {
validLinks = validLinks.filter((_, i) => i !== index);
errorLinks.push({
diff --git a/apps/web/lib/api/links/bulk-create-links.ts b/apps/web/lib/api/links/bulk-create-links.ts
index 041f66f2d9..d6284d264d 100644
--- a/apps/web/lib/api/links/bulk-create-links.ts
+++ b/apps/web/lib/api/links/bulk-create-links.ts
@@ -28,13 +28,20 @@ export async function bulkCreateLinks({
// Create a map of shortLinks to their original indices at the start
const shortLinkToIndexMap = new Map(
- links.map((link, index) => [
- linkConstructorSimple({
+ links.map((link, index) => {
+ const key = encodeKeyIfCaseSensitive({
domain: link.domain,
key: link.key,
- }),
- index,
- ]),
+ });
+
+ return [
+ linkConstructorSimple({
+ domain: link.domain,
+ key,
+ }),
+ index,
+ ];
+ }),
);
// Create all links first using createMany
diff --git a/apps/web/lib/api/links/create-link.ts b/apps/web/lib/api/links/create-link.ts
index 38ef0dd72a..f056ba0029 100644
--- a/apps/web/lib/api/links/create-link.ts
+++ b/apps/web/lib/api/links/create-link.ts
@@ -13,6 +13,7 @@ import {
} from "@dub/utils";
import { linkConstructorSimple } from "@dub/utils/src/functions/link-constructor";
import { waitUntil } from "@vercel/functions";
+import { encodeKeyIfCaseSensitive } from "../case-sensitive-short-links";
import { createId } from "../create-id";
import { combineTagIds } from "../tags/combine-tag-ids";
import { linkCache } from "./cache";
@@ -40,6 +41,11 @@ export async function createLink(link: ProcessedLinkProps) {
const { tagId, tagIds, tagNames, webhookIds, ...rest } = link;
+ key = encodeKeyIfCaseSensitive({
+ domain: link.domain,
+ key,
+ });
+
const response = await prisma.link.create({
data: {
...rest,
diff --git a/apps/web/lib/api/links/process-link.ts b/apps/web/lib/api/links/process-link.ts
index b60cb2f5b3..d97a38522e 100644
--- a/apps/web/lib/api/links/process-link.ts
+++ b/apps/web/lib/api/links/process-link.ts
@@ -19,7 +19,6 @@ import {
parseDateTime,
pluralize,
} from "@dub/utils";
-import { encodeKeyIfCaseSensitive } from "../case-sensitive-short-links";
import { combineTagIds } from "../tags/combine-tag-ids";
import { keyChecks, processKey } from "./utils";
@@ -538,11 +537,6 @@ export async function processLink<T extends Record<string, any>>({
delete payload[tag];
});
- key = encodeKeyIfCaseSensitive({
- domain,
- key,
- });
-
return {
link: {
...payload,
diff --git a/apps/web/lib/api/links/update-link.ts b/apps/web/lib/api/links/update-link.ts
index 95f7360823..f2970d959c 100644
--- a/apps/web/lib/api/links/update-link.ts
+++ b/apps/web/lib/api/links/update-link.ts
@@ -12,6 +12,7 @@ import {
truncate,
} from "@dub/utils";
import { waitUntil } from "@vercel/functions";
+import { encodeKeyIfCaseSensitive } from "../case-sensitive-short-links";
import { createId } from "../create-id";
import { combineTagIds } from "../tags/combine-tag-ids";
import { linkCache } from "./cache";
@@ -62,6 +63,11 @@ export async function updateLink({
const imageUrlNonce = nanoid(7);
+ key = encodeKeyIfCaseSensitive({
+ domain: updatedLink.domain,
+ key: updatedLink.key,
+ });
+
const response = await prisma.link.update({
where: {
id,
diff --git a/apps/web/lib/tinybird/record-link.ts b/apps/web/lib/tinybird/record-link.ts
index be822f1fa1..0d756b3e6e 100644
--- a/apps/web/lib/tinybird/record-link.ts
+++ b/apps/web/lib/tinybird/record-link.ts
@@ -77,13 +77,15 @@ export const transformLinkTB = (link: ExpandedLink) => {
export const recordLink = async (payload: ExpandedLink | ExpandedLink[]) => {
if (Array.isArray(payload)) {
- payload = payload.filter((link) => !DOMAINS_TO_SKIP.includes(link.domain));
+ const tbPayload = payload.filter(
+ (link) => !DOMAINS_TO_SKIP.includes(link.domain),
+ );
- if (!payload.length) {
+ if (!tbPayload.length) {
return;
}
- return await recordLinkTB(payload.map(transformLinkTB));
+ return await recordLinkTB(tbPayload.map(transformLinkTB));
} else {
if (DOMAINS_TO_SKIP.includes(payload.domain)) {
return;