Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/MZ-504 #32

Merged
merged 18 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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"
o "github.com/LerianStudio/midaz/components/ledger/internal/domain/onboarding/organization"
mock "github.com/LerianStudio/midaz/components/ledger/internal/gen/mock/metadata"
Expand All @@ -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,
Expand Down
Loading
Loading