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.

remove invalid link ids in callback event

Steven Tey9e317ca0d56f
diff --git a/apps/web/app/api/cron/links/event/route.ts b/apps/web/app/api/cron/links/event/route.ts
index 2b04926d4c..335a8f3e47 100644
--- a/apps/web/app/api/cron/links/event/route.ts
+++ b/apps/web/app/api/cron/links/event/route.ts
@@ -1,4 +1,5 @@
import { getAnalytics } from "@/lib/analytics";
+import { removeInvalidLinkTags } from "@/lib/api/links";
import { limiter, qstash, receiver } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { recordLink } from "@/lib/tinybird";
@@ -34,6 +35,9 @@ export async function POST(req: Request) {
where: {
id: linkId,
},
+ include: {
+ tags: true,
+ },
});
if (!link) {
@@ -73,6 +77,7 @@ export async function POST(req: Request) {
[link.key.toLowerCase()]: await formatRedisLink(link),
}),
recordLink({ link }),
+ link.tags.length > 0 && removeInvalidLinkTags({ linkId: link.id }),
]);
// increment links usage and send alert if needed
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 052270a3d5..ed7d46bcc1 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -439,6 +439,20 @@ export async function processLink({
};
}
+function combineTagIds({
+ tagId,
+ tagIds,
+}: {
+ tagId?: string | null;
+ tagIds?: string[];
+}): string[] {
+ // Use tagIds if present, fall back to tagId
+ if (tagIds && Array.isArray(tagIds) && tagIds.length > 0) {
+ return tagIds;
+ }
+ return tagId ? [tagId] : [];
+}
+
export async function addLink(link: LinkWithTagIdsProps) {
let { domain, key, url, expiresAt, title, description, image, proxy, geo } =
link;
@@ -816,16 +830,14 @@ export async function transferLink({
});
}
-function combineTagIds({
- tagId,
- tagIds,
-}: {
- tagId?: string | null;
- tagIds?: string[];
-}): string[] {
- // Use tagIds if present, fall back to tagId
- if (tagIds && Array.isArray(tagIds) && tagIds.length > 0) {
- return tagIds;
- }
- return tagId ? [tagId] : [];
+export async function removeInvalidLinkTags({ linkId }: { linkId: string }) {
+ console.log(`Removing invalid LinkTags for linkId: ${linkId}`);
+ return await prisma.linkTag.deleteMany({
+ where: {
+ linkId,
+ tag: {
+ id: undefined,
+ },
+ },
+ });
}