ajoute snapshot

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-06-13 22:52:13 +02:00
commit 0f2d7126eb
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
11 changed files with 862 additions and 19 deletions

View file

@ -3,6 +3,7 @@ package store
import (
"context"
"fmt"
"time"
"github.com/jackc/pgx/v5"
)
@ -129,10 +130,21 @@ func (s *Store) CreateTransaction(ctx context.Context, p CreateTransactionParams
if err != nil {
return Transaction{}, err
}
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Transaction])
tx, err := pgx.CollectOneRow(rows, pgx.RowToStructByName[Transaction])
if err != nil {
return tx, err
}
s.invalidateAccounts(ctx, tx.Date, tx.AccountSourceID, tx.AccountDestID)
return tx, nil
}
func (s *Store) UpdateTransaction(ctx context.Context, id int64, p CreateTransactionParams) (Transaction, error) {
// Récupérer l'ancienne date pour invalider à partir du MIN(ancienne, nouvelle).
old, err := s.GetTransaction(ctx, id)
if err != nil {
return Transaction{}, err
}
rows, err := s.pool.Query(ctx,
`UPDATE transaction SET
date = $2::date,
@ -148,7 +160,16 @@ func (s *Store) UpdateTransaction(ctx context.Context, id int64, p CreateTransac
if err != nil {
return Transaction{}, err
}
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Transaction])
tx, err := pgx.CollectOneRow(rows, pgx.RowToStructByName[Transaction])
if err != nil {
return tx, err
}
// Invalider depuis la date la plus ancienne (ancienne ou nouvelle).
earliest := minDateStr(old.Date, tx.Date)
s.invalidateAccounts(ctx, earliest, tx.AccountSourceID, tx.AccountDestID)
// Si les comptes ont changé, invalider aussi les anciens.
s.invalidateAccounts(ctx, earliest, old.AccountSourceID, old.AccountDestID)
return tx, nil
}
func (s *Store) SetValidated(ctx context.Context, id int64, validated bool) (Transaction, error) {
@ -158,10 +179,45 @@ func (s *Store) SetValidated(ctx context.Context, id int64, validated bool) (Tra
if err != nil {
return Transaction{}, err
}
return pgx.CollectOneRow(rows, pgx.RowToStructByName[Transaction])
tx, err := pgx.CollectOneRow(rows, pgx.RowToStructByName[Transaction])
if err != nil {
return tx, err
}
s.invalidateAccounts(ctx, tx.Date, tx.AccountSourceID, tx.AccountDestID)
return tx, nil
}
func (s *Store) DeleteTransaction(ctx context.Context, id int64) error {
_, err := s.pool.Exec(ctx, `DELETE FROM transaction WHERE id = $1`, id)
return err
tx, err := s.GetTransaction(ctx, id)
if err != nil {
return err
}
if _, err := s.pool.Exec(ctx, `DELETE FROM transaction WHERE id = $1`, id); err != nil {
return err
}
s.invalidateAccounts(ctx, tx.Date, tx.AccountSourceID, tx.AccountDestID)
return nil
}
// invalidateAccounts marque les comptes non-nil comme devant être recalculés depuis dateStr.
func (s *Store) invalidateAccounts(ctx context.Context, dateStr string, accountIDs ...*int32) {
date, err := time.Parse("2006-01-02", dateStr)
if err != nil {
return
}
seen := map[int32]bool{}
for _, id := range accountIDs {
if id != nil && !seen[*id] {
seen[*id] = true
s.InvalidateSnapshot(ctx, *id, date) //nolint:errcheck
}
}
}
// minDateStr retourne la plus petite des deux dates au format YYYY-MM-DD.
func minDateStr(a, b string) string {
if a <= b {
return a
}
return b
}