Check available balance

This commit is contained in:
2022-04-05 16:22:20 +00:00
parent 2d9c380cf4
commit 2feefea737
2 changed files with 50 additions and 19 deletions

View File

@ -1,6 +1,7 @@
package server
import (
"context"
"fmt"
"net/http"
"time"
@ -56,22 +57,29 @@ func (h *Handler) budgetingForMonth(c *gin.Context) {
firstOfMonth, err := getDate(c)
if err != nil {
c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budgetUUID.String())
return
}
categories, err := h.Service.GetCategories(c.Request.Context(), budgetUUID)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
c.Redirect(http.StatusTemporaryRedirect, "/budget/"+budget.ID.String())
return
}
firstOfNextMonth := firstOfMonth.AddDate(0, 1, 0)
cumultativeBalances, err := h.Service.GetCumultativeBalances(c.Request.Context(), budgetUUID)
data, err := h.prepareBudgeting(c.Request.Context(), budget, firstOfNextMonth, firstOfMonth)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, ErrorResponse{fmt.Sprintf("error loading balances: %s", err)})
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, data)
}
func (h *Handler) prepareBudgeting(ctx context.Context, budget postgres.Budget, firstOfNextMonth time.Time, firstOfMonth time.Time) (BudgetingForMonthResponse, error) {
categories, err := h.Service.GetCategories(ctx, budget.ID)
if err != nil {
return BudgetingForMonthResponse{}, fmt.Errorf("error loading categories: %w", err)
}
cumultativeBalances, err := h.Service.GetCumultativeBalances(ctx, budget.ID)
if err != nil {
return BudgetingForMonthResponse{}, fmt.Errorf("error loading balances: %w", err)
}
categoriesWithBalance, moneyUsed := h.calculateBalances(
budget, firstOfNextMonth, firstOfMonth, categories, cumultativeBalances)
@ -86,16 +94,19 @@ func (h *Handler) budgetingForMonth(c *gin.Context) {
cat.AvailableLastMonth = availableBalance
}
data := struct {
Categories []CategoryWithBalance
AvailableBalance numeric.Numeric
}{categoriesWithBalance, availableBalance}
c.JSON(http.StatusOK, data)
data := BudgetingForMonthResponse{categoriesWithBalance, availableBalance}
return data, nil
}
type BudgetingForMonthResponse struct {
Categories []CategoryWithBalance
AvailableBalance numeric.Numeric
}
func (*Handler) getAvailableBalance(budget postgres.Budget,
moneyUsed numeric.Numeric, cumultativeBalances []postgres.GetCumultativeBalancesRow,
firstOfNextMonth time.Time) numeric.Numeric {
firstOfNextMonth time.Time,
) numeric.Numeric {
availableBalance := moneyUsed
for _, bal := range cumultativeBalances {
@ -149,7 +160,8 @@ func (h *Handler) returnBudgetingData(c *gin.Context, budgetUUID uuid.UUID) {
func (h *Handler) calculateBalances(budget postgres.Budget,
firstOfNextMonth time.Time, firstOfMonth time.Time, categories []postgres.GetCategoriesRow,
cumultativeBalances []postgres.GetCumultativeBalancesRow) ([]CategoryWithBalance, numeric.Numeric) {
cumultativeBalances []postgres.GetCumultativeBalancesRow,
) ([]CategoryWithBalance, numeric.Numeric) {
categoriesWithBalance := []CategoryWithBalance{}
moneyUsed2 := numeric.Zero()
@ -168,7 +180,8 @@ func (h *Handler) calculateBalances(budget postgres.Budget,
func (*Handler) CalculateCategoryBalances(cat *postgres.GetCategoriesRow,
cumultativeBalances []postgres.GetCumultativeBalancesRow, firstOfNextMonth time.Time,
moneyUsed *numeric.Numeric, firstOfMonth time.Time, budget postgres.Budget) CategoryWithBalance {
moneyUsed *numeric.Numeric, firstOfMonth time.Time, budget postgres.Budget,
) CategoryWithBalance {
categoryWithBalance := NewCategoryWithBalance(cat)
for _, bal := range cumultativeBalances {
if bal.CategoryID != cat.ID {