Skip to content

Commit

Permalink
feat: create sub command update from products
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwelbm committed Nov 14, 2024
1 parent 2ab7733 commit 4368bc2
Show file tree
Hide file tree
Showing 8 changed files with 382 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 @@ -6,4 +6,5 @@ type Product interface {
Create(organizationID, ledgerID string, inp mmodel.CreateProductInput) (*mmodel.Product, error)
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)
}
19 changes: 17 additions & 2 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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"id": "01932727-1b5a-7540-98c0-6521ffe78ce6",
"name": "Product Practical Metal Sausages BLOCKED",
"ledgerId": "01932715-9f93-7432-90c3-4352bcfe464d",
"organizationId": "01931b04-964a-7caa-a422-c29a95387c00",
"status": {
"code": "BLOCKED",
"description": "Teste Product BLOCKED"
},
"createdAt": "2024-11-13T20:11:34.617671Z",
"updatedAt": "2024-11-13T20:16:30.48922Z",
"deletedAt": null,
"metadata": {
"bitcoin": "35x7shF9VF1npqiTNjMsytJTRBNAoaAh",
"chave": "metadata_chave",
"boolean": true
}
}
37 changes: 37 additions & 0 deletions components/mdz/internal/rest/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,43 @@ func (r *product) GetByID(organizationID, ledgerID, productID string) (*mmodel.P
return &productResp, nil
}

func (r *product) Update(
organizationID, ledgerID, productID string, inp mmodel.UpdateProductInput,
) (*mmodel.Product, 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/%s/products/%s",
r.Factory.Env.URLAPILedger, organizationID, ledgerID, productID)

req, err := http.NewRequest(http.MethodPatch, 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 PATCH request: " + err.Error())
}
defer resp.Body.Close()

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

var productResp mmodel.Product
if err := json.NewDecoder(resp.Body).Decode(&productResp); err != nil {
return nil, errors.New("decoding response JSON:" + err.Error())
}

return &productResp, nil
}

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

func Test_product_Update(t *testing.T) {
productID := "01932727-1b5a-7540-98c0-6521ffe78ce6"
ledgerID := "01932715-9f93-7432-90c3-4352bcfe464d"
organizationID := "01931b04-964a-7caa-a422-c29a95387c00"
name := "Product Practical Metal Sausages BLOCKED"
statusCode := "BLOCKED"
statusDescription := ptr.StringPtr("Teste Product BLOCKED")

metadata := map[string]any{
"bitcoin": "35x7shF9VF1npqiTNjMsytJTRBNAoaAh",
"chave": "metadata_chave",
"boolean": true,
}

inp := mmodel.UpdateProductInput{
Name: name,
Status: mmodel.Status{
Code: statusCode,
Description: statusDescription,
},
Metadata: metadata,
}

expectedResult := &mmodel.Product{
ID: productID,
Name: name,
Status: mmodel.Status{
Code: statusCode,
Description: statusDescription,
},
Metadata: metadata,
}

client := &http.Client{}
httpmock.ActivateNonDefault(client)
defer httpmock.DeactivateAndReset()

URIAPILedger := "http://127.0.0.1:3000"

uri := fmt.Sprintf("%s/v1/organizations/%s/ledgers/%s/products/%s",
URIAPILedger, organizationID, ledgerID, productID)

httpmock.RegisterResponder(http.MethodPatch, uri,
mockutil.MockResponseFromFile(http.StatusOK,
"./.fixtures/product_response_update.json"))

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

product := NewProduct(factory)

result, err := product.Update(organizationID, ledgerID, productID, inp)

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.Status.Code, result.Status.Code)
assert.Equal(t, expectedResult.Status.Description, result.Status.Description)
assert.Equal(t, expectedResult.Metadata, result.Metadata)

