refactor(#65): add additional error handling & logs

This commit is contained in:
OllyNicholass
2024-11-05 22:06:36 +00:00
parent d329989230
commit 3cd3200d1e
+18 -3
View File
@@ -255,10 +255,17 @@ const manageSubscriptionStatusChange = async (
trial_end: subscription.trial_end ? toDateTime(subscription.trial_end).toISOString() : null trial_end: subscription.trial_end ? toDateTime(subscription.trial_end).toISOString() : null
}; };
try {
const { error: upsertError } = await supabaseAdmin const { error: upsertError } = await supabaseAdmin
.from('subscriptions') .from('subscriptions')
.upsert([subscriptionData]); .upsert([subscriptionData]);
if (upsertError) throw new Error(`Subscription insert/update failed: ${upsertError.message}`); if (upsertError) {
throw new Error(`Subscription insert/update failed: ${upsertError.message}`);
}
} catch (error) {
logger.error(error);
}
logger.info(`Inserted/updated subscription [${subscription.id}] for user [${uuid}]`); logger.info(`Inserted/updated subscription [${subscription.id}] for user [${uuid}]`);
// // For a new subscription copy the billing details to the customer object. // // For a new subscription copy the billing details to the customer object.
@@ -293,6 +300,7 @@ const recordProductPurchase = async (userId: string, productId: string, priceId:
const hasProductAccess = async (userId: string, productId: string) => { const hasProductAccess = async (userId: string, productId: string) => {
// Check if user has purchased the product // Check if user has purchased the product
try {
const { data: purchases, error: purchaseError } = await supabaseAdmin const { data: purchases, error: purchaseError } = await supabaseAdmin
.from('purchases') .from('purchases')
.select('*') .select('*')
@@ -305,8 +313,12 @@ const hasProductAccess = async (userId: string, productId: string) => {
// User has purchased the product // User has purchased the product
return true; return true;
} }
} catch (error) {
logger.error(error);
}
// Check if user has an active subscription for the product // Check if user has an active subscription for the product
try {
const { data: subscriptions, error: subError } = await supabaseAdmin const { data: subscriptions, error: subError } = await supabaseAdmin
.from('subscriptions') .from('subscriptions')
.select('*') .select('*')
@@ -320,6 +332,9 @@ const hasProductAccess = async (userId: string, productId: string) => {
// User has an active subscription // User has an active subscription
return true; return true;
} }
} catch (error) {
logger.error(error);
}
return false; return false;
}; };
@@ -490,7 +505,7 @@ const getUserSubscriptions = async (userId: string) => {
.eq('user_id', userId); .eq('user_id', userId);
if (error) { if (error) {
console.error('Error retrieving subscriptions:', error.message); logger.error('Error retrieving subscriptions:', error.message);
throw new Error('Failed to fetch subscriptions'); throw new Error('Failed to fetch subscriptions');
} }
@@ -521,7 +536,7 @@ const getUserSubscriptions = async (userId: string) => {
return subscriptionDetails; return subscriptionDetails;
} catch (error) { } catch (error) {
console.error('An error occurred while retrieving subscription details:', error); logger.error('An error occurred while retrieving subscription details:', error);
throw new Error('Could not retrieve subscription details'); throw new Error('Could not retrieve subscription details');
} }
}; };