Skip to content

Commit

Permalink
feat: add ai_credit_remaining_usage (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf authored Dec 17, 2023
1 parent 6d92c23 commit 475b1ad
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 23 deletions.
3 changes: 2 additions & 1 deletion app/credits/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export default async function Page(props: Props) {
</Heading>

<Text color="gray" size="2">
You currently have <Text weight="medium">{credits}</Text> credits.
You currently have{' '}
<Text weight="medium">{credits.ai_credit}</Text> credits.
</Text>
</Flex>

Expand Down
17 changes: 15 additions & 2 deletions components/episode-ai-summary/episode-ai-summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Tables } from '@/types/supabase/database';
import { DatabaseError } from '@/lib/errors';
import { fetchAccountAICredits } from '@/lib/services/account';
import { createSupabaseServerClient } from '@/lib/services/supabase/server';
import { Button, Flex } from '@radix-ui/themes';
import { Button, CalloutRoot, CalloutText, Flex } from '@radix-ui/themes';
import { cookies } from 'next/headers';
import Link from 'next/link';
import { CgDollar } from 'react-icons/cg';
Expand All @@ -30,7 +30,20 @@ export async function EpisodeAISummary(props: Props) {
}

if (data.length === 0 || data[0].text_summary === null) {
if (credits < 1) {
if (credits.ai_credit_remaining_usage < 1) {
return (
<EpisodeAISummaryPlaceholder>
<CalloutRoot color="amber" size="1">
<CalloutText>
You have used all your spendable credits. Thanks for using
beecast!
</CalloutText>
</CalloutRoot>
</EpisodeAISummaryPlaceholder>
);
}

if (credits.ai_credit < 1) {
return (
<EpisodeAISummaryPlaceholder>
<Button asChild highContrast>
Expand Down
51 changes: 31 additions & 20 deletions lib/services/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type { Session, User } from '@supabase/supabase-js';
import { DatabaseError } from '@/lib/errors';
import { differenceInMinutes } from 'date-fns';
import { cookies } from 'next/headers';
import { z } from 'zod';

import { notifySlack } from './notify/slack';
import { getUser } from './supabase/auth';
Expand Down Expand Up @@ -98,45 +97,57 @@ export const fetchAccount = async () => {
export const fetchAccountAICredits = async () => {
const supabase = createSupabaseServerClient(cookies());

const accountQuery = await supabase
const { data, error } = await supabase
.from('account')
.select('ai_credit')
.select('ai_credit, ai_credit_remaining_usage')
.eq('user_id', (await getUser()).id)
.single();

if (accountQuery.error) {
throw new DatabaseError(accountQuery.error);
if (error) {
throw new DatabaseError(error);
}

return accountQuery.data.ai_credit;
return data;
};

export const validateAccountAICredits = async () => {
const aiCredits = await fetchAccountAICredits();
export const decrementAccountAICredits = async () => {
const supabase = createSupabaseServerClient(cookies());
const credits = await fetchAccountAICredits();

const accountCreditsSchema = z.number().int().positive();
const validatedCredits = accountCreditsSchema.safeParse(aiCredits);
const { data, error } = await supabase
.from('account')
.update({
ai_credit: credits.ai_credit - 1,
ai_credit_remaining_usage: credits.ai_credit_remaining_usage - 1,
})
.eq('user_id', (await getUser()).id)
.select('ai_credit, ai_credit_remaining_usage')
.single();

if (!validatedCredits.success) {
throw validatedCredits.error;
if (error) {
throw new DatabaseError(error);
}

return validatedCredits.data;
return data;
};

export const updateAccountAICredits = async (amount: number) => {
export const incrementAccountAICredits = async () => {
const supabase = createSupabaseServerClient(cookies());
const credits = await fetchAccountAICredits();

const accountQuery = await supabase
const { data, error } = await supabase
.from('account')
.update({ ai_credit: amount })
.update({
ai_credit: credits.ai_credit + 1,
ai_credit_remaining_usage: credits.ai_credit_remaining_usage + 1,
})
.eq('user_id', (await getUser()).id)
.select('ai_credit')
.select('ai_credit, ai_credit_remaining_usage')
.single();

if (accountQuery.error) {
throw new DatabaseError(accountQuery.error);
if (error) {
throw new DatabaseError(error);
}

return accountQuery.data.ai_credit;
return data;
};
3 changes: 3 additions & 0 deletions types/supabase/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface Database {
account: {
Row: {
ai_credit: number;
ai_credit_remaining_usage: number;
avatar_url: string | null;
created_at: string;
id: number;
Expand All @@ -22,6 +23,7 @@ export interface Database {
};
Insert: {
ai_credit?: number;
ai_credit_remaining_usage?: number;
avatar_url?: string | null;
created_at?: string;
id?: number;
Expand All @@ -32,6 +34,7 @@ export interface Database {
};
Update: {
ai_credit?: number;
ai_credit_remaining_usage?: number;
avatar_url?: string | null;
created_at?: string;
id?: number;
Expand Down

0 comments on commit 475b1ad

Please sign in to comment.