The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
offload bitly import to qstash
Steven Tey8b02131004d9
diff --git a/apps/web/app/api/cron/import/bitly/route.ts b/apps/web/app/api/cron/import/bitly/route.tsindex eecf793903..9acc2ea998 100644--- a/apps/web/app/api/cron/import/bitly/route.ts+++ b/apps/web/app/api/cron/import/bitly/route.ts@@ -23,28 +23,35 @@ export async function POST(req: Request) {try {const { projectId, bitlyGroup, keepTags } = body;const bitlyApiKey = await redis.get(`import:bitly:${projectId}`);- let tagsToId;++ let tagsToId: Record<string, string> | null = null;if (keepTags === true) {- const tags = await fetch(- `https://api-ssl.bitly.com/v4/groups/${bitlyGroup}/tags`,- {- headers: {- "Content-Type": "application/json",- Authorization: `Bearer ${bitlyApiKey}`,+ const tagsImported = await redis.get(`import:bitly:${projectId}:tags`);++ if (!tagsImported) {+ const tags = (await fetch(+ `https://api-ssl.bitly.com/v4/groups/${bitlyGroup}/tags`,+ {+ headers: {+ "Content-Type": "application/json",+ Authorization: `Bearer ${bitlyApiKey}`,+ },},- },- )- .then((r) => r.json())- .then((r) => r.tags);+ )+ .then((r) => r.json())+ .then((r) => r.tags)) as string[];++ await prisma.tag.createMany({+ data: tags.map((tag) => ({+ name: tag,+ color: randomBadgeColor(),+ projectId,+ })),+ skipDuplicates: true,+ });+ await redis.set(`import:bitly:${projectId}:tags`, "true");+ }- await prisma.tag.createMany({- data: tags.map((tag) => ({- name: tag,- color: randomBadgeColor(),- projectId,- })),- skipDuplicates: true,- });tagsToId = await prisma.tag.findMany({where: {diff --git a/apps/web/app/api/cron/import/bitly/utils.ts b/apps/web/app/api/cron/import/bitly/utils.tsindex 2d46fc72a9..bdec5484c5 100644--- a/apps/web/app/api/cron/import/bitly/utils.ts+++ b/apps/web/app/api/cron/import/bitly/utils.ts@@ -1,5 +1,7 @@+import { qstash } from "@/lib/cron";import prisma from "@/lib/prisma";import { redis } from "@/lib/upstash";+import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";import { sendEmail } from "emails";import LinksImported from "emails/links-imported";@@ -42,8 +44,7 @@ export const importLinksFromBitly = async ({// convert links to format that can be imported into databaseconst importedLinks = links- .map((link) => {- const { id, long_url: url, title, archived, created_at, tags } = link;+ .map(({ id, long_url: url, title, archived, created_at, tags }) => {const [domain, key] = id.split("/");// if domain is not in project domains, skip (could be a bit.ly link or old short domain)if (!domains.includes(domain)) {@@ -130,8 +131,9 @@ export const importLinksFromBitly = async ({const links = project?.links ?? [];await Promise.all([- // delete key from redis+ // delete keys from redisredis.del(`import:bitly:${projectId}`),+ redis.del(`import:bitly:${projectId}:tags`),// delete tags that have no linksprisma.tag.deleteMany({@@ -160,14 +162,17 @@ export const importLinksFromBitly = async ({]);return count;} else {- return await importLinksFromBitly({- projectId,- userId,- domains,- bitlyGroup,- bitlyApiKey,- searchAfter: nextSearchAfter,- count,+ return await qstash.publishJSON({+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/import/bitly`,+ body: {+ projectId,+ userId,+ bitlyGroup,+ domains,+ keepTags: tagsToId ? true : false,+ searchAfter: nextSearchAfter,+ count,+ },});}};