Skip to content

Commit

Permalink
feat(api): [Transaction] Paginate transactions by date instead of cre…
Browse files Browse the repository at this point in the history
…atedAt (#103)

Also populate category field on transaction record.
  • Loading branch information
bkdev98 committed Jul 12, 2024
1 parent bc11f7f commit 082cf37
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
8 changes: 4 additions & 4 deletions apps/api/v1/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ const router = new Hono()
z.object({
wallet_id: z.string().optional(),
budget_id: z.string().optional(),
before: z.date().optional(),
after: z.date().optional(),
first: z.number().optional(),
last: z.number().optional(),
before: z.date({ coerce: true }).optional(),
after: z.date({ coerce: true }).optional(),
first: z.number({ coerce: true }).optional(),
last: z.number({ coerce: true }).optional(),
}),
),
async (c) => {
Expand Down
11 changes: 7 additions & 4 deletions apps/api/v1/services/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export async function listTransactions({
const transactions = await prisma.transaction.findMany({
where: {
...query,
createdAt: {
date: {
...(pagination.before && {
lt: pagination.before,
}),
Expand All @@ -180,9 +180,12 @@ export async function listTransactions({
},
},
orderBy: {
createdAt: 'desc',
date: 'desc',
},
take: pagination.first || pagination.last,
include: {
category: true,
},
})

const totalCount = await prisma.transaction.count({
Expand All @@ -193,10 +196,10 @@ export async function listTransactions({
hasMore: transactions.length > (pagination.first || pagination.last || 0),
totalCount,
...(pagination.first && {
before: transactions[0]?.createdAt,
before: transactions[0]?.date,
}),
...(pagination.last && {
after: transactions[transactions.length - 1]?.createdAt,
after: transactions[transactions.length - 1]?.date,
}),
}

Expand Down

0 comments on commit 082cf37

Please sign in to comment.