Move creation of starting balances to backend

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-02-02 22:18:15 +01:00
parent b215d3975e
commit 581ab042fc
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
2 changed files with 36 additions and 41 deletions

View file

@ -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()

View file

@ -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)