-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #182 from LerianStudio/feature/MZ-607
Feature/MZ-607
- Loading branch information
Showing
16 changed files
with
650 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package repository | ||
|
||
import "github.com/LerianStudio/midaz/components/mdz/internal/model" | ||
|
||
type Ledger interface { | ||
Create(organizationID string, inp model.LedgerInput) (*model.LedgerCreate, error) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package model | ||
|
||
import "time" | ||
|
||
type LedgerInput struct { | ||
Name string `json:"name,omitempty"` | ||
Status *LedgerStatus `json:"status,omitempty"` | ||
Metadata *LedgerMetadata `json:"metadata,omitempty"` | ||
} | ||
|
||
type LedgerStatus struct { | ||
Code *string `json:"code,omitempty"` | ||
Description *string `json:"description,omitempty"` | ||
} | ||
|
||
type LedgerMetadata struct { | ||
Chave *string `json:"chave,omitempty"` | ||
Bitcoin *string `json:"bitcoin,omitempty"` | ||
Boolean *bool `json:"boolean,omitempty"` | ||
Double *float64 `json:"double,omitempty"` | ||
Int *int `json:"int,omitempty"` | ||
} | ||
|
||
type LedgerCreate struct { | ||
ID string `json:"id,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
OrganizationID string `json:"organizationId,omitempty"` | ||
Status LedgerStatus `json:"status,omitempty"` | ||
CreatedAt time.Time `json:"createdAt,omitempty"` | ||
UpdatedAt time.Time `json:"updatedAt,omitempty"` | ||
DeletedAt time.Time `json:"deletedAt,omitempty"` | ||
Metadata LedgerMetadata `json:"metadata,omitempty"` | ||
} |
19 changes: 19 additions & 0 deletions
19
components/mdz/internal/rest/.fixtures/ledger_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"id": "0192e251-328d-7390-99f5-5c54980115ed", | ||
"name": "Romaguera and Sons", | ||
"organizationId": "0192e250-ed9d-7e5c-a614-9b294151b572", | ||
"status": { | ||
"code": "ACTIVE", | ||
"description": "Teste Ledger" | ||
}, | ||
"createdAt": "2024-10-31T11:23:45.165229097Z", | ||
"updatedAt": "2024-10-31T11:23:45.16522918Z", | ||
"deletedAt": null, | ||
"metadata": { | ||
"bitcoin": "1iR2KqpxRFjLsPUpWmpADMC7piRNsMAAjq", | ||
"boolean": false, | ||
"chave": "metadata_chave", | ||
"double": 10.5, | ||
"int": 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package rest | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/LerianStudio/midaz/components/mdz/internal/model" | ||
"github.com/LerianStudio/midaz/components/mdz/pkg/factory" | ||
) | ||
|
||
type ledger struct { | ||
Factory *factory.Factory | ||
} | ||
|
||
func (r *ledger) Create(organizationID string, inp model.LedgerInput) (*model.LedgerCreate, error) { | ||
jsonData, err := json.Marshal(inp) | ||
if err != nil { | ||
return nil, fmt.Errorf("marshalling JSON: %v", err) | ||
} | ||
|
||
uri := fmt.Sprintf("%s/v1/organizations/%s/ledgers", r.Factory.Env.URLAPILedger, organizationID) | ||
|
||
req, err := http.NewRequest(http.MethodPost, uri, bytes.NewBuffer(jsonData)) | ||
if err != nil { | ||
return nil, errors.New("creating request: " + err.Error()) | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
req.Header.Set("Authorization", "Bearer "+r.Factory.Token) | ||
|
||
resp, err := r.Factory.HTTPClient.Do(req) | ||
if err != nil { | ||
return nil, errors.New("making POST request: " + err.Error()) | ||
} | ||
|
||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusCreated { | ||
if resp.StatusCode == http.StatusUnauthorized { | ||
return nil, errors.New("unauthorized invalid credentials") | ||
} | ||
|
||
return nil, fmt.Errorf("failed to create organization, status code: %d", | ||
resp.StatusCode) | ||
} | ||
|
||
var ledResp model.LedgerCreate | ||
if err := json.NewDecoder(resp.Body).Decode(&ledResp); err != nil { | ||
return nil, errors.New("decoding response JSON:" + err.Error()) | ||
} | ||
|
||
return &ledResp, nil | ||
} | ||
|
||
func NewLedger(f *factory.Factory) *ledger { | ||
return &ledger{f} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package rest | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/LerianStudio/midaz/components/mdz/internal/model" | ||
"github.com/LerianStudio/midaz/components/mdz/pkg/environment" | ||
"github.com/LerianStudio/midaz/components/mdz/pkg/factory" | ||
"github.com/LerianStudio/midaz/components/mdz/pkg/mockutil" | ||
"github.com/LerianStudio/midaz/components/mdz/pkg/ptr" | ||
"github.com/jarcoal/httpmock" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_ledger_Create(t *testing.T) { | ||
ledgerID := "0192e251-328d-7390-99f5-5c54980115ed" | ||
organizationID := "0192e250-ed9d-7e5c-a614-9b294151b572" | ||
|
||
name := "Romaguera and Sons" | ||
code := ptr.StringPtr("ACTIVE") | ||
description := ptr.StringPtr("Teste Ledger") | ||
bitcoin := ptr.StringPtr("1iR2KqpxRFjLsPUpWmpADMC7piRNsMAAjq") | ||
bool := ptr.BoolPtr(false) | ||
chave := ptr.StringPtr("metadata_chave") | ||
double := ptr.Float64Ptr(10.5) | ||
int := ptr.IntPtr(1) | ||
|
||
input := model.LedgerInput{ | ||
Name: name, | ||
Status: &model.LedgerStatus{ | ||
Code: code, | ||
Description: description, | ||
}, | ||
Metadata: &model.LedgerMetadata{ | ||
Bitcoin: bitcoin, | ||
Boolean: bool, | ||
Chave: chave, | ||
Double: double, | ||
Int: int, | ||
}, | ||
} | ||
|
||
expectedResult := &model.LedgerCreate{ | ||
ID: ledgerID, | ||
Name: name, | ||
OrganizationID: organizationID, | ||
Status: model.LedgerStatus{ | ||
Code: code, | ||
Description: description, | ||
}, | ||
Metadata: model.LedgerMetadata{ | ||
Bitcoin: bitcoin, | ||
Boolean: bool, | ||
Chave: chave, | ||
Double: double, | ||
Int: int, | ||
}, | ||
} | ||
|
||
client := &http.Client{} | ||
httpmock.ActivateNonDefault(client) | ||
defer httpmock.DeactivateAndReset() | ||
|
||
URIAPILedger := "http://127.0.0.1:3000" | ||
|
||
uri := fmt.Sprintf("%s/v1/organizations/%s/ledgers", URIAPILedger, organizationID) | ||
|
||
httpmock.RegisterResponder(http.MethodPost, uri, | ||
mockutil.MockResponseFromFile(http.StatusCreated, "./.fixtures/ledger_response.json")) | ||
|
||
factory := &factory.Factory{ | ||
HTTPClient: client, | ||
Env: &environment.Env{ | ||
URLAPILedger: URIAPILedger, | ||
}, | ||
} | ||
|
||
ledServ := NewLedger(factory) | ||
|
||
result, err := ledServ.Create(organizationID, input) | ||
|
||
assert.NoError(t, err) | ||
assert.NotNil(t, result) | ||
assert.Equal(t, expectedResult.ID, result.ID) | ||
assert.Equal(t, expectedResult.Name, result.Name) | ||
assert.Equal(t, expectedResult.OrganizationID, organizationID) | ||
assert.Equal(t, expectedResult.Status.Code, result.Status.Code) | ||
assert.Equal(t, expectedResult.Status.Description, result.Status.Description) | ||
assert.Equal(t, expectedResult.Metadata.Bitcoin, result.Metadata.Bitcoin) | ||
assert.Equal(t, expectedResult.Metadata.Boolean, result.Metadata.Boolean) | ||
assert.Equal(t, expectedResult.Metadata.Chave, result.Metadata.Chave) | ||
assert.Equal(t, expectedResult.Metadata.Double, result.Metadata.Double) | ||
assert.Equal(t, expectedResult.Metadata.Int, result.Metadata.Int) | ||
|
||
info := httpmock.GetCallCountInfo() | ||
assert.Equal(t, 1, info["POST http://127.0.0.1:3000/v1/organizations/0192e250-ed9d-7e5c-a614-9b294151b572/ledgers"]) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.