From a01c76a6816f8ccb7294492821cc5edc1d0a9ae2 Mon Sep 17 00:00:00 2001 From: Storm Date: Sun, 15 Dec 2024 00:50:30 +0100 Subject: [PATCH] fix: Allow for new users to use it --- .../api/v1/commercial_license_trial/route.ts | 28 +++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/app/api/v1/commercial_license_trial/route.ts b/src/app/api/v1/commercial_license_trial/route.ts index 6172260..c2c6855 100644 --- a/src/app/api/v1/commercial_license_trial/route.ts +++ b/src/app/api/v1/commercial_license_trial/route.ts @@ -77,6 +77,9 @@ export async function POST(req: NextRequest): Promise { } } +// Currently, hardcoding this to 10000. +const MAX_REQUESTS_PER_24H = 10000; + /** * Checks the user's authorization token and retrieves user information. * Also checks the user's subscription status and sets the rate limit headers, @@ -100,11 +103,26 @@ export async function checkUserInDB(req: NextRequest): Promise<{ .select("*") .eq("user_id", user.id) .limit(1) - .single(); + .maybeSingle(); if (res.error) { throw convertPgError(res.error); } + if (!res.data) { + return { + user, + rateLimitHeaders: getRateLimitHeaders( + new RateLimiterRes( + MAX_REQUESTS_PER_24H - 1, // -1 because we just consumed 1 email. + 24 * 3600 * 1000, + 0, + undefined + ), + MAX_REQUESTS_PER_24H + ), + }; + } + // If the user has a commercial license trial, we need to check the rate limit. // First, we limit to 60 calls per minute. if ((res.data.calls_last_minute || 0) >= 60) { @@ -126,18 +144,18 @@ export async function checkUserInDB(req: NextRequest): Promise<{ ? addDays(parseISO(res.data.first_call_in_past_24h), 1) : addDays(now, 1); const msDiff = differenceInMilliseconds(nextReset, now); - const maxInPast24h = 10000; // Currently, hardcoding this to 10000. + const rateLimitHeaders = getRateLimitHeaders( new RateLimiterRes( - maxInPast24h - callsInPast24h - 1, // -1 because we just consumed 1 email. + MAX_REQUESTS_PER_24H - callsInPast24h - 1, // -1 because we just consumed 1 email. msDiff, callsInPast24h, undefined ), - maxInPast24h + MAX_REQUESTS_PER_24H ); - if (callsInPast24h >= maxInPast24h) { + if (callsInPast24h >= MAX_REQUESTS_PER_24H) { throw newEarlyResponse( Response.json( {