feat: savings allocations
All checks were successful
Deploy / build-and-deploy (push) Successful in 1m31s

This commit is contained in:
juancwu 2026-05-04 03:19:36 +00:00
commit 2dac136049
17 changed files with 1140 additions and 4 deletions

View file

@ -0,0 +1,95 @@
package repository
import (
"database/sql"
"errors"
"git.juancwu.dev/juancwu/budgit/internal/model"
"github.com/jmoiron/sqlx"
"github.com/shopspring/decimal"
)
var ErrAllocationNotFound = errors.New("allocation not found")
type AllocationRepository interface {
Create(allocation *model.Allocation) error
ByID(id string) (*model.Allocation, error)
ByAccountID(accountID string) ([]*model.Allocation, error)
SumByAccountID(accountID string) (decimal.Decimal, error)
Update(id, name string, amount decimal.Decimal, target *decimal.Decimal) error
Delete(id string) error
}
type allocationRepository struct {
db *sqlx.DB
}
func NewAllocationRepository(db *sqlx.DB) AllocationRepository {
return &allocationRepository{db: db}
}
func (r *allocationRepository) Create(a *model.Allocation) error {
query := `INSERT INTO allocations (id, account_id, name, amount, target_amount, sort_order, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8);`
_, err := r.db.Exec(query, a.ID, a.AccountID, a.Name, a.Amount, a.TargetAmount, a.SortOrder, a.CreatedAt, a.UpdatedAt)
return err
}
func (r *allocationRepository) ByID(id string) (*model.Allocation, error) {
a := &model.Allocation{}
query := `SELECT * FROM allocations WHERE id = $1;`
err := r.db.Get(a, query, id)
if err == sql.ErrNoRows {
return nil, ErrAllocationNotFound
}
return a, err
}
func (r *allocationRepository) ByAccountID(accountID string) ([]*model.Allocation, error) {
var out []*model.Allocation
query := `SELECT * FROM allocations WHERE account_id = $1 ORDER BY sort_order ASC, created_at ASC;`
err := r.db.Select(&out, query, accountID)
return out, err
}
func (r *allocationRepository) SumByAccountID(accountID string) (decimal.Decimal, error) {
var sum decimal.Decimal
query := `SELECT COALESCE(SUM(amount::numeric), 0)::text FROM allocations WHERE account_id = $1;`
if err := r.db.Get(&sum, query, accountID); err != nil {
return decimal.Zero, err
}
return sum, nil
}
func (r *allocationRepository) Update(id, name string, amount decimal.Decimal, target *decimal.Decimal) error {
query := `UPDATE allocations
SET name = $1, amount = $2, target_amount = $3, updated_at = CURRENT_TIMESTAMP
WHERE id = $4;`
res, err := r.db.Exec(query, name, amount, target, id)
if err != nil {
return err
}
n, err := res.RowsAffected()
if err != nil {
return err
}
if n == 0 {
return ErrAllocationNotFound
}
return nil
}
func (r *allocationRepository) Delete(id string) error {
res, err := r.db.Exec(`DELETE FROM allocations WHERE id = $1;`, id)
if err != nil {
return err
}
n, err := res.RowsAffected()
if err != nil {
return err
}
if n == 0 {
return ErrAllocationNotFound
}
return nil
}

View file

