diff --git a/apps/api/v1/routes/transactions.ts b/apps/api/v1/routes/transactions.ts index fae1f749..d2c04eb8 100644 --- a/apps/api/v1/routes/transactions.ts +++ b/apps/api/v1/routes/transactions.ts @@ -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( @@ -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') @@ -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() diff --git a/apps/api/v1/services/transaction.service.ts b/apps/api/v1/services/transaction.service.ts index 2781b74f..13c7a7dd 100644 --- a/apps/api/v1/services/transaction.service.ts +++ b/apps/api/v1/services/transaction.service.ts @@ -102,6 +102,9 @@ export async function findTransaction({ where: { id: transactionId, }, + include: { + category: true, + }, }) } @@ -117,6 +120,9 @@ export async function createTransaction({ ...data, createdByUserId: user.id, }, + include: { + category: true, + }, }) return transaction @@ -134,6 +140,9 @@ export async function updateTransaction({ id: transactionId, }, data, + include: { + category: true, + }, }) return transaction