Skip to content

Commit

Permalink
[sc-62679] SDK Unmerge Support - Golang (#72)
Browse files Browse the repository at this point in the history
* Add support for unmerge endpoint.

* Remove duplicate code.
  • Loading branch information
jessicahearn authored Oct 23, 2024
1 parent b3e4ad6 commit 9877974
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ api.SearchCustomers(&cm.SearchCustomersParams{})
api.ListCustomers(&cm.ListCustomersParams{})
api.UpdateCustomer(&cm.NewCustomer{}, "customerUUID")
api.MergeCustomers(&cm.MergeCustomersParams{})
api.UnmergeCustomers(&cm.UnmergeCustomersParams{})
api.ConnectSubscriptions("customerUUID", []cm.Subscription{})
api.ListCustomersContact(&cm.ListContactsParams{}, "customerUUID")
api.CreateCustomersContact(&cm.NewContact{}, "customerUUID")
Expand Down Expand Up @@ -319,6 +320,7 @@ To work on the library:
* add pre-commit hook `go test ./...` (in `.git/hooks/pre-commit`) to have a working state always.

### Testing
* Run test suite with `go test`
* Use `net/http/httptest` for mocking HTTP server directly, see file `generic_test.go` for examples.
* For `integration_tests` against real API use `github.com/dnaeon/go-vcr` library. Be careful to remove your API credentials from fixtures before committing! If Import API App changes, re-record the affected integration tests (by deleting fixtures).

Expand Down
1 change: 1 addition & 0 deletions chartmogul.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type IApi interface {
ListCustomers(ListCustomersParams *ListCustomersParams) (*Customers, error)
SearchCustomers(SearchCustomersParams *SearchCustomersParams) (*Customers, error)
MergeCustomers(MergeCustomersParams *MergeCustomersParams) error
UnmergeCustomers(UnmergeCustomersParams *UnmergeCustomersParams) error
DeleteCustomer(customerUUID string) error
DeleteCustomerInvoices(dataSourceUUID, customerUUID string) error
DeleteCustomerInvoicesV2(dataSourceUUID, customerUUID string, DeleteCustomerInvoicesParams *DeleteCustomerInvoicesParams) error
Expand Down
16 changes: 16 additions & 0 deletions customers.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ type MergeCustomersParams struct {
Into CustID `json:"into"`
}

// UnmergeCustomersParams - identify target for unmerging.
type UnmergeCustomersParams struct {
CustomerUUID string `json:"customer_uuid"`
ExternalID string `json:"external_id"`
DataSourceUUID string `json:"data_source_uuid"`
MoveToNewCustomer []string `json:"move_to_new_customer,omitempty"`
}

// CustID - use either DataSourceUUID & ExternalID or CustomerUUID
type CustID struct {
DataSourceUUID string `json:"data_source_uuid,omitempty"`
Expand All @@ -146,6 +154,7 @@ const (
customersEndpoint = "customers"
searchCustomersEndpoint = "customers/search"
mergeCustomersEndpoint = "customers/merges"
unmergeCustomersEndpoint = "customers/unmerges"
customerContactsEndpoint = "customers/:uuid/contacts"
)

Expand Down Expand Up @@ -211,6 +220,13 @@ func (api API) MergeCustomers(mergeCustomersParams *MergeCustomersParams) error
return api.merge(mergeCustomersEndpoint, *mergeCustomersParams)
}

// UnmergeCustomers unmerges two cutomers.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
func (api API) UnmergeCustomers(unmergeCustomersParams *UnmergeCustomersParams) error {
return api.unmerge(unmergeCustomersEndpoint, *unmergeCustomersParams)
}

// DeleteCustomer deletes one customer by UUID.
//
// See https://dev.chartmogul.com/v1.0/reference#customers
Expand Down
5 changes: 5 additions & 0 deletions generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ func (api API) merge(path string, input interface{}) error {
return wrapErrors(res, []byte(body), errs)
}

// UPDATE
func (api API) unmerge(path string, input interface{}) error {
return api.merge(path, input)
}

// updateImpl adds another meta level, because this same pattern
// uses multiple HTTP methods in API.
func (api API) updateImpl(path string, uuid string, input interface{}, output interface{}, method string) error {
Expand Down
26 changes: 26 additions & 0 deletions integration_tests/customers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func TestListCustomersIntegration(t *testing.T) {

validateListCustomers(api, ds.UUID, t)
validateSearchCustomers(api, "[email protected]", t)
validateMergeCustomers(api, t)
validateUnmergeCustomers(api, t)
validateCustomerRetrievalAndUpdate(api, customers[1], t)
}

Expand Down Expand Up @@ -113,6 +115,30 @@ func validateSearchCustomers(api *cm.API, email string, t *testing.T) {
}
}

// validateMergeCustomers validates that the specified customers can be correctly merged using the API.
func validateMergeCustomers(api *cm.API, t *testing.T) {
err := api.MergeCustomers(&cm.MergeCustomersParams{
From: cm.CustID{CustomerUUID: "cus_2706d304-76b7-11ee-93d6-5b3d820d37cd"},
Into: cm.CustID{CustomerUUID: "cus_23740208-2c7e-11ee-9ea2-ffd2435982bb"},
})
if err != nil {
t.Fatalf("Failed to merge customers: %v", err)
}
}

// validateUnmergeCustomers validates that the specified customers can be correctly unmerged using the API.
func validateUnmergeCustomers(api *cm.API, t *testing.T) {
err := api.UnmergeCustomers(&cm.UnmergeCustomersParams{
CustomerUUID: "cus_cd9e5f29-6299-40e5-b343-0bd1ed228b4f",
ExternalID: "cus_O075O8NH0LrtG8",
DataSourceUUID: "ds_788ec6ae-dd51-11ee-bd46-a3ec952dc041",
MoveToNewCustomer: []string{"tasks"},
})
if err != nil {
t.Fatalf("Failed to unmerge customers: %v", err)
}
}

// validatecustomerRetrievalAndUpdate checks that a given customer can be retrieved and updated
// correctly through the API.
func validateCustomerRetrievalAndUpdate(api *cm.API, customerToUpdate *cm.Customer, t *testing.T) {
Expand Down

0 comments on commit 9877974

Please sign in to comment.