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.

use shortLinkToIndexMap for correct link–tag mapping in bulk create

Pedro Ladeira706436a07eac
diff --git a/apps/web/lib/api/links/bulk-create-links.ts b/apps/web/lib/api/links/bulk-create-links.ts
index 7722d12352..18a2a313b9 100644
--- a/apps/web/lib/api/links/bulk-create-links.ts
+++ b/apps/web/lib/api/links/bulk-create-links.ts
@@ -125,8 +125,11 @@ export async function bulkCreateLinks({
);
}
- createdLinksData.forEach((link, idx) => {
- const originalLink = links[idx];
+ createdLinksData.forEach((link) => {
+ const originalIndex = shortLinkToIndexMap.get(link.shortLink);
+ if (originalIndex === undefined) return;
+
+ const originalLink = links[originalIndex];
if (!originalLink) return;
const { tagId, tagIds, tagNames } = originalLink;
@@ -167,8 +170,11 @@ export async function bulkCreateLinks({
if (hasWebhooks) {
const linkWebhooksToCreate: { linkId: string; webhookId: string }[] = [];
- createdLinksData.forEach((link, idx) => {
- const originalLink = links[idx];
+ createdLinksData.forEach((link) => {
+ const originalIndex = shortLinkToIndexMap.get(link.shortLink);
+ if (originalIndex === undefined) return;
+
+ const originalLink = links[originalIndex];
if (!originalLink?.webhookIds?.length) return;
originalLink.webhookIds.forEach((webhookId) => {
diff --git a/apps/web/tests/links/bulk-create-link.test.ts b/apps/web/tests/links/bulk-create-link.test.ts
index 3d73db06e0..815abd5c5d 100644
--- a/apps/web/tests/links/bulk-create-link.test.ts
+++ b/apps/web/tests/links/bulk-create-link.test.ts
@@ -7,6 +7,10 @@ import { IntegrationHarness } from "../utils/integration";
import { E2E_LINK, E2E_TAG, E2E_TAG_2 } from "../utils/resource";
import { LinkSchema, expectedLink } from "../utils/schema";
+type LinkWithTags = Link & {
+ tags?: { id: string; name: string; color: string }[];
+};
+
const { domain } = E2E_LINK;
const setupBulkTest = async (ctx: any) => {
@@ -199,3 +203,55 @@ test("POST /links/bulk with multiple tags (by name)", async (ctx) => {
expectedTags: [E2E_TAG_2, E2E_TAG],
});
});
+
+test("POST /links/bulk assigns correct tags to each link (no tag mixing)", async (ctx) => {
+ const testContext = await setupBulkTest(ctx);
+ const { h } = testContext;
+
+ const bulkLinks = [
+ {
+ url: `https://example.com/${randomId()}`,
+ domain,
+ tagNames: [E2E_TAG.name],
+ },
+ {
+ url: `https://example.com/${randomId()}`,
+ domain,
+ tagNames: [E2E_TAG_2.name],
+ },
+ {
+ url: `https://example.com/${randomId()}`,
+ domain,
+ tagNames: [E2E_TAG.name, E2E_TAG_2.name],
+ },
+ ];
+
+ const { status, data: links } = await testContext.http.post<LinkWithTags[]>({
+ path: "/links/bulk",
+ body: bulkLinks,
+ });
+
+ onTestFinished(async () => {
+ await Promise.all([
+ h.deleteLink(links[0].id),
+ h.deleteLink(links[1].id),
+ h.deleteLink(links[2].id),
+ ]);
+ });
+
+ expect(status).toEqual(200);
+ expect(links).toHaveLength(3);
+
+ const link1 = links.find((l) => l.url === bulkLinks[0].url);
+ expect(link1?.tags).toHaveLength(1);
+ expect(link1?.tags?.map((t) => t.id)).toContain(E2E_TAG.id);
+
+ const link2 = links.find((l) => l.url === bulkLinks[1].url);
+ expect(link2?.tags).toHaveLength(1);
+ expect(link2?.tags?.map((t) => t.id)).toContain(E2E_TAG_2.id);
+
+ const link3 = links.find((l) => l.url === bulkLinks[2].url);
+ expect(link3?.tags).toHaveLength(2);
+ expect(link3?.tags?.map((t) => t.id)).toContain(E2E_TAG.id);
+ expect(link3?.tags?.map((t) => t.id)).toContain(E2E_TAG_2.id);
+});