feat(#6): Add a basic product page using the db rather than stripe

This commit is contained in:
Josh Creek
2024-10-15 19:20:40 +01:00
parent e666aa95d0
commit 36a8620e70
3 changed files with 52 additions and 20 deletions
+36 -1
View File
@@ -409,6 +409,40 @@ const getUserCredits = async (userId: string) => {
return creditData.credits_remaining;
};
type ProductWithPrices = Product & {
prices: Price[];
actualPrice?: number;
};
const getActiveProductsWithPrices = async (limit = 10, offset = 0) => {
const {
data: products,
error,
count
} = await supabaseAdmin
.from('products')
.select('*, prices(*)', { count: 'exact' })
.eq('active', true)
.order('name', { ascending: true })
.range(offset, offset + limit - 1);
if (error) {
throw new Error(`Error fetching products: ${error.message}`);
}
const typedProducts: ProductWithPrices[] = products;
for (const product of typedProducts) {
if (product.prices && product.prices.length > 0) {
// Assuming the default price is the first one
const defaultPrice = product.prices[0];
product.actualPrice = defaultPrice.unit_amount! / 100;
}
}
return { products: typedProducts, count }; // Return products and total count
};
export {
upsertProductRecord,
upsertPriceRecord,
@@ -420,5 +454,6 @@ export {
hasProductAccess,
addCredits,
deductCredits,
getUserCredits
getUserCredits,
getActiveProductsWithPrices
};