@ -0,0 +1,107 @@
package repository
import (
"testing"
"time"
"git.juancwu.dev/juancwu/budgit/internal/model"
"git.juancwu.dev/juancwu/budgit/internal/testutil"
"github.com/google/uuid"
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAllocationRepository_CRUD(t *testing.T) {
testutil.ForEachDB(t, func(t *testing.T, dbi testutil.DBInfo) {
accountRepo := NewAccountRepository(dbi.DB)
repo := NewAllocationRepository(dbi.DB)
user := testutil.CreateTestUser(t, dbi.DB, "alloc-crud@example.com", nil)
space := testutil.CreateTestSpace(t, dbi.DB, user.ID, "Alloc Space")
now := time.Now()
account := &model.Account{
ID: uuid.NewString(),
Name: "Savings",
SpaceID: space.ID,
CreatedAt: now,
UpdatedAt: now,
}
require.NoError(t, accountRepo.Create(account))
target := decimal.NewFromInt(3000)
alloc := &model.Allocation{
ID: uuid.NewString(),
AccountID: account.ID,
Name: "Emergency Fund",
Amount: decimal.NewFromInt(500),
TargetAmount: &target,
SortOrder: 0,
CreatedAt: now,
UpdatedAt: now,
}
require.NoError(t, repo.Create(alloc))
fetched, err := repo.ByID(alloc.ID)
require.NoError(t, err)
assert.Equal(t, "Emergency Fund", fetched.Name)
assert.True(t, fetched.Amount.Equal(decimal.NewFromInt(500)))
require.NotNil(t, fetched.TargetAmount)
assert.True(t, fetched.TargetAmount.Equal(target))
alloc2 := &model.Allocation{
ID: uuid.NewString(),
AccountID: account.ID,
Name: "Trip",
Amount: decimal.NewFromInt(250),
SortOrder: 1,
CreatedAt: now,
UpdatedAt: now,
}
require.NoError(t, repo.Create(alloc2))
list, err := repo.ByAccountID(account.ID)
require.NoError(t, err)
require.Len(t, list, 2)
assert.Equal(t, "Emergency Fund", list[0].Name)
assert.Equal(t, "Trip", list[1].Name)
sum, err := repo.SumByAccountID(account.ID)
require.NoError(t, err)
assert.True(t, sum.Equal(decimal.NewFromInt(750)))
require.NoError(t, repo.Update(alloc.ID, "Rainy Day", decimal.NewFromInt(800), nil))
fetched, err = repo.ByID(alloc.ID)
require.NoError(t, err)
assert.Equal(t, "Rainy Day", fetched.Name)
assert.True(t, fetched.Amount.Equal(decimal.NewFromInt(800)))
assert.Nil(t, fetched.TargetAmount)
require.NoError(t, repo.Delete(alloc2.ID))
_, err = repo.ByID(alloc2.ID)
assert.ErrorIs(t, err, ErrAllocationNotFound)
assert.ErrorIs(t, repo.Delete(uuid.NewString()), ErrAllocationNotFound)
})
}
func TestAllocationRepository_UniqueNamePerAccount(t *testing.T) {
testutil.ForEachDB(t, func(t *testing.T, dbi testutil.DBInfo) {
accountRepo := NewAccountRepository(dbi.DB)
repo := NewAllocationRepository(dbi.DB)
user := testutil.CreateTestUser(t, dbi.DB, "alloc-unique@example.com", nil)
space := testutil.CreateTestSpace(t, dbi.DB, user.ID, "Unique Space")
now := time.Now()
account := &model.Account{ID: uuid.NewString(), Name: "A", SpaceID: space.ID, CreatedAt: now, UpdatedAt: now}
require.NoError(t, accountRepo.Create(account))
a := &model.Allocation{ID: uuid.NewString(), AccountID: account.ID, Name: "Goal", Amount: decimal.Zero, CreatedAt: now, UpdatedAt: now}
require.NoError(t, repo.Create(a))
dup := &model.Allocation{ID: uuid.NewString(), AccountID: account.ID, Name: "Goal", Amount: decimal.Zero, CreatedAt: now, UpdatedAt: now}
assert.Error(t, repo.Create(dup))
})
}

View file

@ -71,7 +71,7 @@ func (r *spaceAuditLogRepository) ListAccountEvents(accountID string, limit, off
FROM space_audit_logs a
LEFT JOIN users actor ON actor.id = a.actor_id
LEFT JOIN users target ON target.id = a.target_user_id
WHERE a.action LIKE 'account.%'
WHERE (a.action LIKE 'account.%' OR a.action LIKE 'allocation.%')
AND a.metadata->>'account_id' = $1
ORDER BY a.created_at DESC
LIMIT $2 OFFSET $3;`
@ -84,7 +84,8 @@ func (r *spaceAuditLogRepository) CountAccountEvents(accountID string) (int, err
var count int
err := r.db.Get(&count,
`SELECT COUNT(*) FROM space_audit_logs
WHERE action LIKE 'account.%' AND metadata->>'account_id' = $1;`,
WHERE (action LIKE 'account.%' OR action LIKE 'allocation.%')
AND metadata->>'account_id' = $1;`,
accountID)
return count, err
}