mirror of
https://github.com/acedanger/finance.git
synced 2025-12-05 22:50:12 -08:00
99 lines
3.2 KiB
TypeScript
99 lines
3.2 KiB
TypeScript
import { Router } from 'express';
|
|
import { AccountStatus, AccountType, accountService } from '../data/db.service.js';
|
|
import type { Account } from '../types.js';
|
|
|
|
const router = Router();
|
|
|
|
// GET /api/accounts - Get all accounts
|
|
// biome-ignore lint/suspicious/noExplicitAny: Express handler types require any for req/res
|
|
router.get('/', async (req: any, res: any) => {
|
|
try {
|
|
console.log('GET /api/accounts - Fetching all accounts');
|
|
const accounts = await accountService.getAll();
|
|
console.log('GET /api/accounts - Found accounts:', accounts?.length ?? 0);
|
|
|
|
res.json(accounts || []);
|
|
} catch (error) {
|
|
console.error('Error fetching accounts:', error);
|
|
res.status(500).json({
|
|
error: 'Failed to fetch accounts',
|
|
details: error instanceof Error ? error.message : 'Unknown error',
|
|
});
|
|
}
|
|
});
|
|
|
|
// POST /api/accounts - Create new account
|
|
// biome-ignore lint/suspicious/noExplicitAny: Express handler types require any for req/res
|
|
router.post('/', async (req: any, res: any) => {
|
|
try {
|
|
const accountData = req.body as Omit<Account, 'id' | 'createdAt' | 'updatedAt'>;
|
|
|
|
// Validate required fields
|
|
if (!accountData.name || !accountData.bankName || !accountData.accountNumber) {
|
|
return res.status(400).json({
|
|
error: 'Missing required fields: name, bankName, and accountNumber are required',
|
|
});
|
|
}
|
|
|
|
// Set default values and ensure proper type casting
|
|
const data = {
|
|
...accountData,
|
|
balance: accountData.balance ? Number(accountData.balance) : 0,
|
|
type: (accountData.type as AccountType) || AccountType.CHECKING,
|
|
status: (accountData.status as AccountStatus) || AccountStatus.ACTIVE,
|
|
notes: accountData.notes || undefined,
|
|
};
|
|
|
|
// Create the account
|
|
const newAccount = await accountService.create(data);
|
|
|
|
res.status(201).json(newAccount);
|
|
} catch (error) {
|
|
console.error('Error creating account:', error);
|
|
res.status(500).json({ error: 'Failed to create account' });
|
|
}
|
|
});
|
|
|
|
// GET /api/accounts/:id - Get single account
|
|
// biome-ignore lint/suspicious/noExplicitAny: Express handler types require any for req/res
|
|
router.get('/:id', async (req: any, res: any) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
if (!id) {
|
|
return res.status(400).json({ error: 'Account ID is required' });
|
|
}
|
|
|
|
const account = await accountService.getById(id);
|
|
|
|
if (!account) {
|
|
return res.status(404).json({ error: 'Account not found' });
|
|
}
|
|
|
|
res.json(account);
|
|
} catch (error) {
|
|
console.error('Error fetching account:', error);
|
|
res.status(500).json({ error: 'Failed to fetch account' });
|
|
}
|
|
});
|
|
|
|
// GET /api/accounts/:id/transactions - Get transactions for account
|
|
// biome-ignore lint/suspicious/noExplicitAny: Express handler types require any for req/res
|
|
router.get('/:id/transactions', async (req: any, res: any) => {
|
|
try {
|
|
const { id } = req.params;
|
|
|
|
if (!id) {
|
|
return res.status(400).json({ error: 'Account ID is required' });
|
|
}
|
|
|
|
const transactions = await accountService.getTransactions(id);
|
|
res.json(transactions || []);
|
|
} catch (error) {
|
|
console.error('Error fetching account transactions:', error);
|
|
res.status(500).json({ error: 'Failed to fetch account transactions' });
|
|
}
|
|
});
|
|
|
|
export default router;
|