Skip to content

Commit

Permalink
feat: extract event emission from default controller
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 23, 2024
1 parent 7440ddf commit f3f198f
Show file tree
Hide file tree
Showing 20 changed files with 271 additions and 220 deletions.
4 changes: 2 additions & 2 deletions internal/api/v1/controllers_transactions_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
}
return
}
api.Ok(w, []any{mapTransactionToV1(*res)})
api.Ok(w, []any{mapTransactionToV1(res.Transaction)})
return
}

Expand Down Expand Up @@ -138,5 +138,5 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
return
}

api.Ok(w, []any{mapTransactionToV1(*res)})
api.Ok(w, []any{mapTransactionToV1(res.Transaction)})
}
7 changes: 4 additions & 3 deletions internal/api/v1/controllers_transactions_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/formancehq/go-libs/api"
"github.com/formancehq/go-libs/auth"
"github.com/formancehq/go-libs/pointer"
ledger "github.com/formancehq/ledger/internal"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -228,9 +227,11 @@ func TestTransactionsCreate(t *testing.T) {
ledgerController.EXPECT().
CreateTransaction(gomock.Any(), ledgercontroller.Parameters[ledgercontroller.RunScript]{
DryRun: tc.expectedPreview,
Input: testCase.expectedRunScript,
Input: testCase.expectedRunScript,
}).
Return(pointer.For(expectedTx), nil)
Return(&ledgercontroller.CreateTransactionResult{
Transaction: expectedTx,
}, nil)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())
Expand Down
4 changes: 2 additions & 2 deletions internal/api/v1/controllers_transactions_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func revertTransaction(w http.ResponseWriter, r *http.Request) {
return
}

tx, err := l.RevertTransaction(
ret, err := l.RevertTransaction(
r.Context(),
getCommandParameters(r, ledgercontroller.RevertTransaction{
Force: api.QueryParamBool(r, "disableChecks"),
Expand All @@ -43,5 +43,5 @@ func revertTransaction(w http.ResponseWriter, r *http.Request) {
return
}

api.Created(w, mapTransactionToV1(*tx))
api.Created(w, mapTransactionToV1(ret.ReversedTransaction))
}
4 changes: 3 additions & 1 deletion internal/api/v1/controllers_transactions_revert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ func TestTransactionsRevert(t *testing.T) {
Force: tc.expectForce,
},
}).
Return(pointer.For(tc.returnTx), tc.returnErr)
Return(pointer.For(ledgercontroller.RevertTransactionResult{
ReversedTransaction: tc.returnTx,
}), tc.returnErr)

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())

Expand Down
8 changes: 4 additions & 4 deletions internal/api/v2/controllers_bulk.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func ProcessBulk(ctx context.Context, l ledgercontroller.Controller, bulk Bulk,
}
rs := req.ToRunScript(false)

