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.

update usage stats

Steven Tey6c5786c7c840
diff --git a/apps/web/app/api/cron/links/event/route.ts b/apps/web/app/api/cron/links/event/route.ts
index ac59bc214e..53d2992dd1 100644
--- a/apps/web/app/api/cron/links/event/route.ts
+++ b/apps/web/app/api/cron/links/event/route.ts
@@ -1,5 +1,6 @@
import { limiter, qstash, receiver } from "@/lib/cron";
import prisma from "@/lib/prisma";
+import { getStats } from "@/lib/stats";
import { recordLink } from "@/lib/tinybird";
import { ProjectProps } from "@/lib/types";
import { formatRedisLink, redis } from "@/lib/upstash";
@@ -133,5 +134,49 @@ export async function POST(req: Request) {
}
}
+ if (type === "transfer") {
+ const oldProjectId = await redis.get<string>(
+ `transfer:${linkId}:oldProjectId`,
+ );
+
+ const linkClicks = await getStats({
+ linkId,
+ endpoint: "clicks",
+ interval: "30d",
+ });
+
+ // update old and new project usage
+ await Promise.all([
+ oldProjectId &&
+ prisma.project.update({
+ where: {
+ id: oldProjectId,
+ },
+ data: {
+ usage: {
+ decrement: linkClicks,
+ },
+ linksUsage: {
+ decrement: 1,
+ },
+ },
+ }),
+ link.projectId &&
+ prisma.project.update({
+ where: {
+ id: link.projectId,
+ },
+ data: {
+ usage: {
+ increment: linkClicks,
+ },
+ linksUsage: {
+ increment: 1,
+ },
+ },
+ }),
+ ]);
+ }
+
return new Response("OK", { status: 200 });
}
diff --git a/apps/web/app/api/links/[linkId]/transfer/route.ts b/apps/web/app/api/links/[linkId]/transfer/route.ts
index 908f45739c..c9141279c6 100644
--- a/apps/web/app/api/links/[linkId]/transfer/route.ts
+++ b/apps/web/app/api/links/[linkId]/transfer/route.ts
@@ -2,6 +2,7 @@ import { transferLink } from "@/lib/api/links";
import { withAuth } from "@/lib/auth";
import { qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
+import { redis } from "@/lib/upstash";
import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -51,15 +52,18 @@ export const POST = withAuth(async ({ req, headers, session, link }) => {
linkId: link!.id,
newProjectId: body.newProjectId,
}),
- qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
- body: {
- linkId: link!.id,
- type: "transfer",
- },
- }),
+ // set this in redis so we can use it in the event cron job
+ redis.set(`transfer:${link!.id}:oldProjectId`, link!.projectId),
]);
+ await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
+ body: {
+ linkId: link!.id,
+ type: "transfer",
+ },
+ });
+
return NextResponse.json(response, {
headers,
});
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 340a0f4e2c..7a650c66e8 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -707,6 +707,7 @@ export async function transferLink({
},
data: {
projectId: newProjectId,
+ tagId: null, // remove tags when transferring link
},
});
}
diff --git a/apps/web/ui/modals/delete-link-modal.tsx b/apps/web/ui/modals/delete-link-modal.tsx
index 78df57d36b..f9ea7c52b7 100644
--- a/apps/web/ui/modals/delete-link-modal.tsx
+++ b/apps/web/ui/modals/delete-link-modal.tsx
@@ -85,7 +85,7 @@ function DeleteLinkModal({
setDeleting(false);
});
}}
- className="flex flex-col space-y-6 bg-gray-50 px-4 py-8 text-left sm:rounded-b-2xl sm:px-16"
+ className="flex flex-col space-y-3 bg-gray-50 px-4 py-8 text-left sm:rounded-b-2xl sm:px-16"
>
<div>
<label htmlFor="verification" className="block text-sm text-gray-700">
diff --git a/apps/web/ui/modals/transfer-link-modal.tsx b/apps/web/ui/modals/transfer-link-modal.tsx
index 0117dafd96..1ab714a29a 100644
--- a/apps/web/ui/modals/transfer-link-modal.tsx
+++ b/apps/web/ui/modals/transfer-link-modal.tsx
@@ -103,8 +103,8 @@ function TransferLinkModal({
/>
<h3 className="text-lg font-medium">Transfer {shortlink}</h3>
<p className="text-sm text-gray-500">
- Transfer this link along with its associated analytics to another{" "}
- {APP_NAME} project.
+ Transfer this link and its analytics to another {APP_NAME} project.
+ Link tags will not be transferred.
</p>
</div>