Skip to content

Commit

Permalink
feat(api): [Transaction] add list transactions (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
duongdev authored Jun 9, 2024
1 parent c77f02c commit fc20266
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 8 deletions.
59 changes: 51 additions & 8 deletions apps/api/v1/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
createTransaction,
deleteTransaction,
findTransaction,
listTransactions,
updateTransaction,
} from '../services/transaction.service'
import { findUserWallet } from '../services/wallet.service'
Expand All @@ -26,19 +27,61 @@ const router = new Hono()
zValidator(
'query',
z.object({
order_by: z.enum(['date']).optional(),
order: z.enum(['asc', 'desc']).optional(),
wallet_id: z.string().optional(),
budget_id: z.string().optional(),
from_date: z.string().optional(),
to_date: z.string().optional(),
take: z.number().optional(),
skip: z.number().optional(),
cursor: z.string().optional(),
before: z.date().optional(),
after: z.date().optional(),
first: z.number().optional(),
last: z.number().optional(),
}),
),
async (c) => {
return c.json([])
const user = getAuthUserStrict(c)
const {
wallet_id: walletAccountId,
budget_id: budgetId,
before,
after,
first,
last,
} = c.req.valid('query')

const query: {
createdByUserId?: string
budgetId?: string
walletAccountId?: string
} = {
budgetId,
walletAccountId,
}

// If both budget and wallet are not provided, user can only see their own transactions
if (!budgetId && !walletAccountId) {
query.createdByUserId = user.id
}

// If budget is provided, user must be a member of the budget
if (budgetId) {
const budget = await findBudget({ budgetId })
if (!budget || !(await canUserReadBudget({ user, budget }))) {
return c.json({ message: 'budget not found' }, 404)
}
}

// If wallet is provided, user must have access to the wallet
if (walletAccountId) {
const wallet = await findUserWallet({ user, walletId: walletAccountId })
if (!wallet) {
return c.json({ message: 'wallet not found' }, 404)
}
}

return c.json(
await listTransactions({
query,
pagination: { before, after, first, last },
}),
)
},
)

Expand Down
52 changes: 52 additions & 0 deletions apps/api/v1/services/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,55 @@ export async function deleteTransaction({
},
})
}

export async function listTransactions({
query,
pagination,
}: {
query: {
createdByUserId?: string
budgetId?: string
walletAccountId?: string
}
pagination: {
before?: Date
last?: number
after?: Date
first?: number
}
}) {
const transactions = await prisma.transaction.findMany({
where: {
...query,
createdAt: {
...(pagination.before && {
lt: pagination.before,
}),
...(pagination.after && {
gt: pagination.after,
}),
},
},
orderBy: {
createdAt: 'desc',
},
take: pagination.first || pagination.last,
})

const totalCount = await prisma.transaction.count({
where: query,
})

const paginationMeta = {
hasMore: transactions.length > (pagination.first || pagination.last || 0),
totalCount,
...(pagination.first && {
before: transactions[0]?.createdAt,
}),
...(pagination.last && {
after: transactions[transactions.length - 1]?.createdAt,
}),
}

return { transactions, meta: { paginationMeta } }
}

0 comments on commit fc20266

Please sign in to comment.