-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create sub command delete from products
- Loading branch information
Showing
8 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters