Skip to content

Commit

Permalink
Merge pull request #181 from LerianStudio/feature/MZ-611
Browse files Browse the repository at this point in the history
Feature/MZ-611
  • Loading branch information
qnen authored Oct 31, 2024
2 parents 21152e1 + dbf78e9 commit d12a99f
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func (r *AccountPostgreSQLRepository) ListByAlias(ctx context.Context, organizat
}

// Update an Account entity into Postgresql and returns the Account updated.
func (r *AccountPostgreSQLRepository) Update(ctx context.Context, organizationID, ledgerID, portfolioID, id uuid.UUID, acc *a.Account) (*a.Account, error) {
func (r *AccountPostgreSQLRepository) Update(ctx context.Context, organizationID, ledgerID uuid.UUID, portfolioID *uuid.UUID, id uuid.UUID, acc *a.Account) (*a.Account, error) {
db, err := r.connection.GetDB()
if err != nil {
return nil, err
Expand Down Expand Up @@ -466,15 +466,19 @@ func (r *AccountPostgreSQLRepository) Update(ctx context.Context, organizationID

updates = append(updates, "updated_at = $"+strconv.Itoa(len(args)+1))

args = append(args, record.UpdatedAt, organizationID, ledgerID, portfolioID, id)
args = append(args, record.UpdatedAt, organizationID, ledgerID, id)

query := `UPDATE account SET ` + strings.Join(updates, ", ") +
` WHERE organization_id = $` + strconv.Itoa(len(args)-3) +
` AND ledger_id = $` + strconv.Itoa(len(args)-2) +
` AND portfolio_id = $` + strconv.Itoa(len(args)-1) +
` WHERE organization_id = $` + strconv.Itoa(len(args)-2) +
` AND ledger_id = $` + strconv.Itoa(len(args)-1) +
` AND id = $` + strconv.Itoa(len(args)) +
` AND deleted_at IS NULL`

if portfolioID != nil && *portfolioID != uuid.Nil {
args = append(args, portfolioID)
query += ` AND portfolio_id = $` + strconv.Itoa(len(args))
}

result, err := db.ExecContext(ctx, query, args...)
if err != nil {
var pgErr *pgconn.PgError
Expand Down
2 changes: 1 addition & 1 deletion components/ledger/internal/app/command/update-account.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// UpdateAccount update an account from the repository by given id.
func (uc *UseCase) UpdateAccount(ctx context.Context, organizationID, ledgerID, portfolioID, id uuid.UUID, uai *a.UpdateAccountInput) (*a.Account, error) {
func (uc *UseCase) UpdateAccount(ctx context.Context, organizationID, ledgerID uuid.UUID, portfolioID *uuid.UUID, id uuid.UUID, uai *a.UpdateAccountInput) (*a.Account, error) {
logger := mlog.NewLoggerFromContext(ctx)
logger.Infof("Trying to update account: %v", uai)

Expand Down
33 changes: 29 additions & 4 deletions components/ledger/internal/app/command/update-account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,35 @@ func TestUpdateAccountByIDSuccess(t *testing.T) {

uc.AccountRepo.(*mock.MockRepository).
EXPECT().
Update(gomock.Any(), organizationID, ledgerID, portfolioID, id, account).
Update(gomock.Any(), organizationID, ledgerID, &portfolioID, id, account).
Return(account, nil).
Times(1)
res, err := uc.AccountRepo.Update(context.TODO(), organizationID, ledgerID, portfolioID, id, account)
res, err := uc.AccountRepo.Update(context.TODO(), organizationID, ledgerID, &portfolioID, id, account)

assert.Equal(t, account, res)
assert.Nil(t, err)
}

// TestUpdateAccountByIDWithoutPortfolioSuccess is responsible to test UpdateAccountByIDWithoutPortfolio with success
func TestUpdateAccountByIDWithoutPortfolioSuccess(t *testing.T) {
organizationID := common.GenerateUUIDv7()
ledgerID := common.GenerateUUIDv7()
id := common.GenerateUUIDv7()
account := &a.Account{
ID: id.String(),
UpdatedAt: time.Now(),
}

uc := UseCase{
AccountRepo: mock.NewMockRepository(gomock.NewController(t)),
}

uc.AccountRepo.(*mock.MockRepository).
EXPECT().
Update(gomock.Any(), organizationID, ledgerID, nil, id, account).
Return(account, nil).
Times(1)
res, err := uc.AccountRepo.Update(context.TODO(), organizationID, ledgerID, nil, id, account)

assert.Equal(t, account, res)
assert.Nil(t, err)
Expand All @@ -57,9 +82,9 @@ func TestUpdateAccountByIDError(t *testing.T) {

uc.AccountRepo.(*mock.MockRepository).
EXPECT().
Update(gomock.Any(), organizationID, ledgerID, portfolioID, id, account).
Update(gomock.Any(), organizationID, ledgerID, &portfolioID, id, account).
Return(nil, errors.New(errMSG))
res, err := uc.AccountRepo.Update(context.TODO(), organizationID, ledgerID, portfolioID, id, account)
res, err := uc.AccountRepo.Update(context.TODO(), organizationID, ledgerID, &portfolioID, id, account)

assert.NotEmpty(t, err)
assert.Equal(t, err.Error(), errMSG)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Repository interface {
FindByAlias(ctx context.Context, organizationID, ledgerID uuid.UUID, alias string) (bool, error)
ListByIDs(ctx context.Context, organizationID, ledgerID uuid.UUID, portfolioID *uuid.UUID, ids []uuid.UUID) ([]*Account, error)
ListByAlias(ctx context.Context, organizationID, ledgerID, portfolioID uuid.UUID, alias []string) ([]*Account, error)
Update(ctx context.Context, organizationID, ledgerID, portfolioID, id uuid.UUID, acc *Account) (*Account, error)
Update(ctx context.Context, organizationID, ledgerID uuid.UUID, portfolioID *uuid.UUID, id uuid.UUID, acc *Account) (*Account, error)
Delete(ctx context.Context, organizationID, ledgerID uuid.UUID, portfolioID *uuid.UUID, id uuid.UUID) error
ListAccountsByIDs(ctx context.Context, organizationID, ledgerID uuid.UUID, ids []uuid.UUID) ([]*Account, error)
ListAccountsByAlias(ctx context.Context, organizationID, ledgerID uuid.UUID, aliases []string) ([]*Account, error)
Expand Down

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

35 changes: 34 additions & 1 deletion components/ledger/internal/ports/http/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,39 @@ func (handler *AccountHandler) UpdateAccount(i any, 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)
id := c.Locals("id").(uuid.UUID)

logger.Infof("Initiating update of Account with ID: %s", id.String())

payload := i.(*a.UpdateAccountInput)
logger.Infof("Request to update an Account with details: %#v", payload)

_, err := handler.Command.UpdateAccount(ctx, organizationID, ledgerID, nil, id, payload)
if err != nil {
logger.Errorf("Failed to update Account with ID: %s, Error: %s", id.String(), err.Error())
return commonHTTP.WithError(c, err)
}

account, err := handler.Query.GetAccountByID(c.Context(), organizationID, ledgerID, nil, id)
if err != nil {
logger.Errorf("Failed to retrieve Account with ID: %s, Error: %s", id, err.Error())
return commonHTTP.WithError(c, err)
}

logger.Infof("Successfully updated Account with ID: %s", id.String())

return commonHTTP.OK(c, account)
}

// UpdateAccountFromPortfolio is a method that updates Account information from a given portfolio id and account id.
//
// Will be deprecated in the future. Use UpdateAccount instead.
func (handler *AccountHandler) UpdateAccountFromPortfolio(i any, 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)
portfolioID := c.Locals("portfolio_id").(uuid.UUID)
Expand All @@ -246,7 +279,7 @@ func (handler *AccountHandler) UpdateAccount(i any, c *fiber.Ctx) error {
payload := i.(*a.UpdateAccountInput)
logger.Infof("Request to update an Account with details: %#v", payload)

_, err := handler.Command.UpdateAccount(ctx, organizationID, ledgerID, portfolioID, id, payload)
_, err := handler.Command.UpdateAccount(ctx, organizationID, ledgerID, &portfolioID, id, payload)
if err != nil {
logger.Errorf("Failed to update Account with ID: %s, Error: %s", id.String(), err.Error())
return commonHTTP.WithError(c, err)
Expand Down
10 changes: 6 additions & 4 deletions components/ledger/internal/ports/http/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,18 @@ func NewRouter(lg mlog.Logger, cc *mcasdoor.CasdoorConnection, ah *AccountHandle

// Accounts
f.Post("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts", jwt.ProtectHTTP(), jwt.WithPermissionHTTP("account"), lib.ParseUUIDPathParameters, lib.WithBody(new(a.CreateAccountInput), ah.CreateAccount))
// Will be deprecated in the future. Use "POST /v1/organizations/:organization_id/ledgers/:ledger_id/accounts" instead.
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.CreateAccountFromPortfolio))
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.Patch("/v1/organizations/:organization_id/ledgers/:ledger_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)
f.Delete("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:id", jwt.ProtectHTTP(), jwt.WithPermissionHTTP("account"), lib.ParseUUIDPathParameters, ah.DeleteAccountByID)
// Will be deprecated in the future. Use "POST /v1/organizations/:organization_id/ledgers/:ledger_id/accounts" instead.
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.CreateAccountFromPortfolio))
// Will be deprecated in the future. Use "PATCH /v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:id" instead.
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.UpdateAccountFromPortfolio))
// 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)
f.Delete("/v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:id", jwt.ProtectHTTP(), jwt.WithPermissionHTTP("account"), lib.ParseUUIDPathParameters, ah.DeleteAccountByID)
// Will be deprecated in the future. Use "DELETE /v1/organizations/:organization_id/ledgers/:ledger_id/accounts/:id" instead.
f.Delete("/v1/organizations/:organization_id/ledgers/:ledger_id/portfolios/:portfolio_id/accounts/:id", jwt.ProtectHTTP(), jwt.WithPermissionHTTP("account"), lib.ParseUUIDPathParameters, ah.DeleteAccountByIDFromPortfolio)

Expand Down
64 changes: 54 additions & 10 deletions postman/MIDAZ.postman_collection.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "686cb43c-ad36-4b3a-a7dd-b6962f2dd1fa",
"_postman_id": "d793a589-8710-4422-acc3-634a373f2922",
"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 @@ -1604,7 +1604,7 @@
"response": []
},
{
"name": "Accounts",
"name": "Accounts from Portfolio",
"event": [
{
"listen": "test",
Expand Down Expand Up @@ -1650,10 +1650,31 @@
"response": []
},
{
"name": "Account by Id",
"name": "Accounts",
"event": [
{
"listen": "test",
"script": {
"exec": [
""
],
"type": "text/javascript",
"packages": {}
}
}
],
"request": {
"method": "GET",
"method": "PATCH",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"name\": \"{{$randomBankAccountName}} Blocked Account\", //opcional\n \"alias\": \"Wallet {{$randomBankAccount}}\",\n \"productId\": \"{{product_id}}\",\n \"status\": {\n \"code\": \"BLOCKED\",\n \"description\": \"My blocked account\",\n \"allowSending\": false,\n \"allowReceiving\": false\n },\n \"metadata\": {\n \"chave\": \"metadata_sample_update\",\n \"bitcoinn\": \"{{$randomBitcoin}}\",\n \"boolean\": {{$randomBoolean}},\n \"double\": 10.5,\n \"int\": 1\n }\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url_ledger}}/v1/organizations/{{organization_id}}/ledgers/{{ledger_id}}/accounts/{{account_id}}",
"host": [
Expand Down Expand Up @@ -1746,6 +1767,29 @@
},
"response": []
},
{
"name": "Account by Id",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{url_ledger}}/v1/organizations/{{organization_id}}/ledgers/{{ledger_id}}/accounts/{{account_id}}",
"host": [
"{{url_ledger}}"
],
"path": [
"v1",
"organizations",
"{{organization_id}}",
"ledgers",
"{{ledger_id}}",
"accounts",
"{{account_id}}"
]
}
},
"response": []
},
{
"name": "Accounts",
"request": {
Expand Down Expand Up @@ -1793,7 +1837,7 @@
"response": []
},
{
"name": "Accounts",
"name": "Accounts from Portfolio",
"request": {
"method": "DELETE",
"header": [],
Expand All @@ -1802,7 +1846,7 @@
"formdata": []
},
"url": {
"raw": "{{url_ledger}}/v1/organizations/{{organization_id}}/ledgers/{{ledger_id}}/accounts/{{account_id}}",
"raw": "{{url_ledger}}/v1/organizations/{{organization_id}}/ledgers/{{ledger_id}}/portfolios/{{portfolio_id}}/accounts/{{account_id}}",
"host": [
"{{url_ledger}}"
],
Expand All @@ -1812,6 +1856,8 @@
"{{organization_id}}",
"ledgers",
"{{ledger_id}}",
"portfolios",
"{{portfolio_id}}",
"accounts",
"{{account_id}}"
]
Expand All @@ -1820,7 +1866,7 @@
"response": []
},
{
"name": "Accounts from Portfolio",
"name": "Accounts",
"request": {
"method": "DELETE",
"header": [],
Expand All @@ -1829,7 +1875,7 @@
"formdata": []
},
"url": {
"raw": "{{url_ledger}}/v1/organizations/{{organization_id}}/ledgers/{{ledger_id}}/portfolios/{{portfolio_id}}/accounts/{{account_id}}",
"raw": "{{url_ledger}}/v1/organizations/{{organization_id}}/ledgers/{{ledger_id}}/accounts/{{account_id}}",
"host": [
"{{url_ledger}}"
],
Expand All @@ -1839,8 +1885,6 @@
"{{organization_id}}",
"ledgers",
"{{ledger_id}}",
"portfolios",
"{{portfolio_id}}",
"accounts",
"{{account_id}}"
]
Expand Down

0 comments on commit d12a99f

Please sign in to comment.