Skip to content

Commit

Permalink
use next cache for users invoices
Browse files Browse the repository at this point in the history
  • Loading branch information
collinschaafsma committed Oct 27, 2024
1 parent 2b12110 commit 02fef88
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions services/currentUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "server-only"
import { cache } from "react"
import { unstable_cache as ioCache } from "next/cache"
import { and, eq, inArray } from "drizzle-orm"
import { Session } from "next-auth"
import Stripe from "stripe"
Expand Down Expand Up @@ -168,22 +169,25 @@ const currentUserService: CurrentUserService = {
* @param {number} page - The page number to get.
* @returns {Promise<Invoice[]>} - The invoices for the user.
*/
invoices: cache(async ({ startingAfter, userId }: InvoiceParams) => {
const customerId = await currentUserService.customerId({ userId })
const invoices = await stripe.invoices.list({
customer: customerId || undefined,
limit: invoicesLimit,
starting_after: startingAfter,
})
invoices: ({ startingAfter, userId }: InvoiceParams) =>
// todo: use, use cache once available
ioCache(
async () => {
const customerId = await currentUserService.customerId({ userId })
const invoices = await stripe.invoices.list({
customer: customerId || undefined,
limit: invoicesLimit,
starting_after: startingAfter,
})

return invoices.data
// return await db.query.invoices.findMany({
// where: (invoices, { eq }) => eq(invoices.userId, userId),
// orderBy: (invoices, { desc }) => desc(invoices.created),
// limit: invoicesLimit,
// offset: (page - 1) * invoicesLimit,
// })
}),
return invoices.data
},
[userId],
{
tags: ["invoices", userId],
revalidate: 60 * 60 * 24, // 24 hours
}
)(),
/**
* PaymentMethods
*
Expand Down

0 comments on commit 02fef88

Please sign in to comment.