From b696d05af93b45841987cb56a6e3bd85fdc7ff90 Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Tue, 4 Jun 2024 19:41:58 +0200 Subject: [PATCH 1/6] feature: init transaction --- .../domain/transaction/transaction.go | 7 + .../transaction/internal/ports/http/routes.go | 17 ++- .../transaction/internal/ports/transaction.go | 125 ++++++++---------- 3 files changed, 75 insertions(+), 74 deletions(-) diff --git a/components/transaction/internal/domain/transaction/transaction.go b/components/transaction/internal/domain/transaction/transaction.go index 93545408..850b0887 100644 --- a/components/transaction/internal/domain/transaction/transaction.go +++ b/components/transaction/internal/domain/transaction/transaction.go @@ -38,6 +38,13 @@ func (s Status) IsEmpty() bool { return s.Code == "" && s.Description == nil } +// InputDSL is a struct design to encapsulate payload data. +type InputDSL struct { + TransactionType uuid.UUID `json:"transactionType"` + TransactionTypeCode string `json:"transactionTypeCode"` + Variables map[string]any `json:"variables,omitempty"` +} + // Transaction is a struct designed to encapsulate response payload data. type Transaction struct { ID string `json:"id"` diff --git a/components/transaction/internal/ports/http/routes.go b/components/transaction/internal/ports/http/routes.go index 944d3d1f..c11f5610 100644 --- a/components/transaction/internal/ports/http/routes.go +++ b/components/transaction/internal/ports/http/routes.go @@ -19,8 +19,21 @@ func NewRouter(th *ports.TransactionHandler) *fiber.App { // jwt := lib.NewJWTMiddleware(config.JWKAddress) // -- Routes -- - f.Post("transaction/v1/validate", th.ValidateTransaction) - f.Post("transaction/v1/parser", th.ParserTransactionTemplate) + + // Transactions + f.Post("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions", th.CreateTransaction) + f.Post("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id/commit", th.CommitTransaction) + f.Post("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id/revert", th.RevertTransaction) + //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions", nil) + //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id", nil) + //f.Patch("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id", nil) + + // Operations + //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:account_id/operations", nil) + //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:account_id/operations/:operation_id", nil) + //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/portfolios/:portfolio_id/operations", nil) + //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/portfolios/:portfolio_id/operations/:operation_id", nil) + //f.Patch("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id/operations/:operation_id", nil) // Health f.Get("/health", lib.Ping) diff --git a/components/transaction/internal/ports/transaction.go b/components/transaction/internal/ports/transaction.go index 9d1b7a7f..0ad3b5e0 100644 --- a/components/transaction/internal/ports/transaction.go +++ b/components/transaction/internal/ports/transaction.go @@ -3,13 +3,15 @@ package ports import ( "bytes" "fmt" + "github.com/LerianStudio/midaz/common" + "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" + t "github.com/LerianStudio/midaz/components/transaction/internal/domain/transaction" "io" "mime/multipart" "net/http" "strings" - "github.com/google/uuid" - "github.com/LerianStudio/midaz/common/gold/transaction" "github.com/LerianStudio/midaz/components/transaction/internal/app/command" "github.com/LerianStudio/midaz/components/transaction/internal/app/query" @@ -21,51 +23,16 @@ type TransactionHandler struct { Query *query.UseCase } -// InputDSL is a struct design to encapsulate payload data. -type InputDSL struct { - TransactionType uuid.UUID `json:"transactionType"` - TransactionTypeCode string `json:"transactionTypeCode"` - Variables map[string]any `json:"variables,omitempty"` -} +// CreateTransaction method that create transaction +func (handler *TransactionHandler) CreateTransaction(c *fiber.Ctx) error { + logger := mlog.NewLoggerFromContext(c.UserContext()) -func (handler *TransactionHandler) ValidateTransaction(ctx *fiber.Ctx) error { - fileHeader, err := ctx.FormFile("dsl") + dsl, err := getFileFromHeader(c) if err != nil { - return err - } - - if !strings.Contains(fileHeader.Filename, ".gold") { - return ctx.Status(http.StatusBadRequest).JSON(fiber.Map{ - "code": -1, - "message": fmt.Sprintf("This type o file: %s can't be parsed", fileHeader.Filename), - }) + logger.Error("Failed to validate and parse transaction", err.Error()) + return commonHTTP.WithError(c, err) } - if fileHeader.Size == 0 { - return ctx.Status(http.StatusBadRequest).JSON(fiber.Map{ - "code": -1, - "message": fmt.Sprintf("This file: %s is empty", fileHeader.Filename), - }) - } - - file, err := fileHeader.Open() - if err != nil { - return err - } - defer func(file multipart.File) { - err := file.Close() - if err != nil { - panic(0) - } - }(file) - - buf := new(bytes.Buffer) - if _, err := io.Copy(buf, file); err != nil { - return err - } - - dsl := buf.String() - errListener := transaction.Validate(dsl) if errListener != nil && len(errListener.Errors) > 0 { var err []fiber.Map @@ -78,39 +45,64 @@ func (handler *TransactionHandler) ValidateTransaction(ctx *fiber.Ctx) error { }) } - return ctx.Status(http.StatusBadRequest).JSON(err) + return c.Status(http.StatusBadRequest).JSON(err) } - t := transaction.Parse(dsl) + tran := transaction.Parse(dsl) + + logger.Infof("Transaction parsed and validated") + + return commonHTTP.Created(c, tran) +} + +// CreateTransactionTemplate method that create transaction template +func (handler *TransactionHandler) CreateTransactionTemplate(p any, c *fiber.Ctx) error { + logger := mlog.NewLoggerFromContext(c.UserContext()) + + payload := p.(*t.InputDSL) + logger.Infof("Request to create an transaction with details: %#v", payload) + + return commonHTTP.Created(c, payload) +} + +// CommitTransaction method that commit transaction created before +func (handler *TransactionHandler) CommitTransaction(c *fiber.Ctx) error { + logger := mlog.NewLoggerFromContext(c.UserContext()) + + return commonHTTP.Created(c, logger) +} + +// RevertTransaction method that revert transaction created before +func (handler *TransactionHandler) RevertTransaction(c *fiber.Ctx) error { + logger := mlog.NewLoggerFromContext(c.UserContext()) - return ctx.Status(http.StatusOK).JSON(fiber.Map{ - "transaction": t, - }) + return commonHTTP.Created(c, logger) } -func (handler *TransactionHandler) ParserTransactionTemplate(ctx *fiber.Ctx) error { +// getFileFromHeader method that get file from header and give a string fom this dsl gold file +func getFileFromHeader(ctx *fiber.Ctx) (string, error) { fileHeader, err := ctx.FormFile("dsl") if err != nil { - return err + return "", err } if !strings.Contains(fileHeader.Filename, ".gold") { - return ctx.Status(http.StatusBadRequest).JSON(fiber.Map{ - "code": -1, - "message": fmt.Sprintf("This type o file: %s can't be parsed", fileHeader.Filename), - }) + return "", common.ValidationError{ + Code: "0001", + Message: fmt.Sprintf("This type o file: %s can't be parsed", fileHeader.Filename), + } } if fileHeader.Size == 0 { - return ctx.Status(http.StatusBadRequest).JSON(fiber.Map{ - "code": -1, - "message": fmt.Sprintf("This file: %s is empty", fileHeader.Filename), - }) + return "", common.ValidationError{ + Code: "0001", + Message: fmt.Sprintf("This file: %s is empty", fileHeader.Filename), + } } file, err := fileHeader.Open() if err != nil { - return err + return "", err } defer func(file multipart.File) { err := file.Close() @@ -121,21 +113,10 @@ func (handler *TransactionHandler) ParserTransactionTemplate(ctx *fiber.Ctx) err buf := new(bytes.Buffer) if _, err := io.Copy(buf, file); err != nil { - return err + return "", err } dsl := buf.String() - if err := ctx.BodyParser(&InputDSL{}); err != nil { - return ctx.Status(http.StatusBadRequest).JSON(fiber.Map{ - "code": -1, - "message": fmt.Sprintf("This input: %s can't be parsed", err.Error()), - }) - } - - t := transaction.Parse(dsl) - - return ctx.Status(http.StatusOK).JSON(fiber.Map{ - "template": t, - }) + return dsl, nil } From a55b5839944e385a94037c20aae5e8b9a415a503 Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Tue, 4 Jun 2024 20:35:54 +0200 Subject: [PATCH 2/6] feature: add transaction templates --- components/transaction/internal/ports/http/routes.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/components/transaction/internal/ports/http/routes.go b/components/transaction/internal/ports/http/routes.go index c11f5610..3309d259 100644 --- a/components/transaction/internal/ports/http/routes.go +++ b/components/transaction/internal/ports/http/routes.go @@ -2,6 +2,7 @@ package http import ( lib "github.com/LerianStudio/midaz/common/net/http" + t "github.com/LerianStudio/midaz/components/transaction/internal/domain/transaction" "github.com/LerianStudio/midaz/components/transaction/internal/ports" "github.com/LerianStudio/midaz/components/transaction/internal/service" "github.com/gofiber/fiber/v2" @@ -28,6 +29,13 @@ func NewRouter(th *ports.TransactionHandler) *fiber.App { //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id", nil) //f.Patch("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id", nil) + // Transactions Templates + f.Post("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates", lib.WithBody(new(t.InputDSL), th.CreateTransactionTemplate)) + //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates", nil) + //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates/:code", nil) + //f.Put("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates/:code", nil) + //f.Delete("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates/:code", nil) + // Operations //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:account_id/operations", nil) //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:account_id/operations/:operation_id", nil) From f80d144f6a0dec1023c5b968ebc72f520b3478af Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Wed, 5 Jun 2024 18:13:17 +0200 Subject: [PATCH 3/6] refactor: change ValidateParameters and GetFileFromHeader to httputils and your refactor calls in all classes :hammer: --- common/net/http/headers.go | 2 + common/net/http/httputils.go | 87 +++++++++++++++++++ common/utils.go | 38 -------- .../database/mongodb/metadata.mongodb.go | 5 +- .../internal/app/query/get-all-accounts.go | 3 +- .../internal/app/query/get-all-instruments.go | 3 +- .../internal/app/query/get-all-ledgers.go | 3 +- .../app/query/get-all-metadata-accounts.go | 3 +- .../app/query/get-all-metadata-instruments.go | 3 +- .../app/query/get-all-metadata-ledgers.go | 3 +- .../query/get-all-metadata-organizations.go | 3 +- .../app/query/get-all-metadata-portfolios.go | 3 +- .../app/query/get-all-metadata-products.go | 3 +- .../app/query/get-all-organizations.go | 3 +- .../internal/app/query/get-all-portfolios.go | 3 +- .../internal/app/query/get-all-products.go | 3 +- .../domain/metadata/metadata_repository.go | 4 +- components/ledger/internal/ports/account.go | 3 +- .../ledger/internal/ports/instrument.go | 3 +- components/ledger/internal/ports/ledger.go | 3 +- .../ledger/internal/ports/organization.go | 3 +- components/ledger/internal/ports/portfolio.go | 3 +- components/ledger/internal/ports/product.go | 3 +- .../transaction/internal/ports/transaction.go | 57 +----------- 24 files changed, 128 insertions(+), 119 deletions(-) diff --git a/common/net/http/headers.go b/common/net/http/headers.go index 61eb8d1a..74e9cf0c 100644 --- a/common/net/http/headers.go +++ b/common/net/http/headers.go @@ -5,4 +5,6 @@ const ( headerUserAgent = "User-Agent" headerRealIP = "X-Real-Ip" headerForwardedFor = "X-Forwarded-For" + dsl = "dsl" + fileExtension = ".gold" ) diff --git a/common/net/http/httputils.go b/common/net/http/httputils.go index 66efc86d..ed9ede93 100644 --- a/common/net/http/httputils.go +++ b/common/net/http/httputils.go @@ -1,10 +1,54 @@ package http import ( + "bytes" + "fmt" + "go.mongodb.org/mongo-driver/bson" + "io" + "mime/multipart" "net/http" + "strconv" "strings" + + "github.com/LerianStudio/midaz/common" + "github.com/gofiber/fiber/v2" ) +// QueryHeader entity from query parameter from get apis +type QueryHeader struct { + Metadata *bson.M + Limit int + Page int +} + +// ValidateParameters validate and return struct of default parameters +func ValidateParameters(params map[string]string) *QueryHeader { + var metadata *bson.M + + limit := 10 + + page := 1 + + for key, value := range params { + switch { + case strings.Contains(key, "metadata."): + metadata = &bson.M{key: value} + case strings.Contains(key, "limit"): + limit, _ = strconv.Atoi(value) + case strings.Contains(key, "page"): + page, _ = strconv.Atoi(value) + } + } + + query := &QueryHeader{ + Metadata: metadata, + Limit: limit, + Page: page, + } + + return query +} + // IPAddrFromRemoteAddr removes port information from string. func IPAddrFromRemoteAddr(s string) string { idx := strings.LastIndex(s, ":") @@ -36,3 +80,46 @@ func GetRemoteAddress(r *http.Request) string { return realIP } + +// GetFileFromHeader method that get file from header and give a string fom this dsl gold file +func GetFileFromHeader(ctx *fiber.Ctx) (string, error) { + fileHeader, err := ctx.FormFile(dsl) + if err != nil { + return "", err + } + + if !strings.Contains(fileHeader.Filename, fileExtension) { + return "", common.ValidationError{ + Code: "0001", + Message: fmt.Sprintf("This type o file: %s can't be parsed", fileHeader.Filename), + } + } + + if fileHeader.Size == 0 { + return "", common.ValidationError{ + Code: "0001", + Message: fmt.Sprintf("This file: %s is empty", fileHeader.Filename), + } + } + + file, err := fileHeader.Open() + if err != nil { + return "", err + } + + defer func(file multipart.File) { + err := file.Close() + if err != nil { + panic(0) + } + }(file) + + buf := new(bytes.Buffer) + if _, err := io.Copy(buf, file); err != nil { + return "", err + } + + fileString := buf.String() + + return fileString, nil +} diff --git a/common/utils.go b/common/utils.go index ba3ab619..bdb129ef 100644 --- a/common/utils.go +++ b/common/utils.go @@ -3,10 +3,7 @@ package common import ( "slices" "strconv" - "strings" "unicode" - - "go.mongodb.org/mongo-driver/bson" ) // Contains checks if an item is in a slice. This function uses type parameters to work with any slice type. @@ -130,38 +127,3 @@ func ValidateCurrency(code string) error { return nil } - -// QueryHeader entity from query parameter from get apis -type QueryHeader struct { - Metadata *bson.M - Limit int - Page int -} - -// ValidateParameters validate and return struct of default parameters -func ValidateParameters(params map[string]string) *QueryHeader { - var metadata *bson.M - - limit := 10 - - page := 1 - - for key, value := range params { - switch { - case strings.Contains(key, "metadata."): - metadata = &bson.M{key: value} - case strings.Contains(key, "limit"): - limit, _ = strconv.Atoi(value) - case strings.Contains(key, "page"): - page, _ = strconv.Atoi(value) - } - } - - query := &QueryHeader{ - Metadata: metadata, - Limit: limit, - Page: page, - } - - return query -} diff --git a/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go b/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go index 14b8c373..196f7201 100644 --- a/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go +++ b/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go @@ -4,11 +4,12 @@ import ( "context" "errors" "fmt" + "github.com/LerianStudio/midaz/common" "strings" "time" - "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mmongo" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" m "github.com/LerianStudio/midaz/components/ledger/internal/domain/metadata" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" @@ -57,7 +58,7 @@ func (mmr *MetadataMongoDBRepository) Create(ctx context.Context, collection str } // FindList retrieves metadata from the mongodb all metadata or a list by specify metadata. -func (mmr *MetadataMongoDBRepository) FindList(ctx context.Context, collection string, filter common.QueryHeader) ([]*m.Metadata, error) { +func (mmr *MetadataMongoDBRepository) FindList(ctx context.Context, collection string, filter commonHTTP.QueryHeader) ([]*m.Metadata, error) { db, err := mmr.connection.GetDB(ctx) if err != nil { return nil, err diff --git a/components/ledger/internal/app/query/get-all-accounts.go b/components/ledger/internal/app/query/get-all-accounts.go index 87ab2d7a..21d4d65c 100644 --- a/components/ledger/internal/app/query/get-all-accounts.go +++ b/components/ledger/internal/app/query/get-all-accounts.go @@ -7,13 +7,14 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" a "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/account" "github.com/google/uuid" ) // GetAllAccount fetch all Account from the repository -func (uc *UseCase) GetAllAccount(ctx context.Context, organizationID, ledgerID, portfolioID string, filter common.QueryHeader) ([]*a.Account, error) { +func (uc *UseCase) GetAllAccount(ctx context.Context, organizationID, ledgerID, portfolioID string, filter commonHTTP.QueryHeader) ([]*a.Account, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving accounts") diff --git a/components/ledger/internal/app/query/get-all-instruments.go b/components/ledger/internal/app/query/get-all-instruments.go index 8aed0d5c..5a0ff3fd 100644 --- a/components/ledger/internal/app/query/get-all-instruments.go +++ b/components/ledger/internal/app/query/get-all-instruments.go @@ -7,13 +7,14 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" i "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/instrument" "github.com/google/uuid" ) // GetAllInstruments fetch all Instrument from the repository -func (uc *UseCase) GetAllInstruments(ctx context.Context, organizationID, ledgerID string, filter common.QueryHeader) ([]*i.Instrument, error) { +func (uc *UseCase) GetAllInstruments(ctx context.Context, organizationID, ledgerID string, filter commonHTTP.QueryHeader) ([]*i.Instrument, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving instruments") diff --git a/components/ledger/internal/app/query/get-all-ledgers.go b/components/ledger/internal/app/query/get-all-ledgers.go index e1eddf3c..09a9c111 100644 --- a/components/ledger/internal/app/query/get-all-ledgers.go +++ b/components/ledger/internal/app/query/get-all-ledgers.go @@ -7,13 +7,14 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" l "github.com/LerianStudio/midaz/components/ledger/internal/domain/onboarding/ledger" "github.com/google/uuid" ) // GetAllLedgers fetch all Ledgers from the repository -func (uc *UseCase) GetAllLedgers(ctx context.Context, organizationID string, filter common.QueryHeader) ([]*l.Ledger, error) { +func (uc *UseCase) GetAllLedgers(ctx context.Context, organizationID string, filter commonHTTP.QueryHeader) ([]*l.Ledger, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving ledgers") diff --git a/components/ledger/internal/app/query/get-all-metadata-accounts.go b/components/ledger/internal/app/query/get-all-metadata-accounts.go index cfdbb92f..f1e45100 100644 --- a/components/ledger/internal/app/query/get-all-metadata-accounts.go +++ b/components/ledger/internal/app/query/get-all-metadata-accounts.go @@ -7,13 +7,14 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" a "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/account" "github.com/google/uuid" ) // GetAllMetadataAccounts fetch all Accounts from the repository -func (uc *UseCase) GetAllMetadataAccounts(ctx context.Context, organizationID, ledgerID, portfolioID string, filter common.QueryHeader) ([]*a.Account, error) { +func (uc *UseCase) GetAllMetadataAccounts(ctx context.Context, organizationID, ledgerID, portfolioID string, filter commonHTTP.QueryHeader) ([]*a.Account, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving accounts") diff --git a/components/ledger/internal/app/query/get-all-metadata-instruments.go b/components/ledger/internal/app/query/get-all-metadata-instruments.go index f7af6c3d..85a51835 100644 --- a/components/ledger/internal/app/query/get-all-metadata-instruments.go +++ b/components/ledger/internal/app/query/get-all-metadata-instruments.go @@ -7,13 +7,14 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" i "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/instrument" "github.com/google/uuid" ) // GetAllMetadataInstruments fetch all Instruments from the repository -func (uc *UseCase) GetAllMetadataInstruments(ctx context.Context, organizationID, ledgerID string, filter common.QueryHeader) ([]*i.Instrument, error) { +func (uc *UseCase) GetAllMetadataInstruments(ctx context.Context, organizationID, ledgerID string, filter commonHTTP.QueryHeader) ([]*i.Instrument, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving instruments") diff --git a/components/ledger/internal/app/query/get-all-metadata-ledgers.go b/components/ledger/internal/app/query/get-all-metadata-ledgers.go index ac10d840..5ffe86ac 100644 --- a/components/ledger/internal/app/query/get-all-metadata-ledgers.go +++ b/components/ledger/internal/app/query/get-all-metadata-ledgers.go @@ -7,13 +7,14 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" l "github.com/LerianStudio/midaz/components/ledger/internal/domain/onboarding/ledger" "github.com/google/uuid" ) // GetAllMetadataLedgers fetch all Ledgers from the repository -func (uc *UseCase) GetAllMetadataLedgers(ctx context.Context, organizationID string, filter common.QueryHeader) ([]*l.Ledger, error) { +func (uc *UseCase) GetAllMetadataLedgers(ctx context.Context, organizationID string, filter commonHTTP.QueryHeader) ([]*l.Ledger, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving ledgers") diff --git a/components/ledger/internal/app/query/get-all-metadata-organizations.go b/components/ledger/internal/app/query/get-all-metadata-organizations.go index 332fce64..115196f0 100644 --- a/components/ledger/internal/app/query/get-all-metadata-organizations.go +++ b/components/ledger/internal/app/query/get-all-metadata-organizations.go @@ -7,13 +7,14 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" o "github.com/LerianStudio/midaz/components/ledger/internal/domain/onboarding/organization" "github.com/google/uuid" ) // GetAllMetadataOrganizations fetch all Organizations from the repository -func (uc *UseCase) GetAllMetadataOrganizations(ctx context.Context, filter common.QueryHeader) ([]*o.Organization, error) { +func (uc *UseCase) GetAllMetadataOrganizations(ctx context.Context, filter commonHTTP.QueryHeader) ([]*o.Organization, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving organizations") diff --git a/components/ledger/internal/app/query/get-all-metadata-portfolios.go b/components/ledger/internal/app/query/get-all-metadata-portfolios.go index 1527cc01..850c4b11 100644 --- a/components/ledger/internal/app/query/get-all-metadata-portfolios.go +++ b/components/ledger/internal/app/query/get-all-metadata-portfolios.go @@ -7,13 +7,14 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" p "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/portfolio" "github.com/google/uuid" ) // GetAllMetadataPortfolios fetch all Portfolios from the repository -func (uc *UseCase) GetAllMetadataPortfolios(ctx context.Context, organizationID, ledgerID string, filter common.QueryHeader) ([]*p.Portfolio, error) { +func (uc *UseCase) GetAllMetadataPortfolios(ctx context.Context, organizationID, ledgerID string, filter commonHTTP.QueryHeader) ([]*p.Portfolio, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving portfolios") diff --git a/components/ledger/internal/app/query/get-all-metadata-products.go b/components/ledger/internal/app/query/get-all-metadata-products.go index 3702a5ba..01461d82 100644 --- a/components/ledger/internal/app/query/get-all-metadata-products.go +++ b/components/ledger/internal/app/query/get-all-metadata-products.go @@ -7,13 +7,14 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" r "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/product" "github.com/google/uuid" ) // GetAllMetadataProducts fetch all Products from the repository -func (uc *UseCase) GetAllMetadataProducts(ctx context.Context, organizationID, ledgerID string, filter common.QueryHeader) ([]*r.Product, error) { +func (uc *UseCase) GetAllMetadataProducts(ctx context.Context, organizationID, ledgerID string, filter commonHTTP.QueryHeader) ([]*r.Product, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving products") diff --git a/components/ledger/internal/app/query/get-all-organizations.go b/components/ledger/internal/app/query/get-all-organizations.go index 4a8ff313..79f226f0 100644 --- a/components/ledger/internal/app/query/get-all-organizations.go +++ b/components/ledger/internal/app/query/get-all-organizations.go @@ -7,12 +7,13 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" o "github.com/LerianStudio/midaz/components/ledger/internal/domain/onboarding/organization" ) // GetAllOrganizations fetch all Organizations from the repository -func (uc *UseCase) GetAllOrganizations(ctx context.Context, filter common.QueryHeader) ([]*o.Organization, error) { +func (uc *UseCase) GetAllOrganizations(ctx context.Context, filter commonHTTP.QueryHeader) ([]*o.Organization, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving organizations") diff --git a/components/ledger/internal/app/query/get-all-portfolios.go b/components/ledger/internal/app/query/get-all-portfolios.go index 278accba..9f97362b 100644 --- a/components/ledger/internal/app/query/get-all-portfolios.go +++ b/components/ledger/internal/app/query/get-all-portfolios.go @@ -7,13 +7,14 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" p "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/portfolio" "github.com/google/uuid" ) // GetAllPortfolio fetch all Portfolio from the repository -func (uc *UseCase) GetAllPortfolio(ctx context.Context, organizationID, ledgerID string, filter common.QueryHeader) ([]*p.Portfolio, error) { +func (uc *UseCase) GetAllPortfolio(ctx context.Context, organizationID, ledgerID string, filter commonHTTP.QueryHeader) ([]*p.Portfolio, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving portfolios") diff --git a/components/ledger/internal/app/query/get-all-products.go b/components/ledger/internal/app/query/get-all-products.go index c258505f..1fdabd06 100644 --- a/components/ledger/internal/app/query/get-all-products.go +++ b/components/ledger/internal/app/query/get-all-products.go @@ -7,13 +7,14 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/LerianStudio/midaz/common/mlog" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" "github.com/LerianStudio/midaz/components/ledger/internal/app" r "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/product" "github.com/google/uuid" ) // GetAllProducts fetch all Product from the repository -func (uc *UseCase) GetAllProducts(ctx context.Context, organizationID, ledgerID string, filter common.QueryHeader) ([]*r.Product, error) { +func (uc *UseCase) GetAllProducts(ctx context.Context, organizationID, ledgerID string, filter commonHTTP.QueryHeader) ([]*r.Product, error) { logger := mlog.NewLoggerFromContext(ctx) logger.Infof("Retrieving products") diff --git a/components/ledger/internal/domain/metadata/metadata_repository.go b/components/ledger/internal/domain/metadata/metadata_repository.go index 9f9e81db..c0aa6171 100644 --- a/components/ledger/internal/domain/metadata/metadata_repository.go +++ b/components/ledger/internal/domain/metadata/metadata_repository.go @@ -3,7 +3,7 @@ package metadata import ( "context" - "github.com/LerianStudio/midaz/common" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" ) // Repository provides an interface for operations related on mongodb a metadata entities. @@ -11,7 +11,7 @@ import ( //go:generate mockgen --destination=../../gen/mock/metadata/metadata_mock.go --package=mock . Repository type Repository interface { Create(ctx context.Context, collection string, metadata *Metadata) error - FindList(ctx context.Context, collection string, filter common.QueryHeader) ([]*Metadata, error) + FindList(ctx context.Context, collection string, filter commonHTTP.QueryHeader) ([]*Metadata, error) FindByEntity(ctx context.Context, collection, id string) (*Metadata, error) Update(ctx context.Context, collection, id string, metadata map[string]any) error Delete(ctx context.Context, collection, id string) error diff --git a/components/ledger/internal/ports/account.go b/components/ledger/internal/ports/account.go index f92fdf4d..3d7b6010 100644 --- a/components/ledger/internal/ports/account.go +++ b/components/ledger/internal/ports/account.go @@ -1,7 +1,6 @@ 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" @@ -53,7 +52,7 @@ func (handler *AccountHandler) GetAllAccounts(c *fiber.Ctx) error { logger.Infof("Get Accounts with Portfolio ID: %s", portfolioID) - headerParams := common.ValidateParameters(c.Queries()) + headerParams := commonHTTP.ValidateParameters(c.Queries()) pagination := mpostgres.Pagination{ Limit: headerParams.Limit, diff --git a/components/ledger/internal/ports/instrument.go b/components/ledger/internal/ports/instrument.go index 4342a77b..6cfd02ad 100644 --- a/components/ledger/internal/ports/instrument.go +++ b/components/ledger/internal/ports/instrument.go @@ -1,7 +1,6 @@ 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" @@ -55,7 +54,7 @@ func (handler *InstrumentHandler) GetAllInstruments(c *fiber.Ctx) error { ledgerID := c.Params("ledger_id") logger.Infof("Initiating create of Instrument with ledger ID: %s", ledgerID) - headerParams := common.ValidateParameters(c.Queries()) + headerParams := commonHTTP.ValidateParameters(c.Queries()) pagination := mpostgres.Pagination{ Limit: headerParams.Limit, diff --git a/components/ledger/internal/ports/ledger.go b/components/ledger/internal/ports/ledger.go index bdaa6d7c..d4b2fb83 100644 --- a/components/ledger/internal/ports/ledger.go +++ b/components/ledger/internal/ports/ledger.go @@ -3,7 +3,6 @@ package ports import ( "os" - "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" @@ -70,7 +69,7 @@ func (handler *LedgerHandler) GetAllLedgers(c *fiber.Ctx) error { organizationID := c.Params("organization_id") - headerParams := common.ValidateParameters(c.Queries()) + headerParams := commonHTTP.ValidateParameters(c.Queries()) pagination := mpostgres.Pagination{ Limit: headerParams.Limit, diff --git a/components/ledger/internal/ports/organization.go b/components/ledger/internal/ports/organization.go index 48bed272..e255c609 100644 --- a/components/ledger/internal/ports/organization.go +++ b/components/ledger/internal/ports/organization.go @@ -3,7 +3,6 @@ package ports import ( "os" - "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" @@ -93,7 +92,7 @@ func (handler *OrganizationHandler) GetAllOrganizations(c *fiber.Ctx) error { ctx := c.UserContext() logger := mlog.NewLoggerFromContext(ctx) - headerParams := common.ValidateParameters(c.Queries()) + headerParams := commonHTTP.ValidateParameters(c.Queries()) pagination := mpostgres.Pagination{ Limit: headerParams.Limit, diff --git a/components/ledger/internal/ports/portfolio.go b/components/ledger/internal/ports/portfolio.go index 04760795..a41be3c2 100644 --- a/components/ledger/internal/ports/portfolio.go +++ b/components/ledger/internal/ports/portfolio.go @@ -1,7 +1,6 @@ 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" @@ -52,7 +51,7 @@ func (handler *PortfolioHandler) GetAllPortfolios(c *fiber.Ctx) error { ledgerID := c.Params("ledger_id") logger.Infof("Get Portfolios with Organization: %s and Ledger ID: %s", organizationID, ledgerID) - headerParams := common.ValidateParameters(c.Queries()) + headerParams := commonHTTP.ValidateParameters(c.Queries()) pagination := mpostgres.Pagination{ Limit: headerParams.Limit, diff --git a/components/ledger/internal/ports/product.go b/components/ledger/internal/ports/product.go index f7241a0f..02e5a0a3 100644 --- a/components/ledger/internal/ports/product.go +++ b/components/ledger/internal/ports/product.go @@ -1,7 +1,6 @@ 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" @@ -50,7 +49,7 @@ func (handler *ProductHandler) GetAllProducts(c *fiber.Ctx) error { ledgerID := c.Params("ledger_id") logger.Infof("Get Products with organization ID: %s and ledger ID: %s", organizationID, ledgerID) - headerParams := common.ValidateParameters(c.Queries()) + headerParams := commonHTTP.ValidateParameters(c.Queries()) pagination := mpostgres.Pagination{ Limit: headerParams.Limit, diff --git a/components/transaction/internal/ports/transaction.go b/components/transaction/internal/ports/transaction.go index 0ad3b5e0..a41ff773 100644 --- a/components/transaction/internal/ports/transaction.go +++ b/components/transaction/internal/ports/transaction.go @@ -1,21 +1,14 @@ package ports import ( - "bytes" - "fmt" - "github.com/LerianStudio/midaz/common" + "github.com/LerianStudio/midaz/common/gold/transaction" "github.com/LerianStudio/midaz/common/mlog" commonHTTP "github.com/LerianStudio/midaz/common/net/http" - t "github.com/LerianStudio/midaz/components/transaction/internal/domain/transaction" - "io" - "mime/multipart" - "net/http" - "strings" - - "github.com/LerianStudio/midaz/common/gold/transaction" "github.com/LerianStudio/midaz/components/transaction/internal/app/command" "github.com/LerianStudio/midaz/components/transaction/internal/app/query" + t "github.com/LerianStudio/midaz/components/transaction/internal/domain/transaction" "github.com/gofiber/fiber/v2" + "net/http" ) type TransactionHandler struct { @@ -27,7 +20,7 @@ type TransactionHandler struct { func (handler *TransactionHandler) CreateTransaction(c *fiber.Ctx) error { logger := mlog.NewLoggerFromContext(c.UserContext()) - dsl, err := getFileFromHeader(c) + dsl, err := commonHTTP.GetFileFromHeader(c) if err != nil { logger.Error("Failed to validate and parse transaction", err.Error()) return commonHTTP.WithError(c, err) @@ -78,45 +71,3 @@ func (handler *TransactionHandler) RevertTransaction(c *fiber.Ctx) error { return commonHTTP.Created(c, logger) } - -// getFileFromHeader method that get file from header and give a string fom this dsl gold file -func getFileFromHeader(ctx *fiber.Ctx) (string, error) { - fileHeader, err := ctx.FormFile("dsl") - if err != nil { - return "", err - } - - if !strings.Contains(fileHeader.Filename, ".gold") { - return "", common.ValidationError{ - Code: "0001", - Message: fmt.Sprintf("This type o file: %s can't be parsed", fileHeader.Filename), - } - } - - if fileHeader.Size == 0 { - return "", common.ValidationError{ - Code: "0001", - Message: fmt.Sprintf("This file: %s is empty", fileHeader.Filename), - } - } - - file, err := fileHeader.Open() - if err != nil { - return "", err - } - defer func(file multipart.File) { - err := file.Close() - if err != nil { - panic(0) - } - }(file) - - buf := new(bytes.Buffer) - if _, err := io.Copy(buf, file); err != nil { - return "", err - } - - dsl := buf.String() - - return dsl, nil -} From fce6bfb2e9132a14205a90dda6164c7eaf7e97f4 Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Wed, 5 Jun 2024 18:53:30 +0200 Subject: [PATCH 4/6] fix: add field UseMetadata to use on query on mongodb when not use metadata field remove limit and skip to get all :bug: --- common/net/http/httputils.go | 17 +++++++++++------ .../database/mongodb/metadata.mongodb.go | 9 ++++++--- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/common/net/http/httputils.go b/common/net/http/httputils.go index ed9ede93..64e4f8ed 100644 --- a/common/net/http/httputils.go +++ b/common/net/http/httputils.go @@ -16,9 +16,10 @@ import ( // QueryHeader entity from query parameter from get apis type QueryHeader struct { - Metadata *bson.M - Limit int - Page int + Metadata *bson.M + Limit int + Page int + UseMetadata bool } // ValidateParameters validate and return struct of default parameters @@ -29,10 +30,13 @@ func ValidateParameters(params map[string]string) *QueryHeader { page := 1 + useMetadata := false + for key, value := range params { switch { case strings.Contains(key, "metadata."): metadata = &bson.M{key: value} + useMetadata = true case strings.Contains(key, "limit"): limit, _ = strconv.Atoi(value) case strings.Contains(key, "page"): @@ -41,9 +45,10 @@ func ValidateParameters(params map[string]string) *QueryHeader { } query := &QueryHeader{ - Metadata: metadata, - Limit: limit, - Page: page, + Metadata: metadata, + Limit: limit, + Page: page, + UseMetadata: useMetadata, } return query diff --git a/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go b/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go index 196f7201..c126765b 100644 --- a/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go +++ b/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go @@ -66,9 +66,12 @@ func (mmr *MetadataMongoDBRepository) FindList(ctx context.Context, collection s coll := db.Database(strings.ToLower(mmr.Database)).Collection(strings.ToLower(collection)) - limit := int64(filter.Limit) - skip := int64(filter.Page*filter.Limit - filter.Limit) - opts := options.FindOptions{Limit: &limit, Skip: &skip} + opts := options.FindOptions{} + if filter.UseMetadata { + limit := int64(filter.Limit) + skip := int64(filter.Page*filter.Limit - filter.Limit) + opts = options.FindOptions{Limit: &limit, Skip: &skip} + } cur, err := coll.Find(ctx, filter.Metadata, &opts) if err != nil { From c8b539f4b049633e6e6ad7e76b4d990e22c943f6 Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Wed, 5 Jun 2024 18:54:10 +0200 Subject: [PATCH 5/6] fix: update test and change QueryHeader path :bug: --- .../internal/app/query/get-all-metadata-accounts_test.go | 4 ++-- .../internal/app/query/get-all-metadata-instruments_test.go | 4 ++-- .../internal/app/query/get-all-metadata-ledgers_test.go | 4 ++-- .../internal/app/query/get-all-metadata-organizations_test.go | 4 ++-- .../internal/app/query/get-all-metadata-portfolios_test.go | 4 ++-- .../internal/app/query/get-all-metadata-products_test.go | 4 ++-- components/ledger/internal/gen/mock/metadata/metadata_mock.go | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/components/ledger/internal/app/query/get-all-metadata-accounts_test.go b/components/ledger/internal/app/query/get-all-metadata-accounts_test.go index 98170ee7..dc7af520 100644 --- a/components/ledger/internal/app/query/get-all-metadata-accounts_test.go +++ b/components/ledger/internal/app/query/get-all-metadata-accounts_test.go @@ -3,10 +3,10 @@ package query import ( "context" "errors" - "github.com/LerianStudio/midaz/common" "reflect" "testing" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" meta "github.com/LerianStudio/midaz/components/ledger/internal/domain/metadata" a "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/account" mock "github.com/LerianStudio/midaz/components/ledger/internal/gen/mock/metadata" @@ -19,7 +19,7 @@ import ( // TestGetAllMetadataAccounts is responsible to test TestGetAllMetadataAccounts with success and error func TestGetAllMetadataAccounts(t *testing.T) { collection := reflect.TypeOf(a.Account{}).Name() - filter := common.QueryHeader{ + filter := commonHTTP.QueryHeader{ Metadata: &bson.M{"metadata": 1}, Limit: 10, Page: 1, diff --git a/components/ledger/internal/app/query/get-all-metadata-instruments_test.go b/components/ledger/internal/app/query/get-all-metadata-instruments_test.go index fe7f1422..c81cf2d7 100644 --- a/components/ledger/internal/app/query/get-all-metadata-instruments_test.go +++ b/components/ledger/internal/app/query/get-all-metadata-instruments_test.go @@ -3,10 +3,10 @@ package query import ( "context" "errors" - "github.com/LerianStudio/midaz/common" "reflect" "testing" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" meta "github.com/LerianStudio/midaz/components/ledger/internal/domain/metadata" i "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/instrument" mock "github.com/LerianStudio/midaz/components/ledger/internal/gen/mock/metadata" @@ -19,7 +19,7 @@ import ( // TestGetAllMetadataInstruments is responsible to test TestGetAllMetadataInstruments with success and error func TestGetAllMetadataInstruments(t *testing.T) { collection := reflect.TypeOf(i.Instrument{}).Name() - filter := common.QueryHeader{ + filter := commonHTTP.QueryHeader{ Metadata: &bson.M{"metadata": 1}, Limit: 10, Page: 1, diff --git a/components/ledger/internal/app/query/get-all-metadata-ledgers_test.go b/components/ledger/internal/app/query/get-all-metadata-ledgers_test.go index 70593ca8..36f2c2cc 100644 --- a/components/ledger/internal/app/query/get-all-metadata-ledgers_test.go +++ b/components/ledger/internal/app/query/get-all-metadata-ledgers_test.go @@ -3,10 +3,10 @@ package query import ( "context" "errors" - "github.com/LerianStudio/midaz/common" "reflect" "testing" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" meta "github.com/LerianStudio/midaz/components/ledger/internal/domain/metadata" l "github.com/LerianStudio/midaz/components/ledger/internal/domain/onboarding/ledger" mock "github.com/LerianStudio/midaz/components/ledger/internal/gen/mock/metadata" @@ -19,7 +19,7 @@ import ( // TestGetAllMetadataLedgers is responsible to test TestGetAllMetadataLedgers with success and error func TestGetAllMetadataLedgers(t *testing.T) { collection := reflect.TypeOf(l.Ledger{}).Name() - filter := common.QueryHeader{ + filter := commonHTTP.QueryHeader{ Metadata: &bson.M{"metadata": 1}, Limit: 10, Page: 1, diff --git a/components/ledger/internal/app/query/get-all-metadata-organizations_test.go b/components/ledger/internal/app/query/get-all-metadata-organizations_test.go index 12ee0d72..9177bdc0 100644 --- a/components/ledger/internal/app/query/get-all-metadata-organizations_test.go +++ b/components/ledger/internal/app/query/get-all-metadata-organizations_test.go @@ -3,10 +3,10 @@ package query import ( "context" "errors" - "github.com/LerianStudio/midaz/common" "reflect" "testing" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" meta "github.com/LerianStudio/midaz/components/ledger/internal/domain/metadata" o "github.com/LerianStudio/midaz/components/ledger/internal/domain/onboarding/organization" mock "github.com/LerianStudio/midaz/components/ledger/internal/gen/mock/metadata" @@ -19,7 +19,7 @@ import ( // TestGetAllMetadataOrganizations is responsible to test TestGetAllMetadataOrganizations with success and error func TestGetAllMetadataOrganizations(t *testing.T) { collection := reflect.TypeOf(o.Organization{}).Name() - filter := common.QueryHeader{ + filter := commonHTTP.QueryHeader{ Metadata: &bson.M{"metadata": 1}, Limit: 10, Page: 1, diff --git a/components/ledger/internal/app/query/get-all-metadata-portfolios_test.go b/components/ledger/internal/app/query/get-all-metadata-portfolios_test.go index 6bd4aa14..04c955b3 100644 --- a/components/ledger/internal/app/query/get-all-metadata-portfolios_test.go +++ b/components/ledger/internal/app/query/get-all-metadata-portfolios_test.go @@ -3,10 +3,10 @@ package query import ( "context" "errors" - "github.com/LerianStudio/midaz/common" "reflect" "testing" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" meta "github.com/LerianStudio/midaz/components/ledger/internal/domain/metadata" p "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/portfolio" mock "github.com/LerianStudio/midaz/components/ledger/internal/gen/mock/metadata" @@ -19,7 +19,7 @@ import ( // TestGetAllMetadataPortfolios is responsible to test TestGetAllMetadataPortfolios with success and error func TestGetAllMetadataPortfolios(t *testing.T) { collection := reflect.TypeOf(p.Portfolio{}).Name() - filter := common.QueryHeader{ + filter := commonHTTP.QueryHeader{ Metadata: &bson.M{"metadata": 1}, Limit: 10, Page: 1, diff --git a/components/ledger/internal/app/query/get-all-metadata-products_test.go b/components/ledger/internal/app/query/get-all-metadata-products_test.go index cbf87e23..e56bdf7c 100644 --- a/components/ledger/internal/app/query/get-all-metadata-products_test.go +++ b/components/ledger/internal/app/query/get-all-metadata-products_test.go @@ -3,10 +3,10 @@ package query import ( "context" "errors" - "github.com/LerianStudio/midaz/common" "reflect" "testing" + commonHTTP "github.com/LerianStudio/midaz/common/net/http" meta "github.com/LerianStudio/midaz/components/ledger/internal/domain/metadata" p "github.com/LerianStudio/midaz/components/ledger/internal/domain/portfolio/product" mock "github.com/LerianStudio/midaz/components/ledger/internal/gen/mock/metadata" @@ -19,7 +19,7 @@ import ( // TestGetAllMetadataProducts is responsible to test TestGetAllMetadataProducts with success and error func TestGetAllMetadataProducts(t *testing.T) { collection := reflect.TypeOf(p.Product{}).Name() - filter := common.QueryHeader{ + filter := commonHTTP.QueryHeader{ Metadata: &bson.M{"metadata": 1}, Limit: 10, Page: 1, diff --git a/components/ledger/internal/gen/mock/metadata/metadata_mock.go b/components/ledger/internal/gen/mock/metadata/metadata_mock.go index 40eccd05..dd803897 100644 --- a/components/ledger/internal/gen/mock/metadata/metadata_mock.go +++ b/components/ledger/internal/gen/mock/metadata/metadata_mock.go @@ -13,7 +13,7 @@ import ( context "context" reflect "reflect" - common "github.com/LerianStudio/midaz/common" + http "github.com/LerianStudio/midaz/common/net/http" metadata "github.com/LerianStudio/midaz/components/ledger/internal/domain/metadata" gomock "go.uber.org/mock/gomock" ) @@ -85,7 +85,7 @@ func (mr *MockRepositoryMockRecorder) FindByEntity(arg0, arg1, arg2 any) *gomock } // FindList mocks base method. -func (m *MockRepository) FindList(arg0 context.Context, arg1 string, arg2 common.QueryHeader) ([]*metadata.Metadata, error) { +func (m *MockRepository) FindList(arg0 context.Context, arg1 string, arg2 http.QueryHeader) ([]*metadata.Metadata, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FindList", arg0, arg1, arg2) ret0, _ := ret[0].([]*metadata.Metadata) From bb4621bc8a5a10a03f9312c9ca52a7cacdac6444 Mon Sep 17 00:00:00 2001 From: MartinezAvellan Date: Wed, 5 Jun 2024 18:56:35 +0200 Subject: [PATCH 6/6] fix: make lint, sec and tests :bug: --- common/net/http/httputils.go | 2 +- .../database/mongodb/metadata.mongodb.go | 4 +++- .../gen/mock/metadata/metadata_mock.go | 2 +- .../transaction/internal/ports/http/routes.go | 21 ++++++++----------- .../transaction/internal/ports/transaction.go | 3 ++- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/common/net/http/httputils.go b/common/net/http/httputils.go index 64e4f8ed..a8e4f689 100644 --- a/common/net/http/httputils.go +++ b/common/net/http/httputils.go @@ -3,7 +3,6 @@ package http import ( "bytes" "fmt" - "go.mongodb.org/mongo-driver/bson" "io" "mime/multipart" "net/http" @@ -12,6 +11,7 @@ import ( "github.com/LerianStudio/midaz/common" "github.com/gofiber/fiber/v2" + "go.mongodb.org/mongo-driver/bson" ) // QueryHeader entity from query parameter from get apis diff --git a/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go b/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go index c126765b..8c0cdbcc 100644 --- a/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go +++ b/components/ledger/internal/adapters/database/mongodb/metadata.mongodb.go @@ -4,10 +4,11 @@ import ( "context" "errors" "fmt" - "github.com/LerianStudio/midaz/common" "strings" "time" + "github.com/LerianStudio/midaz/common" + "github.com/LerianStudio/midaz/common/mmongo" commonHTTP "github.com/LerianStudio/midaz/common/net/http" m "github.com/LerianStudio/midaz/components/ledger/internal/domain/metadata" @@ -67,6 +68,7 @@ func (mmr *MetadataMongoDBRepository) FindList(ctx context.Context, collection s coll := db.Database(strings.ToLower(mmr.Database)).Collection(strings.ToLower(collection)) opts := options.FindOptions{} + if filter.UseMetadata { limit := int64(filter.Limit) skip := int64(filter.Page*filter.Limit - filter.Limit) diff --git a/components/ledger/internal/gen/mock/metadata/metadata_mock.go b/components/ledger/internal/gen/mock/metadata/metadata_mock.go index dd803897..e99257d2 100644 --- a/components/ledger/internal/gen/mock/metadata/metadata_mock.go +++ b/components/ledger/internal/gen/mock/metadata/metadata_mock.go @@ -93,7 +93,7 @@ func (m *MockRepository) FindList(arg0 context.Context, arg1 string, arg2 http.Q return ret0, ret1 } -// FindList indicates an expected call of FindList. +// FindList indicates an expected call of FindList.z func (mr *MockRepositoryMockRecorder) FindList(arg0, arg1, arg2 any) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindList", reflect.TypeOf((*MockRepository)(nil).FindList), arg0, arg1, arg2) diff --git a/components/transaction/internal/ports/http/routes.go b/components/transaction/internal/ports/http/routes.go index 3309d259..99b711b1 100644 --- a/components/transaction/internal/ports/http/routes.go +++ b/components/transaction/internal/ports/http/routes.go @@ -25,23 +25,20 @@ func NewRouter(th *ports.TransactionHandler) *fiber.App { f.Post("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions", th.CreateTransaction) f.Post("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id/commit", th.CommitTransaction) f.Post("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id/revert", th.RevertTransaction) - //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions", nil) - //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id", nil) - //f.Patch("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id", nil) + + // f.Patch("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id", nil) // Transactions Templates f.Post("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates", lib.WithBody(new(t.InputDSL), th.CreateTransactionTemplate)) - //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates", nil) - //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates/:code", nil) - //f.Put("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates/:code", nil) - //f.Delete("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates/:code", nil) + + // f.Put("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates/:code", nil) + // f.Delete("/v1/organizations/:organization_id/ledgers/:ledger_id/transaction-templates/:code", nil) // Operations - //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:account_id/operations", nil) - //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:account_id/operations/:operation_id", nil) - //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/portfolios/:portfolio_id/operations", nil) - //f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/portfolios/:portfolio_id/operations/:operation_id", nil) - //f.Patch("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id/operations/:operation_id", nil) + + // f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/portfolios/:portfolio_id/operations", nil) + // f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/portfolios/:portfolio_id/operations/:operation_id", nil) + // f.Patch("/v1/organizations/:organization_id/ledgers/:ledger_id/transactions/:transaction_id/operations/:operation_id", nil) // Health f.Get("/health", lib.Ping) diff --git a/components/transaction/internal/ports/transaction.go b/components/transaction/internal/ports/transaction.go index a41ff773..9dc09130 100644 --- a/components/transaction/internal/ports/transaction.go +++ b/components/transaction/internal/ports/transaction.go @@ -1,6 +1,8 @@ package ports import ( + "net/http" + "github.com/LerianStudio/midaz/common/gold/transaction" "github.com/LerianStudio/midaz/common/mlog" commonHTTP "github.com/LerianStudio/midaz/common/net/http" @@ -8,7 +10,6 @@ import ( "github.com/LerianStudio/midaz/components/transaction/internal/app/query" t "github.com/LerianStudio/midaz/components/transaction/internal/domain/transaction" "github.com/gofiber/fiber/v2" - "net/http" ) type TransactionHandler struct {