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.

import tags in short.io importer

Steven Teyc67d5f6d3382
diff --git a/apps/web/app/api/cron/import/short/route.ts b/apps/web/app/api/cron/import/short/route.ts
index bb506cb96f..dc090357e9 100644
--- a/apps/web/app/api/cron/import/short/route.ts
+++ b/apps/web/app/api/cron/import/short/route.ts
@@ -18,7 +18,15 @@ export async function POST(req: Request) {
}
try {
- const { projectId, userId, domainId, domain, pageToken, count } = body;
+ const {
+ projectId,
+ userId,
+ domainId,
+ domain,
+ importTags,
+ pageToken,
+ count,
+ } = body;
const shortApiKey = (await redis.get(
`import:short:${projectId}`,
)) as string;
@@ -27,6 +35,7 @@ export async function POST(req: Request) {
userId,
domainId,
domain,
+ importTags,
pageToken,
count,
shortApiKey,
diff --git a/apps/web/app/api/cron/import/short/utils.ts b/apps/web/app/api/cron/import/short/utils.ts
index 958cd8e664..b4bcc06e64 100644
--- a/apps/web/app/api/cron/import/short/utils.ts
+++ b/apps/web/app/api/cron/import/short/utils.ts
@@ -2,6 +2,7 @@ import { bulkCreateLinks } from "@/lib/api/links";
import { qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { redis } from "@/lib/upstash";
+import { randomBadgeColor } from "@/ui/links/tag-badge";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { sendEmail } from "emails";
import LinksImported from "emails/links-imported";
@@ -11,6 +12,7 @@ export const importLinksFromShort = async ({
userId,
domainId,
domain,
+ importTags,
pageToken = null,
count = 0,
shortApiKey,
@@ -19,6 +21,7 @@ export const importLinksFromShort = async ({
userId: string;
domainId: number;
domain: string;
+ importTags?: boolean;
pageToken?: string | null;
count?: number;
shortApiKey: string;
@@ -36,6 +39,9 @@ export const importLinksFromShort = async ({
).then((res) => res.json());
const { links, nextPageToken } = data;
+ let tagsToCreate = new Set<string>();
+ let allTags: { id: string; name: string }[] = [];
+
// convert links to format that can be imported into database
const importedLinks = links
.map(
@@ -46,12 +52,14 @@ export const importLinksFromShort = async ({
iphoneURL,
androidURL,
archived,
+ tags,
createdAt,
}) => {
// skip the root domain
if (path.length === 0) {
return null;
}
+ tags.forEach((tag: string) => tagsToCreate.add(tag));
return {
projectId,
userId,
@@ -62,14 +70,49 @@ export const importLinksFromShort = async ({
ios: iphoneURL,
android: androidURL,
archived,
+ tags,
createdAt,
};
},
)
.filter(Boolean);
+ // import tags into database
+ if (importTags && tagsToCreate.size > 0) {
+ await prisma.tag.createMany({
+ data: Array.from(tagsToCreate).map((tag) => ({
+ name: tag,
+ color: randomBadgeColor(),
+ projectId,
+ })),
+ skipDuplicates: true,
+ });
+ allTags = await prisma.tag.findMany({
+ where: {
+ projectId,
+ },
+ select: {
+ id: true,
+ name: true,
+ },
+ });
+ }
+
// bulk create links
- await bulkCreateLinks({ links: importedLinks });
+ await bulkCreateLinks({
+ links: importedLinks.map(({ tags, ...rest }) => {
+ return {
+ ...rest,
+ ...(importTags && {
+ tagIds: tags
+ .map(
+ (tag: string) => allTags.find((t) => t.name === tag)?.id ?? null,
+ )
+ .filter(Boolean),
+ }),
+ };
+ }),
+ });
count += importedLinks.length;
diff --git a/apps/web/app/api/projects/[slug]/import/short/route.ts b/apps/web/app/api/projects/[slug]/import/short/route.ts
index 626266360a..0c30197ca1 100644
--- a/apps/web/app/api/projects/[slug]/import/short/route.ts
+++ b/apps/web/app/api/projects/[slug]/import/short/route.ts
@@ -55,7 +55,7 @@ export const PUT = withAuth(async ({ req, project }) => {
// POST /api/projects/[slug]/import/short - create job to import links from Short.io
export const POST = withAuth(async ({ req, project, session }) => {
- const { selectedDomains } = await req.json();
+ const { selectedDomains, importTags } = await req.json();
// check if there are domains that are not in the project
// if yes, add them to the project
@@ -87,6 +87,7 @@ export const POST = withAuth(async ({ req, project, session }) => {
userId: session?.user?.id,
domainId: id,
domain,
+ importTags,
},
}),
),
diff --git a/apps/web/ui/modals/import-short-modal.tsx b/apps/web/ui/modals/import-short-modal.tsx
index 5f43bad5cb..2afe63720d 100644
--- a/apps/web/ui/modals/import-short-modal.tsx
+++ b/apps/web/ui/modals/import-short-modal.tsx
@@ -59,6 +59,7 @@ function ImportShortModal({
ImportedDomainCountProps[]
>([]);
+ const [importTags, setImportTags] = useState<boolean>(false);
const [importing, setImporting] = useState(false);
useEffect(() => {
@@ -124,6 +125,7 @@ function ImportShortModal({
},
body: JSON.stringify({
selectedDomains,
+ importTags,
}),
}).then(async (res) => {
if (res.ok) {
@@ -178,6 +180,13 @@ function ImportShortModal({
/>
</div>
))}
+ <div className="flex items-center justify-between space-x-2 rounded-md py-1 pl-2 pr-4">
+ <p className="text-xs text-gray-500">Import all tags?</p>
+ <Switch
+ fn={() => setImportTags(!importTags)}
+ checked={importTags}
+ />
+ </div>
</div>
<Button
text="Confirm import"