Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/MIDAZ-249 #258

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions components/mdz/internal/domain/repository/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ import "github.com/LerianStudio/midaz/common/mmodel"

type Product interface {
Create(organizationID, ledgerID string, inp mmodel.CreateProductInput) (*mmodel.Product, error)
Get(organizationID, ledgerID string, limit, page int) (*mmodel.Products, error)
GetByID(organizationID, ledgerID, productID string) (*mmodel.Product, error)
}
30 changes: 30 additions & 0 deletions components/mdz/internal/domain/repository/product_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,13 @@
{
"id": "01932727-1b5a-7540-98c0-6521ffe78ce6",
"name": "Product Licensed Concrete Hat",
"ledgerId": "01932715-9f93-7432-90c3-4352bcfe464d",
"organizationId": "01931b04-964a-7caa-a422-c29a95387c00",
"status": {
"code": "ACTIVE",
"description": "Teste Product"
},
"createdAt": "2024-11-13T20:11:34.617671Z",
"updatedAt": "2024-11-13T20:11:34.617674Z",
"deletedAt": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"items": [
{
"id": "01932727-1b5a-7540-98c0-6521ffe78ce6",
"name": "Product Licensed Concrete Hat",
"ledgerId": "01932715-9f93-7432-90c3-4352bcfe464d",
"organizationId": "01931b04-964a-7caa-a422-c29a95387c00",
"status": {
"code": "ACTIVE",
"description": "Teste Product"
},
"createdAt": "2024-11-13T20:11:34.617671Z",
"updatedAt": "2024-11-13T20:11:34.617674Z",
"deletedAt": null
},
{
"id": "0193271b-877f-7c98-a5a6-43b664d68982",
"name": "Product Refined Cotton Chair",
"ledgerId": "01932715-9f93-7432-90c3-4352bcfe464d",
"organizationId": "01931b04-964a-7caa-a422-c29a95387c00",
"status": {
"code": "ACTIVE",
"description": "Teste Product"
},
"createdAt": "2024-11-13T19:58:55.868976Z",
"updatedAt": "2024-11-13T19:58:55.868979Z",
"deletedAt": null,
"metadata": {
"bitcoin": "3g9ofZcD7KRWL44BWdNa3PyM4PfzgqDG5P",
"chave": "metadata_chave",
"boolean": true
}
}
],
"page": 1,
"limit": 5
}
60 changes: 60 additions & 0 deletions components/mdz/internal/rest/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,66 @@ func (r *product) Create(organizationID, ledgerID string, inp mmodel.CreateProdu
return &productResp, nil
}

func (r *product) Get(organizationID, ledgerID string, limit, page int) (*mmodel.Products, error) {
uri := fmt.Sprintf("%s/v1/organizations/%s/ledgers/%s/products?limit=%d&page=%d",
r.Factory.Env.URLAPILedger, organizationID, ledgerID, limit, page)

req, err := http.NewRequest(http.MethodGet, uri, nil)
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 GET request: " + err.Error())
}
defer resp.Body.Close()

if err := checkResponse(resp, http.StatusOK); err != nil {
return nil, err
}

var productsResp mmodel.Products
if err := json.NewDecoder(resp.Body).Decode(&productsResp); err != nil {
return nil, errors.New("decoding response JSON:" + err.Error())
}

return &productsResp, nil
}

func (r *product) GetByID(organizationID, ledgerID, productID string) (*mmodel.Product, error) {
uri := fmt.Sprintf("%s/v1/organizations/%s/ledgers/%s/products/%s",
r.Factory.Env.URLAPILedger, organizationID, ledgerID, productID)

req, err := http.NewRequest(http.MethodGet, uri, nil)
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 GET request: " + err.Error())
}
defer resp.Body.Close()

if err := checkResponse(resp, http.StatusOK); err != nil {
return nil, err
}

var productResp mmodel.Product
if err := json.NewDecoder(resp.Body).Decode(&productResp); err != nil {
return nil, errors.New("decoding response JSON:" + err.Error())
}

return &productResp, nil
}

func NewProduct(f *factory.Factory) *product {
return &product{f}
}
142 changes: 142 additions & 0 deletions components/mdz/internal/rest/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"testing"
"time"

"github.com/LerianStudio/midaz/common/mmodel"
"github.com/LerianStudio/midaz/components/mdz/pkg/environment"
Expand Down Expand Up @@ -86,3 +87,144 @@ func Test_product_Create(t *testing.T) {
info := httpmock.GetCallCountInfo()
assert.Equal(t, 1, info["POST "+uri])
}

