From 43a4647d7442051575b4aec4f649279c28207eb8 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 22 Apr 2022 18:31:31 +0000 Subject: [PATCH] Add query to find problematic transactions --- postgres/queries/transactions.sql | 17 ++++- postgres/transactions.sql.go | 104 ++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 1 deletion(-) diff --git a/postgres/queries/transactions.sql b/postgres/queries/transactions.sql index 2376d0b..3e9c9d6 100644 --- a/postgres/queries/transactions.sql +++ b/postgres/queries/transactions.sql @@ -45,4 +45,19 @@ AND accounts.id = transactions.account_id; -- name: GetTransactionsByMonthAndCategory :many SELECT * FROM transactions_by_month -WHERE transactions_by_month.budget_id = @budget_id; \ No newline at end of file +WHERE transactions_by_month.budget_id = @budget_id; + +-- name: GetProblematicTransactions :many +SELECT * +FROM transactions +LEFT JOIN accounts + ON transactions.account_id = accounts.id +LEFT JOIN transactions AS otherGroupTransaction + ON transactions.group_id = otherGroupTransaction.group_id + AND transactions.id != otherGroupTransaction.id + AND transactions.account_id != otherGroupTransaction.account_id +LEFT JOIn accounts AS otherGroupAccount + ON otherGroupTransaction.account_id = accounts.id +WHERE transactions.category_id IS NULL +AND accounts.on_budget +AND (otherGroupAccount.id IS NULL OR NOT otherGroupAccount.on_budget); \ No newline at end of file diff --git a/postgres/transactions.sql.go b/postgres/transactions.sql.go index cdab72a..9b720c1 100644 --- a/postgres/transactions.sql.go +++ b/postgres/transactions.sql.go @@ -7,6 +7,7 @@ package postgres import ( "context" + "database/sql" "time" "git.javil.eu/jacob1123/budgeteer/postgres/numeric" @@ -117,6 +118,109 @@ func (q *Queries) GetAllTransactionsForBudget(ctx context.Context, budgetID uuid return items, nil } +const getProblematicTransactions = `-- name: GetProblematicTransactions :many +SELECT transactions.id, transactions.date, transactions.memo, transactions.amount, transactions.account_id, transactions.category_id, transactions.payee_id, transactions.group_id, transactions.status, accounts.id, accounts.budget_id, accounts.name, accounts.on_budget, accounts.is_open, accounts.last_reconciled, othergrouptransaction.id, othergrouptransaction.date, othergrouptransaction.memo, othergrouptransaction.amount, othergrouptransaction.account_id, othergrouptransaction.category_id, othergrouptransaction.payee_id, othergrouptransaction.group_id, othergrouptransaction.status, othergroupaccount.id, othergroupaccount.budget_id, othergroupaccount.name, othergroupaccount.on_budget, othergroupaccount.is_open, othergroupaccount.last_reconciled +FROM transactions +LEFT JOIN accounts + ON transactions.account_id = accounts.id +LEFT JOIN transactions AS otherGroupTransaction + ON transactions.group_id = otherGroupTransaction.group_id + AND transactions.id != otherGroupTransaction.id + AND transactions.account_id != otherGroupTransaction.account_id +LEFT JOIn accounts AS otherGroupAccount + ON otherGroupTransaction.account_id = accounts.id +WHERE transactions.category_id IS NULL +AND accounts.on_budget +AND (otherGroupAccount.id IS NULL OR NOT otherGroupAccount.on_budget) +` + +type GetProblematicTransactionsRow struct { + ID uuid.UUID + Date time.Time + Memo string + Amount numeric.Numeric + AccountID uuid.UUID + CategoryID uuid.NullUUID + PayeeID uuid.NullUUID + GroupID uuid.NullUUID + Status TransactionStatus + ID_2 uuid.NullUUID + BudgetID uuid.NullUUID + Name sql.NullString + OnBudget sql.NullBool + IsOpen sql.NullBool + LastReconciled sql.NullTime + ID_3 uuid.UUID + Date_2 time.Time + Memo_2 string + Amount_2 numeric.Numeric + AccountID_2 uuid.UUID + CategoryID_2 uuid.NullUUID + PayeeID_2 uuid.NullUUID + GroupID_2 uuid.NullUUID + Status_2 TransactionStatus + ID_4 uuid.NullUUID + BudgetID_2 uuid.NullUUID + Name_2 sql.NullString + OnBudget_2 sql.NullBool + IsOpen_2 sql.NullBool + LastReconciled_2 sql.NullTime +} + +func (q *Queries) GetProblematicTransactions(ctx context.Context) ([]GetProblematicTransactionsRow, error) { + rows, err := q.db.QueryContext(ctx, getProblematicTransactions) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetProblematicTransactionsRow + for rows.Next() { + var i GetProblematicTransactionsRow + if err := rows.Scan( + &i.ID, + &i.Date, + &i.Memo, + &i.Amount, + &i.AccountID, + &i.CategoryID, + &i.PayeeID, + &i.GroupID, + &i.Status, + &i.ID_2, + &i.BudgetID, + &i.Name, + &i.OnBudget, + &i.IsOpen, + &i.LastReconciled, + &i.ID_3, + &i.Date_2, + &i.Memo_2, + &i.Amount_2, + &i.AccountID_2, + &i.CategoryID_2, + &i.PayeeID_2, + &i.GroupID_2, + &i.Status_2, + &i.ID_4, + &i.BudgetID_2, + &i.Name_2, + &i.OnBudget_2, + &i.IsOpen_2, + &i.LastReconciled_2, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const getTransaction = `-- name: GetTransaction :one SELECT id, date, memo, amount, group_id, status, account, payee_id, category_id, payee, category_group, category, transfer_account, budget_id, account_id FROM display_transactions WHERE id = $1