Skip to content

Commit

Permalink
fix: make reversal date of reverted transactions matching insertion d…
Browse files Browse the repository at this point in the history
…ate of reverting transactions
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent ae0ca70 commit 34c8da8
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 93 deletions.
8 changes: 7 additions & 1 deletion internal/controller/ledger/controller_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type DefaultController struct {
ledger ledger.Ledger
}

// todo: could decline controller as some kinf of middlewares
func NewDefaultController(
ledger ledger.Ledger,
store Store,
Expand Down Expand Up @@ -402,7 +403,12 @@ func (ctrl *DefaultController) RevertTransaction(ctx context.Context, parameters
return nil, errors.Wrap(err, "failed to get balances")
}

reversedTx := originalTransaction.Reverse(atEffectiveDate)
reversedTx := originalTransaction.Reverse()
if atEffectiveDate {
reversedTx = reversedTx.WithTimestamp(originalTransaction.Timestamp)
} else {
reversedTx = reversedTx.WithTimestamp(*originalTransaction.RevertedAt)
}

// Check balances after the revert
// must be greater than 0
Expand Down
7 changes: 6 additions & 1 deletion internal/controller/ledger/controller_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package ledger
import (
"context"
"database/sql"
"github.com/formancehq/go-libs/pointer"
"github.com/formancehq/go-libs/time"
"math/big"
"testing"

Expand Down Expand Up @@ -87,7 +89,10 @@ func TestRevertTransaction(t *testing.T) {
txToRevert := &ledger.Transaction{}
sqlTX.EXPECT().
RevertTransaction(gomock.Any(), 1).
Return(txToRevert, true, nil)
DoAndReturn(func(_ context.Context, _ int) (*ledger.Transaction, bool, error) {
txToRevert.RevertedAt = pointer.For(time.Now())
return txToRevert, true, nil
})

sqlTX.EXPECT().
GetBalances(gomock.Any(), gomock.Any()).
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/ledger/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ func TestTransactionsList(t *testing.T) {
require.NoError(t, err)
require.True(t, hasBeenReverted)

tx4 := tx3BeforeRevert.Reverse(false).WithTimestamp(now)
tx4 := tx3BeforeRevert.Reverse().WithTimestamp(now)
err = store.CommitTransaction(ctx, &tx4)
require.NoError(t, err)

Expand Down
23 changes: 14 additions & 9 deletions internal/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ package ledger

import (
"encoding/json"
"github.com/formancehq/go-libs/time"
"github.com/invopop/jsonschema"
"github.com/uptrace/bun"
"math/big"
"slices"
"sort"

"github.com/uptrace/bun"

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

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

"github.com/formancehq/go-libs/metadata"
Expand Down Expand Up @@ -62,12 +60,8 @@ func (Transaction) JSONSchemaExtend(schema *jsonschema.Schema) {
schema.Properties.Set("preCommitEffectiveVolumes", postCommitVolumesSchema)
}

func (tx Transaction) Reverse(atEffectiveDate bool) Transaction {
func (tx Transaction) Reverse() Transaction {
ret := NewTransaction().WithPostings(tx.Postings.Reverse()...)
if atEffectiveDate {
ret = ret.WithTimestamp(tx.Timestamp)
}

return ret
}

Expand Down Expand Up @@ -212,6 +206,17 @@ func (tx Transaction) IsReverted() bool {
return tx.RevertedAt != nil && !tx.RevertedAt.IsZero()
}

func (tx Transaction) WithRevertedAt(timestamp time.Time) Transaction {
tx.RevertedAt = &timestamp
return tx
}

func (tx Transaction) WithPostCommitEffectiveVolumes(volumes PostCommitVolumes) Transaction {
tx.PostCommitEffectiveVolumes = volumes

return tx
}

func NewTransaction() Transaction {
return Transaction{
TransactionData: NewTransactionData(),
Expand Down
6 changes: 3 additions & 3 deletions internal/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestReverseTransaction(t *testing.T) {
).
WithTimestamp(tx.Timestamp)

reversed := tx.Reverse(true)
reversed.InsertedAt = time.Time{}
expected.InsertedAt = time.Time{}
reversed := tx.Reverse()
reversed.Timestamp = time.Time{}
expected.Timestamp = time.Time{}
require.Equal(t, expected, reversed)
}
21 changes: 13 additions & 8 deletions openapi/v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1506,6 +1506,9 @@ components:
V2Transaction:
type: object
properties:
insertedAt:
type: string
format: date-time
timestamp:
type: string
format: date-time
Expand All @@ -1527,21 +1530,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
10 changes: 5 additions & 5 deletions pkg/client/.speakeasy/gen.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
lockVersion: 2.0.0
id: a9ac79e1-e429-4ee3-96c4-ec973f19bec3
management:
docChecksum: c2e3359d7485797fe05bc7c81df33a2d
docChecksum: aab50c683ca7f0a7dec27a59b8eb6a91
docVersion: LEDGER_VERSION
speakeasyVersion: 1.351.0
generationVersion: 2.384.1
releaseVersion: 0.4.3
configChecksum: 5b9f3d061f8b36714f92e03d6ef0fc87
releaseVersion: 0.4.8
configChecksum: 7ae8e5ff8f460992fb233a182b16933e
features:
go:
additionalDependencies: 0.1.0
Expand Down Expand Up @@ -145,6 +145,7 @@ generatedFiles:
- /models/components/v2bulkresponse.go
- /models/components/v2bulkelementresult.go
- /models/components/v2transaction.go
- /models/components/v2volume.go
- /models/components/v2posting.go
- /models/components/v2bulkelement.go
- /models/components/v2bulkelementcreatetransaction.go
Expand All @@ -156,7 +157,6 @@ generatedFiles:
- /models/components/v2bulkelementdeletemetadata.go
- /models/components/v2accountscursorresponse.go
- /models/components/v2account.go
- /models/components/v2volume.go
- /models/components/v2accountresponse.go
- /models/components/v2statsresponse.go
- /models/components/v2stats.go
Expand Down Expand Up @@ -327,6 +327,7 @@ generatedFiles:
- docs/models/components/v2bulkelementresultcreatetransaction.md
- docs/models/components/v2bulkelementresult.md
- docs/models/components/v2transaction.md
- docs/models/components/v2volume.md
- docs/models/components/v2posting.md
- docs/models/components/v2bulkelement.md
- docs/models/components/v2bulkelementcreatetransaction.md
Expand All @@ -343,7 +344,6 @@ generatedFiles:
- docs/models/components/v2accountscursorresponsecursor.md
- docs/models/components/v2accountscursorresponse.md
- docs/models/components/v2account.md
- docs/models/components/v2volume.md
- docs/models/components/v2accountresponse.md
- docs/models/components/v2statsresponse.md
- docs/models/components/v2stats.md
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/.speakeasy/gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ generation:
auth:
oAuth2ClientCredentialsEnabled: true
go:
version: 0.4.3
version: 0.4.8
additionalDependencies: {}
allowUnknownFieldsInWeakUnions: false
clientServerStatusCodesAsErrors: true
Expand Down
5 changes: 4 additions & 1 deletion pkg/client/docs/models/components/v2expandedtransaction.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

| Field | Type | Required | Description | Example |
| ---------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| `InsertedAt` | [time.Time](https://pkg.go.dev/time#Time) | :heavy_check_mark: | N/A | |
| `Timestamp` | [time.Time](https://pkg.go.dev/time#Time) | :heavy_check_mark: | N/A | |
| `Postings` | [][components.V2Posting](../../models/components/v2posting.md) | :heavy_check_mark: | N/A | |
| `Reference` | **string* | :heavy_minus_sign: | N/A | ref:001 |
Expand All @@ -13,4 +14,6 @@
| `Reverted` | *bool* | :heavy_check_mark: | N/A | |
| `RevertedAt` | [*time.Time](https://pkg.go.dev/time#Time) | :heavy_minus_sign: | N/A | |
| `PreCommitVolumes` | map[string]map[string][components.V2Volume](../../models/components/v2volume.md) | :heavy_minus_sign: | N/A | {<br/>"orders:1": {<br/>"USD": {<br/>"input": 100,<br/>"output": 10,<br/>"balance": 90<br/>}<br/>},<br/>"orders:2": {<br/>"USD": {<br/>"input": 100,<br/>"output": 10,<br/>"balance": 90<br/>}<br/>}<br/>} |
| `PostCommitVolumes` | map[string]map[string][components.V2Volume](../../models/components/v2volume.md) | :heavy_minus_sign: | N/A | {<br/>"orders:1": {<br/>"USD": {<br/>"input": 100,<br/>"output": 10,<br/>"balance": 90<br/>}<br/>},<br/>"orders:2": {<br/>"USD": {<br/>"input": 100,<br/>"output": 10,<br/>"balance": 90<br/>}<br/>}<br/>} |
| `PostCommitVolumes` | map[string]map[string][components.V2Volume](../../models/components/v2volume.md) | :heavy_minus_sign: | N/A | {<br/>"orders:1": {<br/>"USD": {<br/>"input": 100,<br/>"output": 10,<br/>"balance": 90<br/>}<br/>},<br/>"orders:2": {<br/>"USD": {<br/>"input": 100,<br/>"output": 10,<br/>"balance": 90<br/>}<br/>}<br/>} |
| `PreCommitEffectiveVolumes` | map[string]map[string][components.V2Volume](../../models/components/v2volume.md) | :heavy_minus_sign: | N/A | {<br/>"orders:1": {<br/>"USD": {<br/>"input": 100,<br/>"output": 10,<br/>"balance": 90<br/>}<br/>},<br/>"orders:2": {<br/>"USD": {<br/>"input": 100,<br/>"output": 10,<br/>"balance": 90<br/>}<br/>}<br/>} |
| `PostCommitEffectiveVolumes` | map[string]map[string][components.V2Volume](../../models/components/v2volume.md) | :heavy_minus_sign: | N/A | {<br/>"orders:1": {<br/>"USD": {<br/>"input": 100,<br/>"output": 10,<br/>"balance": 90<br/>}<br/>},<br/>"orders:2": {<br/>"USD": {<br/>"input": 100,<br/>"output": 10,<br/>"balance": 90<br/>}<br/>}<br/>} |
Loading

0 comments on commit 34c8da8

Please sign in to comment.