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 totalClicks count in usage cron

Steven Tey5bc8483c2bb8
diff --git a/apps/web/app/api/cron/usage/utils.ts b/apps/web/app/api/cron/usage/utils.ts
index fc3e18fd09..e7f3fb7f89 100644
--- a/apps/web/app/api/cron/usage/utils.ts
+++ b/apps/web/app/api/cron/usage/utils.ts
@@ -10,7 +10,6 @@ import {
APP_DOMAIN_WITH_NGROK,
capitalize,
getAdjustedBillingCycleStart,
- linkConstructor,
log,
} from "@dub/utils";
@@ -124,39 +123,34 @@ export const updateUsage = async () => {
groupBy: "top_links",
interval: "30d",
root: false,
- }).then(async (data) => {
- const topFive = data.slice(0, 5);
- const topFiveLinkIds = topFive.map(({ link }) => link);
-
- const links = await prisma.link.findMany({
- where: {
- projectId: workspace.id,
- id: {
- in: topFiveLinkIds,
- },
- },
- select: {
- id: true,
- domain: true,
- key: true,
+ });
+
+ const topFive = topLinks.slice(0, 5);
+ const topFiveLinkIds = topFive.map(({ link }) => link);
+
+ const linksMetadata = await prisma.link.findMany({
+ where: {
+ projectId: workspace.id,
+ id: {
+ in: topFiveLinkIds,
},
- });
-
- const allLinks = links.map((link) => ({
- id: link.id,
- shortLink: linkConstructor({
- domain: link.domain,
- key: link.key,
- pretty: true,
- }),
- }));
-
- return topFive.map((d) => ({
- link: allLinks.find((l) => l.id === d.link)?.shortLink || "",
- clicks: d.clicks,
- }));
+ },
+ select: {
+ id: true,
+ shortLink: true,
+ },
});
+ const topFiveLinks = topFive.map((d) => ({
+ link: linksMetadata.find((l) => l.id === d.link)?.shortLink || d.link,
+ clicks: d.clicks,
+ }));
+
+ const totalClicks = topLinks.reduce(
+ (acc, curr) => acc + curr.clicks,
+ 0,
+ );
+
const emails = workspace.users.map(
(user) => user.user.email,
) as string[];
@@ -171,9 +165,9 @@ export const updateUsage = async () => {
email,
workspaceName: workspace.name,
workspaceSlug: workspace.slug,
- totalClicks: workspace.usage,
+ totalClicks,
createdLinks: workspace.linksUsage,
- topLinks,
+ topLinks: topFiveLinks,
}),
}),
);