From 581ab042fc72fb02d5d9464e99f708e1074fc72c Mon Sep 17 00:00:00 2001 From: Knut Ahlers Date: Fri, 2 Feb 2024 22:18:15 +0100 Subject: [PATCH] Move creation of starting balances to backend Signed-off-by: Knut Ahlers --- frontend/components/accountsSidebar.vue | 40 +------------------------ pkg/api/account.go | 37 +++++++++++++++++++++-- 2 files changed, 36 insertions(+), 41 deletions(-) diff --git a/frontend/components/accountsSidebar.vue b/frontend/components/accountsSidebar.vue index 7089d90..e21ed2f 100644 --- a/frontend/components/accountsSidebar.vue +++ b/frontend/components/accountsSidebar.vue @@ -128,7 +128,6 @@ import { Modal } from 'bootstrap' import accList from './accountsSidebarAccList.vue' import { formatNumber } from '../helpers' -import { unallocatedMoneyAcc } from '../constants' export default { components: { accList }, @@ -172,6 +171,7 @@ export default { return fetch('/api/accounts', { body: JSON.stringify({ name: this.modals.addAccount.name, + startingBalance: this.modals.addAccount.startingBalance, type: this.modals.addAccount.type, }), headers: { @@ -179,44 +179,6 @@ export default { }, method: 'POST', }) - .then(resp => resp.json()) - .then(account => { - if (account.type === 'budget') { - return fetch('/api/transactions', { - body: JSON.stringify({ - account: account.id, - amount: this.modals.addAccount.startingBalance, - category: unallocatedMoneyAcc, - cleared: true, - description: 'Starting Balance', - time: new Date(), - }), - headers: { - 'Content-Type': 'application/json', - }, - method: 'POST', - }) - } else if (account.type === 'tracking') { - return fetch('/api/transactions', { - body: JSON.stringify({ - account: account.id, - amount: this.modals.addAccount.startingBalance, - cleared: true, - description: 'Starting Balance', - time: new Date(), - }), - headers: { - 'Content-Type': 'application/json', - }, - method: 'POST', - }) - } else if (account.type === 'category') { - return fetch(`/api/accounts/${unallocatedMoneyAcc}/transfer/${account.id}?amount=${this.modals.addAccount.startingBalance}`, { - method: 'PUT', - }) - } - throw new Error('invalid account type detected') - }) .then(() => this.$emit('update-accounts')) .then(() => { Modal.getInstance(this.$refs.createAccountModal).toggle() diff --git a/pkg/api/account.go b/pkg/api/account.go index f0b26d9..aa952bd 100644 --- a/pkg/api/account.go +++ b/pkg/api/account.go @@ -5,6 +5,7 @@ import ( "errors" "net/http" "strconv" + "time" "git.luzifer.io/luzifer/accounting/pkg/database" "github.com/google/uuid" @@ -14,8 +15,9 @@ import ( func (a apiServer) handleCreateAccount(w http.ResponseWriter, r *http.Request) { var payload struct { - Name string `json:"name"` - Type database.AccountType `json:"type"` + Name string `json:"name"` + StartingBalance float64 `json:"startingBalance"` + Type database.AccountType `json:"type"` } if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { @@ -39,6 +41,37 @@ func (a apiServer) handleCreateAccount(w http.ResponseWriter, r *http.Request) { return } + if payload.StartingBalance != 0 { + switch payload.Type { + case database.AccountTypeBudget: + _, err = a.dbc.CreateTransaction(database.Transaction{ + Time: time.Now(), + Description: "Starting Balance", + Amount: payload.StartingBalance, + Account: uuid.NullUUID{UUID: acc.ID, Valid: true}, + Category: uuid.NullUUID{UUID: database.UnallocatedMoney, Valid: true}, + Cleared: true, + }) + + case database.AccountTypeCategory: + err = a.dbc.TransferMoney(database.UnallocatedMoney, acc.ID, payload.StartingBalance) + + case database.AccountTypeTracking: + _, err = a.dbc.CreateTransaction(database.Transaction{ + Time: time.Now(), + Description: "Starting Balance", + Amount: payload.StartingBalance, + Account: uuid.NullUUID{UUID: acc.ID, Valid: true}, + Cleared: true, + }) + } + + if err != nil { + a.errorResponse(w, err, "creating starting balance transaction", http.StatusInternalServerError) + return + } + } + u, err := a.router.Get("GetAccount").URL("id", acc.ID.String()) if err != nil { a.errorResponse(w, err, "getting redirect url", http.StatusInternalServerError)