From 61b9e5d9c0da8b27c99045b5aebd3db3ab7e61f5 Mon Sep 17 00:00:00 2001 From: Storm Date: Fri, 14 Jun 2024 21:50:37 +0200 Subject: [PATCH] feat: 20k for one particular user --- src/app/[lang]/dashboard/ApiUsage.tsx | 11 +++++++++-- src/app/api/v0/check_email/checkUserInDb.ts | 2 +- src/util/subs.ts | 20 +++++++++++++++++++- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/app/[lang]/dashboard/ApiUsage.tsx b/src/app/[lang]/dashboard/ApiUsage.tsx index f76781f..70462e7 100644 --- a/src/app/[lang]/dashboard/ApiUsage.tsx +++ b/src/app/[lang]/dashboard/ApiUsage.tsx @@ -49,7 +49,11 @@ export function ApiUsage({ subAndCalls.number_of_calls )} - /{subApiMaxCalls(subAndCalls.product_id)} + / + {subApiMaxCalls( + subAndCalls.product_id, + subAndCalls.user_id + )} @@ -57,7 +61,10 @@ export function ApiUsage({ className={styles.capacity} value={ ((subAndCalls.number_of_calls || 0) / - subApiMaxCalls(subAndCalls.product_id)) * + subApiMaxCalls( + subAndCalls.product_id, + subAndCalls.user_id + )) * 100 } /> diff --git a/src/app/api/v0/check_email/checkUserInDb.ts b/src/app/api/v0/check_email/checkUserInDb.ts index a32aaa5..48e6199 100644 --- a/src/app/api/v0/check_email/checkUserInDb.ts +++ b/src/app/api/v0/check_email/checkUserInDb.ts @@ -75,7 +75,7 @@ export async function checkUserInDB(req: NextRequest): Promise { ? 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. diff --git a/src/util/subs.ts b/src/util/subs.ts index 8fbacaf..00f809e 100644 --- a/src/util/subs.ts +++ b/src/util/subs.ts @@ -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 = @@ -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