Skip to content

Commit

Permalink
feat(api): [Transaction] add get transaction detail api (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkdev98 authored Jul 15, 2024
1 parent 90906f3 commit 82e2d0e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 30 deletions.
68 changes: 38 additions & 30 deletions apps/api/v1/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ import {
} from '../services/transaction.service'
import { canUserReadWallet, findUserWallet } from '../services/wallet.service'

const zTransactionParamValidator = zValidator(
'param',
z.object({
transactionId: z.string(),
}),
)

const router = new Hono()

.get(
Expand Down Expand Up @@ -157,14 +164,24 @@ const router = new Hono()
return c.json(transaction, 201)
})

.get('/:transactionId', zTransactionParamValidator, async (c) => {
const user = getAuthUserStrict(c)
const { transactionId } = c.req.valid('param')

const transaction = await findTransaction({ transactionId })

if (
!(transaction && (await canUserReadTransaction({ user, transaction })))
) {
return c.json({ message: 'transaction not found' }, 404)
}

return c.json(transaction)
})

.put(
'/:transactionId',
zValidator(
'param',
z.object({
transactionId: z.string(),
}),
),
zTransactionParamValidator,
zValidator('json', zUpdateTransaction),
async (c) => {
const { transactionId } = c.req.valid('param')
Expand Down Expand Up @@ -236,35 +253,26 @@ const router = new Hono()
},
)

.delete(
'/:transactionId',
zValidator(
'param',
z.object({
transactionId: z.string(),
}),
),
async (c) => {
const { transactionId } = c.req.valid('param')
const user = getAuthUserStrict(c)
.delete('/:transactionId', zTransactionParamValidator, async (c) => {
const { transactionId } = c.req.valid('param')
const user = getAuthUserStrict(c)

const transaction = await findTransaction({ transactionId })
const transaction = await findTransaction({ transactionId })

if (
!(transaction && (await canUserReadTransaction({ user, transaction })))
) {
return c.json({ message: 'transaction not found' }, 404)
}
if (
!(transaction && (await canUserReadTransaction({ user, transaction })))
) {
return c.json({ message: 'transaction not found' }, 404)
}

if (!(await canUserDeleteTransaction({ user, transaction }))) {
return c.json({ message: 'user cannot delete transaction' }, 403)
}
if (!(await canUserDeleteTransaction({ user, transaction }))) {
return c.json({ message: 'user cannot delete transaction' }, 403)
}

await deleteTransaction({ transactionId })
await deleteTransaction({ transactionId })

return c.json(transaction)
},
)
return c.json(transaction)
})

.post('/ai', async (c) => {
const body = await c.req.parseBody()
Expand Down
9 changes: 9 additions & 0 deletions apps/api/v1/services/transaction.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ export async function findTransaction({
where: {
id: transactionId,
},
include: {
category: true,
},
})
}

Expand All @@ -117,6 +120,9 @@ export async function createTransaction({
...data,
createdByUserId: user.id,
},
include: {
category: true,
},
})

return transaction
Expand All @@ -134,6 +140,9 @@ export async function updateTransaction({
id: transactionId,
},
data,
include: {
category: true,
},
})

return transaction
Expand Down

0 comments on commit 82e2d0e

Please sign in to comment.