Skip to content

Commit

Permalink
feat: add first_usage filter on /accounts (#1729)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored Oct 3, 2024
2 parents e48031e + cbbb37a commit 33d980b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
8 changes: 4 additions & 4 deletions components/ledger/internal/api/v2/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ func getPITOOTFilter(r *http.Request) (*ledgerstore.PITFilter, error) {
func getPITFilter(r *http.Request) (*ledgerstore.PITFilter, error) {
pitString := r.URL.Query().Get("pit")

pit := time.Now()
var pit *time.Time

if pitString != "" {
var err error
pit, err = time.ParseTime(pitString)
parsedPit, err := time.ParseTime(pitString)
if err != nil {
return nil, err
}
pit = &parsedPit
}

return &ledgerstore.PITFilter{
PIT: &pit,
PIT: pit,
}, nil
}

Expand Down
2 changes: 2 additions & 0 deletions components/ledger/internal/storage/ledgerstore/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ func (store *Store) accountQueryContext(qb query.Builder, q GetAccountsQuery) (s
}

return fmt.Sprintf("%s -> ? IS NOT NULL", key), []any{value}, nil
case key == "first_usage":
return fmt.Sprintf("%s %s ?", key, query.DefaultComparisonOperatorsMapping[operator]), []any{value}, nil
default:
return "", nil, newErrInvalidQuery("unknown key '%s' when building query", key)
}
Expand Down
2 changes: 1 addition & 1 deletion ee/gateway/build.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM ghcr.io/formancehq/base:22.04
ADD https://raw.githubusercontent.com/formancehq/stack/main/ee/gateway/Caddyfile /etc/caddy/Caddyfile
ADD https://raw.githubusercontent.com/formancehq/gateway/main/Caddyfile /etc/caddy/Caddyfile
COPY gateway /usr/bin/caddy
ENV OTEL_SERVICE_NAME gateway
ENTRYPOINT ["/usr/bin/caddy"]
Expand Down
2 changes: 1 addition & 1 deletion ee/gateway/scratch.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM ghcr.io/formancehq/base:scratch
ADD https://raw.githubusercontent.com/formancehq/stack/main/ee/gateway/Caddyfile /etc/caddy/Caddyfile
ADD https://raw.githubusercontent.com/formancehq/gateway/main/Caddyfile /etc/caddy/Caddyfile
COPY gateway /usr/bin/caddy
ENV OTEL_SERVICE_NAME gateway
ENTRYPOINT ["/usr/bin/caddy"]
Expand Down
20 changes: 19 additions & 1 deletion tests/integration/suite/ledger-list-count-accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var _ = WithModules([]*Module{modules.Ledger}, func() {
"clientType": "silver",
}

timestamp = time.Now().Round(time.Second).UTC()
timestamp = time.Now().Add(24 * time.Hour).Round(time.Second).UTC()
bigInt, _ = big.NewInt(0).SetString("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", 10)
)
BeforeEach(func() {
Expand Down Expand Up @@ -221,6 +221,24 @@ var _ = WithModules([]*Module{modules.Ledger}, func() {
Metadata: metadata1,
}))
})
It("should be listed on api using first_usage filters", func() {
response, err := Client().Ledger.V2ListAccounts(
TestContext(),
operations.V2ListAccountsRequest{
Ledger: "default",
RequestBody: map[string]interface{}{
"$lt": map[string]any{
"first_usage": time.Now(),
},
},
},
)
Expect(err).ToNot(HaveOccurred())
Expect(response.StatusCode).To(Equal(200))

accountsCursorResponse := response.V2AccountsCursorResponse
Expect(accountsCursorResponse.Cursor.Data).To(HaveLen(2))
})
It("should be listable on api using $not filter", func() {
response, err := Client().Ledger.V2ListAccounts(
TestContext(),
Expand Down
26 changes: 2 additions & 24 deletions tests/integration/suite/ledger-list-transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,28 +163,6 @@ var _ = WithModules([]*Module{modules.Ledger}, func() {
})
Then("following next cursor", func() {
BeforeEach(func() {

// Create a new transaction to ensure cursor is stable
_, err := Client().Ledger.V2CreateTransaction(
TestContext(),
operations.V2CreateTransactionRequest{
V2PostTransaction: shared.V2PostTransaction{
Metadata: map[string]string{},
Postings: []shared.V2Posting{
{
Amount: big.NewInt(100),
Asset: "USD",
Source: "world",
Destination: "account:0",
},
},
Timestamp: pointer.For(time.Now()),
},
Ledger: "default",
},
)
Expect(err).ToNot(HaveOccurred())

response, err := Client().Ledger.V2ListTransactions(
TestContext(),
operations.V2ListTransactionsRequest{
Expand Down Expand Up @@ -262,7 +240,7 @@ var _ = WithModules([]*Module{modules.Ledger}, func() {
"pageSize": float64(10),
"options": map[string]any{
"pit": now.Format(time.RFC3339),
"oot": nil,
"oot": nil,
"volumes": false,
"effectiveVolumes": false,
},
Expand Down Expand Up @@ -322,7 +300,7 @@ var _ = WithModules([]*Module{modules.Ledger}, func() {
"pageSize": float64(10),
"options": map[string]any{
"pit": now.Format(time.RFC3339),
"oot": nil,
"oot": nil,
"volumes": false,
"effectiveVolumes": false,
},
Expand Down

0 comments on commit 33d980b

Please sign in to comment.