Skip to content

Commit

Permalink
Merge pull request #462 from LerianStudio/feature/MIDAZ-460
Browse files Browse the repository at this point in the history
Feature/MIDAZ-460
  • Loading branch information
MartinezAvellan authored Jan 17, 2025
2 parents 24b5e6b + e0d36ad commit fa225c9
Show file tree
Hide file tree
Showing 15 changed files with 736 additions and 125 deletions.
59 changes: 59 additions & 0 deletions components/transaction/api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,65 @@ const docTemplate = `{
}
}
}
},
"/v1/organizations/{organization_id}/ledgers/{ledger_id}/transactions/{transaction_id}/revert": {
"post": {
"description": "Revert a Transaction with Transaction ID only",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Transactions"
],
"summary": "Revert a Transaction",
"parameters": [
{
"type": "string",
"description": "Authorization Bearer Token",
"name": "Authorization",
"in": "header",
"required": true
},
{
"type": "string",
"description": "Request ID",
"name": "Midaz-Id",
"in": "header"
},
{
"type": "string",
"description": "Organization ID",
"name": "organization_id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Ledger ID",
"name": "ledger_id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Transaction ID",
"name": "transaction_id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Transaction"
}
}
}
}
}
},
"definitions": {
Expand Down
43 changes: 43 additions & 0 deletions components/transaction/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,49 @@ paths:
tags:
- Operations
x-codegen-request-body-name: operation
/v1/organizations/{organization_id}/ledgers/{ledger_id}/transactions/{transaction_id}/revert:
post:
description: Revert a Transaction with Transaction ID only
parameters:
- description: Authorization Bearer Token
in: header
name: Authorization
required: true
schema:
type: string
- description: Request ID
in: header
name: Midaz-Id
schema:
type: string
- description: Organization ID
in: path
name: organization_id
required: true
schema:
type: string
- description: Ledger ID
in: path
name: ledger_id
required: true
schema:
type: string
- description: Transaction ID
in: path
name: transaction_id
required: true
schema:
type: string
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/Transaction'
description: OK
summary: Revert a Transaction
tags:
- Transactions
components:
schemas:
Amount:
Expand Down
59 changes: 59 additions & 0 deletions components/transaction/api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,65 @@
}
}
}
},
"/v1/organizations/{organization_id}/ledgers/{ledger_id}/transactions/{transaction_id}/revert": {
"post": {
"description": "Revert a Transaction with Transaction ID only",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Transactions"
],
"summary": "Revert a Transaction",
"parameters": [
{
"type": "string",
"description": "Authorization Bearer Token",
"name": "Authorization",
"in": "header",
"required": true
},
{
"type": "string",
"description": "Request ID",
"name": "Midaz-Id",
"in": "header"
},
{
"type": "string",
"description": "Organization ID",
"name": "organization_id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Ledger ID",
"name": "ledger_id",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Transaction ID",
"name": "transaction_id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/Transaction"
}
}
}
}
}
},
"definitions": {
Expand Down
40 changes: 40 additions & 0 deletions components/transaction/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,46 @@ paths:
summary: Update an Operation
tags:
- Operations
/v1/organizations/{organization_id}/ledgers/{ledger_id}/transactions/{transaction_id}/revert:
post:
consumes:
- application/json
description: Revert a Transaction with Transaction ID only
parameters:
- description: Authorization Bearer Token
in: header
name: Authorization
required: true
type: string
- description: Request ID
in: header
name: Midaz-Id
type: string
- description: Organization ID
in: path
name: organization_id
required: true
type: string
- description: Ledger ID
in: path
name: ledger_id
required: true
type: string
- description: Transaction ID
in: path
name: transaction_id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/Transaction'
summary: Revert a Transaction
tags:
- Transactions
/v1/organizations/{organization_id}/ledgers/{ledger_id}/transactions/dsl:
post:
consumes:
Expand Down
64 changes: 61 additions & 3 deletions components/transaction/internal/adapters/http/in/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,18 @@ func (handler *TransactionHandler) CommitTransaction(c *fiber.Ctx) error {

// RevertTransaction method that revert transaction created before
//
// TODO: Implement this method and the swagger documentation related to it
// @Summary Revert a Transaction
// @Description Revert a Transaction with Transaction ID only
// @Tags Transactions
// @Accept json
// @Produce json
// @Param Authorization header string true "Authorization Bearer Token"
// @Param Midaz-Id header string false "Request ID"
// @Param organization_id path string true "Organization ID"
// @Param ledger_id path string true "Ledger ID"
// @Param transaction_id path string true "Transaction ID"
// @Success 200 {object} transaction.Transaction
// @Router /v1/organizations/{organization_id}/ledgers/{ledger_id}/transactions/{transaction_id}/revert [post]
func (handler *TransactionHandler) RevertTransaction(c *fiber.Ctx) error {
ctx := c.UserContext()

Expand All @@ -179,7 +190,53 @@ func (handler *TransactionHandler) RevertTransaction(c *fiber.Ctx) error {
_, span := tracer.Start(ctx, "handler.revert_transaction")
defer span.End()

return http.Created(c, logger)
organizationID := c.Locals("organization_id").(uuid.UUID)
ledgerID := c.Locals("ledger_id").(uuid.UUID)
transactionID := c.Locals("transaction_id").(uuid.UUID)

parent, err := handler.Query.GetParentByTransactionID(ctx, organizationID, ledgerID, transactionID)
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to retrieve Parent Transaction on query", err)

logger.Errorf("Failed to retrieve Parent Transaction with ID: %s, Error: %s", transactionID.String(), err.Error())

return http.WithError(c, err)
}

if parent != nil {
err = pkg.ValidateBusinessError(constant.ErrTransactionIDHasAlreadyParentTransaction, "RevertTransaction")

mopentelemetry.HandleSpanError(&span, "Transaction Has Already Parent Transaction", err)

logger.Errorf("Transaction Has Already Parent Transaction with ID: %s, Error: %s", transactionID.String(), err)

return http.WithError(c, err)
}

tran, err := handler.Query.GetTransactionByID(ctx, organizationID, ledgerID, transactionID)
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to retrieve transaction on query", err)

logger.Errorf("Failed to retrieve Transaction with ID: %s, Error: %s", transactionID.String(), err.Error())

return http.WithError(c, err)
}

if tran.ParentTransactionID != nil {
err = pkg.ValidateBusinessError(constant.ErrTransactionIDIsAlreadyARevert, "RevertTransaction")

mopentelemetry.HandleSpanError(&span, "Transaction Has Already Parent Transaction", err)

logger.Errorf("Transaction Has Already Parent Transaction with ID: %s, Error: %s", transactionID.String(), err)

return http.WithError(c, err)
}

transactionReverted := tran.TransactionRevert()

response := handler.createTransaction(c, logger, transactionReverted)

return response
}

// UpdateTransaction method that patch transaction created before
Expand Down Expand Up @@ -385,6 +442,7 @@ func (handler *TransactionHandler) createTransaction(c *fiber.Ctx, logger mlog.L

organizationID := c.Locals("organization_id").(uuid.UUID)
ledgerID := c.Locals("ledger_id").(uuid.UUID)
transactionID, _ := c.Locals("transaction_id").(uuid.UUID)

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

Expand Down Expand Up @@ -449,7 +507,7 @@ func (handler *TransactionHandler) createTransaction(c *fiber.Ctx, logger mlog.L
return http.WithError(c, err)
}

tran, err := handler.Command.CreateTransaction(ctxCreateTransaction, organizationID, ledgerID, &parserDSL)
tran, err := handler.Command.CreateTransaction(ctxCreateTransaction, organizationID, ledgerID, transactionID, &parserDSL)
if err != nil {
mopentelemetry.HandleSpanError(&spanCreateTransaction, "Failed to create transaction", err)

Expand Down
Loading

0 comments on commit fa225c9

Please sign in to comment.