feat(#29): Add ability to record that a user has purchased a product - part 2

This commit is contained in:
Josh Creek
2024-09-21 20:26:36 +01:00
parent d4f7f5041d
commit 05891ad510
5 changed files with 448 additions and 634 deletions
+376 -624
View File
File diff suppressed because it is too large Load Diff
+22 -2
View File
@@ -11,7 +11,6 @@ const toDateTime = (secs: number) => {
return t;
};
type Product = Tables<'products'>;
type Price = Tables<'prices'>;
@@ -245,11 +244,32 @@ const manageSubscriptionStatusChange = async (
// }
};
const recordProductPurchase = async (userId: string, productId: string, priceId: string) => {
console.log(
`Recording product purchase for user [${userId}] and product [${productId}] with price [${priceId}]`
);
try {
const { error } = await supabaseAdmin
.from('purchases')
.insert([{ user_id: userId, product_id: productId, price_id: priceId }]);
if (error) {
throw new Error(`Failed to record product purchase: ${error.message}`);
}
} catch (error) {
console.error(error);
}
console.log(`Product purchased recorded for user [${userId}] and product [${productId}]`);
};
export {
upsertProductRecord,
upsertPriceRecord,
deleteProductRecord,
deletePriceRecord,
createOrRetrieveCustomer,
manageSubscriptionStatusChange
manageSubscriptionStatusChange,
recordProductPurchase
};