Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add account service #14

Merged
merged 6 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
okanisildar marked this conversation as resolved.
Show resolved Hide resolved

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;
};
Loading