Skip to content

Commit

Permalink
feat: add account service (#14)
Browse files Browse the repository at this point in the history
* 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
altaywtf authored Dec 11, 2023
1 parent 50667eb commit c8d85a8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .env.example
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=
9 changes: 4 additions & 5 deletions env.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ const getGitSha = () => {

const envSchema = z.object({
GIT_SHA: z.string().min(1).default(getGitSha()),
NEXT_PUBLIC_SUPABASE_ANON_KEY: z.string(),
NEXT_PUBLIC_SUPABASE_URL: z.string(),
// We don't have these configurations yet.
SUPABASE_SERVICE_ROLE_KEY: z.string().default(''),
SUPABASE_URL: z.string().default(''),
NEXT_PUBLIC_SUPABASE_ANON_KEY: z.string().min(1),
NEXT_PUBLIC_SUPABASE_URL: z.string().min(1),
SUPABASE_SERVICE_ROLE_KEY: z.string().min(1),
SUPABASE_URL: z.string().min(1),
});

const envValidation = envSchema.safeParse(process.env);
Expand Down
58 changes: 58 additions & 0 deletions lib/services/account.ts
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;
};

0 comments on commit c8d85a8

Please sign in to comment.