diff --git a/v5/client.go b/v5/client.go index fbf30d1..a80a4ad 100644 --- a/v5/client.go +++ b/v5/client.go @@ -943,7 +943,6 @@ func (c *Client) CorporateCustomersFixExternalIds(customers []IdentifiersPair) ( } // CorporateCustomersHistory returns corporate customer's history -// TODO: Check implementation when history becomes available // // For more information see http://help.retailcrm.pro/Developers/ApiVersion5#post--api-v5-customers-corporate-fix-external-ids // @@ -951,8 +950,8 @@ func (c *Client) CorporateCustomersFixExternalIds(customers []IdentifiersPair) ( // // var client = v5.New("https://demo.url", "09jIJ") // -// data, status, err := client.CorporateCustomersHistory(v5.CustomersHistoryRequest{ -// Filter: v5.CustomersHistoryFilter{ +// data, status, err := client.CorporateCustomersHistory(v5.CorporateCustomersHistoryRequest{ +// Filter: v5.CorporateCustomersHistoryFilter{ // SinceID: 20, // }, // }) @@ -968,7 +967,7 @@ func (c *Client) CorporateCustomersFixExternalIds(customers []IdentifiersPair) ( // for _, value := range data.History { // fmt.Printf("%v\n", value) // } -func (c *Client) CorporateCustomersHistory(parameters CustomersHistoryRequest) (CorporateCustomersHistoryResponse, int, *errs.Failure) { +func (c *Client) CorporateCustomersHistory(parameters CorporateCustomersHistoryRequest) (CorporateCustomersHistoryResponse, int, *errs.Failure) { var resp CorporateCustomersHistoryResponse params, _ := query.Values(parameters) diff --git a/v5/client_test.go b/v5/client_test.go index e73a3d7..9de366b 100644 --- a/v5/client_test.go +++ b/v5/client_test.go @@ -920,6 +920,69 @@ func TestClient_CorporateCustomersFixExternalIds_Fail(t *testing.T) { } } +func TestClient_CorporateCustomersHistory(t *testing.T) { + c := client() + f := CorporateCustomersHistoryRequest{ + Filter: CorporateCustomersHistoryFilter{ + SinceID: 20, + }, + } + defer gock.Off() + + gock.New(crmURL). + Get("/customers-corporate/history"). + MatchParam("filter[sinceId]", "20"). + Reply(200). + BodyString(`{"success": true, "history": [{"id": 1}]}`) + + data, status, err := c.CorporateCustomersHistory(f) + if err.Error() != "" { + t.Errorf("%v", err.Error()) + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err.ApiError()) + } + + if data.Success != true { + t.Errorf("%v", err.ApiError()) + } + + if len(data.History) == 0 { + t.Errorf("%v", "Empty history") + } +} + +func TestClient_CorporateCustomersHistory_Fail(t *testing.T) { + c := client() + f := CorporateCustomersHistoryRequest{ + Filter: CorporateCustomersHistoryFilter{ + StartDate: "2020-13-12", + }, + } + + defer gock.Off() + + gock.New(crmURL). + Get("/customers-corporate/history"). + MatchParam("filter[startDate]", "2020-13-12"). + Reply(400). + BodyString(`{"success": false, "errorMsg": "Errors in the input parameters", "errors": {"children[startDate]": "Значение недопустимо."}}`) + + data, status, err := c.CorporateCustomersHistory(f) + if err.Error() != "" { + t.Errorf("%v", err.Error()) + } + + if status < http.StatusBadRequest { + t.Error(statusFail) + } + + if data.Success != false { + t.Error(successFail) + } +} + func TestClient_CorporateCustomersNotes(t *testing.T) { defer gock.Off() diff --git a/v5/filters.go b/v5/filters.go index 05118dd..6a653bd 100644 --- a/v5/filters.go +++ b/v5/filters.go @@ -133,6 +133,16 @@ type CustomersHistoryFilter struct { EndDate string `url:"endDate,omitempty"` } +// CorporateCustomersHistoryFilter type +type CorporateCustomersHistoryFilter struct { + CustomerID int `url:"customerId,omitempty"` + SinceID int `url:"sinceId,omitempty"` + CustomerExternalID string `url:"customerExternalId,omitempty"` + ContactIds []string `url:"contactIds,omitempty,brackets"` + StartDate string `url:"startDate,omitempty"` + EndDate string `url:"endDate,omitempty"` +} + // OrdersFilter type type OrdersFilter struct { Ids []int `url:"ids,omitempty,brackets"` diff --git a/v5/request.go b/v5/request.go index 9d6fc38..2ae19b6 100644 --- a/v5/request.go +++ b/v5/request.go @@ -58,6 +58,13 @@ type CustomersHistoryRequest struct { Page int `url:"page,omitempty"` } +// CorporateCustomersHistoryRequest type +type CorporateCustomersHistoryRequest struct { + Filter CorporateCustomersHistoryFilter `url:"filter,omitempty"` + Limit int `url:"limit,omitempty"` + Page int `url:"page,omitempty"` +} + // OrderRequest type type OrderRequest struct { By string `url:"by,omitempty"` diff --git a/v5/response.go b/v5/response.go index 44e1bdf..8fb8c72 100644 --- a/v5/response.go +++ b/v5/response.go @@ -110,7 +110,6 @@ type CustomersHistoryResponse struct { } // CorporateCustomersHistoryResponse type -// TODO: Update history format in case of changes type CorporateCustomersHistoryResponse struct { Success bool `json:"success,omitempty"` GeneratedAt string `json:"generatedAt,omitempty"`