Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions internal/api/v1/controllers_accounts_count_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -30,7 +31,6 @@ func TestAccountsCount(t *testing.T) {
returnErr error
expectBackendCall bool
}
before := time.Now()

testCases := []testCase{
{
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions internal/api/v1/controllers_balances_aggregates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
40 changes: 38 additions & 2 deletions internal/api/v1/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}