From e52e9c0bfab0f0e06a255cc44de1e3b35c55c5dd Mon Sep 17 00:00:00 2001 From: Steven Tey Date: Sun, 22 Dec 2024 20:54:56 -0800 Subject: [PATCH] handle duplicate tagIds --- apps/web/lib/api/tags/combine-tag-ids.ts | 3 +- apps/web/scripts/bulk-create-links.ts | 95 ------------------------ 2 files changed, 2 insertions(+), 96 deletions(-) delete mode 100644 apps/web/scripts/bulk-create-links.ts diff --git a/apps/web/lib/api/tags/combine-tag-ids.ts b/apps/web/lib/api/tags/combine-tag-ids.ts index 1fb3889665..dddd161189 100644 --- a/apps/web/lib/api/tags/combine-tag-ids.ts +++ b/apps/web/lib/api/tags/combine-tag-ids.ts @@ -10,7 +10,8 @@ export function combineTagIds({ }): string[] | undefined { // Use tagIds if present, fall back to tagId if (tagIds && Array.isArray(tagIds)) { - return tagIds; + // remove duplicates + return [...new Set(tagIds)]; } return tagId === null ? [] : tagId !== undefined ? [tagId] : undefined; } diff --git a/apps/web/scripts/bulk-create-links.ts b/apps/web/scripts/bulk-create-links.ts deleted file mode 100644 index 18cfea2864..0000000000 --- a/apps/web/scripts/bulk-create-links.ts +++ /dev/null @@ -1,95 +0,0 @@ -import z from "@/lib/zod"; -import { bulkCreateLinksBodySchema } from "@/lib/zod/schemas/links"; -import { Client } from "@upstash/qstash"; -import { Redis } from "@upstash/redis"; -import "dotenv-flow/config"; -import * as fs from "fs"; -import * as Papa from "papaparse"; - -// Initiate Redis instance by connecting to REST URL -const redis = new Redis({ - url: process.env.UPSTASH_REDIS_REST_URL || "", - token: process.env.UPSTASH_REDIS_REST_TOKEN || "", -}); - -const qstash = new Client({ - token: process.env.QSTASH_TOKEN || "", -}); - -const projectId = "xxx"; -const userId = "xxx"; -const domain = "xxx"; -const links: z.infer = []; - -async function main() { - Papa.parse(fs.createReadStream("short_urls.csv", "utf-8"), { - header: true, - skipEmptyLines: true, - step: (result: { - data: { - createdAt: string; - domain: string; - shortCode: string; - longUrl: string; - title: string; - tags: string; - }; - }) => { - const { createdAt, domain, shortCode, longUrl, title, tags } = - result.data; - - links.push({ - domain, - key: shortCode, - url: longUrl, - title, - // @ts-ignore - createdAt, - tagNames: tags.split("|"), - }); - }, - complete: async () => { - // const tags = links.flatMap((link) => link.tagNames).filter(Boolean); - // const uniqueTags = Array.from(new Set(tags)); - // await prisma.tag.createMany({ - // data: uniqueTags.map((name) => ({ - // name, - // color: randomBadgeColor(), - // projectId, - // })), - // skipDuplicates: true, - // }); - - // deduplicate links with same domain and key - const processedLinks = links.reduce((acc: typeof links, link) => { - const existingLink = acc.find( - (l) => l.domain === link.domain && l.key === link.key, - ); - - if (existingLink) { - // @ts-ignore combine tags - existingLink?.tagNames.push(...link.tagNames); - } else { - acc.push(link); - } - - return acc; - }, []); - - console.table(processedLinks, ["domain", "key", "tagNames"]); - - await redis.lpush(`import:csv:${projectId}`, ...processedLinks); - - await qstash.publishJSON({ - url: `https://rich-easily-kingfish.ngrok-free.app/api/cron/import/csv`, - body: { - workspaceId: projectId, - userId, - domain, - }, - }); - }, - }); -} - -main();