Skip to content

Commit

Permalink
Merge pull request #173 from LerianStudio/feature/MZ-606
Browse files Browse the repository at this point in the history
Feature/MZ-606
  • Loading branch information
MartinezAvellan authored Oct 30, 2024
2 parents f457ebb + 0964230 commit b40e982
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 3 deletions.
Binary file modified components/mdz/bin/mdz
Binary file not shown.
1 change: 1 addition & 0 deletions components/mdz/internal/domain/repository/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ type Organization interface {
Get(limit, page int) (*model.OrganizationList, error)
GetByID(organizationID string) (*model.OrganizationItem, error)
Update(organizationID string, orgInput model.OrganizationUpdate) (*model.OrganizationItem, error)
Delete(organizationID string) error
}
14 changes: 14 additions & 0 deletions components/mdz/internal/domain/repository/organization_mock.go

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

30 changes: 30 additions & 0 deletions components/mdz/internal/rest/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,36 @@ func (r *organization) Update(organizationID string, orgInput model.Organization
return &orgItemResp, nil
}

func (r *organization) Delete(organizationID string) error {
uri := fmt.Sprintf("%s/v1/organizations/%s", r.Factory.Env.URLAPILedger, organizationID)

req, err := http.NewRequest(http.MethodDelete, uri, nil)
if err != nil {
return 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 errors.New("making GET request: " + err.Error())
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusNoContent {
if resp.StatusCode == http.StatusUnauthorized {
return errors.New("unauthorized invalid credentials")
}

return fmt.Errorf("failed to update organization, status code: %d",
resp.StatusCode)
}

return nil
}

func NewOrganization(f *factory.Factory) *organization {
return &organization{f}
}
28 changes: 28 additions & 0 deletions components/mdz/internal/rest/organization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,31 @@ func TestOrganizationUpdate(t *testing.T) {
info := httpmock.GetCallCountInfo()
assert.Equal(t, 1, info["PATCH http://127.0.0.1:3000/v1/organizations/1a259e90-8f28-491d-8c09-c047293b1a0f"])
}

func Test_organization_Delete(t *testing.T) {
client := &http.Client{}
httpmock.ActivateNonDefault(client)
defer httpmock.DeactivateAndReset()

URIAPILedger := "http://127.0.0.1:3000"
organizationID := "1a259e90-8f28-491d-8c09-c047293b1a0f"

httpmock.RegisterResponder(http.MethodDelete, URIAPILedger+"/v1/organizations/"+organizationID,
httpmock.NewStringResponder(http.StatusNoContent, ""))

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

orgService := NewOrganization(factory)

err := orgService.Delete(organizationID)

assert.NoError(t, err)

info := httpmock.GetCallCountInfo()
assert.Equal(t, 1, info["DELETE http://127.0.0.1:3000/v1/organizations/1a259e90-8f28-491d-8c09-c047293b1a0f"])
}
74 changes: 74 additions & 0 deletions components/mdz/pkg/cmd/organization/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package organization

import (
"fmt"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"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 factoryOrganizationDelete struct {
factory *factory.Factory
repoOrganization repository.Organization
tuiInput func(message string) (string, error)
organizationID string
}

func (f *factoryOrganizationDelete) runE(cmd *cobra.Command, _ []string) error {
if !cmd.Flags().Changed("username") && len(f.organizationID) < 1 {
id, err := tui.Input("Enter your organization-id")
if err != nil {
return err
}

f.organizationID = id
}

err := f.repoOrganization.Delete(f.organizationID)
if err != nil {
return err
}

output.Printf(f.factory.IOStreams.Out,
fmt.Sprintf("The Organization %s has been successfully deleted.", f.organizationID))

return nil
}

func (f *factoryOrganizationDelete) setFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&f.organizationID, "organization-id", "i", "",
"Specify the organization ID to delete")
cmd.Flags().BoolP("help", "h", false, "Displays more information about the Mdz CLI")
}

func newInjectFacDelete(f *factory.Factory) *factoryOrganizationDelete {
return &factoryOrganizationDelete{
factory: f,
repoOrganization: rest.NewOrganization(f),
tuiInput: tui.Input,
}
}

