Skip to content

Commit

Permalink
feat: Sync with stack repository => fcc7dac82d00b8ecb47b09a4b391e7105…
Browse files Browse the repository at this point in the history
…ab69e11
  • Loading branch information
flemzord committed Dec 12, 2023
1 parent 3fff0aa commit 3f8717e
Show file tree
Hide file tree
Showing 65 changed files with 6,966 additions and 954 deletions.
12 changes: 12 additions & 0 deletions .gitrepo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git "subrepo", and this file is maintained by the
; git-subrepo command. See https://github.com/ingydotnet/git-subrepo#readme
;
[subrepo]
remote = ledger
branch = main
commit = b2cc1d284edad80e8ecb5faa83ed0c64aa57e0ba
method = merge
cmdver = 0.4.6
parent = ce1a99ea700f99e23777eddd863e0155537a4c48
3 changes: 3 additions & 0 deletions .goreleaser.ledger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,15 @@ changelog:

release:
prerelease: auto
extra_files:
- glob: openapi.yaml
footer: |
## What to do next?
- Read the [documentation](https://docs.formance.com/)
- Join our [Slack server](https://formance.com/slack)
dockers:
- image_templates: ["ghcr.io/formancehq/{{ .ProjectName }}:{{ if not .IsNightly }}v{{ end }}{{ .Version }}-amd64"]
goarch: amd64
Expand Down
22 changes: 21 additions & 1 deletion Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ VERSION --pass-args --arg-scope-and-set 0.7
ARG core=github.com/formancehq/earthly:v0.5.2
IMPORT $core AS core
IMPORT ../.. AS stack
IMPORT .. AS components

FROM core+base-image

Expand Down Expand Up @@ -69,7 +70,7 @@ deploy:
lint:
FROM core+builder-image
COPY (+sources/*) /src
COPY --pass-args (stack+tidy/go.* --component=ledger) .
COPY --pass-args +tidy/go.* .
WORKDIR /src/components/ledger
DO --pass-args stack+GO_LINT
SAVE ARTIFACT cmd AS LOCAL cmd
Expand All @@ -78,7 +79,10 @@ lint:
SAVE ARTIFACT main.go AS LOCAL main.go

pre-commit:
BUILD --pass-args +tidy
BUILD --pass-args +lint
BUILD --pass-args +copy-libs
BUILD +openapi

bench:
FROM core+builder-image
Expand Down Expand Up @@ -118,3 +122,19 @@ benchstat:
COPY --pass-args github.com/formancehq/stack/components/ledger:$compareAgainstRevision+bench/results.txt /tmp/main.txt
COPY --pass-args +bench/results.txt /tmp/branch.txt
RUN --no-cache benchstat /tmp/main.txt /tmp/branch.txt

openapi:
FROM node:20-alpine
RUN apk update && apk add yq
RUN npm install -g openapi-merge-cli
WORKDIR /src/components/ledger
COPY --dir openapi openapi
RUN openapi-merge-cli --config ./openapi/openapi-merge.json
RUN yq -oy ./openapi.json > openapi.yaml
SAVE ARTIFACT ./openapi.yaml AS LOCAL ./openapi.yaml

tidy:
FROM core+builder-image
COPY --pass-args (+sources/src) /src
WORKDIR /src/components/ledger
DO --pass-args stack+GO_TIDY
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ require (
github.com/sirupsen/logrus v1.9.0
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.3
github.com/uptrace/bun v1.1.14
github.com/uptrace/bun/dialect/pgdialect v1.1.14
github.com/uptrace/bun/extra/bunotel v1.1.14
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/metric v1.16.0
go.opentelemetry.io/otel/trace v1.16.0
github.com/stretchr/testify v1.8.4
github.com/uptrace/bun v1.1.16
github.com/uptrace/bun/dialect/pgdialect v1.1.16
go.opentelemetry.io/otel v1.17.0
go.opentelemetry.io/otel/metric v1.17.0
go.opentelemetry.io/otel/trace v1.17.0
go.uber.org/fx v1.19.2
go.uber.org/mock v0.3.0
gopkg.in/segmentio/analytics-go.v3 v3.1.0
Expand Down Expand Up @@ -110,8 +109,9 @@ require (
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/uptrace/bun/extra/bunotel v1.1.16 // indirect
github.com/uptrace/opentelemetry-go-extra/otellogrus v0.1.21 // indirect
github.com/uptrace/opentelemetry-go-extra/otelsql v0.2.1 // indirect
github.com/uptrace/opentelemetry-go-extra/otelsql v0.2.2 // indirect
github.com/uptrace/opentelemetry-go-extra/otelutil v0.1.21 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
Expand Down
33 changes: 17 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -428,8 +428,9 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY=
github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.4.2 h1:X1TuBLAMDFbaTAChgCBLu3DU3UPyELpnF2jjJ2cz/S8=
github.com/subosito/gotenv v1.4.2/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
Expand All @@ -439,17 +440,17 @@ github.com/tklauser/numcpus v0.6.0 h1:kebhY2Qt+3U6RNK7UqpYNA+tJ23IBEGKkB7JQBfDYm
github.com/tklauser/numcpus v0.6.0/go.mod h1:FEZLMke0lhOUG6w2JadTzp0a+Nl8PF/GFkQ5UVIcaL4=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc h1:9lRDQMhESg+zvGYmW5DyG0UqvY96Bu5QYsTLvCHdrgo=
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc/go.mod h1:bciPuU6GHm1iF1pBvUfxfsH0Wmnc2VbpgvbI9ZWuIRs=
github.com/uptrace/bun v1.1.14 h1:S5vvNnjEynJ0CvnrBOD7MIRW7q/WbtvFXrdfy0lddAM=
github.com/uptrace/bun v1.1.14/go.mod h1:RHk6DrIisO62dv10pUOJCz5MphXThuOTpVNYEYv7NI8=
github.com/uptrace/bun/dialect/pgdialect v1.1.14 h1:b7+V1KDJPQSFYgkG/6YLXCl2uvwEY3kf/GSM7hTHRDY=
github.com/uptrace/bun/dialect/pgdialect v1.1.14/go.mod h1:v6YiaXmnKQ2FlhRD2c0ZfKd+QXH09pYn4H8ojaavkKk=
github.com/uptrace/bun/extra/bundebug v1.1.14 h1:9OCGfP9ZDlh41u6OLerWdhBtJAVGXHr0xtxO4xWi6t0=
github.com/uptrace/bun/extra/bunotel v1.1.14 h1:jKA1zNfD2/Y/O3eFP15ao+V0cMigXN+ReNbsVUqrOhg=
github.com/uptrace/bun/extra/bunotel v1.1.14/go.mod h1:BBuePZ4ciMqoeyRfef4GL7Z75FsiOm3Q3fvNt0z4sQk=
github.com/uptrace/bun v1.1.16 h1:cn9cgEMFwcyYRsQLfxCRMUxyK1WaHwOVrR3TvzEFZ/A=
github.com/uptrace/bun v1.1.16/go.mod h1:7HnsMRRvpLFUcquJxp22JO8PsWKpFQO/gNXqqsuGWg8=
github.com/uptrace/bun/dialect/pgdialect v1.1.16 h1:eUPZ+YCJ69BA+W1X1ZmpOJSkv1oYtinr0zCXf7zCo5g=
github.com/uptrace/bun/dialect/pgdialect v1.1.16/go.mod h1:KQjfx/r6JM0OXfbv0rFrxAbdkPD7idK8VitnjIV9fZI=
github.com/uptrace/bun/extra/bundebug v1.1.16 h1:SgicRQGtnjhrIhlYOxdkOm1Em4s6HykmT3JblHnoTBM=
github.com/uptrace/bun/extra/bunotel v1.1.16 h1:qkLTaTZK3FZk3b2P/stO/krS7KX9Fq5wSOj7Hlb2HG8=
github.com/uptrace/bun/extra/bunotel v1.1.16/go.mod h1:JwEH0kdXFnzYuK8D6eXUrf9HKsYy5wmB+lqQ/+dvH4E=
github.com/uptrace/opentelemetry-go-extra/otellogrus v0.1.21 h1:OXsouNDvuET5o1A4uvoCnAXuuNke8JlfZWceciyUlC8=
github.com/uptrace/opentelemetry-go-extra/otellogrus v0.1.21/go.mod h1:Xm3wlRGm5xzdAGPOvqydXPiGj0Da1q0OlUNm7Utoda4=
github.com/uptrace/opentelemetry-go-extra/otelsql v0.2.1 h1:sCYkntVVoSMuQuyRBaEkedb1qS1KeJJaqKbdtNfTsfM=
github.com/uptrace/opentelemetry-go-extra/otelsql v0.2.1/go.mod h1:1frv9RN1rlTq0jzCq+mVuEQisubZCQ4OU6S/8CaHzGY=
github.com/uptrace/opentelemetry-go-extra/otelsql v0.2.2 h1:USRngIQppxeyb39XzkVHXwQesKK0+JSwnHE/1c7fgic=
github.com/uptrace/opentelemetry-go-extra/otelsql v0.2.2/go.mod h1:1frv9RN1rlTq0jzCq+mVuEQisubZCQ4OU6S/8CaHzGY=
github.com/uptrace/opentelemetry-go-extra/otelutil v0.1.21 h1:HCqo51kNF8wxDMDhxcN5S6DlfZXigMtptRpkvjBCeVc=
github.com/uptrace/opentelemetry-go-extra/otelutil v0.1.21/go.mod h1:2MNqrUmDrt5E0glMuoJI/9FyGVpBKo1FqjSH60UOZFg=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
Expand Down Expand Up @@ -500,8 +501,8 @@ go.opentelemetry.io/contrib/instrumentation/runtime v0.42.0/go.mod h1:rD9feqRYP2
go.opentelemetry.io/contrib/propagators/b3 v1.17.0 h1:ImOVvHnku8jijXqkwCSyYKRDt2YrnGXD4BbhcpfbfJo=
go.opentelemetry.io/contrib/propagators/b3 v1.17.0/go.mod h1:IkfUfMpKWmynvvE0264trz0sf32NRTZL4nuAN9AbWRc=
go.opentelemetry.io/otel v1.3.0/go.mod h1:PWIKzi6JCp7sM0k9yZ43VX+T345uNbAkDKwHVjb2PTs=
go.opentelemetry.io/otel v1.16.0 h1:Z7GVAX/UkAXPKsy94IU+i6thsQS4nb7LviLpnaNeW8s=
go.opentelemetry.io/otel v1.16.0/go.mod h1:vl0h9NUa1D5s1nv3A5vZOYWn8av4K8Ml6JDeHrT/bx4=
go.opentelemetry.io/otel v1.17.0 h1:MW+phZ6WZ5/uk2nd93ANk/6yJ+dVrvNWUjGhnnFU5jM=
go.opentelemetry.io/otel v1.17.0/go.mod h1:I2vmBGtFaODIVMBSTPVDlJSzBDNf93k60E6Ft0nyjo0=
go.opentelemetry.io/otel/exporters/jaeger v1.16.0 h1:YhxxmXZ011C0aDZKoNw+juVWAmEfv/0W2XBOv9aHTaA=
go.opentelemetry.io/otel/exporters/jaeger v1.16.0/go.mod h1:grYbBo/5afWlPpdPZYhyn78Bk04hnvxn2+hvxQhKIQM=
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.16.0 h1:t4ZwRPU+emrcvM2e9DHd0Fsf0JTPVcbfa/BhTDF03d0=
Expand All @@ -522,16 +523,16 @@ go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.39.0 h1:fl2WmyenEf6LYY
go.opentelemetry.io/otel/exporters/stdout/stdoutmetric v0.39.0/go.mod h1:csyQxQ0UHHKVA8KApS7eUO/klMO5sd/av5CNZNU4O6w=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0 h1:+XWJd3jf75RXJq29mxbuXhCXFDG3S3R4vBUeSI2P7tE=
go.opentelemetry.io/otel/exporters/stdout/stdouttrace v1.16.0/go.mod h1:hqgzBPTf4yONMFgdZvL/bK42R/iinTyVQtiWihs3SZc=
go.opentelemetry.io/otel/metric v1.16.0 h1:RbrpwVG1Hfv85LgnZ7+txXioPDoh6EdbZHo26Q3hqOo=
go.opentelemetry.io/otel/metric v1.16.0/go.mod h1:QE47cpOmkwipPiefDwo2wDzwJrlfxxNYodqc4xnGCo4=
go.opentelemetry.io/otel/metric v1.17.0 h1:iG6LGVz5Gh+IuO0jmgvpTB6YVrCGngi8QGm+pMd8Pdc=
go.opentelemetry.io/otel/metric v1.17.0/go.mod h1:h4skoxdZI17AxwITdmdZjjYJQH5nzijUUjm+wtPph5o=
go.opentelemetry.io/otel/sdk v1.3.0/go.mod h1:rIo4suHNhQwBIPg9axF8V9CA72Wz2mKF1teNrup8yzs=
go.opentelemetry.io/otel/sdk v1.16.0 h1:Z1Ok1YsijYL0CSJpHt4cS3wDDh7p572grzNrBMiMWgE=
go.opentelemetry.io/otel/sdk v1.16.0/go.mod h1:tMsIuKXuuIWPBAOrH+eHtvhTL+SntFtXF9QD68aP6p4=
go.opentelemetry.io/otel/sdk/metric v0.39.0 h1:Kun8i1eYf48kHH83RucG93ffz0zGV1sh46FAScOTuDI=
go.opentelemetry.io/otel/sdk/metric v0.39.0/go.mod h1:piDIRgjcK7u0HCL5pCA4e74qpK/jk3NiUoAHATVAmiI=
go.opentelemetry.io/otel/trace v1.3.0/go.mod h1:c/VDhno8888bvQYmbYLqe41/Ldmr/KKunbvWM4/fEjk=
go.opentelemetry.io/otel/trace v1.16.0 h1:8JRpaObFoW0pxuVPapkgH8UhHQj+bJW8jJsCZEu5MQs=
go.opentelemetry.io/otel/trace v1.16.0/go.mod h1:Yt9vYq1SdNz3xdjZZK7wcXv1qv2pwLkqr2QVwea0ef0=
go.opentelemetry.io/otel/trace v1.17.0 h1:/SWhSRHmDPOImIAetP1QAeMnZYiQXrTy4fMMYOdSKWQ=
go.opentelemetry.io/otel/trace v1.17.0/go.mod h1:I/4vKTgFclIsXRVucpH25X0mpFSczM7aHeaz0ZBLWjY=
go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
go.opentelemetry.io/proto/otlp v0.19.0 h1:IVN6GR+mhC4s5yfcTbmzHYODqvWAp3ZedA2SJPI1Nnw=
go.opentelemetry.io/proto/otlp v0.19.0/go.mod h1:H7XAot3MsfNsj7EXtrA2q5xSNQ10UqI405h3+duxN4U=
Expand Down
5 changes: 3 additions & 2 deletions internal/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import (
"runtime"
"time"

"github.com/formancehq/ledger/internal/storage/paginate"
"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"

"github.com/formancehq/ledger/internal/storage/systemstore"
"github.com/formancehq/stack/libs/go-libs/api"

Expand Down Expand Up @@ -95,7 +96,7 @@ func (m *heartbeat) enqueue(ctx context.Context) error {
Set(TotalMemoryProperty, memory.TotalMemory()/1024/1024)

ledgersProperty := map[string]any{}
err = paginate.Iterate(ctx, systemstore.NewListLedgersQuery(10),
err = bunpaginate.Iterate(ctx, systemstore.NewListLedgersQuery(10),
func(ctx context.Context, q systemstore.ListLedgersQuery) (*api.Cursor[systemstore.Ledger], error) {
return m.backend.ListLedgers(ctx, q)
},
Expand Down
52 changes: 29 additions & 23 deletions internal/api/v1/controllers_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,39 @@ package v1
import (
"encoding/json"
"fmt"
"math/big"
"net/http"
"strconv"
"strings"

"github.com/formancehq/stack/libs/go-libs/pointer"

"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"
"github.com/go-chi/chi/v5"

"github.com/formancehq/ledger/internal/api/backend"
"github.com/pkg/errors"

ledger "github.com/formancehq/ledger/internal"
"github.com/formancehq/ledger/internal/storage/ledgerstore"
"github.com/formancehq/ledger/internal/storage/paginate"
sharedapi "github.com/formancehq/stack/libs/go-libs/api"
"github.com/formancehq/stack/libs/go-libs/collectionutils"
"github.com/formancehq/stack/libs/go-libs/metadata"
"github.com/formancehq/stack/libs/go-libs/query"
"github.com/go-chi/chi/v5"
)

type accountWithVolumesAndBalances ledger.ExpandedAccount

func (a accountWithVolumesAndBalances) MarshalJSON() ([]byte, error) {
type aux struct {
ledger.ExpandedAccount
Balances map[string]*big.Int `json:"balances"`
}
return json.Marshal(aux{
ExpandedAccount: ledger.ExpandedAccount(a),
Balances: a.Volumes.Balances(),
})
}

func buildAccountsFilterQuery(r *http.Request) (query.Builder, error) {
clauses := make([]query.Builder, 0)

Expand Down Expand Up @@ -90,25 +106,20 @@ func countAccounts(w http.ResponseWriter, r *http.Request) {
func getAccounts(w http.ResponseWriter, r *http.Request) {
l := backend.LedgerFromContext(r.Context())

q := ledgerstore.GetAccountsQuery{}

if r.URL.Query().Get(QueryKeyCursor) != "" {
err := paginate.UnmarshalCursor(r.URL.Query().Get(QueryKeyCursor), &q)
if err != nil {
sharedapi.BadRequest(w, ErrValidation, fmt.Errorf("invalid '%s' query param", QueryKeyCursor))
return
}
} else {
query, err := bunpaginate.Extract[ledgerstore.GetAccountsQuery](r, func() (*ledgerstore.GetAccountsQuery, error) {
options, err := getPaginatedQueryOptionsOfPITFilterWithVolumes(r)
if err != nil {
sharedapi.BadRequest(w, ErrValidation, err)
return
return nil, err
}
options.QueryBuilder, err = buildAccountsFilterQuery(r)
q = ledgerstore.NewGetAccountsQuery(*options)
return pointer.For(ledgerstore.NewGetAccountsQuery(*options)), nil
})
if err != nil {
sharedapi.BadRequest(w, ErrValidation, err)
return
}

cursor, err := l.GetAccountsWithVolumes(r.Context(), q)
cursor, err := l.GetAccountsWithVolumes(r.Context(), *query)
if err != nil {
sharedapi.InternalServerError(w, r, err)
return
Expand All @@ -121,20 +132,15 @@ func getAccount(w http.ResponseWriter, r *http.Request) {
l := backend.LedgerFromContext(r.Context())

query := ledgerstore.NewGetAccountQuery(chi.URLParam(r, "address"))
if collectionutils.Contains(r.URL.Query()["expand"], "volumes") {
query = query.WithExpandVolumes()
}
if collectionutils.Contains(r.URL.Query()["expand"], "effectiveVolumes") {
query = query.WithExpandEffectiveVolumes()
}
query = query.WithExpandVolumes()

acc, err := l.GetAccountWithVolumes(r.Context(), query)
if err != nil {
sharedapi.InternalServerError(w, r, err)
return
}

sharedapi.Ok(w, acc)
sharedapi.Ok(w, accountWithVolumesAndBalances(*acc))
}

func postAccountMetadata(w http.ResponseWriter, r *http.Request) {
Expand Down
7 changes: 4 additions & 3 deletions internal/api/v1/controllers_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"net/url"
"testing"

"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"

ledger "github.com/formancehq/ledger/internal"
v1 "github.com/formancehq/ledger/internal/api/v1"
"github.com/formancehq/ledger/internal/engine/command"
"github.com/formancehq/ledger/internal/opentelemetry/metrics"
"github.com/formancehq/ledger/internal/storage/ledgerstore"
"github.com/formancehq/ledger/internal/storage/paginate"
sharedapi "github.com/formancehq/stack/libs/go-libs/api"
"github.com/formancehq/stack/libs/go-libs/metadata"
"github.com/formancehq/stack/libs/go-libs/query"
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestGetAccounts(t *testing.T) {
{
name: "using empty cursor",
queryParams: url.Values{
"cursor": []string{paginate.EncodeCursor(ledgerstore.NewGetAccountsQuery(ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{})))},
"cursor": []string{bunpaginate.EncodeCursor(ledgerstore.NewGetAccountsQuery(ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{})))},
},
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{}),
},
Expand Down Expand Up @@ -155,7 +156,7 @@ func TestGetAccount(t *testing.T) {

backend, mock := newTestingBackend(t, true)
mock.EXPECT().
GetAccountWithVolumes(gomock.Any(), ledgerstore.NewGetAccountQuery("foo")).
GetAccountWithVolumes(gomock.Any(), ledgerstore.NewGetAccountQuery("foo").WithExpandVolumes()).
Return(&account, nil)

router := v1.NewRouter(backend, nil, metrics.NewNoOpRegistry())
Expand Down
28 changes: 12 additions & 16 deletions internal/api/v1/controllers_balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"math/big"
"net/http"

"github.com/formancehq/ledger/internal/api/backend"
"github.com/pkg/errors"
"github.com/formancehq/stack/libs/go-libs/pointer"

"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"

"github.com/formancehq/ledger/internal/api/backend"
"github.com/formancehq/ledger/internal/storage/ledgerstore"
"github.com/formancehq/ledger/internal/storage/paginate"
sharedapi "github.com/formancehq/stack/libs/go-libs/api"
"github.com/formancehq/stack/libs/go-libs/query"
)
Expand Down Expand Up @@ -43,25 +44,20 @@ func getBalancesAggregated(w http.ResponseWriter, r *http.Request) {
func getBalances(w http.ResponseWriter, r *http.Request) {
l := backend.LedgerFromContext(r.Context())

q := ledgerstore.GetAccountsQuery{}

if r.URL.Query().Get(QueryKeyCursor) != "" {
err := paginate.UnmarshalCursor(r.URL.Query().Get(QueryKeyCursor), &q)
if err != nil {
sharedapi.BadRequest(w, ErrValidation, errors.Errorf("invalid '%s' query param", QueryKeyCursor))
return
}
} else {
q, err := bunpaginate.Extract[ledgerstore.GetAccountsQuery](r, func() (*ledgerstore.GetAccountsQuery, error) {
options, err := getPaginatedQueryOptionsOfPITFilterWithVolumes(r)
if err != nil {
sharedapi.BadRequest(w, ErrValidation, err)
return
return nil, err
}
options.QueryBuilder, err = buildAccountsFilterQuery(r)
q = ledgerstore.NewGetAccountsQuery(*options)
return pointer.For(ledgerstore.NewGetAccountsQuery(*options)), nil
})
if err != nil {
sharedapi.BadRequest(w, ErrValidation, err)
return
}

cursor, err := l.GetAccountsWithVolumes(r.Context(), q)
cursor, err := l.GetAccountsWithVolumes(r.Context(), *q)
if err != nil {
sharedapi.InternalServerError(w, r, err)
return
Expand Down
4 changes: 2 additions & 2 deletions internal/api/v1/controllers_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
_ "embed"
"net/http"

"github.com/formancehq/ledger/internal/storage/paginate"
"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"

"github.com/formancehq/ledger/internal/storage/systemstore"
"github.com/formancehq/stack/libs/go-libs/collectionutils"
Expand Down Expand Up @@ -33,7 +33,7 @@ func getInfo(backend backend.Backend) func(w http.ResponseWriter, r *http.Reques
return func(w http.ResponseWriter, r *http.Request) {

ledgerNames := make([]string, 0)
if err := paginate.Iterate(r.Context(), systemstore.NewListLedgersQuery(100),
if err := bunpaginate.Iterate(r.Context(), systemstore.NewListLedgersQuery(100),
func(ctx context.Context, q systemstore.ListLedgersQuery) (*sharedapi.Cursor[systemstore.Ledger], error) {
return backend.ListLedgers(ctx, q)
},
Expand Down
Loading

0 comments on commit 3f8717e

Please sign in to comment.