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 project links and clicks count

Steven Tey753cedfbac2d
diff --git a/apps/web/lib/api/links/bulk-delete-links.ts b/apps/web/lib/api/links/bulk-delete-links.ts
index 29c2747652..413250e225 100644
--- a/apps/web/lib/api/links/bulk-delete-links.ts
+++ b/apps/web/lib/api/links/bulk-delete-links.ts
@@ -1,5 +1,6 @@
import { storage } from "@/lib/storage";
import { recordLinkTB, transformLinkTB } from "@/lib/tinybird";
+import { prisma } from "@dub/prisma";
import { R2_URL } from "@dub/utils";
import { linkCache } from "./cache";
import { ExpandedLink } from "./utils";
@@ -25,5 +26,15 @@ export async function bulkDeleteLinks(links: ExpandedLink[]) {
links
.filter((link) => link.image?.startsWith(`${R2_URL}/images/${link.id}`))
.map((link) => storage.delete(link.image!.replace(`${R2_URL}/`, ""))),
+
+ // Update totalLinks for the workspace
+ prisma.project.update({
+ where: {
+ id: links[0].projectId!,
+ },
+ data: {
+ totalLinks: { decrement: links.length },
+ },
+ }),
]);
}
diff --git a/apps/web/lib/api/links/delete-link.ts b/apps/web/lib/api/links/delete-link.ts
index e703294ff4..99a60c9a47 100644
--- a/apps/web/lib/api/links/delete-link.ts
+++ b/apps/web/lib/api/links/delete-link.ts
@@ -32,6 +32,15 @@ export async function deleteLink(linkId: string) {
...transformLinkTB(link),
deleted: true,
}),
+ link.projectId &&
+ prisma.project.update({
+ where: {
+ id: link.projectId,
+ },
+ data: {
+ totalLinks: { decrement: 1 },
+ },
+ }),
]),
);
diff --git a/apps/web/lib/api/links/update-links-usage.ts b/apps/web/lib/api/links/update-links-usage.ts
index 16ff1b24f8..86ebefeef4 100644
--- a/apps/web/lib/api/links/update-links-usage.ts
+++ b/apps/web/lib/api/links/update-links-usage.ts
@@ -18,6 +18,9 @@ export async function updateLinksUsage({
linksUsage: {
increment,
},
+ totalLinks: {
+ increment,
+ },
},
select: {
id: true,
diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.ts
index ed9d14b40d..afb400910e 100644
--- a/apps/web/lib/tinybird/record-click.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@ -165,7 +165,7 @@ export async function recordClick({
// and then we have a cron that will reset it at the start of new billing cycle
url &&
conn.execute(
- "UPDATE Project p JOIN Link l ON p.id = l.projectId SET p.usage = p.usage + 1 WHERE l.id = ?",
+ "UPDATE Project p JOIN Link l ON p.id = l.projectId SET p.usage = p.usage + 1, p.totalClicks = p.totalClicks + 1 WHERE l.id = ?",
[linkId],
),