import { Router } from 'express'; import { asyncHandler } from '../utils'; import * as transactionService from '../services/transactions'; const router = Router(); router.delete( '/', asyncHandler(async (_req, res) => { const result = await transactionService.clearAllTransactions(); res.json(result); }), ); router.get( '/', asyncHandler(async (req, res) => { const q = req.query; const result = await transactionService.getTransactions({ accountId: q.accountId ? Number(q.accountId) : undefined, from: q.from as string | undefined, to: q.to as string | undefined, direction: q.direction as string | undefined, categoryId: q.categoryId ? Number(q.categoryId) : undefined, search: q.search as string | undefined, amountMin: q.amountMin ? Number(q.amountMin) : undefined, amountMax: q.amountMax ? Number(q.amountMax) : undefined, onlyUnconfirmed: q.onlyUnconfirmed === 'true', sortBy: q.sortBy as 'date' | 'amount' | undefined, sortOrder: q.sortOrder as 'asc' | 'desc' | undefined, page: q.page ? Number(q.page) : undefined, pageSize: q.pageSize ? Number(q.pageSize) : undefined, }); res.json(result); }), ); router.put( '/:id', asyncHandler(async (req, res) => { const id = Number(req.params.id); if (isNaN(id)) { res.status(400).json({ error: 'BAD_REQUEST', message: 'Invalid transaction id' }); return; } const { categoryId, comment } = req.body; if (categoryId === undefined && comment === undefined) { res.status(400).json({ error: 'BAD_REQUEST', message: 'No fields to update' }); return; } const result = await transactionService.updateTransaction(id, { categoryId, comment }); if (!result) { res.status(404).json({ error: 'NOT_FOUND', message: 'Transaction not found' }); return; } res.json(result); }), ); export default router;