func newCmdOrganizationDelete(f *factoryOrganizationDelete) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Remove a specific organization in Midaz",
Long: "The /`organization delete/` command allows you to remove a specific organization in Midaz " +
"by specifying the organization ID.",
Example: utils.Format(
"$ mdz organization delete --organization-id 12312",
"$ mdz organization delete -i 12314",
"$ mdz organization delete -h",
),
RunE: f.runE,
}

f.setFlags(cmd)

return cmd
}
55 changes: 55 additions & 0 deletions components/mdz/pkg/cmd/organization/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package organization

import (
"bytes"
"testing"

"github.com/LerianStudio/midaz/components/mdz/internal/domain/repository"
"github.com/LerianStudio/midaz/components/mdz/pkg/factory"
"github.com/LerianStudio/midaz/components/mdz/pkg/iostreams"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func TestNewCmdOrganizationDelete(t *testing.T) {
tests := []struct {
name string
runTest func(t *testing.T)
}{
{
name: "happy road",
runTest: func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockRepo := repository.NewMockOrganization(ctrl)

orgFactory := factoryOrganizationDelete{
factory: &factory.Factory{IOStreams: &iostreams.IOStreams{
Out: &bytes.Buffer{},
Err: &bytes.Buffer{},
}},
repoOrganization: mockRepo,
organizationID: "123",
}

cmd := newCmdOrganizationDelete(&orgFactory)
cmd.SetArgs([]string{
"--organization-id", "123",
})

mockRepo.EXPECT().Delete(gomock.Any()).Return(nil)

err := cmd.Execute()
assert.NoError(t, err)

output := orgFactory.factory.IOStreams.Out.(*bytes.Buffer).String()
assert.Contains(t, output, "The Organization 123 has been successfully deleted.")
},
},
}

for _, tc := range tests {
t.Run(tc.name, tc.runTest)
}
}
1 change: 1 addition & 0 deletions components/mdz/pkg/cmd/organization/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func (f *factoryOrganization) setCmds(cmd *cobra.Command) {
cmd.AddCommand(newCmdOrganizationList(newInjectFacList(f.factory)))
cmd.AddCommand(newCmdOrganizationDescribe(newInjectFacDescribe(f.factory)))
cmd.AddCommand(newCmdOrganizationUpdate(newInjectFacUpdate(f.factory)))
cmd.AddCommand(newCmdOrganizationDelete(newInjectFacDelete(f.factory)))
}

func NewCmdOrganization(f *factory.Factory) *cobra.Command {
Expand Down
6 changes: 3 additions & 3 deletions components/mdz/pkg/cmd/organization/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@ func newCmdOrganizationUpdate(f *factoryOrganizationUpdate) *cobra.Command {
"$ mdz organization update",
"$ mdz organization update -h",
"$ mdz organization update --json-file payload.json",
"$ cat payload.json | mdz organization Update --json-file -",
"$ echo '{...}' | mdz organization Update --json-file -",
"$ mdz organization Update --legal-name 'Gislason LLCT' --doing-business-as 'The ledger.io' --legal-document '48784548000104' --code 'ACTIVE' --description 'Test Ledger' --line1 'Av Santso' --line2 'VJ 222' --zip-code '04696040' --city 'West' --state 'VJ' --country 'MG' --bitcoin '1YLHctiipHZupwrT5sGwuYbks5rn64bm' --boolean true --chave 'metadata_chave' --double '10.5' --int 1",
"$ cat payload.json | mdz organization Update --organization-id '1234' --json-file -",
"$ echo '{...}' | mdz organization Update --organization-id '1234' --json-file -",
"$ mdz organization update --organization-id '1234' --legal-name 'Gislason LLCT' --doing-business-as 'The ledger.io' --legal-document '48784548000104' --code 'ACTIVE' --description 'Test Ledger' --line1 'Av Santso' --line2 'VJ 222' --zip-code '04696040' --city 'West' --state 'VJ' --country 'MG' --bitcoin '1YLHctiipHZupwrT5sGwuYbks5rn64bm' --boolean true --chave 'metadata_chave' --double '10.5' --int 1",
),
RunE: f.runE,
}
Expand Down

0 comments on commit b40e982

Please sign in to comment.