diff --git a/www/utils/supabase/actions.ts b/www/utils/supabase/actions.ts index 6f88e8f..8a56a3d 100644 --- a/www/utils/supabase/actions.ts +++ b/www/utils/supabase/actions.ts @@ -1,15 +1,27 @@ 'use server'; // This directive marks all exports as server actions -import { - createOrRetrieveFreeTrialSubscription, - decrementFreeMessages, -} from '@/utils/supabase/admin'; +import { createFreeTrialSubscription, decrementFreeMessages } from './admin'; +import { createClient } from './client'; +import { getSubscription } from './queries'; + + +export async function createOrRetrieveFreeTrialSubscription(userId: string) { + // Create server-side Supabase client + const supabase = createClient(); + + // First try to get existing subscription with user privileges + const existingSub = await getSubscription(supabase); + if (existingSub) return existingSub; + + // If no subscription exists, create one with admin privileges + return createFreeTrialSubscription(userId); +} export async function getFreeMessageCount(userId: string) { const subscription = await createOrRetrieveFreeTrialSubscription(userId); - return (subscription.metadata as { freeMessages: number })?.freeMessages ?? 0; + return (subscription?.metadata as { freeMessages: number })?.freeMessages ?? 0; } export async function useFreeTrial(userId: string) { return decrementFreeMessages(userId); -} +} \ No newline at end of file diff --git a/www/utils/supabase/admin.ts b/www/utils/supabase/admin.ts index 5dbe77b..33304c4 100644 --- a/www/utils/supabase/admin.ts +++ b/www/utils/supabase/admin.ts @@ -22,29 +22,14 @@ const supabaseAdmin = createClient( process.env.SUPABASE_SERVICE_ROLE_KEY || '' ); -// Trial membership -const createOrRetrieveFreeTrialSubscription = async (userId: string) => { - // Check for existing trial subscription - const { data: existingSub, error: subError } = await supabaseAdmin - .from('subscriptions') - .select('*') - .eq('user_id', userId) - .eq('status', 'trialing') - .maybeSingle(); - if (subError) - throw new Error(`Subscription lookup failed: ${subError.message}`); - - // If trial subscription exists, return it - if (existingSub) return existingSub; - - // Create a new trial subscription +const createFreeTrialSubscription = async (userId: string) => { const subscriptionData: TablesInsert<'subscriptions'> = { id: `free_trial_${userId}`, user_id: userId, status: 'trialing', metadata: { freeMessages: FREE_MESSAGE_LIMIT }, - price_id: null, // or your free tier price ID if you have one + price_id: null, quantity: 1, cancel_at_period_end: false, created: new Date().toISOString(), @@ -63,15 +48,13 @@ const createOrRetrieveFreeTrialSubscription = async (userId: string) => { .from('subscriptions') .insert([subscriptionData]); - if (insertError) - throw new Error( - `Trial subscription creation failed: ${insertError.message}` - ); + if (insertError) { + throw new Error(`Trial subscription creation failed: ${insertError.message}`); + } return subscriptionData; }; -// Add this function to decrement free messages const decrementFreeMessages = async (userId: string) => { const { data: subscription, error: subError } = await supabaseAdmin .from('subscriptions') @@ -384,7 +367,7 @@ export { deletePriceRecord, createOrRetrieveCustomer, manageSubscriptionStatusChange, - createOrRetrieveFreeTrialSubscription, + createFreeTrialSubscription, decrementFreeMessages, FREE_MESSAGE_LIMIT, }; diff --git a/www/utils/supabase/queries.ts b/www/utils/supabase/queries.ts index 4fc9405..48826b4 100644 --- a/www/utils/supabase/queries.ts +++ b/www/utils/supabase/queries.ts @@ -1,25 +1,33 @@ import { cache } from 'react'; import { SupabaseClient } from '@supabase/supabase-js'; import { unstable_cache } from '../unstableCache'; -import { createOrRetrieveFreeTrialSubscription } from './admin'; +import { createFreeTrialSubscription } from './admin'; + +async function createOrRetrieveFreeTrialSubscription(supabase: SupabaseClient, userId: string) { + // First try to get existing subscription with user privileges + const existingSub = await getSubscription(supabase); + if (existingSub) return existingSub; + + // If no subscription exists, create one with admin privileges + return createFreeTrialSubscription(userId); +} export const getChatAccess = unstable_cache( async (supabase: SupabaseClient, userId: string) => { - const subscription = await createOrRetrieveFreeTrialSubscription(userId); + const subscription = await createOrRetrieveFreeTrialSubscription(supabase, userId); - // Check if subscription is active (paid subscription) + // Rest of the function remains the same const isSubscribed = subscription?.status === 'active' && !subscription.cancel_at_period_end; - // Check for trial status and free messages const isTrialing = subscription?.status === 'trialing'; const trialEnded = subscription?.trial_end - ? new Date(subscription.trial_end) < new Date() - : false; + ? new Date(subscription.trial_end) < new Date() + : false; const freeMessages = (isTrialing && !trialEnded) - ? (subscription?.metadata as { freeMessages: number })?.freeMessages ?? 0 - : 0; + ? (subscription?.metadata as { freeMessages: number })?.freeMessages ?? 0 + : 0; return { isSubscribed,