-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor subscription management from PR feedback.
- Loading branch information
Showing
3 changed files
with
40 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters