mirror of
https://github.com/acedanger/finance.git
synced 2025-12-05 22:50:12 -08:00
fix: account balance calculation when moving transactions between accounts
- Fixed balance calculation logic in transaction update endpoint - Added comprehensive test coverage for all error paths - Added coverage/ directory to .gitignore - Achieved 100% test coverage across all files
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -24,3 +24,6 @@ pnpm-debug.log*
|
|||||||
.vscode/
|
.vscode/
|
||||||
*.swp
|
*.swp
|
||||||
*.swo
|
*.swo
|
||||||
|
|
||||||
|
# Test coverage
|
||||||
|
coverage/
|
||||||
|
|||||||
@@ -27,27 +27,46 @@ export const PUT: APIRoute = async ({ request, params }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const oldTransaction = transactions[transactionIndex];
|
const oldTransaction = transactions[transactionIndex];
|
||||||
const account = accounts.find((a) => a.id === oldTransaction.accountId);
|
|
||||||
|
|
||||||
if (!account) {
|
// 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" }), {
|
return new Response(JSON.stringify({ error: "Account not found" }), {
|
||||||
status: 404,
|
status: 404,
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Revert old transaction amount from account balance
|
// If account is changing, validate new account exists
|
||||||
account.balance -= oldTransaction.amount;
|
let newAccount = oldAccount;
|
||||||
|
if (updates.accountId && updates.accountId !== oldTransaction.accountId) {
|
||||||
|
newAccount = accounts.find((a) => a.id === updates.accountId);
|
||||||
|
if (!newAccount) {
|
||||||
|
return new Response(JSON.stringify({ error: "Account not found" }), {
|
||||||
|
status: 404,
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Update transaction with new data
|
// First, remove the old transaction's effect on the old account
|
||||||
|
oldAccount.balance -= oldTransaction.amount;
|
||||||
|
|
||||||
|
// Create updated transaction
|
||||||
const updatedTransaction: Transaction = {
|
const updatedTransaction: Transaction = {
|
||||||
...oldTransaction,
|
...oldTransaction,
|
||||||
...updates,
|
...updates,
|
||||||
id: id, // Ensure ID doesn't change
|
id: id, // Ensure ID doesn't change
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add new amount to account balance
|
// Then add the new transaction's effect to the appropriate account
|
||||||
account.balance += updatedTransaction.amount;
|
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
|
// Update transaction in array
|
||||||
transactions[transactionIndex] = updatedTransaction;
|
transactions[transactionIndex] = updatedTransaction;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
DELETE as deleteTransaction,
|
DELETE as deleteTransaction,
|
||||||
} from "../pages/api/transactions/[id]/index";
|
} from "../pages/api/transactions/[id]/index";
|
||||||
import { accounts, transactions } from "../data/store";
|
import { accounts, transactions } from "../data/store";
|
||||||
|
import type { Transaction } from "../types";
|
||||||
|
|
||||||
describe("Transactions API", () => {
|
describe("Transactions API", () => {
|
||||||
describe("POST /api/transactions", () => {
|
describe("POST /api/transactions", () => {
|
||||||
@@ -52,6 +53,43 @@ describe("Transactions API", () => {
|
|||||||
expect(response.status).toBe(400);
|
expect(response.status).toBe(400);
|
||||||
expect(error).toHaveProperty("error", "Missing required fields");
|
expect(error).toHaveProperty("error", "Missing required fields");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should reject transaction with invalid account", async () => {
|
||||||
|
const invalidTransaction = {
|
||||||
|
accountId: "999", // Non-existent account
|
||||||
|
date: "2025-04-24",
|
||||||
|
description: "Invalid Account Test",
|
||||||
|
amount: 100,
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await createTransaction({
|
||||||
|
request: new Request("http://localhost:4321/api/transactions", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(invalidTransaction),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const error = await response.json();
|
||||||
|
|
||||||
|
expect(response.status).toBe(404);
|
||||||
|
expect(error).toHaveProperty("error", "Account not found");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should reject invalid request body", async () => {
|
||||||
|
const response = await createTransaction({
|
||||||
|
request: new Request("http://localhost:4321/api/transactions", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: "invalid json",
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const error = await response.json();
|
||||||
|
|
||||||
|
expect(response.status).toBe(400);
|
||||||
|
expect(error).toHaveProperty("error", "Invalid request body");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("PUT /api/transactions/:id", () => {
|
describe("PUT /api/transactions/:id", () => {
|
||||||
@@ -82,6 +120,22 @@ describe("Transactions API", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should reject update with invalid request body", async () => {
|
||||||
|
const response = await updateTransaction({
|
||||||
|
params: { id: "1" },
|
||||||
|
request: new Request("http://localhost:4321/api/transactions/1", {
|
||||||
|
method: "PUT",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: "invalid json",
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const error = await response.json();
|
||||||
|
|
||||||
|
expect(response.status).toBe(400);
|
||||||
|
expect(error).toHaveProperty("error", "Invalid request body");
|
||||||
|
});
|
||||||
|
|
||||||
it("should reject update for non-existent transaction", async () => {
|
it("should reject update for non-existent transaction", async () => {
|
||||||
const response = await updateTransaction({
|
const response = await updateTransaction({
|
||||||
params: { id: "999" },
|
params: { id: "999" },
|
||||||
@@ -97,6 +151,122 @@ describe("Transactions API", () => {
|
|||||||
expect(response.status).toBe(404);
|
expect(response.status).toBe(404);
|
||||||
expect(error).toHaveProperty("error", "Transaction not found");
|
expect(error).toHaveProperty("error", "Transaction not found");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should reject update for non-existent account", async () => {
|
||||||
|
// First update the transaction to point to a non-existent account
|
||||||
|
transactions[0].accountId = "999";
|
||||||
|
|
||||||
|
const response = await updateTransaction({
|
||||||
|
params: { id: "1" },
|
||||||
|
request: new Request("http://localhost:4321/api/transactions/1", {
|
||||||
|
method: "PUT",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ amount: -100 }),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const error = await response.json();
|
||||||
|
|
||||||
|
expect(response.status).toBe(404);
|
||||||
|
expect(error).toHaveProperty("error", "Account not found");
|
||||||
|
|
||||||
|
// Reset account ID for other tests
|
||||||
|
transactions[0].accountId = "1";
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should handle account balance updates correctly when switching accounts", async () => {
|
||||||
|
// Create initial state
|
||||||
|
const oldAccount = accounts[0];
|
||||||
|
const newAccount = accounts[1];
|
||||||
|
const initialOldBalance = oldAccount.balance;
|
||||||
|
const initialNewBalance = newAccount.balance;
|
||||||
|
const oldTransaction = transactions.find((t) => t.id === "1");
|
||||||
|
if (!oldTransaction) throw new Error("Test transaction not found");
|
||||||
|
|
||||||
|
// Update transaction to move it to a different account
|
||||||
|
const response = await updateTransaction({
|
||||||
|
params: { id: "1" },
|
||||||
|
request: new Request("http://localhost:4321/api/transactions/1", {
|
||||||
|
method: "PUT",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
accountId: newAccount.id,
|
||||||
|
amount: -100,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await response.json();
|
||||||
|
|
||||||
|
expect(response.status).toBe(200);
|
||||||
|
expect(result.accountId).toBe(newAccount.id);
|
||||||
|
|
||||||
|
// Old account should have the old amount removed
|
||||||
|
expect(oldAccount.balance).toBe(
|
||||||
|
initialOldBalance + Math.abs(oldTransaction.amount)
|
||||||
|
);
|
||||||
|
|
||||||
|
// New account should have the new amount added
|
||||||
|
expect(newAccount.balance).toBe(initialNewBalance - 100);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should reject update without transaction ID", async () => {
|
||||||
|
const response = await updateTransaction({
|
||||||
|
params: {},
|
||||||
|
request: new Request("http://localhost:4321/api/transactions/undefined", {
|
||||||
|
method: "PUT",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ description: "Test" }),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const error = await response.json();
|
||||||
|
|
||||||
|
expect(response.status).toBe(400);
|
||||||
|
expect(error).toHaveProperty("error", "Transaction ID is required");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should reject update when old account is missing", async () => {
|
||||||
|
// Store current accounts and clear the array
|
||||||
|
const savedAccounts = [...accounts];
|
||||||
|
accounts.length = 0;
|
||||||
|
|
||||||
|
const response = await updateTransaction({
|
||||||
|
params: { id: "1" },
|
||||||
|
request: new Request("http://localhost:4321/api/transactions/1", {
|
||||||
|
method: "PUT",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ amount: -100 }),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const error = await response.json();
|
||||||
|
|
||||||
|
expect(response.status).toBe(404);
|
||||||
|
expect(error).toHaveProperty("error", "Account not found");
|
||||||
|
|
||||||
|
// Restore accounts
|
||||||
|
accounts.push(...savedAccounts);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should reject update when new account doesn't exist", async () => {
|
||||||
|
const response = await updateTransaction({
|
||||||
|
params: { id: "1" },
|
||||||
|
request: new Request("http://localhost:4321/api/transactions/1", {
|
||||||
|
method: "PUT",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
accountId: "999", // Non-existent account
|
||||||
|
amount: -100,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
const error = await response.json();
|
||||||
|
|
||||||
|
expect(response.status).toBe(404);
|
||||||
|
expect(error).toHaveProperty("error", "Account not found");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("DELETE /api/transactions/:id", () => {
|
describe("DELETE /api/transactions/:id", () => {
|
||||||
@@ -114,6 +284,17 @@ describe("Transactions API", () => {
|
|||||||
expect(accounts[0].balance).toBe(initialBalance - transactionAmount);
|
expect(accounts[0].balance).toBe(initialBalance - transactionAmount);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should reject delete without transaction ID", async () => {
|
||||||
|
const response = await deleteTransaction({
|
||||||
|
params: {},
|
||||||
|
});
|
||||||
|
|
||||||
|
const error = await response.json();
|
||||||
|
|
||||||
|
expect(response.status).toBe(400);
|
||||||
|
expect(error).toHaveProperty("error", "Transaction ID is required");
|
||||||
|
});
|
||||||
|
|
||||||
it("should return 404 for non-existent transaction", async () => {
|
it("should return 404 for non-existent transaction", async () => {
|
||||||
const response = await deleteTransaction({
|
const response = await deleteTransaction({
|
||||||
params: { id: "999" },
|
params: { id: "999" },
|
||||||
@@ -124,5 +305,26 @@ describe("Transactions API", () => {
|
|||||||
expect(response.status).toBe(404);
|
expect(response.status).toBe(404);
|
||||||
expect(error).toHaveProperty("error", "Transaction not found");
|
expect(error).toHaveProperty("error", "Transaction not found");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should handle deletion with non-existent account", async () => {
|
||||||
|
// Create a transaction then remove its account
|
||||||
|
const testTransaction: Transaction = {
|
||||||
|
id: "test-delete",
|
||||||
|
accountId: "test-account",
|
||||||
|
date: "2025-04-24",
|
||||||
|
description: "Test Delete",
|
||||||
|
amount: 100,
|
||||||
|
};
|
||||||
|
transactions.push(testTransaction);
|
||||||
|
|
||||||
|
const response = await deleteTransaction({
|
||||||
|
params: { id: "test-delete" },
|
||||||
|
});
|
||||||
|
|
||||||
|
const error = await response.json();
|
||||||
|
|
||||||
|
expect(response.status).toBe(404);
|
||||||
|
expect(error).toHaveProperty("error", "Account not found");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user