Skip to content

Commit

Permalink
Merge pull request #179 from LerianStudio/refactor/MZ-616
Browse files Browse the repository at this point in the history
Refactor/MZ-616
  • Loading branch information
qnen authored Oct 30, 2024
2 parents 55dfaa3 + 33ab42b commit bf1b957
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 64 deletions.
6 changes: 6 additions & 0 deletions common/net/http/httputils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type QueryHeader struct {
Limit int
Page int
UseMetadata bool
PortfolioID string
}

// ValidateParameters validate and return struct of default parameters
Expand All @@ -33,6 +34,8 @@ func ValidateParameters(params map[string]string) *QueryHeader {

useMetadata := false

var portfolioID string

for key, value := range params {
switch {
case strings.Contains(key, "metadata."):
Expand All @@ -42,6 +45,8 @@ func ValidateParameters(params map[string]string) *QueryHeader {
limit, _ = strconv.Atoi(value)
case strings.Contains(key, "page"):
page, _ = strconv.Atoi(value)
case strings.Contains(key, "portfolio_id"):
portfolioID = value
}
}

Expand All @@ -50,6 +55,7 @@ func ValidateParameters(params map[string]string) *QueryHeader {
Limit: limit,
Page: page,
UseMetadata: useMetadata,
PortfolioID: portfolioID,
}

return query
Expand Down
21 changes: 10 additions & 11 deletions components/ledger/internal/ports/http/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,31 +43,30 @@ func (handler *AccountHandler) CreateAccount(i any, c *fiber.Ctx) error {
return commonHTTP.Created(c, account)
}

// SearchAllAccounts is a method that retrieves all Accounts.
func (handler *AccountHandler) SearchAllAccounts(i any, c *fiber.Ctx) error {
// GetAllAccounts is a method that retrieves all Accounts.
func (handler *AccountHandler) GetAllAccounts(c *fiber.Ctx) error {
ctx := c.UserContext()
logger := mlog.NewLoggerFromContext(ctx)

organizationID := c.Locals("organization_id").(uuid.UUID)
ledgerID := c.Locals("ledger_id").(uuid.UUID)
payload := i.(*a.SearchAccountsInput)

var portfolioID *uuid.UUID

if !common.IsNilOrEmpty(payload.PortfolioID) {
parsedID := uuid.MustParse(*payload.PortfolioID)
portfolioID = &parsedID

logger.Infof("Search of all Accounts with Portfolio ID: %s", portfolioID)
}

headerParams := commonHTTP.ValidateParameters(c.Queries())

pagination := mpostgres.Pagination{
Limit: headerParams.Limit,
Page: headerParams.Page,
}

if !common.IsNilOrEmpty(&headerParams.PortfolioID) {
parsedID := uuid.MustParse(headerParams.PortfolioID)
portfolioID = &parsedID

logger.Infof("Search of all Accounts with Portfolio ID: %s", portfolioID)
}

if headerParams.Metadata != nil {
logger.Infof("Initiating retrieval of all Accounts by metadata")

Expand Down Expand Up @@ -103,7 +102,7 @@ func (handler *AccountHandler) SearchAllAccounts(i any, c *fiber.Ctx) error {

// GetAllAccountsByIDFromPortfolio is a method that retrieves all Accounts by a given portfolio id.
//
// Will be deprecated in the future. Use SearchAllAccounts instead.
// Will be deprecated in the future. Use GetAllAccounts instead.
func (handler *AccountHandler) GetAllAccountsByIDFromPortfolio(c *fiber.Ctx) error {
ctx := c.UserContext()
logger := mlog.NewLoggerFromContext(ctx)
Expand Down
4 changes: 2 additions & 2 deletions components/ledger/internal/ports/http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ func NewRouter(lg mlog.Logger, cc *mcasdoor.CasdoorConnection, ah *AccountHandle

// Accounts
f.Post("/v1/organizations/:organization_id/ledgers/:ledger_id/portfolios/:portfolio_id/accounts", jwt.ProtectHTTP(), jwt.WithPermissionHTTP("account"), lib.ParseUUIDPathParameters, lib.WithBody(new(a.CreateAccountInput), ah.CreateAccount))
f.Post("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts/search", jwt.ProtectHTTP(), jwt.WithPermissionHTTP("account"), lib.ParseUUIDPathParameters, lib.WithBody(new(a.SearchAccountsInput), ah.SearchAllAccounts))
f.Patch("/v1/organizations/:organization_id/ledgers/:ledger_id/portfolios/:portfolio_id/accounts/:id", jwt.ProtectHTTP(), jwt.WithPermissionHTTP("account"), lib.ParseUUIDPathParameters, lib.WithBody(new(a.UpdateAccountInput), ah.UpdateAccount))
f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts", jwt.ProtectHTTP(), jwt.WithPermissionHTTP("account"), lib.ParseUUIDPathParameters, ah.GetAllAccounts)
f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:id", jwt.ProtectHTTP(), jwt.WithPermissionHTTP("account"), lib.ParseUUIDPathParameters, ah.GetAccountByID)
// Will be deprecated in the future. Use "POST /v1/organizations/:organization_id/ledgers/:ledger_id/accounts/search" instead.
// Will be deprecated in the future. Use "GET /v1/organizations/:organization_id/ledgers/:ledger_id/accounts" instead.
f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/portfolios/:portfolio_id/accounts", jwt.ProtectHTTP(), jwt.WithPermissionHTTP("account"), lib.ParseUUIDPathParameters, ah.GetAllAccountsByIDFromPortfolio)
// Will be deprecated in the future. Use "GET /v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:id" instead.
f.Get("/v1/organizations/:organization_id/ledgers/:ledger_id/portfolios/:portfolio_id/accounts/:id", jwt.ProtectHTTP(), jwt.WithPermissionHTTP("account"), lib.ParseUUIDPathParameters, ah.GetAccountByIDFromPortfolio)
Expand Down
98 changes: 47 additions & 51 deletions postman/MIDAZ.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "a4ddd405-aa36-40c8-a417-6ba7bdf109a3",
"_postman_id": "686cb43c-ad36-4b3a-a7dd-b6962f2dd1fa",
"name": "MIDAZ",
"description": "## **How generate token to use on MIDAZ**\n\n<img src=\"https://content.pstmn.io/4d5e891d-4eee-4fc9-a934-017456eebe95/aW1hZ2UucG5n\" width=\"606\" height=\"573\">\n\n<img src=\"https://content.pstmn.io/033e5a7a-d1d1-434a-988f-31bda4bf5028/aW1hZ2UucG5n\" width=\"616\" height=\"600\">\n\n<img src=\"https://content.pstmn.io/98a264c5-2b90-4f41-b7dd-8cea8f2ea945/aW1hZ2UucG5n\" width=\"937\" height=\"730\">\n\n<img src=\"https://content.pstmn.io/c62093ff-8dd4-4df4-a73e-e3a448935224/aW1hZ2UucG5n\" width=\"937\" height=\"730\">\n\n<img src=\"https://content.pstmn.io/39566f9d-2e0b-4fd2-bf2f-15fbe211f5a9/aW1hZ2UucG5n\" width=\"937\" height=\"730\">",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
Expand Down Expand Up @@ -1555,56 +1555,6 @@
},
"response": []
},
{
"name": "Accounts Search",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n // \"portfolioId\": \"{{portfolio_id}}\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url_ledger}}/v1/organizations/{{organization_id}}/ledgers/{{ledger_id}}/accounts/search?limit=10&page=1",
"host": [
"{{url_ledger}}"
],
"path": [
"v1",
"organizations",
"{{organization_id}}",
"ledgers",
"{{ledger_id}}",
"accounts",
"search"
],
"query": [
{
"key": "metadata.chave",
"value": "mymetadata",
"description": "Search on MongoDB",
"disabled": true
},
{
"key": "limit",
"value": "10",
"description": "Default 10"
},
{
"key": "page",
"value": "1",
"description": "Default 1"
}
]
}
},
"response": []
},
{
"name": "Accounts",
"event": [
Expand Down Expand Up @@ -1748,6 +1698,52 @@
},
"response": []
},
{
"name": "Accounts",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{url_ledger}}/v1/organizations/{{organization_id}}/ledgers/{{ledger_id}}/accounts?limit=10&page=1",
"host": [
"{{url_ledger}}"
],
"path": [
"v1",
"organizations",
"{{organization_id}}",
"ledgers",
"{{ledger_id}}",
"accounts"
],
"query": [
{
"key": "metadata.chave",
"value": "mymetadata",
"description": "Search on MongoDB",
"disabled": true
},
{
"key": "limit",
"value": "10",
"description": "Default 10"
},
{
"key": "page",
"value": "1",
"description": "Default 1"
},
{
"key": "portfolio_id",
"value": "{{portfolio_id}}",
"description": "Retrives only accounts from the specified portfolio",
"disabled": true
}
]
}
},
"response": []
},
{
"name": "Accounts",
"request": {
Expand Down

0 comments on commit bf1b957

Please sign in to comment.