-
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.
Merge pull request #252 from LerianStudio/feature/MIDAZ-247
Feature/MIDAZ-247
- Loading branch information
Showing
9 changed files
with
506 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package repository | ||
|
||
import "github.com/LerianStudio/midaz/common/mmodel" | ||
|
||
type Product interface { | ||
Create(organizationID, ledgerID string, inp mmodel.CreateProductInput) (*mmodel.Product, error) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
components/mdz/internal/rest/.fixtures/product_response_create.json
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,18 @@ | ||
{ | ||
"id": "0193271b-877f-7c98-a5a6-43b664d68982", | ||
"name": "Product Refined Cotton Chair", | ||
"ledgerId": "01932715-9f93-7432-90c3-4352bcfe464d", | ||
"organizationId": "01931b04-964a-7caa-a422-c29a95387c00", | ||
"status": { | ||
"code": "ACTIVE", | ||
"description": "Teste Product" | ||
}, | ||
"createdAt": "2024-11-13T19:58:55.868976395Z", | ||
"updatedAt": "2024-11-13T19:58:55.868979258Z", | ||
"deletedAt": null, | ||
"metadata": { | ||
"bitcoin": "3g9ofZcD7KRWL44BWdNa3PyM4PfzgqDG5P", | ||
"chave": "metadata_chave", | ||
"boolean": true | ||
} | ||
} |
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,56 @@ | ||
package rest | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/LerianStudio/midaz/common/mmodel" | ||
"github.com/LerianStudio/midaz/components/mdz/pkg/factory" | ||
) | ||
|
||
type product struct { | ||
Factory *factory.Factory | ||
} | ||
|
||
func (r *product) Create(organizationID, ledgerID string, inp mmodel.CreateProductInput) (*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", | ||
r.Factory.Env.URLAPILedger, organizationID, ledgerID) | ||
|
||
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 err := checkResponse(resp, http.StatusCreated); 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} | ||
} |
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,88 @@ | ||
package rest | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/LerianStudio/midaz/common/mmodel" | ||
"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_product_Create(t *testing.T) { | ||
productID := "0193271b-877f-7c98-a5a6-43b664d68982" | ||
ledgerID := "01932715-9f93-7432-90c3-4352bcfe464d" | ||
organizationID := "01931b04-964a-7caa-a422-c29a95387c00" | ||
|
||
name := "Product Refined Cotton Chair" | ||
code := "ACTIVE" | ||
description := ptr.StringPtr("Teste Product") | ||
|
||
metadata := map[string]any{ | ||
"bitcoin": "3g9ofZcD7KRWL44BWdNa3PyM4PfzgqDG5P", | ||
"chave": "metadata_chave", | ||
"boolean": true, | ||
} | ||
|
||
input := mmodel.CreateProductInput{ | ||
Name: name, | ||
Status: mmodel.Status{ | ||
Code: code, | ||
Description: description, | ||
}, | ||
Metadata: metadata, | ||
} | ||
|
||
expectedResult := &mmodel.Product{ | ||
ID: productID, | ||
Name: name, | ||
LedgerID: ledgerID, | ||
OrganizationID: organizationID, | ||
Status: mmodel.Status{ | ||
Code: code, | ||
Description: description, | ||
}, | ||
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", | ||
URIAPILedger, organizationID, ledgerID) | ||
|
||
httpmock.RegisterResponder(http.MethodPost, uri, | ||
mockutil.MockResponseFromFile(http.StatusCreated, "./.fixtures/product_response_create.json")) | ||
|
||
factory := &factory.Factory{ | ||
HTTPClient: client, | ||
Env: &environment.Env{ | ||
URLAPILedger: URIAPILedger, | ||
}, | ||
} | ||
|
||
productServ := NewProduct(factory) | ||
|
||
result, err := productServ.Create(organizationID, ledgerID, 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, result.OrganizationID) | ||
assert.Equal(t, expectedResult.LedgerID, result.LedgerID) | ||
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["POST "+uri]) | ||
} |
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,155 @@ | ||
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 factoryProductCreate struct { | ||
factory *factory.Factory | ||
repoProduct repository.Product | ||
tuiInput func(message string) (string, error) | ||
flagsCreate | ||
} | ||
|
||
type flagsCreate struct { | ||
OrganizationID string | ||
LedgerID string | ||
Name string | ||
Code string | ||
Description string | ||
Metadata string | ||
JSONFile string | ||
} | ||
|
||
func (f *factoryProductCreate) runE(cmd *cobra.Command, _ []string) error { | ||
product := mmodel.CreateProductInput{} | ||
|
||
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("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.createRequestFromFlags(&product) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
resp, err := f.repoProduct.Create(f.OrganizationID, f.LedgerID, product) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
output.Printf(f.factory.IOStreams.Out, | ||
fmt.Sprintf("The Product ID %s has been successfully created", resp.ID)) | ||
|
||
return nil | ||
} | ||
|
||
func (f *factoryProductCreate) createRequestFromFlags(portfolio *mmodel.CreateProductInput) error { | ||
var err error | ||
|
||
portfolio.Name, err = utils.AssignStringField(f.Name, "name", f.tuiInput) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
portfolio.Status.Code = f.Code | ||
|
||
if len(f.Description) > 0 { | ||
portfolio.Status.Description = &f.Description | ||
} | ||
|
||
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 *factoryProductCreate) 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.Name, "name", "", | ||
"name new ledger your organization") | ||
cmd.Flags().StringVar(&f.Code, "status-code", "", | ||
"code for the organization (e.g., ACTIVE).") | ||
cmd.Flags().StringVar(&f.Description, "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}'") | ||
cmd.Flags().StringVar(&f.JSONFile, "json-file", "", | ||
`Path to a JSON file containing the attributes of the Product 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) *factoryProductCreate { | ||
return &factoryProductCreate{ | ||
factory: f, | ||
repoProduct: rest.NewProduct(f), | ||
tuiInput: tui.Input, | ||
} | ||
} | ||
|
||
func newCmdProductCreate(f *factoryProductCreate) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "create", | ||
Short: "Creates a new product for clustering customers.", | ||
Long: utils.Format( | ||
"The create subcommand allows you to set up a new product, defining", | ||
"the policies and grouping rules to organize customers according to", | ||
"specific characteristics. This feature is useful for establishing", | ||
"new clusters and targeting business strategies at specific groups.", | ||
), | ||
Example: utils.Format( | ||
"$ mdz product create", | ||
"$ mdz product create -h", | ||
"$ mdz product create --json-file payload.json", | ||
"$ cat payload.json | mdz product create --json-file -", | ||
), | ||
RunE: f.runE, | ||
} | ||
|
||
f.setFlags(cmd) | ||
|
||
return cmd | ||
} |
Oops, something went wrong.