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.

Fix remaining type errors and start returning tag data from bulkCreateLinks

Tim Wilson470974eb04e4
diff --git a/apps/web/app/api/cron/import/bitly/utils.ts b/apps/web/app/api/cron/import/bitly/utils.ts
index e1c7c463e8..2bd6f81afb 100644
--- a/apps/web/app/api/cron/import/bitly/utils.ts
+++ b/apps/web/app/api/cron/import/bitly/utils.ts
@@ -160,7 +160,7 @@ export const importLinksFromBitly = async ({
prisma.tag.deleteMany({
where: {
projectId,
- linksNew: {
+ links: {
none: {},
},
},
diff --git a/apps/web/app/api/cron/import/rebrandly/utils.ts b/apps/web/app/api/cron/import/rebrandly/utils.ts
index 9d057ce5f6..4d73967e11 100644
--- a/apps/web/app/api/cron/import/rebrandly/utils.ts
+++ b/apps/web/app/api/cron/import/rebrandly/utils.ts
@@ -139,7 +139,7 @@ export const importLinksFromRebrandly = async ({
prisma.tag.deleteMany({
where: {
projectId,
- linksNew: {
+ links: {
none: {},
},
},
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 8e9b4982d9..6853f2a7c5 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -618,19 +618,19 @@ export async function bulkCreateLinks({
// split links into domains
const linksByDomain: Record<string, Record<string, RedisLinkProps>> = {};
- const [validTagIds, ..._rest] = await Promise.all([
- prisma.tag
- .findMany({
- where: {
- id: {
- in: linkTags.map(({ tagId }) => tagId),
- },
- },
- select: {
- id: true,
+ const [validTags, ..._rest] = await Promise.all([
+ prisma.tag.findMany({
+ where: {
+ id: {
+ in: linkTags.map(({ tagId }) => tagId),
},
- })
- .then((tags) => tags.map(({ id }) => id)),
+ },
+ select: {
+ id: true,
+ name: true,
+ color: true,
+ },
+ }),
...createdLinks.map(async (link) => {
const { domain, key } = link;
@@ -650,12 +650,16 @@ export async function bulkCreateLinks({
pipeline.hset(domain, links);
});
+ const validLinkTags = linkTags.filter(({ tagId }) =>
+ validTags.map(({ id }) => id).includes(tagId),
+ );
+
await Promise.all([
pipeline.exec(),
// create link tags for valid tagIds
linkTags.length > 0 &&
prisma.linkTag.createMany({
- data: linkTags.filter(({ tagId }) => validTagIds.includes(tagId)),
+ data: validLinkTags,
skipDuplicates: true,
}),
]);
@@ -665,10 +669,17 @@ export async function bulkCreateLinks({
domain: link.domain,
key: link.key,
});
+ const linkTags = validLinkTags.filter(({ linkId }) => linkId === link.id);
+ const tags = validTags.filter(({ id }) =>
+ linkTags.map(({ tagId }) => tagId).includes(id),
+ );
+
return {
...link,
shortLink,
qrCode: `https://api.dub.co/qr?url=${shortLink}`,
+ tags,
+ tagId: tags?.[0]?.id ?? null,
};
});
}
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index d3a2d36243..fe718ecf6d 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -9,6 +9,7 @@ export interface LinkWithTagsProps extends LinkProps {
export interface LinkWithTagIdsProps extends LinkProps {
tagIds: string[];
+ tagId?: string;
}
export interface SimpleLinkProps {
diff --git a/apps/web/scripts/sync-link-tags.ts b/apps/web/scripts/sync-link-tags.ts
index 1356181ac4..b00f6f63ba 100644
--- a/apps/web/scripts/sync-link-tags.ts
+++ b/apps/web/scripts/sync-link-tags.ts
@@ -1,53 +1,53 @@
-import prisma from "@/lib/prisma";
-import "dotenv-flow/config";
-
-/**
- * Propagates LinkTag records for links with tagIds
- */
-async function main() {
- // Get all links with Tags but without TagLinks
- const links = await prisma.link.findMany({
- where: {
- tagId: {
- not: null,
- },
- tags: {
- none: {},
- },
- },
- take: 5000,
- });
-
- // make sure all tagId exist
- const tagIds = links.map(({ tagId }) => tagId!);
- const tags = await prisma.tag.findMany({
- where: {
- id: {
- in: tagIds,
- },
- },
- });
-
- const missingTags = tagIds.filter(
- (tagId) => tags.find((tag) => tag.id === tagId) === undefined,
- );
- console.log(`Missing tags: ${missingTags.length}`, missingTags);
-
- // filter out links with tagIds that don't exist
- const linksWithValidTags = links.filter(
- ({ tagId }) => tags.find((tag) => tag.id === tagId) !== undefined,
- );
-
- console.log(`Updating ${links.length} links...`);
-
- const response = await prisma.linkTag.createMany({
- data: linksWithValidTags.map((link) => ({
- linkId: link.id,
- tagId: link.tagId!, // cause we filtered out links without tagId
- })),
- });
-
- console.log(`Created ${response.count} LinkTag records`);
-}
-
-main();
+// import prisma from "@/lib/prisma";
+// import "dotenv-flow/config";
+
+// /**
+// * Propagates LinkTag records for links with tagIds
+// */
+// async function main() {
+// // Get all links with Tags but without TagLinks
+// const links = await prisma.link.findMany({
+// where: {
+// tagId: {
+// not: null,
+// },
+// tags: {
+// none: {},
+// },
+// },
+// take: 5000,
+// });
+
+// // make sure all tagId exist
+// const tagIds = links.map(({ tagId }) => tagId!);
+// const tags = await prisma.tag.findMany({
+// where: {
+// id: {
+// in: tagIds,
+// },
+// },
+// });
+
+// const missingTags = tagIds.filter(
+// (tagId) => tags.find((tag) => tag.id === tagId) === undefined,
+// );
+// console.log(`Missing tags: ${missingTags.length}`, missingTags);
+
+// // filter out links with tagIds that don't exist
+// const linksWithValidTags = links.filter(
+// ({ tagId }) => tags.find((tag) => tag.id === tagId) !== undefined,
+// );
+
+// console.log(`Updating ${links.length} links...`);
+
+// const response = await prisma.linkTag.createMany({
+// data: linksWithValidTags.map((link) => ({
+// linkId: link.id,
+// tagId: link.tagId!, // cause we filtered out links without tagId
+// })),
+// });
+
+// console.log(`Created ${response.count} LinkTag records`);
+// }
+
+// main();