Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | 1x 1x 1x 1x 1x 8x 8x 8x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 6x 6x 8x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 8x 2x 2x 2x 2x 2x 3x 3x 3x 8x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 8x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 8x 1x 1x 1x 1x 1x 8x 1x 1x 4x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 4x 1x 1x 1x 1x 1x 2x 2x 2x 2x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | 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<Transaction>;
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 });
};
|