feat(#29): Add example product tool requiring purchase to access

This commit is contained in:
Josh Creek
2024-09-22 12:06:39 +01:00
parent a4764c547e
commit fce4c0e0a5
11 changed files with 101 additions and 3 deletions
+1 -1
View File
@@ -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>
+10
View File
@@ -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"]
+36 -1
View File
@@ -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
};