Skip to content

Commit

Permalink
Merge branch 'develop' into feature/MZ-617
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinezAvellan authored Nov 1, 2024
2 parents 5daafa6 + 32a00b1 commit 2c64b8b
Show file tree
Hide file tree
Showing 27 changed files with 1,243 additions and 180 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
## [1.26.0-beta.12](https://github.com/LerianStudio/midaz/compare/v1.26.0-beta.11...v1.26.0-beta.12) (2024-11-01)


### Features

* create rest list ledger ([88102a2](https://github.com/LerianStudio/midaz/commit/88102a215dbbeec4089560da09f4c644c4743784))
* create sub command list from ledger and test unit with output golden ([3d68791](https://github.com/LerianStudio/midaz/commit/3d68791977bb3ebfd8876de7c75d7c744bcb28f1))

## [1.26.0-beta.11](https://github.com/LerianStudio/midaz/compare/v1.26.0-beta.10...v1.26.0-beta.11) (2024-11-01)


### Features

* create comamnd ledger and sub command create ([d4a8538](https://github.com/LerianStudio/midaz/commit/d4a85386237e2ca040587591cc7c8a489d9c44dd))
* create rest create ledger ([a0435ac](https://github.com/LerianStudio/midaz/commit/a0435acd44ececb0977c90e92a402809b7348bad))
* test unit command create ledger ([93754de](https://github.com/LerianStudio/midaz/commit/93754deae69c2167e4ca9d3bc2def0b1fdd9e8ff))

## [1.26.0-beta.10](https://github.com/LerianStudio/midaz/compare/v1.26.0-beta.9...v1.26.0-beta.10) (2024-10-31)


Expand Down
4 changes: 4 additions & 0 deletions components/mdz/.env.example
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 modified components/mdz/bin/mdz
Binary file not shown.
8 changes: 8 additions & 0 deletions components/mdz/internal/domain/repository/ledger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package repository

import "github.com/LerianStudio/midaz/components/mdz/internal/model"

type Ledger interface {
Create(organizationID string, inp model.LedgerInput) (*model.LedgerCreate, error)
Get(organizationID string, limit, page int) (*model.LedgerList, error)
}
71 changes: 71 additions & 0 deletions components/mdz/internal/domain/repository/ledger_mock.go

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

50 changes: 50 additions & 0 deletions components/mdz/internal/model/ledger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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"`
}

type LedgerList struct {
Items []LedgerItems `json:"items"`
Page int `json:"page"`
Limit int `json:"limit"`
}

type LedgerItems struct {
ID string `json:"id"`
Name string `json:"name"`
OrganizationID string `json:"organizationId"`
Status *LedgerStatus `json:"status"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt *time.Time `json:"deletedAt"`
Metadata *LedgerMetadata `json:"metadata,omitempty"`
}
19 changes: 19 additions & 0 deletions components/mdz/internal/rest/.fixtures/ledger_response.json
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
}
}
75 changes: 75 additions & 0 deletions components/mdz/internal/rest/.fixtures/ledger_response_list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"items": [
{
"id": "0192e362-b270-7158-a647-7a59e4e26a27",
"name": "Ankunding - Paucek",
"organizationId": "0192e250-ed9d-7e5c-a614-9b294151b572",
"status": {
"code": "ACTIVE",
"description": "Teste Ledger"
},
"createdAt": "2024-10-31T16:22:29.232078Z",
"updatedAt": "2024-10-31T16:22:29.232078Z",
"deletedAt": null,
"metadata": {
"bitcoin": "3HH89s3LPALardk1jLt2PcjAJng",
"boolean": true,
"chave": "metadata_chave",
"double": 10.5,
"int": 1
}
},
{
"id": "0192e258-2c81-7e37-b6ba-a2007495c652",
"name": "Zieme - Mante",
"organizationId": "0192e250-ed9d-7e5c-a614-9b294151b572",
"status": {
"code": "ACTIVE",
"description": "Teste Ledger"
},
"createdAt": "2024-10-31T11:31:22.369928Z",
"updatedAt": "2024-10-31T11:31:22.369928Z",
"deletedAt": null,
"metadata": {
"bitcoinn": "329aaP47xTc8hQxXB92896U2RBXGEt",
"boolean": true,
"chave": "metadata_chave",
"double": 10.5,
"int": 1
}
},
{
"id": "0192e257-f5c0-7687-8534-303bae7aa4aa",
"name": "Lang LLC",
"organizationId": "0192e250-ed9d-7e5c-a614-9b294151b572",
"status": {
"code": "ACTIVE",
"description": null
},
"createdAt": "2024-10-31T11:31:08.352409Z",
"updatedAt": "2024-10-31T11:31:08.352409Z",
"deletedAt": null
},
{
"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.165229Z",
"updatedAt": "2024-10-31T11:23:45.165229Z",
"deletedAt": null,
"metadata": {
"bitcoinn": "1iR2KqpxRFjLsPUpWmpADMC7piRNsMAAjq",
"boolean": false,
"chave": "metadata_chave",
"double": 10.5,
"int": 1
}
}
],
"page": 1,
"limit": 5
}
95 changes: 95 additions & 0 deletions components/mdz/internal/rest/ledger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
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 (r *ledger) Get(organizationID string, limit, page int) (*model.LedgerList, error) {
uri := fmt.Sprintf("%s/v1/organizations/%s/ledgers?limit=%d&page=%d",
r.Factory.Env.URLAPILedger, organizationID, 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 POST request: " + err.Error())
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
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.LedgerList
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}
}
Loading

0 comments on commit 2c64b8b

Please sign in to comment.