diff --git a/components/mdz/.env.example b/components/mdz/.env.example new file mode 100644 index 00000000..fb0f53c5 --- /dev/null +++ b/components/mdz/.env.example @@ -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 diff --git a/components/mdz/bin/mdz b/components/mdz/bin/mdz index 139e6174..f85dcf03 100755 Binary files a/components/mdz/bin/mdz and b/components/mdz/bin/mdz differ diff --git a/components/mdz/internal/domain/repository/ledger.go b/components/mdz/internal/domain/repository/ledger.go new file mode 100644 index 00000000..8cdc5bbe --- /dev/null +++ b/components/mdz/internal/domain/repository/ledger.go @@ -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) +} diff --git a/components/mdz/internal/domain/repository/ledger_mock.go b/components/mdz/internal/domain/repository/ledger_mock.go new file mode 100644 index 00000000..ed554f26 --- /dev/null +++ b/components/mdz/internal/domain/repository/ledger_mock.go @@ -0,0 +1,56 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: /Users/maxwelbm/Workspace/midaz/components/mdz/internal/domain/repository/ledger.go +// +// Generated by this command: +// +// mockgen -source=/Users/maxwelbm/Workspace/midaz/components/mdz/internal/domain/repository/ledger.go -destination=/Users/maxwelbm/Workspace/midaz/components/mdz/internal/domain/repository/ledger_mock.go -package repository +// + +// Package repository is a generated GoMock package. +package repository + +import ( + reflect "reflect" + + model "github.com/LerianStudio/midaz/components/mdz/internal/model" + gomock "go.uber.org/mock/gomock" +) + +// MockLedger is a mock of Ledger interface. +type MockLedger struct { + ctrl *gomock.Controller + recorder *MockLedgerMockRecorder + isgomock struct{} +} + +// MockLedgerMockRecorder is the mock recorder for MockLedger. +type MockLedgerMockRecorder struct { + mock *MockLedger +} + +// NewMockLedger creates a new mock instance. +func NewMockLedger(ctrl *gomock.Controller) *MockLedger { + mock := &MockLedger{ctrl: ctrl} + mock.recorder = &MockLedgerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockLedger) EXPECT() *MockLedgerMockRecorder { + return m.recorder +} + +// Create mocks base method. +func (m *MockLedger) Create(organizationID string, inp model.LedgerInput) (*model.LedgerCreate, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Create", organizationID, inp) + ret0, _ := ret[0].(*model.LedgerCreate) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Create indicates an expected call of Create. +func (mr *MockLedgerMockRecorder) Create(organizationID, inp any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*MockLedger)(nil).Create), organizationID, inp) +} diff --git a/components/mdz/internal/model/ledger.go b/components/mdz/internal/model/ledger.go new file mode 100644 index 00000000..1862e41c --- /dev/null +++ b/components/mdz/internal/model/ledger.go @@ -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"` +} diff --git a/components/mdz/internal/rest/.fixtures/ledger_response.json b/components/mdz/internal/rest/.fixtures/ledger_response.json new file mode 100644 index 00000000..f87a7ca7 --- /dev/null +++ b/components/mdz/internal/rest/.fixtures/ledger_response.json @@ -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 + } +} diff --git a/components/mdz/internal/rest/ledger.go b/components/mdz/internal/rest/ledger.go new file mode 100644 index 00000000..7d03eaa5 --- /dev/null +++ b/components/mdz/internal/rest/ledger.go @@ -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} +} diff --git a/components/mdz/internal/rest/ledger_test.go b/components/mdz/internal/rest/ledger_test.go new file mode 100644 index 00000000..7bc6343f --- /dev/null +++ b/components/mdz/internal/rest/ledger_test.go @@ -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"]) +} diff --git a/components/mdz/paylaod_create.json b/components/mdz/paylaod_create.json deleted file mode 100644 index 539e44e8..00000000 --- a/components/mdz/paylaod_create.json +++ /dev/null @@ -1,20 +0,0 @@ -{ - "legalName": "Mx2", - "doingBusinessAs": "The ledger.io", - "legalDocument": "48784548000104", - "status": { - "code": "ACTIVE", - "description": "Teste Ledger" - }, - "address": { - "line1": "Avenida Paulista, 1234", - "line2": "CJ 203", - "zipCode": "04696040", - "country": "BR" - }, - "metadata": { - "chave": "metadata_chave", - "double": 10.5, - "int": 1 - } -} diff --git a/components/mdz/paylaod_upload.json b/components/mdz/paylaod_upload.json deleted file mode 100644 index f659847b..00000000 --- a/components/mdz/paylaod_upload.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "parentOrganizationId": "0192dac4-73ef-7af7-928f-3487cd3215bd", - "legalName": "Wisoky - Luettgen", - "doingBusinessAs": "The lsdff .io", - "legalDocument": "48784548000104", - "address": { - "line1": "", - "line2": null, - "zipCode": "", - "city": "", - "state": "", - "country": "NA" - }, - "status": { - "code": "", - "description": "Teste Blocked" - }, - "createdAt": "2024-10-29T23:06:41.946751Z", - "updatedAt": "2024-10-30T00:24:33.453089Z", - "deletedAt": null, - "metadata": { - "bitcoinn": "3AeMhmkRCL1kgrZaSWyNYZYJqydh5tcFY", - "boolean": false, - "chave": "metadata_chave_update", - "double": 10.5, - "int": 1 - } -} diff --git a/components/mdz/pkg/cmd/ledger/create.go b/components/mdz/pkg/cmd/ledger/create.go new file mode 100644 index 00000000..5219b11e --- /dev/null +++ b/components/mdz/pkg/cmd/ledger/create.go @@ -0,0 +1,211 @@ +package ledger + +import ( + "errors" + "fmt" + "strconv" + + "github.com/LerianStudio/midaz/components/mdz/internal/domain/repository" + "github.com/LerianStudio/midaz/components/mdz/internal/model" + "github.com/LerianStudio/midaz/components/mdz/internal/rest" + "github.com/LerianStudio/midaz/components/mdz/pkg/cmd/utils" + "github.com/LerianStudio/midaz/components/mdz/pkg/factory" + "github.com/LerianStudio/midaz/components/mdz/pkg/output" + "github.com/LerianStudio/midaz/components/mdz/pkg/tui" + "github.com/spf13/cobra" +) + +type factoryLedgerCreate struct { + factory *factory.Factory + repoLedger repository.Ledger + tuiInput func(message string) (string, error) + flagsCreate +} + +type flagsCreate struct { + OrganizationID string + Name string + Code string + Description string + Chave string + Bitcoin string + Boolean string + Double string + Int string + JSONFile string +} + +func (f *factoryLedgerCreate) runE(cmd *cobra.Command, _ []string) error { + led := model.LedgerInput{} + + if !cmd.Flags().Changed("organization-id") && len(f.OrganizationID) < 1 { + id, err := tui.Input("Enter your organization-id") + if err != nil { + return err + } + + f.OrganizationID = id + } + + if cmd.Flags().Changed("json-file") { + err := utils.FlagFileUnmarshalJSON(f.JSONFile, &led) + if err != nil { + return errors.New("failed to decode the given 'json' file. Verify if " + + "the file format is JSON or fix its content according to the JSON format " + + "specification at https://www.json.org/json-en.html") + } + } else { + err := f.createRequestFromFlags(&led) + if err != nil { + return err + } + } + + resp, err := f.repoLedger.Create(f.OrganizationID, led) + if err != nil { + return err + } + + output.Printf(f.factory.IOStreams.Out, + fmt.Sprintf("The ledger_id %s has been successfully created", resp.ID)) + + return nil +} + +func (f *factoryLedgerCreate) createRequestFromFlags(led *model.LedgerInput) error { + var err error + + led.Name, err = utils.AssignStringField(f.Name, "name", f.tuiInput) + if err != nil { + return err + } + + var status *model.LedgerStatus + + if len(f.Code) > 0 || len(f.Description) > 0 { + tempStatus := model.LedgerStatus{} + + if len(f.Code) > 0 { + tempStatus.Code = &f.Code + } + + if len(f.Description) > 0 { + tempStatus.Description = &f.Description + } + + status = &tempStatus + } + + led.Status = status + + metadata, err := buildMetadata(f) + if err != nil { + return err + } + + led.Metadata = metadata + + return nil +} + +func buildMetadata(f *factoryLedgerCreate) (*model.LedgerMetadata, error) { + if len(f.Chave) == 0 && len(f.Bitcoin) == 0 && len(f.Boolean) == 0 && len(f.Double) == 0 && len(f.Int) == 0 { + return nil, nil + } + + tempMetadata := model.LedgerMetadata{} + + if len(f.Chave) > 0 { + tempMetadata.Chave = &f.Chave + } + + if len(f.Bitcoin) > 0 { + tempMetadata.Bitcoin = &f.Bitcoin + } + + if len(f.Boolean) > 0 { + var err error + + tempMetadata.Boolean, err = utils.ParseAndAssign(f.Boolean, strconv.ParseBool) + if err != nil { + return nil, fmt.Errorf("invalid boolean field: %v", err) + } + } + + if len(f.Double) > 0 { + var err error + tempMetadata.Double, err = utils.ParseAndAssign(f.Double, func(s string) (float64, error) { + return strconv.ParseFloat(s, 64) + }) + + if err != nil { + return nil, fmt.Errorf("invalid double field: %v", err) + } + } + + if len(f.Int) > 0 { + var err error + + tempMetadata.Int, err = utils.ParseAndAssign(f.Int, strconv.Atoi) + if err != nil { + return nil, fmt.Errorf("invalid int field: %v", err) + } + } + + return &tempMetadata, nil +} + +func (f *factoryLedgerCreate) setFlags(cmd *cobra.Command) { + cmd.Flags().StringVar(&f.OrganizationID, + "organization-id", "", "Specify the organization ID.") + cmd.Flags().StringVar(&f.Name, "name", "", + "name new ledger your organization") + cmd.Flags().StringVar(&f.Code, "code", "", + "code for the organization (e.g., ACTIVE).") + cmd.Flags().StringVar(&f.Description, "description", "", + "Description of the current status of the ledger.") + cmd.Flags().StringVar(&f.Chave, "chave", "", + "Custom metadata key for the ledger.") + cmd.Flags().StringVar(&f.Bitcoin, "bitcoin", "", + "Bitcoin address or value associated with the ledger.") + cmd.Flags().StringVar(&f.Boolean, "boolean", "", + "Boolean metadata for custom use.") + cmd.Flags().StringVar(&f.Double, "double", "", + "Floating-point number metadata for custom use.") + cmd.Flags().StringVar(&f.Int, "int", "", + "Integer metadata for custom use.") + cmd.Flags().StringVar(&f.JSONFile, "json-file", "", + `Path to a JSON file containing the attributes of the Ledger being + created; you can use - for reading from stdin`) + cmd.Flags().BoolP("help", "h", false, + "Displays more information about the Mdz CLI") +} + +func newInjectFacCreate(f *factory.Factory) *factoryLedgerCreate { + return &factoryLedgerCreate{ + factory: f, + repoLedger: rest.NewLedger(f), + tuiInput: tui.Input, + } +} + +func newCmdLedgerCreate(f *factoryLedgerCreate) *cobra.Command { + cmd := &cobra.Command{ + Use: "create", + Short: "Creates a new ledger for the organization", + Long: `It creates a ledger within the organization, allowing the creation + of multiple records to separate and organize transactions by context, + such as geographical location or business units.`, + Example: utils.Format( + "$ mdz ledger create", + "$ mdz ledger create -h", + "$ mdz ledger create --json-file payload.json", + "$ cat payload.json | mdz ledger create --json-file -", + ), + RunE: f.runE, + } + + f.setFlags(cmd) + + return cmd +} diff --git a/components/mdz/pkg/cmd/ledger/create_test.go b/components/mdz/pkg/cmd/ledger/create_test.go new file mode 100644 index 00000000..2cfd9f44 --- /dev/null +++ b/components/mdz/pkg/cmd/ledger/create_test.go @@ -0,0 +1,93 @@ +package ledger + +import ( + "bytes" + "testing" + + "github.com/LerianStudio/midaz/components/mdz/internal/domain/repository" + "github.com/LerianStudio/midaz/components/mdz/internal/model" + "github.com/LerianStudio/midaz/components/mdz/pkg/factory" + "github.com/LerianStudio/midaz/components/mdz/pkg/iostreams" + "github.com/LerianStudio/midaz/components/mdz/pkg/ptr" + "github.com/stretchr/testify/assert" + "go.uber.org/mock/gomock" +) + +func Test_newCmdLedgerCreate(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockRepo := repository.NewMockLedger(ctrl) + + ledgerID := "0192e251-328d-7390-99f5-5c54980115ed" + organizationID := "0192e250-ed9d-7e5c-a614-9b294151b572" + + name := "Romaguera and Sons" + code := "ACTIVE" + description := "Teste Ledger" + bitcoin := "1iR2KqpxRFjLsPUpWmpADMC7piRNsMAAjq" + bool := "false" + chave := "metadata_chave" + double := "10.5" + int := "1" + + orgFactory := factoryLedgerCreate{ + factory: &factory.Factory{IOStreams: &iostreams.IOStreams{ + Out: &bytes.Buffer{}, + Err: &bytes.Buffer{}, + }}, + repoLedger: mockRepo, + tuiInput: func(message string) (string, error) { + return name, nil + }, + flagsCreate: flagsCreate{ + OrganizationID: organizationID, + Name: name, + Code: code, + Description: description, + Bitcoin: bitcoin, + Boolean: bool, + Chave: chave, + Double: double, + Int: int, + }, + } + + cmd := newCmdLedgerCreate(&orgFactory) + cmd.SetArgs([]string{ + "--organization-id", organizationID, + "--name", name, + "--code", code, + "--description", description, + "--bitcoin", bitcoin, + "--boolean", bool, + "--chave", chave, + "--double", double, + "--int", int, + }) + + result := &model.LedgerCreate{ + ID: ledgerID, + Name: name, + OrganizationID: organizationID, + Status: model.LedgerStatus{ + Code: ptr.StringPtr(code), + Description: ptr.StringPtr(description), + }, + Metadata: model.LedgerMetadata{ + Bitcoin: ptr.StringPtr(bitcoin), + Boolean: ptr.BoolPtr(true), + Chave: ptr.StringPtr(chave), + Double: ptr.Float64Ptr(10.5), + Int: ptr.IntPtr(1), + }, + } + + mockRepo.EXPECT().Create(gomock.Any(), gomock.Any()).Return(result, nil) + err := cmd.Execute() + assert.NoError(t, err) + + output := orgFactory.factory.IOStreams.Out.(*bytes.Buffer).String() + assert.Contains(t, output, "The ledger_id 0192e251-328d-7390-99f5-5c54980115ed has been successfully created") + +} diff --git a/components/mdz/pkg/cmd/ledger/ledger.go b/components/mdz/pkg/cmd/ledger/ledger.go new file mode 100644 index 00000000..e0091bf8 --- /dev/null +++ b/components/mdz/pkg/cmd/ledger/ledger.go @@ -0,0 +1,38 @@ +package ledger + +import ( + "github.com/LerianStudio/midaz/components/mdz/pkg/cmd/utils" + "github.com/LerianStudio/midaz/components/mdz/pkg/factory" + "github.com/spf13/cobra" +) + +type factoryLedger struct { + factory *factory.Factory +} + +func (f *factoryLedger) setCmds(cmd *cobra.Command) { + cmd.AddCommand(newCmdLedgerCreate(newInjectFacCreate(f.factory))) +} + +func NewCmdLedger(f *factory.Factory) *cobra.Command { + fOrg := factoryLedger{ + factory: f, + } + cmd := &cobra.Command{ + Use: "ledger", + Short: "Manages ledgers to organize transactions within an organization", + Long: `The ledger command allows you to create and manage financial records + called ledgers, which store all the transactions and operations + of an organization. Each organization can have multiple ledgers, + allowing you to segment records as needed, for example, + by country or by project.`, + Example: utils.Format( + "$ mdz ledger", + "$ mdz ledger -h", + ), + } + cmd.Flags().BoolP("help", "h", false, "Displays more information about the Midaz CLI") + fOrg.setCmds(cmd) + + return cmd +} diff --git a/components/mdz/pkg/cmd/organization/update.go b/components/mdz/pkg/cmd/organization/update.go index 419d719f..5e291bc1 100644 --- a/components/mdz/pkg/cmd/organization/update.go +++ b/components/mdz/pkg/cmd/organization/update.go @@ -47,7 +47,7 @@ type flagsUpdate struct { func (f *factoryOrganizationUpdate) runE(cmd *cobra.Command, _ []string) error { org := model.OrganizationUpdate{} - if !cmd.Flags().Changed("username") && len(f.OrganizationID) < 1 { + if !cmd.Flags().Changed("organization-id") && len(f.OrganizationID) < 1 { id, err := tui.Input("Enter your organization-id") if err != nil { return err @@ -167,7 +167,7 @@ func buildMetadata(f *factoryOrganizationUpdate) (*model.Metadata, error) { tempMetadata.Boolean, err = utils.ParseAndAssign(f.Boolean, strconv.ParseBool) if err != nil { - return nil, fmt.Errorf("campo boolean inválido: %v", err) + return nil, fmt.Errorf("invalid boolean field: %v", err) } } @@ -178,7 +178,7 @@ func buildMetadata(f *factoryOrganizationUpdate) (*model.Metadata, error) { }) if err != nil { - return nil, fmt.Errorf("campo double inválido: %v", err) + return nil, fmt.Errorf("invalid double field: %v", err) } } @@ -187,7 +187,7 @@ func buildMetadata(f *factoryOrganizationUpdate) (*model.Metadata, error) { tempMetadata.Int, err = utils.ParseAndAssign(f.Int, strconv.Atoi) if err != nil { - return nil, fmt.Errorf("campo int inválido: %v", err) + return nil, fmt.Errorf("invalid int field: %v", err) } } diff --git a/components/mdz/pkg/cmd/root/root.go b/components/mdz/pkg/cmd/root/root.go index f6491121..2731286b 100644 --- a/components/mdz/pkg/cmd/root/root.go +++ b/components/mdz/pkg/cmd/root/root.go @@ -3,6 +3,7 @@ package root import ( "errors" + "github.com/LerianStudio/midaz/components/mdz/pkg/cmd/ledger" "github.com/LerianStudio/midaz/components/mdz/pkg/cmd/login" "github.com/LerianStudio/midaz/components/mdz/pkg/cmd/organization" "github.com/LerianStudio/midaz/components/mdz/pkg/cmd/utils" @@ -21,6 +22,7 @@ func (f *factoryRoot) setCmds(cmd *cobra.Command) { cmd.AddCommand(version.NewCmdVersion(f.factory)) cmd.AddCommand(login.NewCmdLogin(f.factory)) cmd.AddCommand(organization.NewCmdOrganization(f.factory)) + cmd.AddCommand(ledger.NewCmdLedger(f.factory)) } func (f *factoryRoot) setFlags(cmd *cobra.Command) { diff --git a/components/mdz/pkg/mockutil/mockutil.go b/components/mdz/pkg/mockutil/mockutil.go new file mode 100644 index 00000000..f42cd89e --- /dev/null +++ b/components/mdz/pkg/mockutil/mockutil.go @@ -0,0 +1,24 @@ +package mockutil + +import ( + "net/http" + "os" + "path/filepath" + + "github.com/jarcoal/httpmock" +) + +func loadJSONResponse(filename string) ([]byte, error) { + filename = filepath.Clean(filename) + return os.ReadFile(filename) +} + +func MockResponseFromFile(status int, path string) httpmock.Responder { + data, err := loadJSONResponse(path) + if err != nil { + return httpmock.NewStringResponder(http.StatusInternalServerError, + "Failed to load mock response") + } + + return httpmock.NewBytesResponder(status, data) +}