Merge pull request #95 from jcreek/76-add-pagination-for-stripe-orders-on-account-page

76 add pagination for stripe orders on account page
This commit is contained in:
Olly
2024-11-19 20:05:16 +00:00
committed by GitHub
3 changed files with 146 additions and 11 deletions
+19 -3
View File
@@ -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,21 @@ 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);
const hasNextPage = charges.has_more;
// 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',
@@ -626,7 +642,7 @@ const getUserTransactions = async (userId: string) => {
receipt_url: charge.receipt_url
}));
return transactions;
return { transactions, hasNextPage };
} catch (error) {
console.error('An error occurred while retrieving transactions:', error);
throw new Error('Could not retrieve transactions');