mirror of
https://github.com/acedanger/finance.git
synced 2025-12-05 22:50:12 -08:00
Remove unused files and implement API routes for account and transaction management
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import type { APIRoute } from "astro";
|
||||
import { transactions, accounts } from "../../../../data/store";
|
||||
import type { Transaction } from "../../../../types";
|
||||
|
||||
export const PUT: APIRoute = async ({ request, params }) => {
|
||||
try {
|
||||
const updates = (await request.json()) as Partial<Transaction>;
|
||||
const transactionIndex = transactions.findIndex((t) => t.id === params.id);
|
||||
|
||||
if (transactionIndex === -1) {
|
||||
return new Response(JSON.stringify({ error: "Transaction not found" }), {
|
||||
status: 404,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
const oldTransaction = transactions[transactionIndex];
|
||||
const account = accounts.find((a) => a.id === oldTransaction.accountId);
|
||||
|
||||
if (!account) {
|
||||
return new Response(JSON.stringify({ error: "Account not found" }), {
|
||||
status: 404,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
// Revert old transaction amount from account balance
|
||||
account.balance -= oldTransaction.amount;
|
||||
|
||||
// Update transaction with new data
|
||||
const updatedTransaction: Transaction = {
|
||||
...oldTransaction,
|
||||
...updates,
|
||||
id: params.id, // Ensure ID doesn't change
|
||||
};
|
||||
|
||||
// Add new amount to account balance
|
||||
account.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 transactionIndex = transactions.findIndex((t) => t.id === params.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 });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user