Skip to content

Commit

Permalink
chore: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
altaywtf committed Dec 15, 2023
1 parent 20bc46f commit 7a9d3bd
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
34 changes: 34 additions & 0 deletions app/credits/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { RouteModal } from '@/components/route-modal';
import { fetchAccountAICredits } from '@/lib/services/account';
import { getPrices } from '@/lib/services/stripe/client';
import {
CalloutIcon,
CalloutRoot,
CalloutText,
Flex,
Heading,
} from '@radix-ui/themes';
import { CgInfo } from 'react-icons/cg';

export default async function Page() {
const _prices = await getPrices();
const credits = await fetchAccountAICredits();

return (
<RouteModal>
<Flex direction="column" gap="4">
<Heading as="h2" size="6">
Your credits
</Heading>

<CalloutRoot size="1">
<CalloutIcon>
<CgInfo />
</CalloutIcon>

<CalloutText>You have {credits} credits remaining.</CalloutText>
</CalloutRoot>
</Flex>
</RouteModal>
);
}
11 changes: 11 additions & 0 deletions lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,14 @@ export class DatabaseError extends ExtendableError {
this.underlyingError = error;
}
}

export const createExternalServiceError = (serviceName: string) =>
class ExternalServiceError extends ExtendableError {
serviceName = serviceName;
underlyingError: unknown;

constructor(error: unknown) {
super(`Error from ${serviceName}`);
this.underlyingError = error;
}
};
23 changes: 23 additions & 0 deletions lib/services/stripe/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { env } from '@/env.mjs';
import { createExternalServiceError } from '@/lib/errors';
import { Stripe } from 'stripe';

const stripe = new Stripe(env.STRIPE_SECRET_KEY);
const StripeError = createExternalServiceError('Stripe');

const getProduct = async () => {
const products = await stripe.products.list();
const product = products.data.find((p) => p.metadata.project === 'beecast');

if (!product) {
throw new StripeError('Product not found!');
}

return product;
};

export const getPrices = async () => {
const product = await getProduct();
const prices = await stripe.prices.list({ product: product.id });
return prices.data;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"react": "^18",
"react-dom": "^18",
"react-icons": "^4.12.0",
"stripe": "^14.9.0",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
24 changes: 11 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7a9d3bd

Please sign in to comment.