Skip to content

Commit d6116f7

Browse files
committed
Add check for the subsciption on the backend.
1 parent c1799b9 commit d6116f7

3 files changed

Lines changed: 90 additions & 4 deletions

File tree

packages/backend/src/modules/base/checkUsage.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
1+
import { PLANS } from '@utils/constants';
12
import { supabase } from '../../utils/supabase';
2-
import { BASIC_PLAN_MAX_REQUESTS } from '../../utils/constants';
33

44
export async function checkUsage(
55
userId: string,
66
): Promise<{ error?: string; status?: number }> {
7+
8+
const { data: proSubscriptionExists, error: subscriptionCheckError } = await supabase.rpc('check_subscription', { p_user_id: userId });
9+
const maxRequests = proSubscriptionExists ? PLANS.PRO.REQUESTS_PER_MONTH : PLANS.BASIC.REQUESTS_PER_MONTH;
10+
11+
if (subscriptionCheckError) {
12+
console.error('Error checking subscription:', subscriptionCheckError);
13+
return { error: 'API200 Error: Internal server error', status: 500 };
14+
}
15+
716
const { data, error } = await supabase.rpc('increment_usage', {
817
p_user_id: userId,
9-
p_max_requests: BASIC_PLAN_MAX_REQUESTS,
18+
p_max_requests: maxRequests,
1019
});
1120

1221
if (error) {
@@ -19,7 +28,7 @@ export async function checkUsage(
1928

2029
if (!result?.allowed) {
2130
return {
22-
error: `API200 Error: Monthly usage limit exceeded (${result?.c_count}/${BASIC_PLAN_MAX_REQUESTS})`,
31+
error: `API200 Error: Monthly usage limit exceeded (${result?.c_count}/${maxRequests})`,
2332
status: 429,
2433
};
2534
}
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
export const BASIC_PLAN_MAX_REQUESTS = 1000;
21
export const DAY = 24 * 60 * 60 * 1000;
2+
3+
export const PLANS = {
4+
BASIC: {
5+
REQUESTS_PER_MONTH: 100
6+
},
7+
PRO: {
8+
REQUESTS_PER_MONTH: 10000
9+
}
10+
} as const;

packages/backend/src/utils/database.types.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,27 @@ export type Database = {
3030
}
3131
Relationships: []
3232
}
33+
customers: {
34+
Row: {
35+
created_at: string
36+
customer_id: string
37+
email: string
38+
updated_at: string
39+
}
40+
Insert: {
41+
created_at?: string
42+
customer_id: string
43+
email: string
44+
updated_at?: string
45+
}
46+
Update: {
47+
created_at?: string
48+
customer_id?: string
49+
email?: string
50+
updated_at?: string
51+
}
52+
Relationships: []
53+
}
3354
endpoints: {
3455
Row: {
3556
cache_enabled: boolean
@@ -314,23 +335,67 @@ export type Database = {
314335
}
315336
Relationships: []
316337
}
338+
subscriptions: {
339+
Row: {
340+
created_at: string
341+
customer_id: string
342+
price_id: string | null
343+
product_id: string | null
344+
scheduled_change: string | null
345+
subscription_id: string
346+
subscription_status: string
347+
updated_at: string
348+
}
349+
Insert: {
350+
created_at?: string
351+
customer_id: string
352+
price_id?: string | null
353+
product_id?: string | null
354+
scheduled_change?: string | null
355+
subscription_id: string
356+
subscription_status: string
357+
updated_at?: string
358+
}
359+
Update: {
360+
created_at?: string
361+
customer_id?: string
362+
price_id?: string | null
363+
product_id?: string | null
364+
scheduled_change?: string | null
365+
subscription_id?: string
366+
subscription_status?: string
367+
updated_at?: string
368+
}
369+
Relationships: [
370+
{
371+
foreignKeyName: "subscriptions_customer_id_fkey"
372+
columns: ["customer_id"]
373+
isOneToOne: false
374+
referencedRelation: "customers"
375+
referencedColumns: ["customer_id"]
376+
},
377+
]
378+
}
317379
usages: {
318380
Row: {
319381
billing_started_at: string
320382
calls_count: number
321383
id: number
384+
updated_at: string | null
322385
user_id: string
323386
}
324387
Insert: {
325388
billing_started_at?: string
326389
calls_count?: number
327390
id?: number
391+
updated_at?: string | null
328392
user_id: string
329393
}
330394
Update: {
331395
billing_started_at?: string
332396
calls_count?: number
333397
id?: number
398+
updated_at?: string | null
334399
user_id?: string
335400
}
336401
Relationships: []
@@ -340,6 +405,10 @@ export type Database = {
340405
[_ in never]: never
341406
}
342407
Functions: {
408+
check_subscription: {
409+
Args: { p_user_id: string }
410+
Returns: boolean
411+
}
343412
get_route_data: {
344413
Args: {
345414
p_service_name: string

0 commit comments

Comments
 (0)