diff --git a/src/lib/db.js b/src/lib/db.js index 903a1c6..4cbaa4f 100644 --- a/src/lib/db.js +++ b/src/lib/db.js @@ -138,24 +138,15 @@ export async function deleteBudgetTransaction(id) { } export async function addBudgetTransaction(budgetId, transactionId, amount, notes, ruleId = null) { - const existingTransactions = await db` - select amount from budget_transaction - where transaction_id = ${transactionId} + + const exsisting = await db` + select id from budget_transaction + where budget_id = ${budgetId} and transaction_id = ${transactionId} `; - const realTransactionAmount = await db` - select amount from transaction - where id = ${transactionId} - `; - - if (existingTransactions.length > 0) { - if ( - existingTransactions.reduce((acc, curr) => acc + curr.amount, 0) + amount > - realTransactionAmount - ) { - return -1; - } - } - + if (exsisting.length > 0) { + // If the transaction already exists in the budget, update it + return updateBudgetTransaction(exsisting[0].id, amount, notes); + } // Add a transaction to a budget const result = await db` insert into budget_transaction (budget_id, transaction_id, amount, notes, rule_id) diff --git a/src/lib/editTransaction.svelte b/src/lib/editTransaction.svelte index 2728c7c..b29bb7d 100644 --- a/src/lib/editTransaction.svelte +++ b/src/lib/editTransaction.svelte @@ -1,20 +1,30 @@ @@ -65,60 +130,56 @@ Out of Budgets - + - Add to budget - - Amount - - Notes - -

Must a sensible number

- + + + {#if deleting} + Delete Budget Transaction + Deleting budget transaction - {deletingText} + Are you sure? + + + {:else} + Add to budget + + Amount + + Notes + + + {/if} {/if} diff --git a/src/lib/transactionList.svelte b/src/lib/transactionList.svelte index b2463b9..5420647 100644 --- a/src/lib/transactionList.svelte +++ b/src/lib/transactionList.svelte @@ -5,9 +5,10 @@ let { transactions, budgetTransactions, budgets } = $props(); let editing = $state(false); - function editNotes(transaction, remaining) { + function editNotes(transaction, remaining, budgetTransactions) { currentTransaction = transaction; currentTransaction.amount = remaining; + currentTransaction.budgetTransactions = budgetTransactions; editing = true; } @@ -65,7 +66,7 @@
diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 2420a1c..103b912 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -8,20 +8,23 @@ let underAllocatedTrans = $derived(data.underAllocatedTrans); let budgets = $derived(data.budgets); let budgetTransactions = $derived(data.budgetTransactions); - let last30days = $derived(data.last30DaysTransactionsSums); + let last30days = $derived(data.last30DaysTransactionsSums.reverse()); + + $inspect(last30days); let chartData = $derived( - last30days - .reduce((acc, curr) => [...acc, acc[acc.length - 1] + Number(curr.sum)], [Number(total)]) - .reverse() - ); - let chartDates = $derived( - [ - 'now', - ...last30days.map((day) => `${day.date.getMonth() + 1}/${day.date.getDate()}`) - ].reverse() + last30days.reduce( + (acc, curr) => [...acc, acc[acc.length - 1] + Number(curr.sum)], + [Number(total)] + ) ); + $inspect(chartData); + let chartDates = $derived([ + 'now', + ...last30days.map((day) => `${day.date.getMonth() + 1}/${day.date.getDate()}`) + ]); + const option = $derived({ xAxis: { type: 'category', @@ -33,7 +36,23 @@ series: [ { data: chartData, - type: 'line' + type: 'line', + label: { + show: false, + position: 'top', + formatter: (params) => { + return `$${params.value.toFixed(2)}`; + }, + fontSize: 20, + padding: 10, + backgroundColor: 'rgba(255, 255, 255, 0.8)', + borderRadius: 5 + }, + emphasis: { + label: { + show: true // Labels appear on hover + } + } } ] }); diff --git a/src/routes/api/budget/[slug]/transaction/+server.js b/src/routes/api/budget/[slug]/transaction/+server.js index f064537..7c8bc3e 100644 --- a/src/routes/api/budget/[slug]/transaction/+server.js +++ b/src/routes/api/budget/[slug]/transaction/+server.js @@ -22,7 +22,6 @@ export async function PATCH({ params, request }) { const { amount, notes, transactionId } = body; console.log({ slug, transactionId, amount }); - // Call the deleteBudget function from db.js (budgetId, transactionId, amount) return updateBudgetTransaction(transactionId, amount, notes) .then(() => new Response(`Budget transaction updated successfully`, { status: 200 })) .catch( @@ -36,7 +35,6 @@ export async function DELETE({ params, request }) { const { transactionId } = slug; console.log({ slug }); - // Call the deleteBudget function from db.js (budgetId, transactionId) return deleteBudgetTransaction(slug) .then(() => new Response(`Budget transaction deleted successfully`, { status: 200 })) .catch(