info := httpmock.GetCallCountInfo()
assert.Equal(t, 1, info["PATCH "+uri])
}
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 @@ -14,6 +14,7 @@ func (f *factoryProduct) setCmds(cmd *cobra.Command) {
cmd.AddCommand(newCmdProductCreate(newInjectFacCreate(f.factory)))
cmd.AddCommand(newCmdProductList(newInjectFacList(f.factory)))
cmd.AddCommand(newCmdProductDescribe(newInjectFacDescribe(f.factory)))
cmd.AddCommand(newCmdProductUpdate(newInjectFacUpdate(f.factory)))
}

func NewCmdProduct(f *factory.Factory) *cobra.Command {
Expand Down
166 changes: 166 additions & 0 deletions components/mdz/pkg/cmd/product/update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package product

import (
"encoding/json"
"errors"
"fmt"

"github.com/LerianStudio/midaz/common/mmodel"
"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 factoryProductUpdate struct {
factory *factory.Factory
repoProduct repository.Product
tuiInput func(message string) (string, error)
flagsUpdate
}

type flagsUpdate struct {
OrganizationID string
LedgerID string
ProductID string
Name string
StatusCode string
StatusDescription string
Metadata string
JSONFile string
}

func (f *factoryProductUpdate) 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 *factoryProductUpdate) runE(cmd *cobra.Command, _ []string) error {
product := mmodel.UpdateProductInput{}

if err := f.ensureFlagInput(cmd); err != nil {
return err
}

if cmd.Flags().Changed("json-file") {
err := utils.FlagFileUnmarshalJSON(f.JSONFile, &product)
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.UpdateRequestFromFlags(&product)
if err != nil {
return err
}
}

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

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

return nil
}

func (f *factoryProductUpdate) UpdateRequestFromFlags(portfolio *mmodel.UpdateProductInput) error {
portfolio.Name = f.Name
portfolio.Status.Code = f.StatusCode

if len(f.StatusDescription) > 0 {
portfolio.Status.Description = &f.StatusDescription
}

var metadata map[string]any
if err := json.Unmarshal([]byte(f.Metadata), &metadata); err != nil {
return errors.New("Error parsing metadata: " + err.Error())
}

portfolio.Metadata = metadata

return nil
}

func (f *factoryProductUpdate) 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().StringVar(&f.Name, "name", "", "Legal name of the Product.")
cmd.Flags().StringVar(&f.StatusCode, "status-code", "",
"code for the organization (e.g., ACTIVE).")
cmd.Flags().StringVar(&f.StatusDescription, "status-description", "",
"Description of the current status of the ledger.")
cmd.Flags().StringVar(&f.Metadata, "metadata", "{}",
"Metadata in JSON format, ex: '{\"key1\": \"value\", \"key2\": 123}'")

// Flags command Update
cmd.Flags().StringVar(&f.JSONFile, "json-file", "", "Path to a JSON file containing "+
"the attributes of the Organization being Updated; you can use - for reading from stdin")
cmd.Flags().BoolP("help", "h", false, "Displays more information about the Mdz CLI")
}

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

func newCmdProductUpdate(f *factoryProductUpdate) *cobra.Command {
cmd := &cobra.Command{
Use: "update",
Short: "Updates an existing product with new policies.",
Long: utils.Format(
"The update subcommand allows you to adjust the policies and settings",
"of an existing product. With it, you can modify the clustering rules,",
"adapting the grouping of clients according to changes in business",
"strategies and needs.",
),
Example: utils.Format(
"$ mdz product update",
"$ mdz product update -h",
"$ mdz product update --json-file payload.json",
"$ cat payload.json | mdz product update --organization-id '1234' --ledger-id '4421' --product-id '45232' --json-file -",
"$ mdz portfolio update --organization-id '1234' --ledger-id '4421' --portfolio-id '55232' --name 'Gislason LLCT'",
),
RunE: f.runE,
}

f.setFlags(cmd)

return cmd
}
Loading

0 comments on commit 4368bc2

Please sign in to comment.