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.

Improve usage quota resets for workspaces

Kiran K40abef6b4d34
diff --git a/apps/web/app/api/cron/usage/route.ts b/apps/web/app/api/cron/usage/route.ts
index db588295ee..5fcb936f9c 100644
--- a/apps/web/app/api/cron/usage/route.ts
+++ b/apps/web/app/api/cron/usage/route.ts
@@ -1,5 +1,5 @@
import { verifySignature } from "@/lib/cron";
-import { getSearchParams, log } from "@dub/utils";
+import { log } from "@dub/utils";
import { NextResponse } from "next/server";
import { updateUsage } from "./utils";
@@ -12,10 +12,8 @@ export async function GET(req: Request) {
return new Response("Unauthorized", { status: 401 });
}
- const { skip } = getSearchParams(req.url);
-
try {
- await updateUsage(skip ? parseInt(skip) : undefined);
+ await updateUsage();
return NextResponse.json({
response: "success",
diff --git a/apps/web/app/api/cron/usage/utils.ts b/apps/web/app/api/cron/usage/utils.ts
index 309ae5c8b7..c85d572747 100644
--- a/apps/web/app/api/cron/usage/utils.ts
+++ b/apps/web/app/api/cron/usage/utils.ts
@@ -14,9 +14,19 @@ import ClicksSummary from "emails/clicks-summary";
const limit = 100;
-export const updateUsage = async (skip?: number) => {
+export const updateUsage = async () => {
const workspaces = await prisma.project.findMany({
where: {
+ OR: [
+ {
+ lastUsageResetAt: {
+ lt: new Date(new Date().getTime() - 24 * 60 * 60 * 1000), // 24 hours ago
+ },
+ },
+ {
+ lastUsageResetAt: null,
+ },
+ ],
domains: {
some: {
verified: true,
@@ -36,7 +46,9 @@ export const updateUsage = async (skip?: number) => {
},
sentEmails: true,
},
- skip: skip || 0,
+ orderBy: {
+ createdAt: "asc",
+ },
take: limit,
});
@@ -45,6 +57,18 @@ export const updateUsage = async (skip?: number) => {
return;
}
+ // Update lastUsageResetAt for workspaces
+ await prisma.project.updateMany({
+ where: {
+ id: {
+ in: workspaces.map(({ id }) => id),
+ },
+ },
+ data: {
+ lastUsageResetAt: new Date(),
+ },
+ });
+
// Reset billing cycles for workspaces that have
// adjustedBillingCycleStart that matches today's date
const billingReset = workspaces.filter(
@@ -226,9 +250,7 @@ export const updateUsage = async (skip?: number) => {
);
return await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/usage?skip=${
- skip ? skip + limit : limit
- }`,
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/usage`,
method: "GET",
});
};
diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma
index 898eec9386..1e21276dd5 100644
--- a/apps/web/prisma/schema.prisma
+++ b/apps/web/prisma/schema.prisma
@@ -93,32 +93,31 @@ model Project {
slug String @unique
logo String?
- plan String @default("free")
- stripeId String? @unique // Stripe subscription ID
+ plan String @default("free")
+ stripeId String? @unique // Stripe subscription ID
billingCycleStart Int // day of the month when the billing cycle starts
- stripeConnectId String? @unique // for Stripe Connect
- usage Int @default(0)
- usageLimit Int @default(1000)
- aiUsage Int @default(0)
- aiLimit Int @default(10)
- linksUsage Int @default(0)
- linksLimit Int @default(25)
- domainsLimit Int @default(3)
- tagsLimit Int @default(5)
- usersLimit Int @default(1)
-
- users ProjectUsers[]
- invites ProjectInvite[]
- sentEmails SentEmail[]
- links Link[]
- domains Domain[]
- tags Tag[]
- customers Customer[]
-
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
-
- inviteCode String? @unique
+ stripeConnectId String? @unique // for Stripe Connect
+ usage Int @default(0)
+ usageLimit Int @default(1000)
+ aiUsage Int @default(0)
+ aiLimit Int @default(10)
+ linksUsage Int @default(0)
+ linksLimit Int @default(25)
+ domainsLimit Int @default(3)
+ tagsLimit Int @default(5)
+ usersLimit Int @default(1)
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+ inviteCode String? @unique
+ lastUsageResetAt DateTime?
+
+ users ProjectUsers[]
+ invites ProjectInvite[]
+ sentEmails SentEmail[]
+ links Link[]
+ domains Domain[]
+ tags Tag[]
+ customers Customer[]
defaultDomains DefaultDomains[]
}