func Test_product_Get(t *testing.T) {
organizationID := "01931b04-964a-7caa-a422-c29a95387c00"
ledgerID := "01931b04-c2d1-7a41-83ac-c5d6d8a3c22c"

limit := 2
page := 1

expectedResult := mmodel.Products{
Page: page,
Limit: limit,
Items: []mmodel.Product{
{
ID: "01932727-1b5a-7540-98c0-6521ffe78ce6",
Name: "Product Licensed Concrete Hat",
Status: mmodel.Status{
Code: "ACTIVE",
Description: ptr.StringPtr("Teste Product"),
},
OrganizationID: organizationID,
LedgerID: ledgerID,
CreatedAt: time.Date(2024, 11, 11, 18, 51, 33, 15793, time.UTC),
UpdatedAt: time.Date(2024, 11, 11, 18, 51, 33, 15793, time.UTC),
DeletedAt: nil,
},
{
ID: "0193271b-877f-7c98-a5a6-43b664d68982",
Name: "Toy30 Portfolio",
Status: mmodel.Status{
Code: "ACTIVE",
Description: ptr.StringPtr("Teste Product"),
},
OrganizationID: organizationID,
LedgerID: ledgerID,
CreatedAt: time.Date(2024, 11, 11, 18, 51, 27, 447406, time.UTC),
UpdatedAt: time.Date(2024, 11, 11, 18, 51, 27, 447406, time.UTC),
DeletedAt: nil,
Metadata: map[string]any{
"bitcoin": "3g9ofZcD7KRWL44BWdNa3PyM4PfzgqDG5P",
"chave": "metadata_chave",
"boolean": true,
},
},
},
}

client := &http.Client{}
httpmock.ActivateNonDefault(client)
defer httpmock.DeactivateAndReset()

URIAPILedger := "http://127.0.0.1:3000"

uri := fmt.Sprintf("%s/v1/organizations/%s/ledgers/%s/products?limit=%d&page=%d",
URIAPILedger, organizationID, ledgerID, limit, page)

httpmock.RegisterResponder(http.MethodGet, uri,
mockutil.MockResponseFromFile(http.StatusOK, "./.fixtures/product_response_list.json"))

factory := &factory.Factory{
HTTPClient: client,
Env: &environment.Env{
URLAPILedger: URIAPILedger,
},
}

product := NewProduct(factory)

result, err := product.Get(organizationID, ledgerID, limit, page)

assert.NoError(t, err)
assert.NotNil(t, result)

for i, v := range result.Items {
assert.Equal(t, expectedResult.Items[i].ID, v.ID)
assert.Equal(t, expectedResult.Items[i].Metadata, v.Metadata)
}

assert.Equal(t, expectedResult.Limit, limit)
assert.Equal(t, expectedResult.Page, page)

info := httpmock.GetCallCountInfo()
assert.Equal(t, 1, info["GET "+uri])
}

func Test_product_GetByID(t *testing.T) {
productID := "01932727-1b5a-7540-98c0-6521ffe78ce6"
ledgerID := "01932715-9f93-7432-90c3-4352bcfe464d"
organizationID := "01931b04-964a-7caa-a422-c29a95387c00"

URIAPILedger := "http://127.0.0.1:3000"

expectedResult := &mmodel.Product{
ID: productID,
Name: "Product Licensed Concrete Hat",
LedgerID: ledgerID,
OrganizationID: organizationID,
Status: mmodel.Status{
Code: "ACTIVE",
Description: ptr.StringPtr("Teste Product"),
},
CreatedAt: time.Date(2024, 11, 13, 20, 11, 34, 617671000, time.UTC),
UpdatedAt: time.Date(2024, 11, 13, 20, 11, 34, 617674000, time.UTC),
DeletedAt: nil,
}

client := &http.Client{}
httpmock.ActivateNonDefault(client)
defer httpmock.DeactivateAndReset()

uri := fmt.Sprintf("%s/v1/organizations/%s/ledgers/%s/products/%s",
URIAPILedger, organizationID, ledgerID, productID)

httpmock.RegisterResponder(http.MethodGet, uri,
mockutil.MockResponseFromFile(http.StatusOK, "./.fixtures/product_response_get_by_id.json"))

factory := &factory.Factory{
HTTPClient: client,
Env: &environment.Env{
URLAPILedger: URIAPILedger,
},
}

product := NewProduct(factory)

result, err := product.GetByID(organizationID, ledgerID, productID)
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, result.OrganizationID)
assert.Equal(t, expectedResult.LedgerID, result.LedgerID)
assert.Equal(t, expectedResult.Status.Code, result.Status.Code)
assert.Equal(t, expectedResult.Status.Description, result.Status.Description)
assert.Equal(t, expectedResult.CreatedAt, result.CreatedAt)
assert.Equal(t, expectedResult.UpdatedAt, result.UpdatedAt)
assert.Equal(t, expectedResult.DeletedAt, result.DeletedAt)
assert.Equal(t, expectedResult.Metadata, result.Metadata)

info := httpmock.GetCallCountInfo()
assert.Equal(t, 1, info["GET "+uri])
}
Loading
Loading