Skip to content

Commit

Permalink
fix: make 'expand' query param more smart by splitting ','
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Sep 27, 2024
1 parent 12f7aed commit d3698dd
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 23 deletions.
17 changes: 14 additions & 3 deletions internal/api/v2/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package v2
import (
"io"
"net/http"
"slices"
"strconv"
"strings"

"github.com/formancehq/go-libs/api"

Expand All @@ -12,7 +14,6 @@ import (
"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/time"

"github.com/formancehq/go-libs/collectionutils"
"github.com/formancehq/go-libs/pointer"
"github.com/formancehq/go-libs/query"
)
Expand Down Expand Up @@ -70,11 +71,21 @@ func getPITFilterWithVolumes(r *http.Request) (*ledgercontroller.PITFilterWithVo
}
return &ledgercontroller.PITFilterWithVolumes{
PITFilter: *pit,
ExpandVolumes: collectionutils.Contains(r.URL.Query()["expand"], "volumes"),
ExpandEffectiveVolumes: collectionutils.Contains(r.URL.Query()["expand"], "effectiveVolumes"),
ExpandVolumes: hasExpandVolumes(r),
ExpandEffectiveVolumes: hasExpandEffectiveVolumes(r),
}, nil
}

func hasExpandVolumes(r *http.Request) bool {
parts := strings.Split(r.URL.Query().Get("expand"), ",")
return slices.Contains(parts, "volumes")
}

func hasExpandEffectiveVolumes(r *http.Request) bool {
parts := strings.Split(r.URL.Query().Get("expand"), ",")
return slices.Contains(parts, "effectiveVolumes")
}

func getFiltersForVolumes(r *http.Request) (*ledgercontroller.FiltersForVolumes, error) {
pit, err := getPITOOTFilter(r)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/api/v2/controllers_accounts_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"net/url"

"github.com/formancehq/go-libs/api"
"github.com/formancehq/go-libs/collectionutils"
"github.com/formancehq/go-libs/platform/postgres"
"github.com/formancehq/ledger/internal/api/common"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
Expand All @@ -22,10 +21,10 @@ func readAccount(w http.ResponseWriter, r *http.Request) {
}

query := ledgercontroller.NewGetAccountQuery(param)
if collectionutils.Contains(r.URL.Query()["expand"], "volumes") {
if hasExpandVolumes(r) {
query = query.WithExpandVolumes()
}
if collectionutils.Contains(r.URL.Query()["expand"], "effectiveVolumes") {
if hasExpandEffectiveVolumes(r) {
query = query.WithExpandEffectiveVolumes()
}
pitFilter, err := getPITFilter(r)
Expand Down
5 changes: 2 additions & 3 deletions internal/api/v2/controllers_transactions_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strconv"

"github.com/formancehq/go-libs/api"
"github.com/formancehq/go-libs/collectionutils"
"github.com/formancehq/go-libs/platform/postgres"
"github.com/formancehq/ledger/internal/api/common"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
Expand All @@ -22,10 +21,10 @@ func readTransaction(w http.ResponseWriter, r *http.Request) {
}

query := ledgercontroller.NewGetTransactionQuery(int(txId))
if collectionutils.Contains(r.URL.Query()["expand"], "volumes") {
if hasExpandVolumes(r) {
query = query.WithExpandVolumes()
}
if collectionutils.Contains(r.URL.Query()["expand"], "effectiveVolumes") {
if hasExpandEffectiveVolumes(r) {
query = query.WithExpandEffectiveVolumes()
}

Expand Down
21 changes: 13 additions & 8 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3228,6 +3228,9 @@ components:
V2Transaction:
type: object
properties:
insertedAt:
type: string
format: date-time
timestamp:
type: string
format: date-time
Expand All @@ -3249,21 +3252,23 @@ components:
revertedAt:
type: string
format: date-time
preCommitVolumes:
$ref: '#/components/schemas/V2AggregatedVolumes'
postCommitVolumes:
$ref: '#/components/schemas/V2AggregatedVolumes'
preCommitEffectiveVolumes:
$ref: '#/components/schemas/V2AggregatedVolumes'
postCommitEffectiveVolumes:
$ref: '#/components/schemas/V2AggregatedVolumes'
required:
- postings
- timestamp
- id
- metadata
- reverted
- insertedAt
V2ExpandedTransaction:
allOf:
- $ref: '#/components/schemas/V2Transaction'
- type: object
properties:
preCommitVolumes:
$ref: '#/components/schemas/V2AggregatedVolumes'
postCommitVolumes:
$ref: '#/components/schemas/V2AggregatedVolumes'
$ref: '#/components/schemas/V2Transaction'
V2PostTransaction:
type: object
required:
Expand Down
18 changes: 12 additions & 6 deletions test/e2e/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,21 @@ var _ = Context("Ledger integration tests", func() {
transactionFromAPI, err := GetTransaction(ctx, testServer.GetValue(), operations.V2GetTransactionRequest{
Ledger: createLedgerRequest.Ledger,
ID: tx.ID,
Expand: pointer.For("volumes,effectiveVolumes"),
})
Expect(err).To(BeNil())
Expect(&components.V2Transaction{
Timestamp: transactionFromAPI.Timestamp,
Postings: transactionFromAPI.Postings,
Reference: transactionFromAPI.Reference,
Metadata: transactionFromAPI.Metadata,
ID: transactionFromAPI.ID,
Reverted: transactionFromAPI.Reverted,
InsertedAt: transactionFromAPI.InsertedAt,
Timestamp: transactionFromAPI.Timestamp,
Postings: transactionFromAPI.Postings,
Reference: transactionFromAPI.Reference,
Metadata: transactionFromAPI.Metadata,
ID: transactionFromAPI.ID,
Reverted: transactionFromAPI.Reverted,
PreCommitEffectiveVolumes: transactionFromAPI.PreCommitEffectiveVolumes,
PostCommitEffectiveVolumes: transactionFromAPI.PostCommitEffectiveVolumes,
PreCommitVolumes: transactionFromAPI.PreCommitVolumes,
PostCommitVolumes: transactionFromAPI.PostCommitVolumes,
}).To(Equal(tx))
})
})
Expand Down

0 comments on commit d3698dd

Please sign in to comment.