diff --git a/postgres/accounts.sql.go b/postgres/accounts.sql.go index e913dd0..ab5de4e 100644 --- a/postgres/accounts.sql.go +++ b/postgres/accounts.sql.go @@ -15,7 +15,7 @@ const createAccount = `-- name: CreateAccount :one INSERT INTO accounts (name, budget_id) VALUES ($1, $2) -RETURNING id, budget_id, name, on_budget +RETURNING id, budget_id, name, on_budget, is_open ` type CreateAccountParams struct { @@ -31,12 +31,13 @@ func (q *Queries) CreateAccount(ctx context.Context, arg CreateAccountParams) (A &i.BudgetID, &i.Name, &i.OnBudget, + &i.IsOpen, ) return i, err } const getAccount = `-- name: GetAccount :one -SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget FROM accounts +SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget, accounts.is_open FROM accounts WHERE accounts.id = $1 ` @@ -48,13 +49,15 @@ func (q *Queries) GetAccount(ctx context.Context, id uuid.UUID) (Account, error) &i.BudgetID, &i.Name, &i.OnBudget, + &i.IsOpen, ) return i, err } const getAccounts = `-- name: GetAccounts :many -SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget FROM accounts +SELECT accounts.id, accounts.budget_id, accounts.name, accounts.on_budget, accounts.is_open FROM accounts WHERE accounts.budget_id = $1 +AND accounts.is_open = TRUE ORDER BY accounts.name ` @@ -72,6 +75,7 @@ func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]Accoun &i.BudgetID, &i.Name, &i.OnBudget, + &i.IsOpen, ); err != nil { return nil, err } @@ -87,13 +91,14 @@ func (q *Queries) GetAccounts(ctx context.Context, budgetID uuid.UUID) ([]Accoun } const getAccountsWithBalance = `-- name: GetAccountsWithBalance :many -SELECT accounts.id, accounts.name, accounts.on_budget, +SELECT accounts.id, accounts.name, accounts.on_budget, accounts.is_open, (SELECT MAX(transactions.date) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.status = 'Reconciled')::date as last_reconciled, (SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW())::decimal(12,2) as working_balance, (SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW() AND transactions.status IN ('Cleared', 'Reconciled'))::decimal(12,2) as cleared_balance, (SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW() AND transactions.status = 'Reconciled')::decimal(12,2) as reconciled_balance FROM accounts WHERE accounts.budget_id = $1 +AND accounts.is_open = TRUE ORDER BY accounts.name ` @@ -101,6 +106,7 @@ type GetAccountsWithBalanceRow struct { ID uuid.UUID Name string OnBudget bool + IsOpen bool LastReconciled time.Time WorkingBalance numeric.Numeric ClearedBalance numeric.Numeric @@ -120,6 +126,7 @@ func (q *Queries) GetAccountsWithBalance(ctx context.Context, budgetID uuid.UUID &i.ID, &i.Name, &i.OnBudget, + &i.IsOpen, &i.LastReconciled, &i.WorkingBalance, &i.ClearedBalance, @@ -141,6 +148,7 @@ func (q *Queries) GetAccountsWithBalance(ctx context.Context, budgetID uuid.UUID const searchAccounts = `-- name: SearchAccounts :many SELECT accounts.id, accounts.budget_id, accounts.name, 'account' as type FROM accounts WHERE accounts.budget_id = $1 +AND accounts.is_open = TRUE AND accounts.name LIKE $2 ORDER BY accounts.name ` @@ -188,25 +196,33 @@ func (q *Queries) SearchAccounts(ctx context.Context, arg SearchAccountsParams) const updateAccount = `-- name: UpdateAccount :one UPDATE accounts SET name = $1, - on_budget = $2 -WHERE accounts.id = $3 -RETURNING id, budget_id, name, on_budget + on_budget = $2, + is_open = $3 +WHERE accounts.id = $4 +RETURNING id, budget_id, name, on_budget, is_open ` type UpdateAccountParams struct { Name string OnBudget bool + IsOpen bool ID uuid.UUID } func (q *Queries) UpdateAccount(ctx context.Context, arg UpdateAccountParams) (Account, error) { - row := q.db.QueryRowContext(ctx, updateAccount, arg.Name, arg.OnBudget, arg.ID) + row := q.db.QueryRowContext(ctx, updateAccount, + arg.Name, + arg.OnBudget, + arg.IsOpen, + arg.ID, + ) var i Account err := row.Scan( &i.ID, &i.BudgetID, &i.Name, &i.OnBudget, + &i.IsOpen, ) return i, err } diff --git a/postgres/models.go b/postgres/models.go index bacb571..4e33e88 100644 --- a/postgres/models.go +++ b/postgres/models.go @@ -36,6 +36,7 @@ type Account struct { BudgetID uuid.UUID Name string OnBudget bool + IsOpen bool } type Assignment struct { diff --git a/postgres/queries/accounts.sql b/postgres/queries/accounts.sql index 5232c9d..0e62900 100644 --- a/postgres/queries/accounts.sql +++ b/postgres/queries/accounts.sql @@ -11,27 +11,31 @@ WHERE accounts.id = $1; -- name: GetAccounts :many SELECT accounts.* FROM accounts WHERE accounts.budget_id = $1 +AND accounts.is_open = TRUE ORDER BY accounts.name; -- name: GetAccountsWithBalance :many -SELECT accounts.id, accounts.name, accounts.on_budget, +SELECT accounts.id, accounts.name, accounts.on_budget, accounts.is_open, (SELECT MAX(transactions.date) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.status = 'Reconciled')::date as last_reconciled, (SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW())::decimal(12,2) as working_balance, (SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW() AND transactions.status IN ('Cleared', 'Reconciled'))::decimal(12,2) as cleared_balance, (SELECT SUM(transactions.amount) FROM transactions WHERE transactions.account_id = accounts.id AND transactions.date < NOW() AND transactions.status = 'Reconciled')::decimal(12,2) as reconciled_balance FROM accounts WHERE accounts.budget_id = $1 +AND accounts.is_open = TRUE ORDER BY accounts.name; -- name: SearchAccounts :many SELECT accounts.id, accounts.budget_id, accounts.name, 'account' as type FROM accounts WHERE accounts.budget_id = @budget_id +AND accounts.is_open = TRUE AND accounts.name LIKE @search ORDER BY accounts.name; -- name: UpdateAccount :one UPDATE accounts SET name = $1, - on_budget = $2 -WHERE accounts.id = $3 + on_budget = $2, + is_open = $3 +WHERE accounts.id = $4 RETURNING *; \ No newline at end of file diff --git a/postgres/schema/0016_closed-accounts.sql b/postgres/schema/0016_closed-accounts.sql new file mode 100644 index 0000000..daad799 --- /dev/null +++ b/postgres/schema/0016_closed-accounts.sql @@ -0,0 +1,5 @@ +-- +goose Up +ALTER TABLE accounts ADD COLUMN is_open BOOLEAN NOT NULL DEFAULT TRUE; + +-- +goose Down +ALTER TABLE accounts DROP COLUMN is_open; \ No newline at end of file diff --git a/server/account.go b/server/account.go index 0c057af..65b0908 100644 --- a/server/account.go +++ b/server/account.go @@ -39,6 +39,7 @@ type TransactionsResponse struct { type EditAccountRequest struct { Name string `json:"name"` OnBudget bool `json:"onBudget"` + IsOpen bool `json:"isOpen"` } func (h *Handler) editAccount(c *gin.Context) { @@ -59,6 +60,7 @@ func (h *Handler) editAccount(c *gin.Context) { updateParams := postgres.UpdateAccountParams{ Name: request.Name, OnBudget: request.OnBudget, + IsOpen: request.IsOpen, ID: accountUUID, } account, err := h.Service.UpdateAccount(c.Request.Context(), updateParams) diff --git a/web/src/components/Modal.vue b/web/src/components/Modal.vue index faea3ca..d628210 100644 --- a/web/src/components/Modal.vue +++ b/web/src/components/Modal.vue @@ -7,7 +7,7 @@ const props = defineProps<{ }>(); const emit = defineEmits<{ - (e: 'submit'): void, + (e: 'submit', event : {cancel:boolean}): boolean, (e: 'open'): void, }>(); @@ -20,8 +20,12 @@ function openDialog() { visible.value = true; }; function submitDialog() { + const e = {cancel: false}; + emit("submit", e); + if(e.cancel) + return; + visible.value = false; - emit("submit"); } @@ -38,9 +42,9 @@ function submitDialog() { v-if="visible" class="fixed inset-0 bg-gray-600 bg-opacity-50 overflow-y-auto h-full w-full" > -
+
-

{{ buttonText }}

+

{{ buttonText }}