From ff7cdfe4eba731b92550ae0b5353ca860fddb5df Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Fri, 31 May 2024 16:21:35 +0200 Subject: [PATCH 1/6] refactor: move pagination in domain to mpostgres to can be use for all components :hammer: --- .../ledger/internal/domain => common/mpostgres}/pagination.go | 2 +- components/ledger/internal/ports/account.go | 4 ++-- components/ledger/internal/ports/instrument.go | 4 ++-- components/ledger/internal/ports/ledger.go | 4 ++-- components/ledger/internal/ports/organization.go | 4 ++-- components/ledger/internal/ports/portfolio.go | 4 ++-- components/ledger/internal/ports/product.go | 4 ++-- 7 files changed, 13 insertions(+), 13 deletions(-) rename {components/ledger/internal/domain => common/mpostgres}/pagination.go (94%) diff --git a/components/ledger/internal/domain/pagination.go b/common/mpostgres/pagination.go similarity index 94% rename from components/ledger/internal/domain/pagination.go rename to common/mpostgres/pagination.go index 2ea7975c..25c066c4 100644 --- a/components/ledger/internal/domain/pagination.go +++ b/common/mpostgres/pagination.go @@ -1,4 +1,4 @@ -package domain +package mpostgres // Pagination is a struct designed to encapsulate pagination response payload data. type Pagination struct { diff --git a/components/ledger/internal/ports/account.go b/components/ledger/internal/ports/account.go index a326a515..f92fdf4d 100644 --- a/components/ledger/internal/ports/account.go +++ b/components/ledger/internal/ports/account.go @@ -3,10 +3,10 @@ package ports import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + "github.com/LerianStudio/midaz/common/mpostgres" commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app/command" "github.com/LerianStudio/midaz/components/ledger/internal/app/query" - "github.com/LerianStudio/midaz/components/ledger/internal/domain" a "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/account" "github.com/gofiber/fiber/v2" "go.mongodb.org/mongo-driver/bson" @@ -55,7 +55,7 @@ func (handler *AccountHandler) GetAllAccounts(c *fiber.Ctx) error { headerParams := common.ValidateParameters(c.Queries()) - pagination := domain.Pagination{ + pagination := mpostgres.Pagination{ Limit: headerParams.Limit, Page: headerParams.Page, } diff --git a/components/ledger/internal/ports/instrument.go b/components/ledger/internal/ports/instrument.go index c5a1451f..4342a77b 100644 --- a/components/ledger/internal/ports/instrument.go +++ b/components/ledger/internal/ports/instrument.go @@ -3,10 +3,10 @@ package ports import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + "github.com/LerianStudio/midaz/common/mpostgres" commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app/command" "github.com/LerianStudio/midaz/components/ledger/internal/app/query" - "github.com/LerianStudio/midaz/components/ledger/internal/domain" i "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/instrument" "github.com/gofiber/fiber/v2" "github.com/google/uuid" @@ -57,7 +57,7 @@ func (handler *InstrumentHandler) GetAllInstruments(c *fiber.Ctx) error { headerParams := common.ValidateParameters(c.Queries()) - pagination := domain.Pagination{ + pagination := mpostgres.Pagination{ Limit: headerParams.Limit, Page: headerParams.Page, } diff --git a/components/ledger/internal/ports/ledger.go b/components/ledger/internal/ports/ledger.go index 57a25618..cdc72640 100644 --- a/components/ledger/internal/ports/ledger.go +++ b/components/ledger/internal/ports/ledger.go @@ -1,10 +1,10 @@ package ports import ( + "github.com/LerianStudio/midaz/common/mpostgres" "os" "github.com/LerianStudio/midaz/common" - "github.com/LerianStudio/midaz/components/ledger/internal/domain" "go.mongodb.org/mongo-driver/bson" "github.com/LerianStudio/midaz/common/mlog" @@ -73,7 +73,7 @@ func (handler *LedgerHandler) GetAllLedgers(c *fiber.Ctx) error { headerParams := common.ValidateParameters(c.Queries()) - pagination := domain.Pagination{ + pagination := mpostgres.Pagination{ Limit: headerParams.Limit, Page: headerParams.Page, } diff --git a/components/ledger/internal/ports/organization.go b/components/ledger/internal/ports/organization.go index 3f253868..5a39765d 100644 --- a/components/ledger/internal/ports/organization.go +++ b/components/ledger/internal/ports/organization.go @@ -1,9 +1,9 @@ package ports import ( + "github.com/LerianStudio/midaz/common/mpostgres" "os" - "github.com/LerianStudio/midaz/components/ledger/internal/domain" "go.mongodb.org/mongo-driver/bson" "github.com/LerianStudio/midaz/common" @@ -96,7 +96,7 @@ func (handler *OrganizationHandler) GetAllOrganizations(c *fiber.Ctx) error { headerParams := common.ValidateParameters(c.Queries()) - pagination := domain.Pagination{ + pagination := mpostgres.Pagination{ Limit: headerParams.Limit, Page: headerParams.Page, } diff --git a/components/ledger/internal/ports/portfolio.go b/components/ledger/internal/ports/portfolio.go index c4eab38b..04760795 100644 --- a/components/ledger/internal/ports/portfolio.go +++ b/components/ledger/internal/ports/portfolio.go @@ -3,10 +3,10 @@ package ports import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + "github.com/LerianStudio/midaz/common/mpostgres" commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app/command" "github.com/LerianStudio/midaz/components/ledger/internal/app/query" - "github.com/LerianStudio/midaz/components/ledger/internal/domain" p "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/portfolio" "github.com/gofiber/fiber/v2" "go.mongodb.org/mongo-driver/bson" @@ -54,7 +54,7 @@ func (handler *PortfolioHandler) GetAllPortfolios(c *fiber.Ctx) error { headerParams := common.ValidateParameters(c.Queries()) - pagination := domain.Pagination{ + pagination := mpostgres.Pagination{ Limit: headerParams.Limit, Page: headerParams.Page, } diff --git a/components/ledger/internal/ports/product.go b/components/ledger/internal/ports/product.go index 6e9a623d..f7241a0f 100644 --- a/components/ledger/internal/ports/product.go +++ b/components/ledger/internal/ports/product.go @@ -3,10 +3,10 @@ package ports import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + "github.com/LerianStudio/midaz/common/mpostgres" commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app/command" "github.com/LerianStudio/midaz/components/ledger/internal/app/query" - "github.com/LerianStudio/midaz/components/ledger/internal/domain" r "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/product" "github.com/gofiber/fiber/v2" "go.mongodb.org/mongo-driver/bson" @@ -52,7 +52,7 @@ func (handler *ProductHandler) GetAllProducts(c *fiber.Ctx) error { headerParams := common.ValidateParameters(c.Queries()) - pagination := domain.Pagination{ + pagination := mpostgres.Pagination{ Limit: headerParams.Limit, Page: headerParams.Page, } From 21797bcd5e7eb2fe5f88ad64a0df75434647a034 Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Fri, 31 May 2024 16:43:57 +0200 Subject: [PATCH 2/6] refactor: add pagination, rename table to transaction without s :hammer: --- .../postgres/transaction.postgresql.go | 20 ++++++++++++++++--- .../domain/transaction/transaction.go | 2 +- .../transaction/transaction_repository.go | 2 +- .../gen/mock/transaction/transaction_mock.go | 8 ++++---- .../000001_create_transaction_table.down.sql | 2 +- .../000001_create_transaction_table.up.sql | 4 ++-- 6 files changed, 26 insertions(+), 12 deletions(-) diff --git a/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go b/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go index 165024c8..eff70e0f 100644 --- a/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go +++ b/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go @@ -4,6 +4,7 @@ import ( "context" "database/sql" "errors" + sqrl "github.com/Masterminds/squirrel" "reflect" "strconv" "strings" @@ -19,12 +20,14 @@ import ( // TransactionPostgreSQLRepository is a Postgresql-specific implementation of the TransactionRepository. type TransactionPostgreSQLRepository struct { connection *mpostgres.PostgresConnection + tableName string } // NewTransactionPostgreSQLRepository returns a new instance of TransactionPostgreSQLRepository using the given Postgres connection. func NewTransactionPostgreSQLRepository(pc *mpostgres.PostgresConnection) *TransactionPostgreSQLRepository { c := &TransactionPostgreSQLRepository{ connection: pc, + tableName: "transaction", } _, err := c.connection.GetDB() @@ -83,7 +86,7 @@ func (r *TransactionPostgreSQLRepository) Create(ctx context.Context, transactio } // FindAll retrieves Transactions entities from the database. -func (r *TransactionPostgreSQLRepository) FindAll(ctx context.Context, organizationID, ledgerID uuid.UUID) ([]*t.Transaction, error) { +func (r *TransactionPostgreSQLRepository) FindAll(ctx context.Context, organizationID, ledgerID uuid.UUID, limit, page int) ([]*t.Transaction, error) { db, err := r.connection.GetDB() if err != nil { return nil, err @@ -91,8 +94,19 @@ func (r *TransactionPostgreSQLRepository) FindAll(ctx context.Context, organizat var transactions []*t.Transaction - rows, err := db.QueryContext(ctx, "SELECT * FROM transaction WHERE organization_id = $1 AND ledger_id = $2 AND deleted_at IS NULL ORDER BY created_at DESC", - organizationID, ledgerID) + findAll := sqrl.Select("*"). + From(r.tableName). + Where(sqrl.Expr("organization_id = ?", organizationID)). + Where(sqrl.Expr("ledger_id = ?", ledgerID)). + Where(sqrl.Eq{"deleted_at": nil}). + OrderBy("created_at DESC"). + Limit(uint64(limit)). + Offset(uint64((page - 1) * limit)). + PlaceholderFormat(sqrl.Dollar) + + query, args, err := findAll.ToSql() + + rows, err := db.QueryContext(ctx, query, args...) if err != nil { return nil, err } diff --git a/components/transaction/internal/domain/transaction/transaction.go b/components/transaction/internal/domain/transaction/transaction.go index 2336537c..04200b78 100644 --- a/components/transaction/internal/domain/transaction/transaction.go +++ b/components/transaction/internal/domain/transaction/transaction.go @@ -1,9 +1,9 @@ package transaction import ( - "database/sql" "time" + "database/sql" "github.com/google/uuid" ) diff --git a/components/transaction/internal/domain/transaction/transaction_repository.go b/components/transaction/internal/domain/transaction/transaction_repository.go index ee7bd50a..92aae6e4 100644 --- a/components/transaction/internal/domain/transaction/transaction_repository.go +++ b/components/transaction/internal/domain/transaction/transaction_repository.go @@ -11,7 +11,7 @@ import ( //go:generate mockgen --destination=../../gen/mock/transaction/transaction_mock.go --package=mock . Repository type Repository interface { Create(ctx context.Context, transaction *Transaction) (*Transaction, error) - FindAll(ctx context.Context, organizationID, ledgerID uuid.UUID) ([]*Transaction, error) + FindAll(ctx context.Context, organizationID, ledgerID uuid.UUID, limit, page int) ([]*Transaction, error) Find(ctx context.Context, organizationID, ledgerID, id uuid.UUID) (*Transaction, error) ListByIDs(ctx context.Context, organizationID, ledgerID uuid.UUID, ids []uuid.UUID) ([]*Transaction, error) Update(ctx context.Context, organizationID, ledgerID, id uuid.UUID, transaction *Transaction) (*Transaction, error) diff --git a/components/transaction/internal/gen/mock/transaction/transaction_mock.go b/components/transaction/internal/gen/mock/transaction/transaction_mock.go index ec7e4c1e..efd9d9ce 100644 --- a/components/transaction/internal/gen/mock/transaction/transaction_mock.go +++ b/components/transaction/internal/gen/mock/transaction/transaction_mock.go @@ -86,18 +86,18 @@ func (mr *MockRepositoryMockRecorder) Find(arg0, arg1, arg2, arg3 any) *gomock.C } // FindAll mocks base method. -func (m *MockRepository) FindAll(arg0 context.Context, arg1, arg2 uuid.UUID) ([]*transaction.Transaction, error) { +func (m *MockRepository) FindAll(arg0 context.Context, arg1, arg2 uuid.UUID, arg3, arg4 int) ([]*transaction.Transaction, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "FindAll", arg0, arg1, arg2) + ret := m.ctrl.Call(m, "FindAll", arg0, arg1, arg2, arg3, arg4) ret0, _ := ret[0].([]*transaction.Transaction) ret1, _ := ret[1].(error) return ret0, ret1 } // FindAll indicates an expected call of FindAll. -func (mr *MockRepositoryMockRecorder) FindAll(arg0, arg1, arg2 any) *gomock.Call { +func (mr *MockRepositoryMockRecorder) FindAll(arg0, arg1, arg2, arg3, arg4 any) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindAll", reflect.TypeOf((*MockRepository)(nil).FindAll), arg0, arg1, arg2) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindAll", reflect.TypeOf((*MockRepository)(nil).FindAll), arg0, arg1, arg2, arg3, arg4) } // ListByIDs mocks base method. diff --git a/components/transaction/migrations/000001_create_transaction_table.down.sql b/components/transaction/migrations/000001_create_transaction_table.down.sql index d9c846fc..da4ae96a 100644 --- a/components/transaction/migrations/000001_create_transaction_table.down.sql +++ b/components/transaction/migrations/000001_create_transaction_table.down.sql @@ -1 +1 @@ -DROP TABLE IF EXISTS transactions; \ No newline at end of file +DROP TABLE IF EXISTS "transaction"; \ No newline at end of file diff --git a/components/transaction/migrations/000001_create_transaction_table.up.sql b/components/transaction/migrations/000001_create_transaction_table.up.sql index 3d1eb244..58fcf055 100644 --- a/components/transaction/migrations/000001_create_transaction_table.up.sql +++ b/components/transaction/migrations/000001_create_transaction_table.up.sql @@ -1,4 +1,4 @@ -CREATE TABLE IF NOT EXISTS transactions ( +CREATE TABLE IF NOT EXISTS "transaction" ( id UUID PRIMARY KEY NOT NULL DEFAULT (uuid_generate_v4()), parent_transaction_id UUID, description TEXT NOT NULL, @@ -13,5 +13,5 @@ CREATE TABLE IF NOT EXISTS transactions ( created_at TIMESTAMP WITH TIME ZONE, updated_at TIMESTAMP WITH TIME ZONE, deleted_at TIMESTAMP WITH TIME ZONE, - FOREIGN KEY (parent_transaction_id) REFERENCES transactions (id) + FOREIGN KEY (parent_transaction_id) REFERENCES "transaction" (id) ) \ No newline at end of file From 8498c6bf1bf9df64c3a2f2295ceb861565226564 Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Fri, 31 May 2024 18:58:19 +0200 Subject: [PATCH 3/6] refactor: some adjusts :hammer: --- components/transaction/internal/gen/inject.go | 3 ++- components/transaction/internal/gen/wire_gen.go | 5 +++-- components/transaction/internal/ports/http/routes.go | 3 ++- .../transaction/internal/ports/{http => }/transaction.go | 2 +- 4 files changed, 8 insertions(+), 5 deletions(-) rename components/transaction/internal/ports/{http => }/transaction.go (99%) diff --git a/components/transaction/internal/gen/inject.go b/components/transaction/internal/gen/inject.go index 178d8490..5ae224c6 100644 --- a/components/transaction/internal/gen/inject.go +++ b/components/transaction/internal/gen/inject.go @@ -6,6 +6,7 @@ package gen import ( "fmt" "github.com/LerianStudio/midaz/components/transaction/internal/adapters/database/postgres" + "github.com/LerianStudio/midaz/components/transaction/internal/ports" "sync" "github.com/LerianStudio/midaz/common" @@ -59,7 +60,7 @@ var ( httpHandler.NewRouter, service.NewServer, postgres.NewTransactionPostgreSQLRepository, - wire.Struct(new(httpHandler.TransactionHandler), "*"), + wire.Struct(new(ports.TransactionHandler), "*"), wire.Struct(new(command.UseCase), "*"), wire.Struct(new(query.UseCase), "*"), wire.Bind(new(t.Repository), new(*postgres.TransactionPostgreSQLRepository)), diff --git a/components/transaction/internal/gen/wire_gen.go b/components/transaction/internal/gen/wire_gen.go index 93580d3c..4324f76c 100644 --- a/components/transaction/internal/gen/wire_gen.go +++ b/components/transaction/internal/gen/wire_gen.go @@ -16,6 +16,7 @@ import ( "github.com/LerianStudio/midaz/components/transaction/internal/app/command" "github.com/LerianStudio/midaz/components/transaction/internal/app/query" "github.com/LerianStudio/midaz/components/transaction/internal/domain/transaction" + "github.com/LerianStudio/midaz/components/transaction/internal/ports" "github.com/LerianStudio/midaz/components/transaction/internal/ports/http" "github.com/LerianStudio/midaz/components/transaction/internal/service" "github.com/google/wire" @@ -35,7 +36,7 @@ func InitializeService() *service.Service { queryUseCase := &query.UseCase{ TransactionRepo: transactionPostgreSQLRepository, } - transactionHandler := &http.TransactionHandler{ + transactionHandler := &ports.TransactionHandler{ Command: useCase, Query: queryUseCase, } @@ -82,7 +83,7 @@ func setupMongoDBConnection(cfg *service.Config) *mmongo.MongoConnection { var ( serviceSet = wire.NewSet(common.InitLocalEnvConfig, mzap.InitializeLogger, setupPostgreSQLConnection, - setupMongoDBConnection, service.NewConfig, http.NewRouter, service.NewServer, postgres.NewTransactionPostgreSQLRepository, wire.Struct(new(http.TransactionHandler), "*"), wire.Struct(new(command.UseCase), "*"), wire.Struct(new(query.UseCase), "*"), wire.Bind(new(transaction.Repository), new(*postgres.TransactionPostgreSQLRepository)), + setupMongoDBConnection, service.NewConfig, http.NewRouter, service.NewServer, postgres.NewTransactionPostgreSQLRepository, wire.Struct(new(ports.TransactionHandler), "*"), wire.Struct(new(command.UseCase), "*"), wire.Struct(new(query.UseCase), "*"), wire.Bind(new(transaction.Repository), new(*postgres.TransactionPostgreSQLRepository)), ) svcSet = wire.NewSet(wire.Struct(new(service.Service), "Server", "Logger")) diff --git a/components/transaction/internal/ports/http/routes.go b/components/transaction/internal/ports/http/routes.go index 388bd077..944d3d1f 100644 --- a/components/transaction/internal/ports/http/routes.go +++ b/components/transaction/internal/ports/http/routes.go @@ -2,12 +2,13 @@ package http import ( lib "github.com/LerianStudio/midaz/common/net/http" + "github.com/LerianStudio/midaz/components/transaction/internal/ports" "github.com/LerianStudio/midaz/components/transaction/internal/service" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cors" ) -func NewRouter(th *TransactionHandler) *fiber.App { +func NewRouter(th *ports.TransactionHandler) *fiber.App { f := fiber.New() _ = service.NewConfig() diff --git a/components/transaction/internal/ports/http/transaction.go b/components/transaction/internal/ports/transaction.go similarity index 99% rename from components/transaction/internal/ports/http/transaction.go rename to components/transaction/internal/ports/transaction.go index bce08b9b..9d1b7a7f 100644 --- a/components/transaction/internal/ports/http/transaction.go +++ b/components/transaction/internal/ports/transaction.go @@ -1,4 +1,4 @@ -package http +package ports import ( "bytes" From ad6510803495b9f234a2b92f37bbadd908ca27ba Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Mon, 3 Jun 2024 12:09:59 +0200 Subject: [PATCH 4/6] feat(transaction): exemples files for test :sparkles: --- components/transaction/examples/empty.gold | 0 components/transaction/examples/error.gold | 19 +++++ .../examples/transaction-template.gold | 38 +++++++++ .../transaction/examples/transaction.gold | 85 +++++++++++++++++++ .../transaction/examples/transaction.yml | 0 5 files changed, 142 insertions(+) create mode 100644 components/transaction/examples/empty.gold create mode 100644 components/transaction/examples/error.gold create mode 100644 components/transaction/examples/transaction-template.gold create mode 100644 components/transaction/examples/transaction.gold create mode 100644 components/transaction/examples/transaction.yml diff --git a/components/transaction/examples/empty.gold b/components/transaction/examples/empty.gold new file mode 100644 index 00000000..e69de29b diff --git a/components/transaction/examples/error.gold b/components/transaction/examples/error.gold new file mode 100644 index 00000000..3b654c4f --- /dev/null +++ b/components/transaction/examples/error.gold @@ -0,0 +1,19 @@ +(transaction + (chart-of-accounts-group-name PAG_CONTAS_CODE_123182371973821973981) + (metadata + (m 1) + (Cpf 43049498x) + ) + (from 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :amount BRL 2|4) + (description "description for the transaction not for the operation") + (send BRL 30|4 :from + (sources + (from 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :remaining) + ) + ) + (distribute + (to @McGeegor :share 50 :of 25 :desc whatever) + (to @jeff :amount BRL 2|4) + (to @cl3v_ :remaining) + ) +) \ No newline at end of file diff --git a/components/transaction/examples/transaction-template.gold b/components/transaction/examples/transaction-template.gold new file mode 100644 index 00000000..c45077f4 --- /dev/null +++ b/components/transaction/examples/transaction-template.gold @@ -0,0 +1,38 @@ +(transaction V1 + (chart-of-accounts-group-name p2p_transfer_group) + (description "P2P sent") + (code P2P_TRANSFER) + (pending false) + (metadata + (anyKey anyValue) + (anotherKey anotherValue) + ) + (send BRL $amount|$scale + (source + (from $sourceAccount :amount BRL $amount|2 + (description "P2P sent to $destination") + (chart-of-accounts p2p_debit_source_8123891) + (metadata + (anyKey anyValue) + (anotherKey anotherValue) + ) + ) + ) + ) + (distribute + (to $destination :amount BRL $amount|$scale + (description "P2P sent $destination") + (chart-of-accounts p2p_credit_destination_1231412) + ) + (distribute :remaining + (to $destination :remaining + (description "P2P sent $destination") + (chart-of-accounts p2p_credit_destination_1231412) + (metadata + (anyKey anyValue) + (anotherKey anotherValue) + ) + ) + ) + ) +) \ No newline at end of file diff --git a/components/transaction/examples/transaction.gold b/components/transaction/examples/transaction.gold new file mode 100644 index 00000000..ccac48d3 --- /dev/null +++ b/components/transaction/examples/transaction.gold @@ -0,0 +1,85 @@ +(transaction V1 + (chart-of-accounts-group-name PAG_CONTAS_CODE_123182371973821973981) + (description "mcgregor for the transaction not for the operation") + (metadata + (m 1) + (Cpf 43049498x) + ) + (send BRL 30|4 + (source + (from 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :amount BRL 2|4) + (from 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :share 100 :of 25 :desc whatever) + (from 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :remaining) + (from 3172933b-50d2-4b17-96aa-9b378d6a6eac :amount BRL 2|4 + (description "description for the transaction not for the operation") + ) + (from 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :share 50 :of 100 :desc whatever + (description "description for the transaction not for the operation") + ) + (from 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :remaining + (description "description for the transaction not for the operation") + ) + (from 3172933b-50d2-4b17-96aa-9b378d6a6eac :amount BRL 2|4 + (metadata + (Cpf 43049498x) + (1 m) + ) + ) + (from 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :share 100 :of 25 :desc whatever + (metadata + (Cpf 43049498x) + (1 m) + ) + ) + (from 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :remaining + (metadata + (Cpf 43049498x) + (1 m) + ) + ) + (source :remaining + (from 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :amount BRL 2|4) + ) + ) + ) + (distribute + (to @McGeegor :share 50 :of 25 :desc whatever) + (to @jeff :amount BRL 2|4) + (to @cl3v_ :remaining) + (to 1a9ba2dd-d778-415f-a61a-387d64994eeb :share 50 :of 100 :desc whatever + (description "description for the transaction not for the operation") + ) + (to 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :amount BRL 2|4 + (description "description for the transaction not for the operation") + ) + (to 9fe618ed-cec1-4a5b-90d8-2f5819a8c3dd :remaining + (description "description for the transaction not for the operation") + ) + (to 540b504c-8f9d-480f-9354-3728cf7025ef :share 50 :of 25 :desc whatever + (description "description for the transaction not for the operation") + (metadata + (m m) + (Cpf 43049498x) + ) + ) + (to 540b504c-8f9d-480f-9354-3728cf7025ef :amount BRL 2|4 + (description "description for the transaction not for the operation") + (metadata + (m m) + (Cpf 43049498x) + ) + ) + (to 540b504c-8f9d-480f-9354-3728cf7025ef :remaining + (description "description for the transaction not for the operation") + (metadata + (m m) + (Cpf 43049498x) + ) + ) + (distribute :remaining + (to 540b504c-8f9d-480f-9354-3728cf7025ef :share 38) + (to @McGregor :share 50 :of 25 :desc whatever) + (to 54748fc9-53f1-4d5b-bdc3-660c64f1e974 :amount BRL 2|4) + ) + ) +) \ No newline at end of file diff --git a/components/transaction/examples/transaction.yml b/components/transaction/examples/transaction.yml new file mode 100644 index 00000000..e69de29b From 0e7bf06ed509301522307cc1e8bd9ef5829efe81 Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Mon, 3 Jun 2024 12:34:55 +0200 Subject: [PATCH 5/6] refactor: update database and entity struct :hammer: --- .../domain/transaction/transaction.go | 24 ++++++++++++++++--- .../000001_create_transaction_table.up.sql | 1 + .../000002_create_operation_table.up.sql | 10 +++++--- ...000003_create_instrument_rate_table.up.sql | 3 +++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/components/transaction/internal/domain/transaction/transaction.go b/components/transaction/internal/domain/transaction/transaction.go index 04200b78..31e5268f 100644 --- a/components/transaction/internal/domain/transaction/transaction.go +++ b/components/transaction/internal/domain/transaction/transaction.go @@ -14,6 +14,7 @@ type TransactionPostgreSQLModel struct { Description string Template string Status string + StatusDescription *string Amount *float64 AmountScale *float64 InstrumentCode string @@ -26,13 +27,24 @@ type TransactionPostgreSQLModel struct { Metadata map[string]any } +// Status structure for marshaling/unmarshalling JSON. +type Status struct { + Code string `json:"code"` + Description *string `json:"description"` +} + +// IsEmpty method that set empty or nil in fields +func (s Status) IsEmpty() bool { + return s.Code == "" && s.Description == nil +} + // Transaction is a struct designed to encapsulate response payload data. type Transaction struct { ID string `json:"id"` ParentTransactionID *string `json:"parentTransactionId,omitempty"` Description string `json:"description"` Template string `json:"template"` - Status string `json:"status"` + Status Status `json:"status"` Amount *float64 `json:"amount"` AmountScale *float64 `json:"amountScale"` InstrumentCode string `json:"InstrumentCode"` @@ -47,12 +59,17 @@ type Transaction struct { // ToEntity converts an TransactionPostgreSQLModel to entity Transaction func (t *TransactionPostgreSQLModel) ToEntity() *Transaction { + status := Status{ + Code: t.Status, + Description: t.StatusDescription, + } + transaction := &Transaction{ ID: t.ID, ParentTransactionID: t.ParentTransactionID, Description: t.Description, Template: t.Template, - Status: t.Status, + Status: status, Amount: t.Amount, AmountScale: t.AmountScale, InstrumentCode: t.InstrumentCode, @@ -78,7 +95,8 @@ func (t *TransactionPostgreSQLModel) FromEntity(transaction *Transaction) { ParentTransactionID: transaction.ParentTransactionID, Description: transaction.Description, Template: transaction.Template, - Status: transaction.Status, + Status: transaction.Status.Code, + StatusDescription: transaction.Status.Description, Amount: transaction.Amount, AmountScale: transaction.AmountScale, InstrumentCode: transaction.InstrumentCode, diff --git a/components/transaction/migrations/000001_create_transaction_table.up.sql b/components/transaction/migrations/000001_create_transaction_table.up.sql index 58fcf055..3f924cdd 100644 --- a/components/transaction/migrations/000001_create_transaction_table.up.sql +++ b/components/transaction/migrations/000001_create_transaction_table.up.sql @@ -4,6 +4,7 @@ CREATE TABLE IF NOT EXISTS "transaction" ( description TEXT NOT NULL, template TEXT NOT NULL, status TEXT NOT NULL, + status_description TEXT, amount NUMERIC NOT NULL, amount_scale NUMERIC NOT NULL, instrument_code TEXT NOT NULL, diff --git a/components/transaction/migrations/000002_create_operation_table.up.sql b/components/transaction/migrations/000002_create_operation_table.up.sql index 4d740ff0..d9fa7553 100644 --- a/components/transaction/migrations/000002_create_operation_table.up.sql +++ b/components/transaction/migrations/000002_create_operation_table.up.sql @@ -2,10 +2,8 @@ CREATE TABLE IF NOT EXISTS operation ( id UUID PRIMARY KEY NOT NULL DEFAULT (uuid_generate_v4()), transaction_id UUID NOT NULL, description TEXT NOT NULL, - ledger_id UUID NOT NULL, type TEXT NOT NULL, instrument_code TEXT NOT NULL, - status TEXT NOT NULL, amount NUMERIC NOT NULL, amount_scale NUMERIC NOT NULL, available_balance NUMERIC NOT NULL, @@ -14,10 +12,16 @@ CREATE TABLE IF NOT EXISTS operation ( available_balance_after NUMERIC NOT NULL, on_hold_balance_after NUMERIC NOT NULL, balance_scale_after NUMERIC NOT NULL, + status TEXT NOT NULL, + status_description TEXT, account_id UUID NOT NULL, account_alias TEXT NOT NULL, portfolio_id UUID NOT NULL, + chart_of_accounts TEXT NOT NULL, + organization_id UUID NOT NULL, + ledger_id UUID NOT NULL, created_at TIMESTAMP WITH TIME ZONE, updated_at TIMESTAMP WITH TIME ZONE, - deleted_at TIMESTAMP WITH TIME ZONE + deleted_at TIMESTAMP WITH TIME ZONE, + FOREIGN KEY (transaction_id) REFERENCES "transaction" (id) ) \ No newline at end of file diff --git a/components/transaction/migrations/000003_create_instrument_rate_table.up.sql b/components/transaction/migrations/000003_create_instrument_rate_table.up.sql index c2158197..797a258b 100644 --- a/components/transaction/migrations/000003_create_instrument_rate_table.up.sql +++ b/components/transaction/migrations/000003_create_instrument_rate_table.up.sql @@ -6,6 +6,9 @@ CREATE TABLE IF NOT EXISTS instrument_rate ( scale NUMERIC NOT NULL, source TEXT NOT NULL, status TEXT NOT NULL, + status_description TEXT, + organization_id UUID NOT NULL, + ledger_id UUID NOT NULL, created_at TIMESTAMP WITH TIME ZONE, updated_at TIMESTAMP WITH TIME ZONE, deleted_at TIMESTAMP WITH TIME ZONE From 0281101e99125b103eacafb07f6549137a099bae Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Mon, 3 Jun 2024 13:36:16 +0200 Subject: [PATCH 6/6] fix: make lint :bug: --- components/ledger/internal/ports/ledger.go | 5 ++--- components/ledger/internal/ports/organization.go | 5 ++--- .../adapters/database/postgres/transaction.postgresql.go | 5 ++++- .../transaction/internal/domain/transaction/transaction.go | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/components/ledger/internal/ports/ledger.go b/components/ledger/internal/ports/ledger.go index cdc72640..bdaa6d7c 100644 --- a/components/ledger/internal/ports/ledger.go +++ b/components/ledger/internal/ports/ledger.go @@ -1,18 +1,17 @@ package ports import ( - "github.com/LerianStudio/midaz/common/mpostgres" "os" "github.com/LerianStudio/midaz/common" - "go.mongodb.org/mongo-driver/bson" - "github.com/LerianStudio/midaz/common/mlog" + "github.com/LerianStudio/midaz/common/mpostgres" commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app/command" "github.com/LerianStudio/midaz/components/ledger/internal/app/query" l "github.com/LerianStudio/midaz/components/ledger/internal/domain/onboarding/ledger" "github.com/gofiber/fiber/v2" + "go.mongodb.org/mongo-driver/bson" ) // LedgerHandler struct contains a ledger use case for managing ledger related operations. diff --git a/components/ledger/internal/ports/organization.go b/components/ledger/internal/ports/organization.go index 5a39765d..48bed272 100644 --- a/components/ledger/internal/ports/organization.go +++ b/components/ledger/internal/ports/organization.go @@ -1,18 +1,17 @@ package ports import ( - "github.com/LerianStudio/midaz/common/mpostgres" "os" - "go.mongodb.org/mongo-driver/bson" - "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + "github.com/LerianStudio/midaz/common/mpostgres" commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app/command" "github.com/LerianStudio/midaz/components/ledger/internal/app/query" o "github.com/LerianStudio/midaz/components/ledger/internal/domain/onboarding/organization" "github.com/gofiber/fiber/v2" + "go.mongodb.org/mongo-driver/bson" ) // OrganizationHandler struct contains an organization use case for managing organization related operations. diff --git a/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go b/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go index eff70e0f..7bab215f 100644 --- a/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go +++ b/components/transaction/internal/adapters/database/postgres/transaction.postgresql.go @@ -4,7 +4,6 @@ import ( "context" "database/sql" "errors" - sqrl "github.com/Masterminds/squirrel" "reflect" "strconv" "strings" @@ -13,6 +12,7 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mpostgres" t "github.com/LerianStudio/midaz/components/transaction/internal/domain/transaction" + sqrl "github.com/Masterminds/squirrel" "github.com/google/uuid" "github.com/lib/pq" ) @@ -105,6 +105,9 @@ func (r *TransactionPostgreSQLRepository) FindAll(ctx context.Context, organizat PlaceholderFormat(sqrl.Dollar) query, args, err := findAll.ToSql() + if err != nil { + return nil, err + } rows, err := db.QueryContext(ctx, query, args...) if err != nil { diff --git a/components/transaction/internal/domain/transaction/transaction.go b/components/transaction/internal/domain/transaction/transaction.go index 31e5268f..93545408 100644 --- a/components/transaction/internal/domain/transaction/transaction.go +++ b/components/transaction/internal/domain/transaction/transaction.go @@ -1,9 +1,9 @@ package transaction import ( + "database/sql" "time" - "database/sql" "github.com/google/uuid" )