mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 02:53:50 +00:00
feat(#6): Add a basic product page using the db rather than stripe
This commit is contained in:
@@ -409,6 +409,40 @@ const getUserCredits = async (userId: string) => {
|
|||||||
return creditData.credits_remaining;
|
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 {
|
export {
|
||||||
upsertProductRecord,
|
upsertProductRecord,
|
||||||
upsertPriceRecord,
|
upsertPriceRecord,
|
||||||
@@ -420,5 +454,6 @@ export {
|
|||||||
hasProductAccess,
|
hasProductAccess,
|
||||||
addCredits,
|
addCredits,
|
||||||
deductCredits,
|
deductCredits,
|
||||||
getUserCredits
|
getUserCredits,
|
||||||
|
getActiveProductsWithPrices
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -6,12 +6,11 @@
|
|||||||
let enablePagination = false;
|
let enablePagination = false;
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
const response = await fetch('/products');
|
const response = await fetch('/products?limit=20&offset=0');
|
||||||
const responseJson = await response.json();
|
const responseJson = await response.json();
|
||||||
products = responseJson.data;
|
products = responseJson.products;
|
||||||
if (responseJson.has_more) {
|
var count = responseJson.count;
|
||||||
enablePagination = true;
|
// enablePagination needs to be set to true if there are more than 20 products
|
||||||
}
|
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,19 @@
|
|||||||
import { json } from '@sveltejs/kit';
|
import { json } from '@sveltejs/kit';
|
||||||
import { stripe as stripeClient } from '$lib/utils/stripe';
|
import { getActiveProductsWithPrices } from '$lib/utils/supabase/admin';
|
||||||
|
|
||||||
export const GET = async () => {
|
|
||||||
const products = await stripeClient.products.list({
|
|
||||||
limit: 100
|
|
||||||
});
|
|
||||||
|
|
||||||
|
export const GET = async ({ url }) => {
|
||||||
try {
|
try {
|
||||||
// Get the price for each product
|
const limitParam = url.searchParams.get('limit');
|
||||||
for (const product of products.data) {
|
const offsetParam = url.searchParams.get('offset');
|
||||||
const price = await stripeClient.prices.retrieve(product.default_price as string);
|
|
||||||
if (price) {
|
const limit = limitParam ? parseInt(limitParam, 10) : 10;
|
||||||
product.actualPrice = price.unit_amount / 100;
|
const offset = offsetParam ? parseInt(offsetParam, 10) : 0;
|
||||||
}
|
|
||||||
}
|
const { products, count } = await getActiveProductsWithPrices(limit, offset);
|
||||||
|
|
||||||
|
return json({ products, count });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
return json({ error: error.message }, { status: 500 });
|
||||||
}
|
}
|
||||||
return json(products);
|
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user