From 88e1dbdfde00706606de4cfd9b698bdb8181420f Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 14 Apr 2022 20:21:55 +0000 Subject: [PATCH 1/4] Simplify query by using COALESCE --- postgres/cumultative-balances.sql.go | 2 +- postgres/queries/cumultative-balances.sql | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/postgres/cumultative-balances.sql.go b/postgres/cumultative-balances.sql.go index 98f3aed..de14e6d 100644 --- a/postgres/cumultative-balances.sql.go +++ b/postgres/cumultative-balances.sql.go @@ -19,7 +19,7 @@ SELECT COALESCE(ass.date, tra.date), COALESCE(ass.category_id, tra.category_id), COALESCE(tra.amount, 0)::decimal(12,2) as transactions FROM assignments_by_month as ass FULL OUTER JOIN transactions_by_month as tra ON ass.date = tra.date AND ass.category_id = tra.category_id -WHERE (ass.budget_id IS NULL OR ass.budget_id = $1) AND (tra.budget_id IS NULL OR tra.budget_id = $1) +WHERE COALESCE(ass.budget_id, tra.budget_id) = $1 ORDER BY COALESCE(ass.date, tra.date), COALESCE(ass.amount, tra.amount) ` diff --git a/postgres/queries/cumultative-balances.sql b/postgres/queries/cumultative-balances.sql index cc22480..4803d8b 100644 --- a/postgres/queries/cumultative-balances.sql +++ b/postgres/queries/cumultative-balances.sql @@ -4,5 +4,5 @@ SELECT COALESCE(ass.date, tra.date), COALESCE(ass.category_id, tra.category_id), COALESCE(tra.amount, 0)::decimal(12,2) as transactions FROM assignments_by_month as ass FULL OUTER JOIN transactions_by_month as tra ON ass.date = tra.date AND ass.category_id = tra.category_id -WHERE (ass.budget_id IS NULL OR ass.budget_id = @budget_id) AND (tra.budget_id IS NULL OR tra.budget_id = @budget_id) +WHERE COALESCE(ass.budget_id, tra.budget_id) = @budget_id ORDER BY COALESCE(ass.date, tra.date), COALESCE(ass.amount, tra.amount); \ No newline at end of file From c50fc6330359c859a6adeaaf544a2b62ef01f4ed Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 14 Apr 2022 20:23:05 +0000 Subject: [PATCH 2/4] Add on_budget query to transactions_by_month --- ...9_ignore-off-budget-in-transactions-by-month.sql | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 postgres/schema/0019_ignore-off-budget-in-transactions-by-month.sql diff --git a/postgres/schema/0019_ignore-off-budget-in-transactions-by-month.sql b/postgres/schema/0019_ignore-off-budget-in-transactions-by-month.sql new file mode 100644 index 0000000..2c555d6 --- /dev/null +++ b/postgres/schema/0019_ignore-off-budget-in-transactions-by-month.sql @@ -0,0 +1,13 @@ +-- +goose Up +CREATE OR REPLACE VIEW transactions_by_month AS + SELECT date_trunc('month', transactions.date)::date as date, transactions.category_id, accounts.budget_id, SUM(amount) as amount + FROM transactions + INNER JOIN accounts ON accounts.id = transactions.account_id AND accounts.on_budget + GROUP BY date_trunc('month', transactions.date), transactions.category_id, accounts.budget_id; + +-- +goose Down +CREATE OR REPLACE VIEW transactions_by_month AS + SELECT date_trunc('month', transactions.date)::date as date, transactions.category_id, accounts.budget_id, SUM(amount) as amount + FROM transactions + INNER JOIN accounts ON accounts.id = transactions.account_id + GROUP BY date_trunc('month', transactions.date), transactions.category_id, accounts.budget_id; \ No newline at end of file From f5274f69fcccd2a932730068cd413945607064c0 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 14 Apr 2022 20:33:39 +0000 Subject: [PATCH 3/4] Return all transactions until paging is implemented --- postgres/queries/transactions.sql | 3 +-- postgres/transactions.sql.go | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/postgres/queries/transactions.sql b/postgres/queries/transactions.sql index 562b391..2376d0b 100644 --- a/postgres/queries/transactions.sql +++ b/postgres/queries/transactions.sql @@ -34,8 +34,7 @@ WHERE t.budget_id = $1; -- name: GetTransactionsForAccount :many SELECT t.* FROM display_transactions AS t -WHERE t.account_id = $1 -LIMIT 200; +WHERE t.account_id = $1; -- name: DeleteAllTransactions :execrows DELETE FROM transactions diff --git a/postgres/transactions.sql.go b/postgres/transactions.sql.go index 68ec37c..cdab72a 100644 --- a/postgres/transactions.sql.go +++ b/postgres/transactions.sql.go @@ -183,7 +183,6 @@ const getTransactionsForAccount = `-- name: GetTransactionsForAccount :many SELECT t.id, t.date, t.memo, t.amount, t.group_id, t.status, t.account, t.payee_id, t.category_id, t.payee, t.category_group, t.category, t.transfer_account, t.budget_id, t.account_id FROM display_transactions AS t WHERE t.account_id = $1 -LIMIT 200 ` func (q *Queries) GetTransactionsForAccount(ctx context.Context, accountID uuid.UUID) ([]DisplayTransaction, error) { From df7b691bddf70d47658588c72b766b96b7b5f192 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 14 Apr 2022 20:33:51 +0000 Subject: [PATCH 4/4] Add dummy category for uncategorized transactions --- server/budgeting.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server/budgeting.go b/server/budgeting.go index 9e0a7d1..8ab0cae 100644 --- a/server/budgeting.go +++ b/server/budgeting.go @@ -139,6 +139,11 @@ func (h *Handler) calculateBalances(budget postgres.Budget, month Month, categoriesWithBalance := []CategoryWithBalance{} moneyUsed := numeric.Zero() + categories = append(categories, postgres.GetCategoriesRow{ + Group: "Income", + Name: "No Category", + ID: uuid.UUID{}, + }) for i := range categories { cat := &categories[i] if cat.ID == budget.IncomeCategoryID {