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 links usage cron and add usageLastChecked field to Project model

Kiran K06867c5f68c4
diff --git a/apps/web/app/api/cron/links/usage/route.ts b/apps/web/app/api/cron/links/usage/route.ts
index 14f628f05b..6e7f0dfc50 100644
--- a/apps/web/app/api/cron/links/usage/route.ts
+++ b/apps/web/app/api/cron/links/usage/route.ts
@@ -7,8 +7,6 @@ import { Project } from "@prisma/client";
// Cron to update the links usage of each workspace.
// Runs once every 5 mins (*/5 * * * *)
-const take = 30;
-
export async function GET(req: Request) {
const validSignature = await verifySignature(req);
@@ -25,13 +23,26 @@ export async function GET(req: Request) {
linksUsage: true,
linksLimit: true,
},
- take,
+ orderBy: {
+ usageLastChecked: "asc",
+ },
+ take: 30,
});
try {
- await Promise.allSettled(
- workspaces.map((workspace) => sendUsageEmail(workspace)),
- );
+ await Promise.allSettled([
+ prisma.project.updateMany({
+ where: {
+ id: {
+ in: workspaces.map(({ id }) => id),
+ },
+ },
+ data: {
+ usageLastChecked: new Date(),
+ },
+ }),
+ ...workspaces.map((workspace) => sendUsageEmail(workspace)),
+ ]);
return new Response("OK");
} catch (error) {
diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma
index c715b60475..b90ff560a7 100644
--- a/apps/web/prisma/schema.prisma
+++ b/apps/web/prisma/schema.prisma
@@ -109,9 +109,11 @@ model Project {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
-
+ usageLastChecked DateTime @default(now())
inviteCode String? @unique
defaultDomains DefaultDomains[]
+
+ @@index(usageLastChecked(sort: Asc))
}
model ProjectInvite {