feat(#76): Add pagination for stripe orders on account page

This commit is contained in:
Josh Creek
2024-11-11 20:04:02 +00:00
parent 62f7525238
commit c77dc6ac47
3 changed files with 110 additions and 10 deletions
+16 -2
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,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',