Skip to content

Commit

Permalink
Merge pull request #406 from LerianStudio/develop
Browse files Browse the repository at this point in the history
merge: develop to main v1.34.0
  • Loading branch information
MartinezAvellan authored Dec 19, 2024
2 parents 2c7d5f1 + 78a17d9 commit 134e445
Show file tree
Hide file tree
Showing 24 changed files with 243 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ on:
types: [released]

jobs:
bump_formula:
brew_release:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-24.04
steps:
- name: Update Homebrew Formula
uses: dawidd6/action-homebrew-bump-formula@v4
with:
token: ${{ secrets.RELEASE_GITHUB_TOKEN }}
formula: mdz
formula: mdz
1 change: 0 additions & 1 deletion .github/workflows/release-notification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:

- name: Github Releases To Discord
uses: SethCohen/github-releases-to-discord@v1.13.1
if: '!github.event.prerelease'
with:
webhook_url: ${{ secrets.DISCORD_WEBHOOK_URL }}
color: "2105893"
Expand Down
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ linters:
- depguard
- dogsled
- dupword # Detects duplicate words.
- durationcheck
- errchkjson
- gocyclo
- loggercheck
Expand Down
2 changes: 1 addition & 1 deletion .releaserc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,5 @@ branches:
- main
- name: develop
prerelease: "beta"
- name: hotfix/**
- name: hotfix/*
prerelease: "hf"
30 changes: 30 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
## [1.34.0-beta.2](https://github.com/LerianStudio/midaz/compare/v1.34.0-beta.1...v1.34.0-beta.2) (2024-12-19)


### Features

* add http utils func that help to get idempotency key and ttl ([cce1bcf](https://github.com/LerianStudio/midaz/commit/cce1bcfc8add465849cb1d36caad7a29ba883648))
* create a func that convert uuid.string to uuid :sparkles: ([f535250](https://github.com/LerianStudio/midaz/commit/f53525007586cb61bcee7a21406fca2036d062f2))
* create a string to sha-256 convert func :sparkles: ([c675219](https://github.com/LerianStudio/midaz/commit/c675219aeba97b08151e10c2f4e3a228cbed4094))
* create header key to Idempotency and ttl :sparkles: ([b9124ed](https://github.com/LerianStudio/midaz/commit/b9124ed2991f2caae8c04880bbcdfc7151f30e63))
* create Idempotency business error ([4c18470](https://github.com/LerianStudio/midaz/commit/4c18470c9ce56ae1e5d59f8ea1af55627539d1d8))


### Bug Fixes

* adjust redis consumer :bug: ([f7edc63](https://github.com/LerianStudio/midaz/commit/f7edc63b4a775bcc5fdb32ac57f5557dc973bad9))
* go test :bug: ([ab60f3b](https://github.com/LerianStudio/midaz/commit/ab60f3bf4dc6ff58ad9ce4f2ceb417aa0e049512))
* golint :bug: ([32083c3](https://github.com/LerianStudio/midaz/commit/32083c3deb72794b43cb3928e03a5fe1a1ad8c19))
* update go mod :bug: ([5ead58a](https://github.com/LerianStudio/midaz/commit/5ead58af6b04569123c6d9ef52128e1b962b3c05))

## [1.34.0-beta.1](https://github.com/LerianStudio/midaz/compare/v1.33.0...v1.34.0-beta.1) (2024-12-19)


### Bug Fixes

* added if pre release bump formule brew ([0c576cc](https://github.com/LerianStudio/midaz/commit/0c576cc4bddbcafe3b5a3277e19a284e36ff4672))
* change ubuntu-release to ubuntu-24.04 :bug: ([8044333](https://github.com/LerianStudio/midaz/commit/804433391ea027c4275ce75ba967b473e151b5f7))
* remove if :bug: ([d6e370a](https://github.com/LerianStudio/midaz/commit/d6e370ad97473314e07f70f091401ffdcfd2f666))
* this is the correct pattern to match all branches that start with hotfix/ followed by any text. :bug: ([0ff571c](https://github.com/LerianStudio/midaz/commit/0ff571cdbe4a16dc8eaed805f27f1fee529a09fb))
* update git action :bug: ([7acb849](https://github.com/LerianStudio/midaz/commit/7acb8494480685e708d808fafe179b8b110ade6e))

## [1.33.0](https://github.com/LerianStudio/midaz/compare/v1.32.0...v1.33.0) (2024-12-18)


Expand Down
2 changes: 1 addition & 1 deletion components/audit/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# ENV_NAME=production

# APP
VERSION=v1.31.1
VERSION=v1.34.0
SERVER_PORT=3005
SERVER_ADDRESS=:${SERVER_PORT}

Expand Down
2 changes: 1 addition & 1 deletion components/ledger/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# ENV_NAME=production

# APP
VERSION=v1.31.1
VERSION=v1.34.0
SERVER_PORT=3000
SERVER_ADDRESS=:${SERVER_PORT}

Expand Down
2 changes: 1 addition & 1 deletion components/mdz/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ CLIENT_ID=9670e0ca55a29a466d31
CLIENT_SECRET=dd03f916cacf4a98c6a413d9c38ba102dce436a9
URL_API_AUTH=http://127.0.0.1:8080
URL_API_LEDGER=http://127.0.0.1:3000
VERSION=v1.31.1
VERSION=v1.34.0
2 changes: 1 addition & 1 deletion components/transaction/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# ENV_NAME=production

# APP
VERSION=v1.31.1
VERSION=v1.34.0
APP_CONTEXT=/transaction/v1
SERVER_PORT=3002
SERVER_ADDRESS=:${SERVER_PORT}
Expand Down
46 changes: 30 additions & 16 deletions components/transaction/internal/adapters/http/in/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ package in
import (
"context"
"encoding/json"
"github.com/LerianStudio/midaz/pkg/mmodel"
"go.mongodb.org/mongo-driver/bson"
"go.opentelemetry.io/otel/trace"
"os"
"reflect"
"strings"

"github.com/LerianStudio/midaz/components/transaction/internal/adapters/postgres/operation"
"github.com/LerianStudio/midaz/components/transaction/internal/adapters/postgres/transaction"
"github.com/LerianStudio/midaz/components/transaction/internal/services/command"
Expand All @@ -20,9 +13,15 @@ import (
goldModel "github.com/LerianStudio/midaz/pkg/gold/transaction/model"
"github.com/LerianStudio/midaz/pkg/mgrpc/account"
"github.com/LerianStudio/midaz/pkg/mlog"
"github.com/LerianStudio/midaz/pkg/mmodel"
"github.com/LerianStudio/midaz/pkg/mopentelemetry"
"github.com/LerianStudio/midaz/pkg/mpostgres"
"github.com/LerianStudio/midaz/pkg/net/http"
"go.mongodb.org/mongo-driver/bson"
"go.opentelemetry.io/otel/trace"
"os"
"reflect"
"strings"

"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
Expand Down Expand Up @@ -387,6 +386,23 @@ func (handler *TransactionHandler) createTransaction(c *fiber.Ctx, logger mlog.L
organizationID := c.Locals("organization_id").(uuid.UUID)
ledgerID := c.Locals("ledger_id").(uuid.UUID)

_, spanRedis := tracer.Start(ctx, "handler.create_transaction_idempotency")

ts, _ := pkg.StructToJSONString(parserDSL)
hash := pkg.HashSHA256(ts)
key, ttl := http.GetIdempotencyKeyAndTTL(c)

err := handler.Command.CreateOrCheckIdempotencyKey(ctx, organizationID, ledgerID, key, hash, ttl)
if err != nil {
mopentelemetry.HandleSpanError(&spanRedis, "Redis idempotency key", err)

logger.Error("Redis idempotency key:", err.Error())

return http.WithError(c, err)
}

spanRedis.End()

_, spanValidateDSL := tracer.Start(ctx, "handler.create_transaction_validate_dsl")

validate, err := goldModel.ValidateSendSourceAndDistribute(parserDSL)
Expand All @@ -400,10 +416,10 @@ func (handler *TransactionHandler) createTransaction(c *fiber.Ctx, logger mlog.L

spanValidateDSL.End()

token := http.GetTokenHeader(c)

ctxGetAccounts, spanGetAccounts := tracer.Start(ctx, "handler.create_transaction.get_accounts")

token := http.GetTokenHeader(c)

accounts, err := handler.getAccounts(ctxGetAccounts, logger, token, organizationID, ledgerID, validate.Aliases)
if err != nil {
mopentelemetry.HandleSpanError(&spanGetAccounts, "Failed to get accounts", err)
Expand Down Expand Up @@ -444,10 +460,6 @@ func (handler *TransactionHandler) createTransaction(c *fiber.Ctx, logger mlog.L

spanCreateTransaction.End()

tran.Source = validate.Sources
tran.Destination = validate.Destinations
transactionID := uuid.MustParse(tran.ID)

e := make(chan error)
result := make(chan []*operation.Operation)

Expand Down Expand Up @@ -488,7 +500,7 @@ func (handler *TransactionHandler) createTransaction(c *fiber.Ctx, logger mlog.L
ctxUpdateTransactionStatus, spanUpdateTransactionStatus := tracer.Start(ctx, "handler.create_transaction.update_transaction_status")

//TODO: use event driven and broken and parts
_, err = handler.Command.UpdateTransactionStatus(ctxUpdateTransactionStatus, organizationID, ledgerID, transactionID, constant.APPROVED)
_, err = handler.Command.UpdateTransactionStatus(ctxUpdateTransactionStatus, organizationID, ledgerID, tran.IDtoUUID(), constant.APPROVED)
if err != nil {
mopentelemetry.HandleSpanError(&spanUpdateTransactionStatus, "Failed to update transaction status", err)

Expand All @@ -502,7 +514,7 @@ func (handler *TransactionHandler) createTransaction(c *fiber.Ctx, logger mlog.L
ctxGetTransaction, spanGetTransaction := tracer.Start(ctx, "handler.create_transaction.get_transaction")

//TODO: use event driven and broken and parts
tran, err = handler.Query.GetTransactionByID(ctxGetTransaction, organizationID, ledgerID, transactionID)
tran, err = handler.Query.GetTransactionByID(ctxGetTransaction, organizationID, ledgerID, tran.IDtoUUID())
if err != nil {
mopentelemetry.HandleSpanError(&spanGetTransaction, "Failed to retrieve transaction", err)

Expand All @@ -513,11 +525,13 @@ func (handler *TransactionHandler) createTransaction(c *fiber.Ctx, logger mlog.L

spanGetTransaction.End()

tran.Source = validate.Sources
tran.Destination = validate.Destinations
tran.Operations = operations

logger.Infof("Successfully updated Transaction with Organization ID: %s, Ledger ID: %s and ID: %s", organizationID.String(), ledgerID.String(), tran.ID)

go handler.logTransaction(ctx, operations, organizationID, ledgerID, transactionID)
go handler.logTransaction(ctx, operations, organizationID, ledgerID, tran.IDtoUUID())

return http.Created(c, tran)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ type Transaction struct {
Operations []*operation.Operation `json:"operations"`
} // @name Transaction

// IDtoUUID is a func that convert UUID string to uuid.UUID
func (t Transaction) IDtoUUID() uuid.UUID {
return uuid.MustParse(t.ID)
}

// ToEntity converts an TransactionPostgreSQLModel to entity Transaction
func (t *TransactionPostgreSQLModel) ToEntity() *Transaction {
status := Status{
Expand Down
54 changes: 14 additions & 40 deletions components/transaction/internal/adapters/redis/consumer.redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
//go:generate mockgen --destination=redis.mock.go --package=redis . RedisRepository
type RedisRepository interface {
Set(ctx context.Context, key, value string, ttl time.Duration) error
Get(ctx context.Context, key string) error
Get(ctx context.Context, key string) (string, error)
Del(ctx context.Context, key string) error
}

Expand Down Expand Up @@ -49,23 +49,19 @@ func (rr *RedisConsumerRepository) Set(ctx context.Context, key, value string, t
return err
}

if ttl <= 0 {
ttl = mredis.RedisTTL
}

logger.Infof("value of ttl: %v", ttl)
logger.Infof("value of ttl: %v", ttl*time.Minute)

statusCMD := rds.Set(ctx, key, value, ttl)
if statusCMD.Err() != nil {
mopentelemetry.HandleSpanError(&span, "Failed to set on redis", statusCMD.Err())
err = rds.Set(ctx, key, value, ttl*time.Minute).Err()
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to set on redis", err)

return statusCMD.Err()
return err
}

return nil
}

func (rr *RedisConsumerRepository) Get(ctx context.Context, key string) error {
func (rr *RedisConsumerRepository) Get(ctx context.Context, key string) (string, error) {
logger := pkg.NewLoggerFromContext(ctx)
tracer := pkg.NewTracerFromContext(ctx)

Expand All @@ -76,43 +72,21 @@ func (rr *RedisConsumerRepository) Get(ctx context.Context, key string) error {
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to get redis", err)

return err
return "", err
}

stringCMD := rds.Get(ctx, key)
if stringCMD.Err() != nil {
mopentelemetry.HandleSpanError(&span, "Failed to get on redis", stringCMD.Err())
val, err := rds.Get(ctx, key).Result()
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to get on redis", err)

return stringCMD.Err()
return "", err
}

logger.Infof("get cmd: %v", stringCMD.String())
logger.Infof("value : %v", val)

return nil
return val, nil
}

func (rr *RedisConsumerRepository) Del(ctx context.Context, key string) error {
logger := pkg.NewLoggerFromContext(ctx)
tracer := pkg.NewTracerFromContext(ctx)

ctx, span := tracer.Start(ctx, "redis.del")
defer span.End()

rds, err := rr.conn.GetClient(ctx)
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to del redis", err)

return err
}

intCMD := rds.Del(ctx, key)
if intCMD.Err() != nil {
mopentelemetry.HandleSpanError(&span, "Failed to del on redis", intCMD.Err())

return intCMD.Err()
}

logger.Infof("del cmd: %v", intCMD.String())

return nil
}
10 changes: 6 additions & 4 deletions components/transaction/internal/adapters/redis/redis.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package command

import (
"context"
"github.com/LerianStudio/midaz/pkg"
"github.com/LerianStudio/midaz/pkg/constant"
"github.com/LerianStudio/midaz/pkg/mopentelemetry"
"github.com/google/uuid"
"time"
)

func (uc *UseCase) CreateOrCheckIdempotencyKey(ctx context.Context, organizationID, ledgerID uuid.UUID, key, hash string, ttl time.Duration) error {
logger := pkg.NewLoggerFromContext(ctx)
tracer := pkg.NewTracerFromContext(ctx)

ctx, span := tracer.Start(ctx, "command.create-idempotency-key")
defer span.End()

logger.Infof("Trying to create or check idempotency key in redis")

if key == "" {
key = hash
}

internalKey := organizationID.String() + ":" + ledgerID.String() + ":" + key

value, err := uc.RedisRepo.Get(ctx, internalKey)
if err != nil {
logger.Error("Error to get idempotency key on redis failed:", err.Error())
}

if value == "" {
err = uc.RedisRepo.Set(ctx, internalKey, hash, ttl)
if err != nil {
logger.Error("Error to set idempotency key on redis failed:", err.Error())
}
} else {
err = pkg.ValidateBusinessError(constant.ErrIdempotencyKey, "CreateOrCheckIdempotencyKey", key)

mopentelemetry.HandleSpanError(&span, "Failed exists value on redis with this key", err)

return err
}

return nil
}
Loading

0 comments on commit 134e445

Please sign in to comment.