Skip to content

Commit

Permalink
feat: create sub command delete from products
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwelbm committed Nov 14, 2024
1 parent 4368bc2 commit 80d3a62
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 2 deletions.
1 change: 1 addition & 0 deletions components/mdz/internal/domain/repository/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ type Product interface {
Get(organizationID, ledgerID string, limit, page int) (*mmodel.Products, error)
GetByID(organizationID, ledgerID, productID string) (*mmodel.Product, error)
Update(organizationID, ledgerID, productID string, inp mmodel.UpdateProductInput) (*mmodel.Product, error)
Delete(organizationID, ledgerID, productID string) error
}
14 changes: 14 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.

26 changes: 26 additions & 0 deletions components/mdz/internal/rest/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,32 @@ func (r *product) Update(
return &productResp, nil
}

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

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 Delete request: " + err.Error())
}

defer resp.Body.Close()

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

return nil
}

func NewProduct(f *factory.Factory) *product {
return &product{f}
}
33 changes: 33 additions & 0 deletions components/mdz/internal/rest/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,36 @@ func Test_product_Update(t *testing.T) {
info := httpmock.GetCallCountInfo()
assert.Equal(t, 1, info["PATCH "+uri])
}

func Test_product_Delete(t *testing.T) {
productID := "01930219-2c25-7a37-a5b9-610d44ae0a27"
ledgerID := "0192fc1e-14bf-7894-b167-6e4a878b3a95"
organizationID := "0192fc1d-f34d-78c9-9654-83e497349241"
URIAPILedger := "http://127.0.0.1:3000"

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.MethodDelete, uri,
httpmock.NewStringResponder(http.StatusNoContent, ""))

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

product := NewProduct(factory)

err := product.Delete(organizationID, ledgerID, productID)

assert.NoError(t, err)

info := httpmock.GetCallCountInfo()
assert.Equal(t, 1, info["DELETE "+uri])
}
4 changes: 2 additions & 2 deletions components/mdz/pkg/cmd/portfolio/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (f *factoryPortfolioDelete) ensureFlagInput(cmd *cobra.Command) error {
f.LedgerID = id
}

if !cmd.Flags().Changed("asset-id") && len(f.PortfolioID) < 1 {
id, err := tui.Input("Enter your asset-id")
if !cmd.Flags().Changed("portfolio-id") && len(f.PortfolioID) < 1 {
id, err := tui.Input("Enter your portfolio-id")
if err != nil {
return err
}
Expand Down
107 changes: 107 additions & 0 deletions components/mdz/pkg/cmd/product/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package product

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 factoryProductDelete struct {
factory *factory.Factory
repoProduct repository.Product
tuiInput func(message string) (string, error)
OrganizationID string
LedgerID string
ProductID string
}

func (f *factoryProductDelete) ensureFlagInput(cmd *cobra.Command) error {
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("ledger-id") && len(f.LedgerID) < 1 {
id, err := tui.Input("Enter your ledger-id")
if err != nil {
return err
}

f.LedgerID = id
}

if !cmd.Flags().Changed("product-id") && len(f.ProductID) < 1 {
id, err := tui.Input("Enter your product-id")
if err != nil {
return err
}

f.ProductID = id
}

return nil
}

func (f *factoryProductDelete) runE(cmd *cobra.Command, _ []string) error {
if err := f.ensureFlagInput(cmd); err != nil {
return err
}

err := f.repoProduct.Delete(f.OrganizationID, f.LedgerID, f.ProductID)
if err != nil {
return err
}

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

return nil
}

func (f *factoryProductDelete) setFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&f.OrganizationID, "organization-id", "", "Specify the organization ID.")
cmd.Flags().StringVar(&f.LedgerID, "ledger-id", "", "Specify the ledger ID")
cmd.Flags().StringVar(&f.ProductID, "product-id", "", "Specify the portfolio ID")
cmd.Flags().BoolP("help", "h", false, "Displays more information about the Mdz CLI")
}

func newInjectFacDelete(f *factory.Factory) *factoryProductDelete {
return &factoryProductDelete{
factory: f,
repoProduct: rest.NewProduct(f),
tuiInput: tui.Input,
}
}

func newCmdProductDelete(f *factoryProductDelete) *cobra.Command {
cmd := &cobra.Command{
Use: "delete",
Short: "Removes an existing product.",
Long: utils.Format(
"The delete subcommand allows you to delete a product, removing its",
"settings and clustering rules. It is useful for deactivating obsolete",
"clusters or adjusting the organization of products without changing",
"the structure of customers.",
),
Example: utils.Format(
"$ mdz product delete --organization-id '1234' --ledger-id '4421' --product-id '55232'",
"$ mdz product delete -i 12314",
"$ mdz product delete -h",
),
RunE: f.runE,
}

f.setFlags(cmd)

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

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 Test_newCmdProductDelete(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockRepo := repository.NewMockProduct(ctrl)

factory := factoryProductDelete{
factory: &factory.Factory{IOStreams: &iostreams.IOStreams{
Out: &bytes.Buffer{},
Err: &bytes.Buffer{},
}},
repoProduct: mockRepo,
OrganizationID: "321",
LedgerID: "123",
ProductID: "444",
}

cmd := newCmdProductDelete(&factory)
cmd.SetArgs([]string{
"--organization-id", "321",
"--ledger-id", "123",
"--product-id", "444",
})

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

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

output := factory.factory.IOStreams.Out.(*bytes.Buffer).String()
assert.Contains(t, output, "The Product ID 444 has been successfully deleted.")
}
1 change: 1 addition & 0 deletions components/mdz/pkg/cmd/product/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func (f *factoryProduct) setCmds(cmd *cobra.Command) {
cmd.AddCommand(newCmdProductList(newInjectFacList(f.factory)))
cmd.AddCommand(newCmdProductDescribe(newInjectFacDescribe(f.factory)))
cmd.AddCommand(newCmdProductUpdate(newInjectFacUpdate(f.factory)))
cmd.AddCommand(newCmdProductDelete(newInjectFacDelete(f.factory)))
}

func NewCmdProduct(f *factory.Factory) *cobra.Command {
Expand Down

0 comments on commit 80d3a62

Please sign in to comment.