Skip to content

Commit

Permalink
feat: 20k for one particular user
Browse files Browse the repository at this point in the history
  • Loading branch information
storm1729 committed Jun 14, 2024
1 parent 9bde510 commit 61b9e5d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/app/[lang]/dashboard/ApiUsage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,22 @@ export function ApiUsage({
subAndCalls.number_of_calls
)}
</Text>
/{subApiMaxCalls(subAndCalls.product_id)}
/
{subApiMaxCalls(
subAndCalls.product_id,
subAndCalls.user_id
)}
</Text>
</div>

<Capacity
className={styles.capacity}
value={
((subAndCalls.number_of_calls || 0) /
subApiMaxCalls(subAndCalls.product_id)) *
subApiMaxCalls(
subAndCalls.product_id,
subAndCalls.user_id
)) *
100
}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/v0/check_email/checkUserInDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export async function checkUserInDB(req: NextRequest): Promise<UserWithSub> {
? parseISO(subAndCalls.current_period_end as string) // Safe to type cast here, if there's a subscription, there's a current_period_end.
: addMonths(now, 1);
const msDiff = differenceInMilliseconds(nextReset, now);
const max = subApiMaxCalls(subAndCalls.product_id);
const max = subApiMaxCalls(subAndCalls.product_id, user.id);
const rateLimitHeaders = getRateLimitHeaders(
new RateLimiterRes(
max - numberOfCalls - 1, // -1 because we just consumed 1 email.
Expand Down
20 changes: 19 additions & 1 deletion src/util/subs.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isBefore } from "date-fns";

// We're hardcoding these as env variables.
export const SAAS_10K_PRODUCT_ID = process.env.NEXT_PUBLIC_SAAS_10K_PRODUCT_ID;
export const SAAS_100K_PRODUCT_ID =
Expand All @@ -14,7 +16,23 @@ if (
}

// Return the max monthly calls
export function subApiMaxCalls(productId: string | null | undefined): number {
export function subApiMaxCalls(
productId: string | null | undefined,
userId: string | null
): number {
const today = new Date();
const julySeventh = new Date(today.getFullYear(), 6, 7); // Months are 0-indexed, so 6 is July

// TODO: This is extremely hard-coded, and should be refactored to something
// inside the DB, with an admin dashboard to update etc. But for now, let's
// do like this.
if (
userId === "fe601de1-adc5-442f-a1b3-b58a01503474" &&
isBefore(today, julySeventh)
) {
return 20_000;
}

return productId === SAAS_100K_PRODUCT_ID
? 100_000
: productId === SAAS_10K_PRODUCT_ID
Expand Down

0 comments on commit 61b9e5d

Please sign in to comment.