-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add DEEPGRAM and OPENAI keys to env * feat: init account service * Revert "feat: add DEEPGRAM and OPENAI keys to env" This reverts commit d12879b. * feat: add service role key * feat: use supabase server client * feat: use `currentUser` in the account service
- Loading branch information
Showing
3 changed files
with
66 additions
and
6 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,2 +1,5 @@ | ||
NEXT_PUBLIC_SUPABASE_ANON_KEY= | ||
NEXT_PUBLIC_SUPABASE_URL= | ||
NEXT_PUBLIC_SUPABASE_ANON_KEY= | ||
|
||
SUPABASE_SERVICE_ROLE_KEY= | ||
SUPABASE_URL= |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { cookies } from 'next/headers'; | ||
|
||
import { createSupabaseServerClient } from './supabase/server'; | ||
|
||
const getUserId = async () => { | ||
const supabase = createSupabaseServerClient(cookies()); | ||
|
||
const userQuery = await supabase.auth.getUser(); | ||
|
||
if (userQuery.error) { | ||
throw new Error(userQuery.error.message); | ||
} | ||
|
||
return userQuery.data.user.id; | ||
}; | ||
|
||
export const fetchAccountAICredits = async () => { | ||
const supabase = createSupabaseServerClient(cookies()); | ||
|
||
const accountQuery = await supabase | ||
.from('account') | ||
.select('ai_credit') | ||
.eq('user_id', await getUserId()) | ||
.single(); | ||
|
||
if (accountQuery.error) { | ||
throw new Error(accountQuery.error.message); | ||
} | ||
|
||
return accountQuery.data.ai_credit || 0; | ||
}; | ||
|
||
export const validateAccountAICredits = async () => { | ||
const aiCredits = await fetchAccountAICredits(); | ||
|
||
if (aiCredits <= 0) { | ||
throw new Error('User does not have any AI credits'); | ||
} | ||
|
||
return aiCredits; | ||
}; | ||
|
||
export const updateAccountAICredits = async (amount: number) => { | ||
const supabase = createSupabaseServerClient(cookies()); | ||
|
||
const accountQuery = await supabase | ||
.from('account') | ||
.update({ ai_credit: amount }) | ||
.eq('user_id', await getUserId()) | ||
.select('ai_credit') | ||
.single(); | ||
|
||
if (accountQuery.error) { | ||
throw new Error(accountQuery.error.message); | ||
} | ||
|
||
return accountQuery.data.ai_credit || 0; | ||
}; |