From 7f4aeda8f7165b14bcb0c086ca1b0502b973e0c5 Mon Sep 17 00:00:00 2001 From: Geoffrey Ragot Date: Wed, 17 Dec 2025 16:10:48 +0100 Subject: [PATCH] fix: regression on v1 endpoints --- .../api/v1/controllers_accounts_count_test.go | 9 ++--- .../api/v1/controllers_balances_aggregates.go | 1 + internal/api/v1/utils.go | 40 ++++++++++++++++++- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/internal/api/v1/controllers_accounts_count_test.go b/internal/api/v1/controllers_accounts_count_test.go index 3ce9e9416..fb28f6a25 100644 --- a/internal/api/v1/controllers_accounts_count_test.go +++ b/internal/api/v1/controllers_accounts_count_test.go @@ -1,18 +1,19 @@ package v1 import ( - "github.com/formancehq/ledger/internal/api/common" "net/http" "net/http/httptest" "net/url" "os" "testing" + "github.com/formancehq/ledger/internal/api/common" + "errors" + "github.com/formancehq/go-libs/v2/api" "github.com/formancehq/go-libs/v2/auth" "github.com/formancehq/go-libs/v2/query" - "github.com/formancehq/go-libs/v2/time" ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" @@ -30,7 +31,6 @@ func TestAccountsCount(t *testing.T) { returnErr error expectBackendCall bool } - before := time.Now() testCases := []testCase{ { @@ -117,13 +117,12 @@ func TestAccountsCount(t *testing.T) { router := NewRouter(systemController, auth.NewNoAuth(), "develop", os.Getenv("DEBUG") == "true") - req := httptest.NewRequest(http.MethodHead, "/xxx/accounts?pit="+before.Format(time.RFC3339Nano), nil) + req := httptest.NewRequest(http.MethodHead, "/xxx/accounts", nil) rec := httptest.NewRecorder() params := url.Values{} if testCase.queryParams != nil { params = testCase.queryParams } - params.Set("pit", before.Format(time.RFC3339Nano)) req.URL.RawQuery = params.Encode() router.ServeHTTP(rec, req) diff --git a/internal/api/v1/controllers_balances_aggregates.go b/internal/api/v1/controllers_balances_aggregates.go index 135a5791a..086479cf2 100644 --- a/internal/api/v1/controllers_balances_aggregates.go +++ b/internal/api/v1/controllers_balances_aggregates.go @@ -18,6 +18,7 @@ func buildAggregatedBalancesQuery(r *http.Request) query.Builder { } func getBalancesAggregated(w http.ResponseWriter, r *http.Request) { + rq, err := getResourceQuery[ledgercontroller.GetAggregatedVolumesOptions](r, func(q *ledgercontroller.GetAggregatedVolumesOptions) error { q.UseInsertionDate = true diff --git a/internal/api/v1/utils.go b/internal/api/v1/utils.go index c6e2d3242..b171ca45c 100644 --- a/internal/api/v1/utils.go +++ b/internal/api/v1/utils.go @@ -4,6 +4,7 @@ import ( "net/http" "strings" + "github.com/formancehq/go-libs/v2/time" ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger" "github.com/formancehq/go-libs/v2/bun/bunpaginate" @@ -72,8 +73,43 @@ func getResourceQuery[v any](r *http.Request, modifiers ...func(*v) error) (*led } } + pit, err := getPIT(r) + if err != nil { + return nil, err + } + + oot, err := getOOT(r) + if err != nil { + return nil, err + } + return &ledgercontroller.ResourceQuery[v]{ - Expand: r.URL.Query()["expand"], - Opts: options, + Expand: r.URL.Query()["expand"], + Opts: options, + PIT: pit, + OOT: oot, }, nil } + +func getPIT(r *http.Request) (*time.Time, error) { + return getDate(r, "pit") +} + +func getOOT(r *http.Request) (*time.Time, error) { + return getDate(r, "oot") +} + +func getDate(r *http.Request, key string) (*time.Time, error) { + dateString := r.URL.Query().Get(key) + + if dateString == "" { + return nil, nil + } + + date, err := time.ParseTime(dateString) + if err != nil { + return nil, err + } + + return &date, nil +}