mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 02:53:50 +00:00
feat(#29): Add example product tool requiring purchase to access
This commit is contained in:
@@ -20,4 +20,4 @@
|
||||
<li><a href="/">Submenu 2</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="/">Item 3</a></li>
|
||||
<li><a href="/tools/exampleproduct">Example Product</a></li>
|
||||
|
||||
Vendored
+10
@@ -201,6 +201,7 @@ export type Database = {
|
||||
id: string
|
||||
metadata: Json | null
|
||||
price_id: string | null
|
||||
product_id: string
|
||||
quantity: number | null
|
||||
status: Database["public"]["Enums"]["subscription_status"] | null
|
||||
trial_end: string | null
|
||||
@@ -218,6 +219,7 @@ export type Database = {
|
||||
id: string
|
||||
metadata?: Json | null
|
||||
price_id?: string | null
|
||||
product_id: string
|
||||
quantity?: number | null
|
||||
status?: Database["public"]["Enums"]["subscription_status"] | null
|
||||
trial_end?: string | null
|
||||
@@ -235,6 +237,7 @@ export type Database = {
|
||||
id?: string
|
||||
metadata?: Json | null
|
||||
price_id?: string | null
|
||||
product_id?: string
|
||||
quantity?: number | null
|
||||
status?: Database["public"]["Enums"]["subscription_status"] | null
|
||||
trial_end?: string | null
|
||||
@@ -249,6 +252,13 @@ export type Database = {
|
||||
referencedRelation: "prices"
|
||||
referencedColumns: ["id"]
|
||||
},
|
||||
{
|
||||
foreignKeyName: "subscriptions_product_id_fkey"
|
||||
columns: ["product_id"]
|
||||
isOneToOne: false
|
||||
referencedRelation: "products"
|
||||
referencedColumns: ["id"]
|
||||
},
|
||||
{
|
||||
foreignKeyName: "subscriptions_user_id_fkey"
|
||||
columns: ["user_id"]
|
||||
|
||||
@@ -211,6 +211,7 @@ const manageSubscriptionStatusChange = async (
|
||||
user_id: uuid,
|
||||
metadata: subscription.metadata,
|
||||
status: subscription.status,
|
||||
product_id: subscription.items.data[0].price.product as string,
|
||||
price_id: subscription.items.data[0].price.id,
|
||||
quantity: 1, //subscription.quantity,
|
||||
cancel_at_period_end: subscription.cancel_at_period_end,
|
||||
@@ -264,6 +265,39 @@ const recordProductPurchase = async (userId: string, productId: string, priceId:
|
||||
console.log(`Product purchased recorded for user [${userId}] and product [${productId}]`);
|
||||
};
|
||||
|
||||
const hasProductAccess = async (userId: string, productId: string) => {
|
||||
// Check if user has purchased the product
|
||||
const { data: purchases, error: purchaseError } = await supabaseAdmin
|
||||
.from('purchases')
|
||||
.select('*')
|
||||
.eq('user_id', userId)
|
||||
.eq('product_id', productId);
|
||||
|
||||
if (purchaseError) throw new Error(purchaseError.message);
|
||||
|
||||
if (purchases && purchases.length > 0) {
|
||||
// User has purchased the product
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if user has an active subscription for the product
|
||||
const { data: subscriptions, error: subError } = await supabaseAdmin
|
||||
.from('subscriptions')
|
||||
.select('*')
|
||||
.eq('user_id', userId)
|
||||
.eq('product_id', productId)
|
||||
.in('status', ['active', 'trialing']);
|
||||
|
||||
if (subError) throw new Error(subError.message);
|
||||
|
||||
if (subscriptions && subscriptions.length > 0) {
|
||||
// User has an active subscription
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
export {
|
||||
upsertProductRecord,
|
||||
upsertPriceRecord,
|
||||
@@ -271,5 +305,6 @@ export {
|
||||
deletePriceRecord,
|
||||
createOrRetrieveCustomer,
|
||||
manageSubscriptionStatusChange,
|
||||
recordProductPurchase
|
||||
recordProductPurchase,
|
||||
hasProductAccess
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user