mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-13 02:53:50 +00:00
feat(#65): Replace existing console logs
This commit is contained in:
@@ -4,6 +4,7 @@ import type { Database, Tables, TablesInsert } from '$lib/types/supabase';
|
||||
import { PUBLIC_SUPABASE_URL } from '$env/static/public';
|
||||
import { SUPABASE_SERVICE_ROLE_KEY } from '$env/static/private';
|
||||
import { stripe as stripeClient } from '$lib/utils/stripe';
|
||||
import logger from '$lib/utils/logger/logger';
|
||||
|
||||
const toDateTime = (secs: number) => {
|
||||
const t = new Date(+0); // Unix epoch start.
|
||||
@@ -39,7 +40,7 @@ const upsertProductRecord = async (product: stripe.Product) => {
|
||||
|
||||
const { error: upsertError } = await supabaseAdmin.from('products').upsert([productData]);
|
||||
if (upsertError) throw new Error(`Product insert/update failed: ${upsertError.message}`);
|
||||
console.log(`Product inserted/updated: ${product.id}`);
|
||||
logger.info(`Product inserted/updated: ${product.id}`);
|
||||
};
|
||||
|
||||
const upsertPriceRecord = async (price: stripe.Price, retryCount = 0, maxRetries = 3) => {
|
||||
@@ -61,7 +62,7 @@ const upsertPriceRecord = async (price: stripe.Price, retryCount = 0, maxRetries
|
||||
|
||||
if (upsertError?.message.includes('foreign key constraint')) {
|
||||
if (retryCount < maxRetries) {
|
||||
console.log(`Retry attempt ${retryCount + 1} for price ID: ${price.id}`);
|
||||
logger.info(`Retry attempt ${retryCount + 1} for price ID: ${price.id}`);
|
||||
await new Promise((resolve) => setTimeout(resolve, 2000));
|
||||
await upsertPriceRecord(price, retryCount + 1, maxRetries);
|
||||
} else {
|
||||
@@ -72,7 +73,7 @@ const upsertPriceRecord = async (price: stripe.Price, retryCount = 0, maxRetries
|
||||
} else if (upsertError) {
|
||||
throw new Error(`Price insert/update failed: ${upsertError.message}`);
|
||||
} else {
|
||||
console.log(`Price inserted/updated: ${price.id}`);
|
||||
logger.info(`Price inserted/updated: ${price.id}`);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -82,13 +83,13 @@ const deleteProductRecord = async (product: stripe.Product) => {
|
||||
.delete()
|
||||
.eq('id', product.id);
|
||||
if (deletionError) throw new Error(`Product deletion failed: ${deletionError.message}`);
|
||||
console.log(`Product deleted: ${product.id}`);
|
||||
logger.info(`Product deleted: ${product.id}`);
|
||||
};
|
||||
|
||||
const deletePriceRecord = async (price: stripe.Price) => {
|
||||
const { error: deletionError } = await supabaseAdmin.from('prices').delete().eq('id', price.id);
|
||||
if (deletionError) throw new Error(`Price deletion failed: ${deletionError.message}`);
|
||||
console.log(`Price deleted: ${price.id}`);
|
||||
logger.info(`Price deleted: ${price.id}`);
|
||||
};
|
||||
|
||||
const upsertCustomerToSupabase = async (uuid: string, customerId: string) => {
|
||||
@@ -258,7 +259,7 @@ const manageSubscriptionStatusChange = async (
|
||||
.from('subscriptions')
|
||||
.upsert([subscriptionData]);
|
||||
if (upsertError) throw new Error(`Subscription insert/update failed: ${upsertError.message}`);
|
||||
console.log(`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.
|
||||
// // NOTE: This is a costly operation and should happen at the very end.
|
||||
@@ -271,7 +272,7 @@ const manageSubscriptionStatusChange = async (
|
||||
};
|
||||
|
||||
const recordProductPurchase = async (userId: string, productId: string, priceId: string) => {
|
||||
console.log(
|
||||
logger.info(
|
||||
`Recording product purchase for user [${userId}] and product [${productId}] with price [${priceId}]`
|
||||
);
|
||||
|
||||
@@ -284,10 +285,10 @@ const recordProductPurchase = async (userId: string, productId: string, priceId:
|
||||
throw new Error(`Failed to record product purchase: ${error.message}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
logger.error(error);
|
||||
}
|
||||
|
||||
console.log(`Product purchased recorded for user [${userId}] and product [${productId}]`);
|
||||
logger.info(`Product purchased recorded for user [${userId}] and product [${productId}]`);
|
||||
};
|
||||
|
||||
const hasProductAccess = async (userId: string, productId: string) => {
|
||||
@@ -381,7 +382,7 @@ const updateUserCredits = async (userId: string, creditsChange: number, descript
|
||||
|
||||
await logCreditTransaction(userId, creditsChange, description);
|
||||
|
||||
console.log(
|
||||
logger.info(
|
||||
`${creditsChange > 0 ? 'Added' : 'Deducted'} ${Math.abs(creditsChange)} credits to user [${userId}]`
|
||||
);
|
||||
};
|
||||
@@ -390,7 +391,7 @@ const addCredits = async (userId: string, creditsToAdd: number, description: str
|
||||
try {
|
||||
await updateUserCredits(userId, creditsToAdd, description);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
logger.error(error);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -398,7 +399,7 @@ const deductCredits = async (userId: string, creditsToDeduct: number, descriptio
|
||||
try {
|
||||
await updateUserCredits(userId, -creditsToDeduct, description);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
logger.error(error);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { fail, redirect } from '@sveltejs/kit';
|
||||
import type { Actions, PageServerLoad } from './$types';
|
||||
import logger from '$lib/utils/logger/logger';
|
||||
|
||||
export const load: PageServerLoad = async ({ locals: { supabase, safeGetSession } }) => {
|
||||
const { session } = await safeGetSession();
|
||||
@@ -27,20 +28,23 @@ export const actions: Actions = {
|
||||
if (!session) {
|
||||
return fail(401, { name });
|
||||
}
|
||||
const { error } = await supabase.from('users').update({
|
||||
name: name,
|
||||
}).eq('id', session?.user.id);
|
||||
const { error } = await supabase
|
||||
.from('users')
|
||||
.update({
|
||||
name: name
|
||||
})
|
||||
.eq('id', session?.user.id);
|
||||
|
||||
console.error(error);
|
||||
logger.error(error);
|
||||
|
||||
if (error) {
|
||||
return fail(500, {
|
||||
name,
|
||||
name
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
name,
|
||||
name
|
||||
};
|
||||
},
|
||||
signout: async ({ locals: { supabase, safeGetSession } }) => {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { type RequestHandler, redirect } from '@sveltejs/kit';
|
||||
import { stripe as stripeClient } from '$lib/utils/stripe';
|
||||
import { getStripeCustomerId } from '$lib/utils/supabase/admin';
|
||||
import type Stripe from 'stripe';
|
||||
import logger from '$lib/utils/logger/logger';
|
||||
|
||||
// Create a checkout session
|
||||
export const POST: RequestHandler = async ({ request, cookies, locals: { safeGetSession } }) => {
|
||||
@@ -34,7 +35,7 @@ export const POST: RequestHandler = async ({ request, cookies, locals: { safeGet
|
||||
try {
|
||||
stripeCustomerId = await getStripeCustomerId(userEmail!, userId);
|
||||
} catch (error) {
|
||||
console.error('Error fetching Stripe customer ID:', error);
|
||||
logger.error('Error fetching Stripe customer ID:', error);
|
||||
}
|
||||
|
||||
const stripeCheckoutSessionObject = {
|
||||
|
||||
@@ -2,6 +2,7 @@ import { type RequestHandler, redirect } from '@sveltejs/kit';
|
||||
import { stripe as stripeClient } from '$lib/utils/stripe';
|
||||
import { getStripeCustomerId } from '$lib/utils/supabase/admin';
|
||||
import type Stripe from 'stripe';
|
||||
import logger from '$lib/utils/logger/logger';
|
||||
|
||||
// Create a subscription checkout session
|
||||
export const POST: RequestHandler = async ({ request, cookies, locals: { safeGetSession } }) => {
|
||||
@@ -29,7 +30,7 @@ export const POST: RequestHandler = async ({ request, cookies, locals: { safeGet
|
||||
try {
|
||||
stripeCustomerId = await getStripeCustomerId(userEmail!, userId);
|
||||
} catch (error) {
|
||||
console.error('Error fetching Stripe customer ID:', error);
|
||||
logger.error('Error fetching Stripe customer ID:', error);
|
||||
}
|
||||
|
||||
const stripeCheckoutSessionObject = {
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
recordProductPurchase
|
||||
} from '$lib/utils/supabase/admin';
|
||||
import { stripe as stripeClient } from '$lib/utils/stripe';
|
||||
import logger from '$lib/utils/logger/logger';
|
||||
|
||||
export const POST: RequestHandler = async ({ request }) => {
|
||||
const body = await request.text();
|
||||
@@ -26,7 +27,7 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||
try {
|
||||
event = stripeClient.webhooks.constructEvent(body, signature, STRIPE_ENDPOINT_SECRET);
|
||||
} catch (err) {
|
||||
console.error(`⚠️ Webhook signature verification failed.`, err.message);
|
||||
logger.error(`⚠️ Webhook signature verification failed.`, err.message);
|
||||
return {
|
||||
status: 400,
|
||||
body: {}
|
||||
@@ -104,7 +105,7 @@ export const POST: RequestHandler = async ({ request }) => {
|
||||
break;
|
||||
}
|
||||
default:
|
||||
console.log(`Unhandled event type ${event.type}`);
|
||||
logger.info(`Unhandled event type ${event.type}`);
|
||||
}
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : 'An unknown error has occurred';
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import { redirect } from '@sveltejs/kit';
|
||||
import Stripe from 'stripe';
|
||||
import { getProductById, upsertCustomerToSupabase } from '$lib/utils/supabase/admin';
|
||||
import logger from '$lib/utils/logger/logger';
|
||||
|
||||
export const load = async ({ fetch, locals: { safeGetSession } }) => {
|
||||
const { session } = await safeGetSession();
|
||||
|
||||
const response = await fetch('/api/checkout/status');
|
||||
if (!response.ok) {
|
||||
console.error('Failed to fetch checkout status');
|
||||
logger.error('Failed to fetch checkout status');
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -30,7 +31,7 @@ export const load = async ({ fetch, locals: { safeGetSession } }) => {
|
||||
const stripeCustomerId = checkoutSession.customer as string;
|
||||
await upsertCustomerToSupabase(session.user.id, stripeCustomerId);
|
||||
} else {
|
||||
console.error('User ID is undefined');
|
||||
logger.error('User ID is undefined');
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { getActiveProductsWithPrices } from '$lib/utils/supabase/admin';
|
||||
import logger from '$lib/utils/logger/logger';
|
||||
|
||||
export const GET = async ({ url }) => {
|
||||
try {
|
||||
@@ -13,7 +14,7 @@ export const GET = async ({ url }) => {
|
||||
|
||||
return json({ products, count });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
logger.error(error);
|
||||
return json({ error: error.message }, { status: 500 });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user