Skip to content

Commit

Permalink
Merge pull request #33 from LerianStudio/develop
Browse files Browse the repository at this point in the history
Develop to Main
  • Loading branch information
MartinezAvellan authored Jun 5, 2024
2 parents aec3823 + e191d25 commit 32031ba
Show file tree
Hide file tree
Showing 34 changed files with 223 additions and 183 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
## [1.8.0-beta.1](https://github.com/LerianStudio/midaz/compare/v1.7.0...v1.8.0-beta.1) (2024-06-05)


### Features

* add transaction templates ([a55b583](https://github.com/LerianStudio/midaz/commit/a55b5839944e385a94037c20aae5e8b9a415a503))
* init transaction ([b696d05](https://github.com/LerianStudio/midaz/commit/b696d05af93b45841987cb56a6e3bd85fdc7ff90))


### Bug Fixes

* add field UseMetadata to use on query on mongodb when not use metadata field remove limit and skip to get all :bug: ([fce6bfb](https://github.com/LerianStudio/midaz/commit/fce6bfb2e9132a14205a90dda6164c7eaf7e97f4))
* make lint, sec and tests :bug: ([bb4621b](https://github.com/LerianStudio/midaz/commit/bb4621bc8a5a10a03f9312c9ca52a7cacdac6444))
* update test and change QueryHeader path :bug: ([c8b539f](https://github.com/LerianStudio/midaz/commit/c8b539f4b049633e6e6ad7e76b4d990e22c943f6))

## [1.7.0](https://github.com/LerianStudio/midaz/compare/v1.6.0...v1.7.0) (2024-06-05)


Expand Down
2 changes: 2 additions & 0 deletions common/net/http/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ const (
headerUserAgent = "User-Agent"
headerRealIP = "X-Real-Ip"
headerForwardedFor = "X-Forwarded-For"
dsl = "dsl"
fileExtension = ".gold"
)
92 changes: 92 additions & 0 deletions common/net/http/httputils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,59 @@
package http

import (
"bytes"
"fmt"
"io"
"mime/multipart"
"net/http"
"strconv"
"strings"

"github.com/LerianStudio/midaz/common"
"github.com/gofiber/fiber/v2"
"go.mongodb.org/mongo-driver/bson"
)

// QueryHeader entity from query parameter from get apis
type QueryHeader struct {
Metadata *bson.M
Limit int
Page int
UseMetadata bool
}

// ValidateParameters validate and return struct of default parameters
func ValidateParameters(params map[string]string) *QueryHeader {
var metadata *bson.M

limit := 10

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"):
page, _ = strconv.Atoi(value)
}
}

query := &QueryHeader{
Metadata: metadata,
Limit: limit,
Page: page,
UseMetadata: useMetadata,
}

return query
}

// IPAddrFromRemoteAddr removes port information from string.
func IPAddrFromRemoteAddr(s string) string {
idx := strings.LastIndex(s, ":")
Expand Down Expand Up @@ -36,3 +85,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
}
38 changes: 0 additions & 38 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"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"
Expand Down Expand Up @@ -57,17 +59,21 @@ 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
}

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 {
Expand Down
3 changes: 2 additions & 1 deletion components/ledger/internal/app/query/get-all-accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
3 changes: 2 additions & 1 deletion components/ledger/internal/app/query/get-all-instruments.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
3 changes: 2 additions & 1 deletion components/ledger/internal/app/query/get-all-ledgers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Loading

0 comments on commit 32031ba

Please sign in to comment.