Skip to content

Commit

Permalink
feat(sc-57431): Support Opportunities API (#71)
Browse files Browse the repository at this point in the history
* feat(sc-57431): Support Opportunities API

* feat(sc-57431): Support Opportunities API : fix test data

* feat(sc-57431): Support Opportunities API : lint

Co-authored-by: Soeun <[email protected]>

---------

Co-authored-by: Soeun <[email protected]>
  • Loading branch information
kamilpavlicko and SoeunSona authored Mar 26, 2024
1 parent 02667da commit b3e4ad6
Show file tree
Hide file tree
Showing 9 changed files with 954 additions and 5 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ api.ListCustomersContact(&cm.ListContactsParams{}, "customerUUID")
api.CreateCustomersContact(&cm.NewContact{}, "customerUUID")
api.ListCustomerNotes(&cm.ListNotesParams{}, "customerUUID")
api.CreateCustomerNote(&cm.NewNote{}, "customerUUID")
api.ListCustomerOpportunities(&cm.ListOpportunitiesParams{}, "customerUUID")
api.CreateCustomerOpportunity(&cm.NewOpportunity{}, "customerUUID")
```

#### [Contacts](https://dev.chartmogul.com/reference/contacts)
Expand All @@ -127,6 +129,7 @@ api.MergeContacts("intoContactUUID", "fromContactUUID")
```

#### [Customer Notes](https://dev.chartmogul.com/reference/customer-notes)

```go
api.CreateNote(&cm.NewNote{})
api.RetrieveNote("noteUUID")
Expand All @@ -135,6 +138,16 @@ api.UpdateNote(&cm.UpdateNote{}, "noteUUID")
api.DeleteNote("noteUUID")
```

#### [Opportunities](https://dev.chartmogul.com/reference/opportunities)

```go
api.CreateOpportunity(&cm.NewOpportunity{})
api.RetrieveOpportunity("opportunityUUID")
api.ListOpportunities(&cm.ListOpportunitiesParams{})
api.UpdateOpportunity(&cm.UpdateOpportunity{}, "opportunityUUID")
api.DeleteOpportunity("opportunityUUID")
```

#### [Plans](https://dev.chartmogul.com/reference#plans)

```go
Expand Down
5 changes: 0 additions & 5 deletions contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ type NewContact struct {
Custom []Custom `json:"custom,omitempty"`
}

type Custom struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}

// ListContactsParams = parameters for listing contacts in API.
type ListContactsParams struct {
CustomerUUID string `json:"customer_uuid,omitempty"`
Expand Down
26 changes: 26 additions & 0 deletions customers.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,29 @@ func (api API) CreateCustomerNote(input *NewNote, customerUUID string) (*Note, e
}
return result, api.create(customerNotesEndpoint, input, result)
}

// ListCustomerOpporunities
//
// See https://dev.chartmogul.com/reference/list-opportunities
func (api API) ListCustomerOpporunities(listOpportunitiesParams *ListOpportunitiesParams, customerUUID string) (*Opportunities, error) {
result := &Opportunities{}
query := make([]interface{}, 0, 1)
if listOpportunitiesParams != nil {
if listOpportunitiesParams.CustomerUUID == "" {
listOpportunitiesParams.CustomerUUID = customerUUID
}
query = append(query, *listOpportunitiesParams)
}
return result, api.list(opportunitiesEndpoint, result, query...)
}

// CreateCustomerOpportunity
//
// See https://dev.chartmogul.com/reference/create-an-opportunity
func (api API) CreateCustomerOpportunity(input *NewOpportunity, customerUUID string) (*Opportunity, error) {
result := &Opportunity{}
if input.CustomerUUID == "" {
input.CustomerUUID = customerUUID
}
return result, api.create(opportunitiesEndpoint, input, result)
}
117 changes: 117 additions & 0 deletions customers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,3 +530,120 @@ func TestCreateCustomerNote(t *testing.T) {
t.Fatal("Unexpected result")
}
}

func TestListCustomerOpportunities(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
t.Errorf("Unexpected method %v", r.Method)
}
if r.RequestURI != "/v/opportunities?customer_uuid=cus_00000000-0000-0000-0000-000000000000&per_page=1" {
t.Errorf("Unexpected URI %v", r.RequestURI)
}
w.WriteHeader(http.StatusOK)
//nolint
w.Write([]byte(`{
"entries": [{
"uuid": "00000000-0000-0000-0000-000000000000",
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000",
"owner": "[email protected]",
"pipeline": "New business 1",
"pipeline_stage": "Discovery",
"estimated_close_date": "2023-12-22",
"currency": "USD",
"amount_in_cents": 100,
"type": "recurring",
"forecast_category": "pipeline",
"win_likelihood": 3,
"custom": {"from_campaign": true},
"created_at": "2024-03-13T07:33:28.356Z",
"updated_at": "2024-03-13T07:33:28.356Z"
}],
"has_more": false,
"cursor": "88abf99"
}`))
}))
defer server.Close()
SetURL(server.URL + "/v/%v")

tested := &API{
ApiKey: "token",
}
uuid := "cus_00000000-0000-0000-0000-000000000000"
params := &ListOpportunitiesParams{Cursor: Cursor{PerPage: 1}}
opportunities, err := tested.ListCustomerOpporunities(params, uuid)

if err != nil {
spew.Dump(err)
t.Fatal("Not expected to fail")
}
if len(opportunities.Entries) == 0 {
spew.Dump(opportunities)
t.Fatal("Unexpected result")
}
}

func TestCreateCustomerOpportunity(t *testing.T) {
server := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
t.Errorf("Unexpected method %v", r.Method)
}
if r.RequestURI != "/v/opportunities" {
t.Errorf("Unexpected URI %v", r.RequestURI)
}
w.WriteHeader(http.StatusCreated)
//nolint
w.Write([]byte(`{
"uuid": "00000000-0000-0000-0000-000000000000",
"customer_uuid": "cus_00000000-0000-0000-0000-000000000000",
"owner": "[email protected]",
"pipeline": "New business 1",
"pipeline_stage": "Discovery",
"estimated_close_date": "2023-12-22",
"currency": "USD",
"amount_in_cents": 100,
"type": "recurring",
"forecast_category": "pipeline",
"win_likelihood": 3,
"custom": {"from_campaign": true},
"created_at": "2024-03-13T07:33:28.356Z",
"updated_at": "2024-03-13T07:33:28.356Z"
}`))
}))
defer server.Close()
SetURL(server.URL + "/v/%v")

tested := &API{
ApiKey: "token",
}

opportunity, err := tested.CreateCustomerOpportunity(&NewOpportunity{
Owner: "[email protected]",
Pipeline: "New business 1",
PipelineStage: "Discovery",
EstimatedCloseDate: "2023-12-22",
Currency: "USD",
AmountInCents: 100,
Type: "recurring",
ForecastCategory: "pipeline",
WinLikelihood: 3,
Custom: []Custom{
{
Key: "from_campaign",
Value: true,
},
},
}, "cus_00000000-0000-0000-0000-000000000000")

if err != nil {
spew.Dump(err)
t.Fatal("Not expected to fail")
}
if opportunity.UUID != "00000000-0000-0000-0000-000000000000" {
spew.Dump(opportunity)
t.Fatal("Unexpected result")
}
}
Loading

0 comments on commit b3e4ad6

Please sign in to comment.