From c77dc6ac47cc6c271ef8815578183b94427c25b7 Mon Sep 17 00:00:00 2001 From: Josh Creek <8179928+jcreek@users.noreply.github.com> Date: Mon, 11 Nov 2024 20:04:02 +0000 Subject: [PATCH 1/5] feat(#76): Add pagination for stripe orders on account page --- src/lib/utils/supabase/admin.ts | 18 +++++++- src/routes/account/+page.server.ts | 33 +++++++++++++- src/routes/account/+page.svelte | 69 +++++++++++++++++++++++++++--- 3 files changed, 110 insertions(+), 10 deletions(-) diff --git a/src/lib/utils/supabase/admin.ts b/src/lib/utils/supabase/admin.ts index 659f3ed..06bb5b2 100644 --- a/src/lib/utils/supabase/admin.ts +++ b/src/lib/utils/supabase/admin.ts @@ -590,7 +590,12 @@ const getUserSubscriptions = async (userId: string) => { } }; -const getUserTransactions = async (userId: string) => { +const getUserTransactions = async ( + userId: string, + perPage: number, + startingAfter: string | undefined = undefined, + endingBefore: string | undefined = undefined +) => { try { // Step 1: Retrieve the customer ID associated with the user const { data, error } = await supabaseAdmin @@ -610,10 +615,19 @@ const getUserTransactions = async (userId: string) => { } // Step 2: Fetch charge transactions from Stripe - const charges = await stripeClient.charges.list({ customer: customerId }); + const params: stripe.ChargeListParams = { + customer: customerId, + limit: perPage + }; + + if (startingAfter) params.starting_after = startingAfter; + if (endingBefore) params.ending_before = endingBefore; + + const charges = await stripeClient.charges.list(params); // Step 3: Format transaction data const transactions = charges.data.map((charge) => ({ + id: charge.id, amount: (charge.amount / 100).toFixed(2), currency: charge.currency.toUpperCase(), description: charge.description ?? 'No description provided', diff --git a/src/routes/account/+page.server.ts b/src/routes/account/+page.server.ts index f03bd02..3bb27e6 100644 --- a/src/routes/account/+page.server.ts +++ b/src/routes/account/+page.server.ts @@ -4,6 +4,8 @@ import logger from '$lib/utils/logger/logger'; import { getUserSubscriptions, getUserTransactions } from '$lib/utils/supabase/admin'; import { requestAccountDeletion } from '$lib/utils/supabase/admin'; +const transactionsPerPage = 5; + export const load: PageServerLoad = async ({ locals: { supabase, safeGetSession } }) => { const { session } = await safeGetSession(); @@ -19,9 +21,9 @@ export const load: PageServerLoad = async ({ locals: { supabase, safeGetSession const subscriptions = await getUserSubscriptions(session.user.id); - const transactions = await getUserTransactions(session.user.id); + const transactions = await getUserTransactions(session.user.id, transactionsPerPage); - return { session, user, subscriptions, transactions }; + return { session, user, subscriptions, transactions, pageSize: transactionsPerPage }; }; export const actions: Actions = { @@ -78,5 +80,32 @@ export const actions: Actions = { return { name }; + }, + paginate: async ({ request, locals: { safeGetSession } }) => { + const formData = await request.formData(); + const pageSize = transactionsPerPage; + const startingAfter = formData.get('startingAfter')?.toString() || undefined; + const endingBefore = formData.get('endingBefore')?.toString() || undefined; + + const { session } = await safeGetSession(); + if (!session) return fail(401, { error: 'Not authenticated' }); + + try { + const transactions = await getUserTransactions( + session.user.id, + pageSize, + startingAfter, + endingBefore + ); + + const serializableTransactions = transactions.map((transaction) => ({ ...transaction })); + + const stringifiedTransactions: string = JSON.stringify(serializableTransactions); + + return { stringifiedTransactions }; + } catch (error) { + logger.error('Failed to fetch transactions', { userId: session.user.id, error }); + return fail(500, { error: 'Failed to load transactions' }); + } } }; diff --git a/src/routes/account/+page.svelte b/src/routes/account/+page.svelte index 1c1e4fb..0bf4dbe 100644 --- a/src/routes/account/+page.svelte +++ b/src/routes/account/+page.svelte @@ -1,12 +1,13 @@
No transactions found.
+ {/if} + + +