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 the usage updates

Kiran K7fbb6ca70320
diff --git a/apps/web/app/api/cron/domains/transfer/route.ts b/apps/web/app/api/cron/domains/transfer/route.ts
index 4c68793a3c..8468bf4247 100644
--- a/apps/web/app/api/cron/domains/transfer/route.ts
+++ b/apps/web/app/api/cron/domains/transfer/route.ts
@@ -7,7 +7,6 @@ import {
domainTransferredEmail,
recordLinks,
updateLinksInRedis,
- updateLinksUsage,
} from "./utils";
const schema = z.object({
@@ -63,7 +62,6 @@ export async function POST(req: Request) {
where: { linkId: { in: linkIds } },
}),
updateLinksInRedis({ links, newWorkspaceId, domain }),
- updateLinksUsage({ links, newWorkspaceId, currentWorkspaceId }),
recordLinks({ links, newWorkspaceId }),
]);
diff --git a/apps/web/app/api/cron/domains/transfer/utils.ts b/apps/web/app/api/cron/domains/transfer/utils.ts
index 098da709be..af90912717 100644
--- a/apps/web/app/api/cron/domains/transfer/utils.ts
+++ b/apps/web/app/api/cron/domains/transfer/utils.ts
@@ -1,4 +1,3 @@
-import { getAnalytics } from "@/lib/analytics";
import prisma from "@/lib/prisma";
import { recordLink } from "@/lib/tinybird";
import { formatRedisLink, redis } from "@/lib/upstash";
@@ -39,59 +38,6 @@ export const updateLinksInRedis = async ({
await pipeline.exec();
};
-// Update links & click usage
-export const updateLinksUsage = async ({
- currentWorkspaceId,
- newWorkspaceId,
- links,
-}: {
- currentWorkspaceId: string;
- newWorkspaceId: string;
- links: Link[];
-}) => {
- const linkClicks: number[] = await Promise.all(
- links.map((link) =>
- getAnalytics({
- linkId: link.id,
- endpoint: "clicks",
- interval: "30d",
- }),
- ),
- );
-
- const totalClicks = linkClicks.reduce((acc, curr) => acc + curr, 0);
- const linksCount = links.length;
-
- await Promise.all([
- prisma.project.update({
- where: {
- id: currentWorkspaceId,
- },
- data: {
- usage: {
- decrement: totalClicks,
- },
- linksUsage: {
- decrement: linksCount,
- },
- },
- }),
- prisma.project.update({
- where: {
- id: newWorkspaceId,
- },
- data: {
- usage: {
- increment: totalClicks,
- },
- linksUsage: {
- increment: linksCount,
- },
- },
- }),
- ]);
-};
-
// Send email to the owner after the domain transfer is completed
export const domainTransferredEmail = async ({
domain,
diff --git a/apps/web/app/api/domains/[domain]/transfer/route.ts b/apps/web/app/api/domains/[domain]/transfer/route.ts
index c4073a29d3..9f7ba341e2 100644
--- a/apps/web/app/api/domains/[domain]/transfer/route.ts
+++ b/apps/web/app/api/domains/[domain]/transfer/route.ts
@@ -1,3 +1,4 @@
+import { getAnalytics } from "@/lib/analytics";
import { setRootDomain } from "@/lib/api/domains";
import { DubApiError } from "@/lib/api/errors";
import { withAuth } from "@/lib/auth";
@@ -94,6 +95,14 @@ export const POST = withAuth(
});
}
+ const totalLinkClicks = await getAnalytics({
+ domain,
+ workspaceId: workspace.id,
+ endpoint: "clicks",
+ interval: "30d",
+ excludeRoot: true,
+ });
+
// Update the domain to use the new workspace
const [domainResponse] = await Promise.all([
prisma.domain.update({
@@ -110,6 +119,22 @@ export const POST = withAuth(
}),
rewrite: domainRecord.type === "rewrite",
}),
+ prisma.project.update({
+ where: { id: workspace.id },
+ data: {
+ usage: {
+ decrement: totalLinkClicks,
+ },
+ },
+ }),
+ prisma.project.update({
+ where: { id: newWorkspaceId },
+ data: {
+ linksUsage: {
+ increment: totalLinkClicks,
+ },
+ },
+ }),
]);
await qstash.publishJSON({