refactor: improve account validation in transaction updates and enhance test setup with mock API context

This commit is contained in:
GitHub Copilot
2025-04-24 11:23:14 -04:00
parent 96093200f5
commit c424691658
4 changed files with 133 additions and 104 deletions

View File

@@ -40,13 +40,14 @@ export const PUT: APIRoute = async ({ request, params }) => {
// If account is changing, validate new account exists
let newAccount = oldAccount;
if (updates.accountId && updates.accountId !== oldTransaction.accountId) {
newAccount = accounts.find((a) => a.id === updates.accountId);
if (!newAccount) {
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