diff --git a/handler/treasure_chest.go b/handler/treasure_chest.go index 7af68b3..0d016d2 100644 --- a/handler/treasure_chest.go +++ b/handler/treasure_chest.go @@ -10,6 +10,7 @@ import ( "spend-sparrow/utils" "github.com/a-h/templ" + "github.com/google/uuid" ) type TreasureChest interface { @@ -51,7 +52,15 @@ func (h TreasureChestImpl) handleTreasureChestPage() http.HandlerFunc { return } - comp := t.TreasureChest(treasureChests) + transactionsRecurring, err := h.transactionRecurring.GetAll(user) + if err != nil { + handleError(w, r, err) + return + } + + monthlySums := h.calculateMonthlySums(treasureChests, transactionsRecurring) + + comp := t.TreasureChest(treasureChests, monthlySums) h.r.RenderLayout(r, w, comp, user) } } @@ -94,7 +103,8 @@ func (h TreasureChestImpl) handleTreasureChestItemComp() http.HandlerFunc { if r.URL.Query().Get("edit") == "true" { comp = t.EditTreasureChest(treasureChest, treasureChests, transactionsRec) } else { - comp = t.TreasureChestItem(treasureChest) + monthlySums := h.calculateMonthlySums(treasureChests, transactionsRecurring) + comp = t.TreasureChestItem(treasureChest, monthlySums) } h.r.Render(r, w, comp) } @@ -129,7 +139,16 @@ func (h TreasureChestImpl) handleUpdateTreasureChest() http.HandlerFunc { } } - comp := t.TreasureChestItem(treasureChest) + transactionsRecurring, err := h.transactionRecurring.GetAllByTreasureChest(user, treasureChest.Id.String()) + if err != nil { + handleError(w, r, err) + return + } + + treasureChests := make([]*types.TreasureChest, 1) + treasureChests[0] = treasureChest + monthlySums := h.calculateMonthlySums(treasureChests, transactionsRecurring) + comp := t.TreasureChestItem(treasureChest, monthlySums) h.r.Render(r, w, comp) } } @@ -151,3 +170,19 @@ func (h TreasureChestImpl) handleDeleteTreasureChest() http.HandlerFunc { } } } + +func (h TreasureChestImpl) calculateMonthlySums( + treasureChests []*types.TreasureChest, + transactionsRecurring []*types.TransactionRecurring, +) map[uuid.UUID]int64 { + monthlySums := make(map[uuid.UUID]int64) + for _, tc := range treasureChests { + monthlySums[tc.Id] = 0 + } + for _, t := range transactionsRecurring { + if t.TreasureChestId != nil && t.Value > 0 && t.IntervalMonths > 0 { + monthlySums[*t.TreasureChestId] += t.Value / t.IntervalMonths + } + } + return monthlySums +} diff --git a/service/transaction_recurring.go b/service/transaction_recurring.go index 0f0fd76..7f54f6f 100644 --- a/service/transaction_recurring.go +++ b/service/transaction_recurring.go @@ -29,6 +29,7 @@ var ( type TransactionRecurring interface { Add(user *types.User, transactionRecurring types.TransactionRecurringInput) (*types.TransactionRecurring, error) Update(user *types.User, transactionRecurring types.TransactionRecurringInput) (*types.TransactionRecurring, error) + GetAll(user *types.User) ([]*types.TransactionRecurring, error) GetAllByAccount(user *types.User, accountId string) ([]*types.TransactionRecurring, error) GetAllByTreasureChest(user *types.User, treasureChestId string) ([]*types.TransactionRecurring, error) Delete(user *types.User, id string) error @@ -158,6 +159,27 @@ func (s TransactionRecurringImpl) Update( return transactionRecurring, nil } +func (s TransactionRecurringImpl) GetAll(user *types.User) ([]*types.TransactionRecurring, error) { + transactionRecurringMetric.WithLabelValues("get_all_by_account").Inc() + if user == nil { + return nil, ErrUnauthorized + } + + transactionRecurrings := make([]*types.TransactionRecurring, 0) + err := s.db.Select(&transactionRecurrings, ` + SELECT * + FROM transaction_recurring + WHERE user_id = ? + ORDER BY created_at DESC`, + user.Id) + err = db.TransformAndLogDbError("transactionRecurring GetAll", nil, err) + if err != nil { + return nil, err + } + + return transactionRecurrings, nil +} + func (s TransactionRecurringImpl) GetAllByAccount(user *types.User, accountId string) ([]*types.TransactionRecurring, error) { transactionRecurringMetric.WithLabelValues("get_all_by_account").Inc() if user == nil { diff --git a/template/treasurechest/treasure_chest.templ b/template/treasurechest/treasure_chest.templ index 21dce89..8e8f981 100644 --- a/template/treasurechest/treasure_chest.templ +++ b/template/treasurechest/treasure_chest.templ @@ -5,7 +5,7 @@ import "spend-sparrow/template/svg" import "spend-sparrow/types" import "github.com/google/uuid" -templ TreasureChest(treasureChests []*types.TreasureChest) { +templ TreasureChest(treasureChests []*types.TreasureChest, monthlySums map[uuid.UUID]int64) {
{ treasureChest.Name }
++ if treasureChest.ParentId != nil { + + { displayBalance(monthlySums[treasureChest.Id]) } per month + } +
if treasureChest.ParentId != nil { if treasureChest.CurrentBalance < 0 { -{ displayBalance(treasureChest.CurrentBalance) }
+{ displayBalance(treasureChest.CurrentBalance) }
} else { -{ displayBalance(treasureChest.CurrentBalance) }
+{ displayBalance(treasureChest.CurrentBalance) }
} }