add correct backfile

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-06-14 13:41:42 +02:00
commit 28e082f0fc
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
8 changed files with 317 additions and 35 deletions

View file

@ -171,6 +171,37 @@ func nullableDate(t time.Time) any {
return t
}
// ComputePRUs retourne le PRU (coût moyen pondéré) par instrument pour un compte,
// calculé sur toutes les transactions d'achat (account_dest_id) jusqu'à asOf inclus.
// PRU = sum(quantite_source) / sum(quantite_dest)
// Retourne uniquement les instruments qui ont des achats.
func (s *Store) ComputePRUs(ctx context.Context, accountID int32, asOf time.Time) (map[int32]float64, error) {
rows, err := s.pool.Query(ctx, `
SELECT instrument_dest_id,
SUM(quantite_source) / NULLIF(SUM(quantite_dest), 0) AS pru
FROM transaction
WHERE account_dest_id = $1
AND date <= $2::date
AND quantite_source IS NOT NULL
GROUP BY instrument_dest_id
`, accountID, asOf)
if err != nil {
return nil, err
}
defer rows.Close()
result := make(map[int32]float64)
for rows.Next() {
var instrumentID int32
var pru float64
if err := rows.Scan(&instrumentID, &pru); err != nil {
return nil, err
}
result[instrumentID] = pru
}
return result, rows.Err()
}
// GetPriceAt retourne le dernier prix connu pour un instrument à une date donnée.
// Retourne (0, false, nil) si aucun prix n'est trouvé.
func (s *Store) GetPriceAt(ctx context.Context, instrumentID int32, date time.Time) (float64, bool, error) {
@ -251,6 +282,30 @@ func (s *Store) GetInvalidations(ctx context.Context) ([]Invalidation, error) {
return out, rows.Err()
}
// GetInvalidationsForOwner retourne les invalidations des comptes appartenant à ownerID.
func (s *Store) GetInvalidationsForOwner(ctx context.Context, ownerID int32) ([]Invalidation, error) {
rows, err := s.pool.Query(ctx, `
SELECT ai.account_id, ai.recompute_from
FROM account_snapshot_invalidation ai
JOIN account a ON a.id = ai.account_id
WHERE a.owner_id = $1
ORDER BY ai.account_id
`, ownerID)
if err != nil {
return nil, err
}
defer rows.Close()
var out []Invalidation
for rows.Next() {
var inv Invalidation
if err := rows.Scan(&inv.AccountID, &inv.RecomputeFrom); err != nil {
return nil, err
}
out = append(out, inv)
}
return out, rows.Err()
}
// ClearInvalidation supprime le flag d'invalidation une fois le recalcul terminé.
func (s *Store) ClearInvalidation(ctx context.Context, accountID int32) error {
_, err := s.pool.Exec(ctx,