import type { APIRoute } from 'astro'; import { transactions, accounts } from '../../../../data/store'; import type { Transaction } from '../../../../types'; export const PUT: APIRoute = async ({ request, params }) => { const { id } = params; if (!id) { return new Response(JSON.stringify({ error: 'Transaction ID is required' }), { status: 400, headers: { 'Content-Type': 'application/json' }, }); } try { const updates = (await request.json()) as Partial; const transactionIndex = transactions.findIndex((t) => t.id === id); if (transactionIndex === -1) { return new Response(JSON.stringify({ error: 'Transaction not found' }), { status: 404, headers: { 'Content-Type': 'application/json' }, }); } const oldTransaction = transactions[transactionIndex]; // Get the old account first const oldAccount = accounts.find((a) => a.id === oldTransaction.accountId); if (!oldAccount) { return new Response(JSON.stringify({ error: 'Account not found' }), { status: 404, headers: { 'Content-Type': 'application/json' }, }); } // If account is changing, validate new account exists let newAccount = oldAccount; if (updates.accountId && updates.accountId !== oldTransaction.accountId) { const foundAccount = accounts.find((a) => a.id === updates.accountId); if (!foundAccount) { return new Response(JSON.stringify({ error: 'Account not found' }), { status: 404, headers: { 'Content-Type': 'application/json' }, }); } newAccount = foundAccount; } // First, remove the old transaction's effect on the old account oldAccount.balance -= oldTransaction.amount; // Create updated transaction const updatedTransaction: Transaction = { ...oldTransaction, ...updates, id: id, // Ensure ID doesn't change }; // Then add the new transaction's effect to the appropriate account if (newAccount === oldAccount) { // If same account, just add the new amount oldAccount.balance += updatedTransaction.amount; } else { // If different account, add to the new account newAccount.balance += updatedTransaction.amount; } // Update transaction in array transactions[transactionIndex] = updatedTransaction; return new Response(JSON.stringify(updatedTransaction), { status: 200, headers: { 'Content-Type': 'application/json' }, }); } catch (error) { return new Response(JSON.stringify({ error: 'Invalid request body' }), { status: 400, headers: { 'Content-Type': 'application/json' }, }); } }; export const DELETE: APIRoute = async ({ params }) => { const { id } = params; if (!id) { return new Response(JSON.stringify({ error: 'Transaction ID is required' }), { status: 400, headers: { 'Content-Type': 'application/json' }, }); } const transactionIndex = transactions.findIndex((t) => t.id === id); if (transactionIndex === -1) { return new Response(JSON.stringify({ error: 'Transaction not found' }), { status: 404, headers: { 'Content-Type': 'application/json' }, }); } const transaction = transactions[transactionIndex]; const account = accounts.find((a) => a.id === transaction.accountId); if (!account) { return new Response(JSON.stringify({ error: 'Account not found' }), { status: 404, headers: { 'Content-Type': 'application/json' }, }); } // Update account balance account.balance -= transaction.amount; // Remove transaction from array transactions.splice(transactionIndex, 1); return new Response(null, { status: 204 }); };