tx, err := l.CreateTransaction(ctx, ledgercontroller.Parameters[ledgercontroller.RunScript]{
createTransactionResult, err := l.CreateTransaction(ctx, ledgercontroller.Parameters[ledgercontroller.RunScript]{
DryRun: false,
IdempotencyKey: element.IdempotencyKey,
Input: *rs,
Expand Down Expand Up @@ -115,7 +115,7 @@ func ProcessBulk(ctx context.Context, l ledgercontroller.Controller, bulk Bulk,
}
} else {
ret = append(ret, Result{
Data: tx,
Data: createTransactionResult.Transaction,
ResponseType: element.Action,
})
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func ProcessBulk(ctx context.Context, l ledgercontroller.Controller, bulk Bulk,
return nil, errorsInBulk, fmt.Errorf("error parsing element %d: %s", i, err)
}

tx, err := l.RevertTransaction(ctx, ledgercontroller.Parameters[ledgercontroller.RevertTransaction]{
revertTransactionResult, err := l.RevertTransaction(ctx, ledgercontroller.Parameters[ledgercontroller.RevertTransaction]{
DryRun: false,
IdempotencyKey: element.IdempotencyKey,
Input: ledgercontroller.RevertTransaction{
Expand All @@ -210,7 +210,7 @@ func ProcessBulk(ctx context.Context, l ledgercontroller.Controller, bulk Bulk,
}
} else {
ret = append(ret, Result{
Data: tx,
Data: revertTransactionResult.ReversedTransaction,
ResponseType: element.Action,
})
}
Expand Down
14 changes: 8 additions & 6 deletions internal/api/v2/controllers_bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ func TestBulk(t *testing.T) {
Timestamp: now,
}, false),
}).
Return(&ledger.Transaction{
TransactionData: ledger.TransactionData{
Postings: postings,
Metadata: metadata.Metadata{},
Timestamp: now,
Return(&ledgercontroller.CreateTransactionResult{
Transaction: ledger.Transaction{
TransactionData: ledger.TransactionData{
Postings: postings,
Metadata: metadata.Metadata{},
Timestamp: now,
},
},
}, nil)
},
Expand Down Expand Up @@ -164,7 +166,7 @@ func TestBulk(t *testing.T) {
TransactionID: 1,
},
}).
Return(&ledger.Transaction{}, nil)
Return(&ledgercontroller.RevertTransactionResult{}, nil)
},
expectResults: []Result{{
Data: map[string]any{
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v2/controllers_transactions_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
return
}

api.Ok(w, res)
api.Ok(w, res.Transaction)
}
7 changes: 4 additions & 3 deletions internal/api/v2/controllers_transactions_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/formancehq/go-libs/api"
"github.com/formancehq/go-libs/auth"
"github.com/formancehq/go-libs/pointer"
ledger "github.com/formancehq/ledger/internal"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
"github.com/pkg/errors"
Expand Down Expand Up @@ -408,11 +407,13 @@ func TestTransactionCreate(t *testing.T) {
expect := ledgerController.EXPECT().
CreateTransaction(gomock.Any(), ledgercontroller.Parameters[ledgercontroller.RunScript]{
DryRun: tc.expectedDryRun,
Input: testCase.expectedRunScript,
Input: testCase.expectedRunScript,
})

if tc.returnError == nil {
expect.Return(pointer.For(expectedTx), nil)
expect.Return(&ledgercontroller.CreateTransactionResult{
Transaction: expectedTx,
}, nil)
} else {
expect.Return(nil, tc.returnError)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/api/v2/controllers_transactions_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func revertTransaction(w http.ResponseWriter, r *http.Request) {
return
}

tx, err := l.RevertTransaction(
ret, err := l.RevertTransaction(
r.Context(),
getCommandParameters(r, ledgercontroller.RevertTransaction{
Force: api.QueryParamBool(r, "force"),
Expand All @@ -43,5 +43,5 @@ func revertTransaction(w http.ResponseWriter, r *http.Request) {
return
}

api.Created(w, tx)
api.Created(w, ret.ReversedTransaction)
}
5 changes: 3 additions & 2 deletions internal/api/v2/controllers_transactions_revert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/formancehq/go-libs/api"
"github.com/formancehq/go-libs/auth"
"github.com/formancehq/go-libs/pointer"
ledger "github.com/formancehq/ledger/internal"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -76,7 +75,9 @@ func TestTransactionsRevert(t *testing.T) {
Force: tc.expectForce,
},
}).
Return(pointer.For(tc.returnTx), tc.returnErr)
Return(&ledgercontroller.RevertTransactionResult{
ReversedTransaction: tc.returnTx,
}, tc.returnErr)

router := NewRouter(systemController, auth.NewNoAuth(), "develop", testing.Verbose())

Expand Down
46 changes: 44 additions & 2 deletions internal/controller/ledger/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package ledger

import (
"context"
"github.com/formancehq/go-libs/metadata"
"github.com/formancehq/ledger/internal/machine/vm"

"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/migrations"
Expand Down Expand Up @@ -35,15 +37,15 @@ type Controller interface {
// * ErrTransactionReferenceConflict
// * ErrIdempotencyKeyConflict
// * ErrInsufficientFunds
CreateTransaction(ctx context.Context, parameters Parameters[RunScript]) (*ledger.Transaction, error)
CreateTransaction(ctx context.Context, parameters Parameters[RunScript]) (*CreateTransactionResult, error)
// RevertTransaction allow to revert a transaction.
// It can return following errors:
// * ErrInsufficientFunds
// * ErrAlreadyReverted
// * ErrNotFound
// Parameter force indicate we want to force revert the transaction even if the accounts does not have funds
// Parameter atEffectiveDate indicate we want to set the timestamp of the newly created transaction on the timestamp of the reverted transaction
RevertTransaction(ctx context.Context, parameters Parameters[RevertTransaction]) (*ledger.Transaction, error)
RevertTransaction(ctx context.Context, parameters Parameters[RevertTransaction]) (*RevertTransactionResult, error)
// SaveTransactionMetadata allow to add metadata to an existing transaction
// It can return following errors:
// * ErrNotFound
Expand All @@ -67,3 +69,43 @@ type Controller interface {
// Export allow to export the logs of a ledger
Export(ctx context.Context, w ExportWriter) error
}

type RunScript = vm.RunScript
type Script = vm.Script
type ScriptV1 = vm.ScriptV1

type CreateTransactionResult struct {
Transaction ledger.Transaction
AccountMetadata ledger.AccountMetadata
}

type RevertTransaction struct {
Force bool
AtEffectiveDate bool
TransactionID int
}

type RevertTransactionResult struct {
RevertedTransaction ledger.Transaction
ReversedTransaction ledger.Transaction
}

type SaveTransactionMetadata struct {
TransactionID int
Metadata metadata.Metadata
}

type SaveAccountMetadata struct {
Address string
Metadata metadata.Metadata
}

type DeleteTransactionMetadata struct {
TransactionID int
Key string
}

type DeleteAccountMetadata struct {
Address string
Key string
}
Loading

0 comments on commit f3f198f

Please sign in to comment.