feat(dashboard): #191 add development of treasurechests
This commit was merged in pull request #207.
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"spend-sparrow/internal/types"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jmoiron/sqlx"
|
||||
)
|
||||
|
||||
@@ -39,7 +40,9 @@ func (s Dashboard) MainChart(
|
||||
}
|
||||
|
||||
timeEntries := make([]types.DashboardMainChartEntry, 0)
|
||||
|
||||
var lastEntry *types.DashboardMainChartEntry
|
||||
|
||||
for _, t := range transactions {
|
||||
if t.Error != nil {
|
||||
continue
|
||||
@@ -64,6 +67,7 @@ func (s Dashboard) MainChart(
|
||||
if t.AccountId != nil {
|
||||
lastEntry.Value += t.Value
|
||||
}
|
||||
|
||||
if t.TreasureChestId != nil {
|
||||
lastEntry.Savings += t.Value
|
||||
}
|
||||
@@ -94,6 +98,7 @@ func (s Dashboard) TreasureChests(
|
||||
treasureChests = sortTreasureChests(treasureChests)
|
||||
|
||||
result := make([]*types.DashboardTreasureChest, 0)
|
||||
|
||||
for _, t := range treasureChests {
|
||||
if t.ParentId == nil {
|
||||
result = append(result, &types.DashboardTreasureChest{
|
||||
@@ -112,3 +117,59 @@ func (s Dashboard) TreasureChests(
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s Dashboard) TreasureChest(
|
||||
ctx context.Context,
|
||||
user *types.User,
|
||||
treausureChestId *uuid.UUID,
|
||||
) ([]types.DashboardMainChartEntry, error) {
|
||||
if user == nil {
|
||||
return nil, ErrUnauthorized
|
||||
}
|
||||
|
||||
transactions := make([]types.Transaction, 0)
|
||||
err := s.db.SelectContext(ctx, &transactions, `
|
||||
SELECT *
|
||||
FROM "transaction"
|
||||
WHERE user_id = ?
|
||||
AND treasure_chest_id = ?
|
||||
ORDER BY timestamp`, user.Id, treausureChestId)
|
||||
err = db.TransformAndLogDbError(ctx, "dashboard Chart", nil, err)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
timeEntries := make([]types.DashboardMainChartEntry, 0)
|
||||
|
||||
var lastEntry *types.DashboardMainChartEntry
|
||||
|
||||
for _, t := range transactions {
|
||||
if t.Error != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
newDay := t.Timestamp.Truncate(24 * time.Hour)
|
||||
if lastEntry == nil {
|
||||
lastEntry = &types.DashboardMainChartEntry{
|
||||
Day: newDay,
|
||||
Value: 0,
|
||||
}
|
||||
} else if lastEntry.Day != newDay {
|
||||
timeEntries = append(timeEntries, *lastEntry)
|
||||
lastEntry = &types.DashboardMainChartEntry{
|
||||
Day: newDay,
|
||||
Value: lastEntry.Value,
|
||||
}
|
||||
}
|
||||
|
||||
if t.TreasureChestId != nil {
|
||||
lastEntry.Value += t.Value
|
||||
}
|
||||
}
|
||||
|
||||
if lastEntry != nil {
|
||||
timeEntries = append(timeEntries, *lastEntry)
|
||||
}
|
||||
|
||||
return timeEntries, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user