add owner handle

Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
GnomeZworc 2026-06-14 13:40:51 +02:00
commit f862abe5a4
Signed by: nicolas.boufideline
GPG key ID: 4406BBBF8845D632
12 changed files with 274 additions and 55 deletions

View file

@ -106,9 +106,22 @@ func (s *Store) ListAccountTransactions(ctx context.Context, accountID int32, f
return pgx.CollectRows(rows, pgx.RowToStructByName[AccountTransaction])
}
func (s *Store) GetTransaction(ctx context.Context, id int64) (Transaction, error) {
rows, err := s.pool.Query(ctx,
`SELECT `+txCols+` FROM transaction WHERE id = $1`, id)
// GetTransaction retourne une transaction. Si ownerID > 0, vérifie que l'un des
// comptes impliqués appartient à cet owner (retourne ErrNoRows sinon).
func (s *Store) GetTransaction(ctx context.Context, id int64, ownerID int32) (Transaction, error) {
q := `SELECT t.id, t.date::text, t.account_source_id, t.instrument_source_id, t.quantite_source,
t.account_dest_id, t.instrument_dest_id, t.quantite_dest,
t.tiers, t.label, t.categorie, t.validated, t.recurring_rule_id
FROM transaction t
LEFT JOIN account src ON src.id = t.account_source_id
LEFT JOIN account dst ON dst.id = t.account_dest_id
WHERE t.id = $1`
args := []any{id}
if ownerID > 0 {
q += ` AND (src.owner_id = $2 OR dst.owner_id = $2)`
args = append(args, ownerID)
}
rows, err := s.pool.Query(ctx, q, args...)
if err != nil {
return Transaction{}, err
}
@ -138,9 +151,9 @@ func (s *Store) CreateTransaction(ctx context.Context, p CreateTransactionParams
return tx, nil
}
func (s *Store) UpdateTransaction(ctx context.Context, id int64, p CreateTransactionParams) (Transaction, error) {
func (s *Store) UpdateTransaction(ctx context.Context, id int64, p CreateTransactionParams, ownerID int32) (Transaction, error) {
// Récupérer l'ancienne date pour invalider à partir du MIN(ancienne, nouvelle).
old, err := s.GetTransaction(ctx, id)
old, err := s.GetTransaction(ctx, id, ownerID)
if err != nil {
return Transaction{}, err
}
@ -172,7 +185,11 @@ func (s *Store) UpdateTransaction(ctx context.Context, id int64, p CreateTransac
return tx, nil
}
func (s *Store) SetValidated(ctx context.Context, id int64, validated bool) (Transaction, error) {
func (s *Store) SetValidated(ctx context.Context, id int64, validated bool, ownerID int32) (Transaction, error) {
// Vérifie l'ownership avant modification.
if _, err := s.GetTransaction(ctx, id, ownerID); err != nil {
return Transaction{}, err
}
rows, err := s.pool.Query(ctx,
`UPDATE transaction SET validated = $2 WHERE id = $1 RETURNING `+txCols,
id, validated)
@ -187,8 +204,8 @@ func (s *Store) SetValidated(ctx context.Context, id int64, validated bool) (Tra
return tx, nil
}
func (s *Store) DeleteTransaction(ctx context.Context, id int64) error {
tx, err := s.GetTransaction(ctx, id)
func (s *Store) DeleteTransaction(ctx context.Context, id int64, ownerID int32) error {
tx, err := s.GetTransaction(ctx, id, ownerID)
if err != nil {
return err
}