feat(subscription): implement feature gating for subscription-based access in admin panel
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { api } from '../lib/api';
|
||||
import type { ApiResponse } from '../lib/api';
|
||||
import { useAuthStore } from '../stores/authStore';
|
||||
import type { MySubscriptionData } from '../types';
|
||||
|
||||
export function useSubscription() {
|
||||
const primaryRole = useAuthStore((s) => s.primaryRole);
|
||||
const enabled = primaryRole === 'doctor' || primaryRole === 'clinic' || primaryRole === 'secretary';
|
||||
|
||||
const { data } = useQuery<ApiResponse<MySubscriptionData>>({
|
||||
queryKey: ['subscription-my'],
|
||||
queryFn: () => api.get('/api/v1/subscription/my'),
|
||||
enabled,
|
||||
staleTime: 2 * 60 * 1000,
|
||||
});
|
||||
|
||||
const sub = data?.data?.subscription ?? null;
|
||||
const features: Record<string, boolean> = sub?.plan?.features ?? {};
|
||||
const maxSecretaries: number = sub?.plan?.max_secretaries ?? 1;
|
||||
const hasPlan = sub !== null;
|
||||
|
||||
return {
|
||||
subscription: sub,
|
||||
hasFeature: (key: string) => hasPlan && (features[key] ?? false),
|
||||
maxSecretaries,
|
||||
hasPlan,
|
||||
isExpiringSoon: (sub?.days_remaining ?? 0) > 0 && (sub?.days_remaining ?? 0) <= 7,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user