diff --git a/v5/client.go b/v5/client.go index 934306f..b8a5f01 100644 --- a/v5/client.go +++ b/v5/client.go @@ -109,9 +109,21 @@ func checkBy(by string) string { return context } +// fillSite add site code to parameters if present +func fillSite(p *url.Values, site []string) { + if len(site) > 0 { + s := site[0] + + if s != "" { + p.Add("site", s) + } + } +} + // ApiVersions get available API versions func (c *Client) ApiVersions() (*VersionResponse, int, error) { var resp VersionResponse + data, status, err := c.GetRequest(fmt.Sprintf("%s/api-versions", unversionedPrefix)) if err != nil { return &resp, status, err @@ -125,6 +137,7 @@ func (c *Client) ApiVersions() (*VersionResponse, int, error) { // ApiCredentials get available API methods func (c *Client) ApiCredentials() (*CredentialResponse, int, error) { var resp CredentialResponse + data, status, err := c.GetRequest(fmt.Sprintf("%s/credentials", unversionedPrefix)) if err != nil { return &resp, status, err @@ -177,13 +190,7 @@ func (c *Client) CustomerCreate(customer Customer, site ...string) (*CustomerCha "customer": {string(customerJson[:])}, } - if len(site) > 0 { - s := site[0] - - if s != "" { - p.Add("site", s) - } - } + fillSite(&p, site) data, status, err := c.PostRequest(fmt.Sprintf("%s/customers/create", versionedPrefix), p) if err != nil { @@ -212,13 +219,7 @@ func (c *Client) CustomerEdit(customer Customer, by string, site ...string) (*Cu "customer": {string(customerJson[:])}, } - if len(site) > 0 { - s := site[0] - - if s != "" { - p.Add("site", s) - } - } + fillSite(&p, site) data, status, err := c.PostRequest(fmt.Sprintf("%s/customers/%s/edit", versionedPrefix, uid), p) if err != nil { @@ -240,13 +241,7 @@ func (c *Client) CustomersUpload(customers []Customer, site ...string) (*Custome "customers": {string(uploadJson[:])}, } - if len(site) > 0 { - s := site[0] - - if s != "" { - p.Add("site", s) - } - } + fillSite(&p, site) data, status, err := c.PostRequest(fmt.Sprintf("%s/customers/upload", versionedPrefix), p) if err != nil { @@ -259,7 +254,7 @@ func (c *Client) CustomersUpload(customers []Customer, site ...string) (*Custome } // CustomersFixExternalIds method -func (c *Client) CustomersFixExternalIds(customers []CustomerIdentifiers) (*SucessfulResponse, int, error) { +func (c *Client) CustomersFixExternalIds(customers []IdentifiersPair) (*SucessfulResponse, int, error) { var resp SucessfulResponse customersJson, _ := json.Marshal(&customers) @@ -327,6 +322,98 @@ func (c *Client) Orders(parameters OrdersRequest) (*OrdersResponse, int, error) return &resp, status, err } +// OrderCreate method +func (c *Client) OrderCreate(order Order, site ...string) (*OrderChangeResponse, int, error) { + var resp OrderChangeResponse + orderJson, _ := json.Marshal(&order) + + p := url.Values{ + "order": {string(orderJson[:])}, + } + + fillSite(&p, site) + + data, status, err := c.PostRequest(fmt.Sprintf("%s/orders/create", versionedPrefix), p) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// CustomerEdit method +func (c *Client) OrderEdit(order Order, by string, site ...string) (*OrderChangeResponse, int, error) { + var resp OrderChangeResponse + var uid = strconv.Itoa(order.Id) + var context = checkBy(by) + + if context == "externalId" { + uid = order.ExternalId + } + + orderJson, _ := json.Marshal(&order) + + p := url.Values{ + "by": {string(context)}, + "order": {string(orderJson[:])}, + } + + fillSite(&p, site) + + data, status, err := c.PostRequest(fmt.Sprintf("%s/orders/%s/edit", versionedPrefix, uid), p) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// OrdersUpload method +func (c *Client) OrdersUpload(orders []Order, site ...string) (*OrdersUploadResponse, int, error) { + var resp OrdersUploadResponse + + uploadJson, _ := json.Marshal(&orders) + + p := url.Values{ + "orders": {string(uploadJson[:])}, + } + + fillSite(&p, site) + + data, status, err := c.PostRequest(fmt.Sprintf("%s/orders/upload", versionedPrefix), p) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// OrdersFixExternalIds method +func (c *Client) OrdersFixExternalIds(orders []IdentifiersPair) (*SucessfulResponse, int, error) { + var resp SucessfulResponse + + ordersJson, _ := json.Marshal(&orders) + + p := url.Values{ + "orders": {string(ordersJson[:])}, + } + + data, status, err := c.PostRequest(fmt.Sprintf("%s/orders/fix-external-ids", versionedPrefix), p) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + // OrdersHistory method func (c *Client) OrdersHistory(parameters OrdersHistoryRequest) (*CustomersHistoryResponse, int, error) { var resp CustomersHistoryResponse diff --git a/v5/response.go b/v5/response.go index b4c3e24..e0156b3 100644 --- a/v5/response.go +++ b/v5/response.go @@ -57,8 +57,8 @@ type CustomerChangeResponse struct { // CustomersUploadResponse type type CustomersUploadResponse struct { - Success bool `json:"success"` - UploadedCustomers []CustomerIdentifiers `json:"uploadedCustomers,omitempty,brackets"` + Success bool `json:"success"` + UploadedCustomers []IdentifiersPair `json:"uploadedCustomers,omitempty,brackets"` } // CustomersHistoryResponse type @@ -82,6 +82,18 @@ type OrdersResponse struct { Orders []Order `json:"orders,omitempty,brackets"` } +// OrderChangeResponse type +type OrderChangeResponse struct { + Success bool `json:"success"` + Id int `json:"id,omitempty"` +} + +// OrdersUploadResponse type +type OrdersUploadResponse struct { + Success bool `json:"success"` + UploadedOrders []IdentifiersPair `json:"uploadedOrders,omitempty,brackets"` +} + // OrdersHistoryResponse type type OrdersHistoryResponse struct { Success bool `json:"success,omitempty"` diff --git a/v5/types.go b/v5/types.go index 7ec2c3c..f221282 100644 --- a/v5/types.go +++ b/v5/types.go @@ -78,6 +78,12 @@ type Property struct { Value string `json:"value,omitempty"` } +// IdentifiersPair type +type IdentifiersPair struct { + Id int `json:"id,omitempty"` + ExternalId string `json:"externalId,omitempty"` +} + /** Customer related types */ @@ -123,12 +129,6 @@ type CustomerPhone struct { Number string `json:"number,omitempty"` } -// CustomerIdentifiers type -type CustomerIdentifiers struct { - Id int `json:"id,omitempty"` - ExternalId string `json:"externalId,omitempty"` -} - // CustomerHistoryRecord type type CustomerHistoryRecord struct { Id int `json:"id,omitempty"` diff --git a/v5_tests/customers_test.go b/v5_tests/customers_test.go index ff802ce..18a589b 100644 --- a/v5_tests/customers_test.go +++ b/v5_tests/customers_test.go @@ -159,7 +159,7 @@ func TestClient_CustomersFixExternalIds(t *testing.T) { t.Fail() } - customers := []v5.CustomerIdentifiers{{ + customers := []v5.IdentifiersPair{{ Id: cr.Id, ExternalId: RandomString(8), }} diff --git a/v5_tests/orders_test.go b/v5_tests/orders_test.go index 3f82dee..3158eb4 100644 --- a/v5_tests/orders_test.go +++ b/v5_tests/orders_test.go @@ -1,6 +1,7 @@ package v5_tests import ( + "fmt" "net/http" "testing" @@ -33,10 +34,55 @@ func TestClient_OrdersOrders(t *testing.T) { } } -func TestClient_OrdersOrder(t *testing.T) { +func TestClient_OrderChange(t *testing.T) { c := client() - data, status, err := c.Order("asdf1510920687", "externalId", "") + random := RandomString(8) + + f := v5.Order{ + FirstName: "Понтелей", + LastName: "Турбин", + Patronymic: "Аристархович", + ExternalId: random, + Email: fmt.Sprintf("%s@example.com", random), + } + + cr, sc, err := c.OrderCreate(f) + if err != nil { + t.Errorf("%s", err) + t.Fail() + } + + if sc != http.StatusCreated { + t.Errorf("%s", err) + t.Fail() + } + + if cr.Success != true { + t.Errorf("%s", err) + t.Fail() + } + + f.Id = cr.Id + f.CustomerComment = "test comment" + + ed, se, err := c.OrderEdit(f, "id") + if err != nil { + t.Errorf("%s", err) + t.Fail() + } + + if se != http.StatusOK { + t.Errorf("%s", err) + t.Fail() + } + + if ed.Success != true { + t.Errorf("%s", err) + t.Fail() + } + + data, status, err := c.Order(f.ExternalId, "externalId", "") if err != nil { t.Errorf("%s", err) t.Fail() @@ -51,6 +97,88 @@ func TestClient_OrdersOrder(t *testing.T) { t.Errorf("%s", err) t.Fail() } + + if data.Order.ExternalId != f.ExternalId { + t.Errorf("%s", err) + t.Fail() + } +} + +func TestClient_OrdersUpload(t *testing.T) { + c := client() + orders := make([]v5.Order, 3) + + for i := range orders { + orders[i] = v5.Order{ + FirstName: fmt.Sprintf("Name_%s", RandomString(8)), + LastName: fmt.Sprintf("Test_%s", RandomString(8)), + ExternalId: RandomString(8), + Email: fmt.Sprintf("%s@example.com", RandomString(8)), + } + } + + data, status, err := c.OrdersUpload(orders) + if err != nil { + t.Errorf("%s", err) + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Errorf("%s", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%s", err) + t.Fail() + } +} + +func TestClient_OrdersFixExternalIds(t *testing.T) { + c := client() + f := v5.Order{ + FirstName: fmt.Sprintf("Name_%s", RandomString(8)), + LastName: fmt.Sprintf("Test_%s", RandomString(8)), + ExternalId: RandomString(8), + Email: fmt.Sprintf("%s@example.com", RandomString(8)), + } + + cr, sc, err := c.OrderCreate(f) + if err != nil { + t.Errorf("%s", sc) + t.Fail() + } + + if sc != http.StatusCreated { + t.Errorf("%s", sc) + t.Fail() + } + + if cr.Success != true { + t.Errorf("%s", sc) + t.Fail() + } + + orders := []v5.IdentifiersPair{{ + Id: cr.Id, + ExternalId: RandomString(8), + }} + + fx, fe, err := c.OrdersFixExternalIds(orders) + if err != nil { + t.Errorf("%s", err) + t.Fail() + } + + if fe != http.StatusOK { + t.Errorf("%s", err) + t.Fail() + } + + if fx.Success != true { + t.Errorf("%s", err) + t.Fail() + } } func TestClient_OrdersHistory(t *testing.T) { diff --git a/v5_tests/tasks_test.go b/v5_tests/tasks_test.go index 537d81f..d3c273a 100644 --- a/v5_tests/tasks_test.go +++ b/v5_tests/tasks_test.go @@ -7,26 +7,6 @@ import ( "github.com/retailcrm/api-client-go/v5" ) -func TestClient_TasksTask(t *testing.T) { - c := client() - - data, st, err := c.Task(88) - if err != nil { - t.Errorf("%s", err) - t.Fail() - } - - if st != http.StatusOK { - t.Errorf("%s", err) - t.Fail() - } - - if data.Success != true { - t.Errorf("%s", err) - t.Fail() - } -} - func TestClient_TasksTasks(t *testing.T) { c := client() f := v5.TasksRequest{ @@ -83,6 +63,22 @@ func TestClient_TaskChange(t *testing.T) { f.Id = cr.Id f.Commentary = random2 + gt, sg, err := c.Task(f.Id) + if err != nil { + t.Errorf("%s", err) + t.Fail() + } + + if sg != http.StatusOK { + t.Errorf("%s", err) + t.Fail() + } + + if gt.Success != true { + t.Errorf("%s", err) + t.Fail() + } + data, status, err := c.TaskEdit(f) if err != nil { t.Errorf("%s", err) diff --git a/v5_tests/users_test.go b/v5_tests/users_test.go index 6578ab5..b7bd51a 100644 --- a/v5_tests/users_test.go +++ b/v5_tests/users_test.go @@ -2,11 +2,15 @@ package v5_tests import ( "net/http" + "os" + "strconv" "testing" "github.com/retailcrm/api-client-go/v5" ) +var user, _ = strconv.Atoi(os.Getenv("RETAILCRM_USER")) + func TestClient_UsersUsers(t *testing.T) { c := client() f := v5.UsersRequest{ @@ -36,7 +40,7 @@ func TestClient_UsersUsers(t *testing.T) { func TestClient_UsersUser(t *testing.T) { c := client() - data, st, err := c.User(6) + data, st, err := c.User(user) if err != nil { t.Errorf("%s", err) t.Fail() @@ -79,7 +83,7 @@ func TestClient_UsersGroups(t *testing.T) { func TestClient_UsersUpdate(t *testing.T) { c := client() - data, st, err := c.UserStatus(6, "busy") + data, st, err := c.UserStatus(user, "busy") if err != nil { t.Errorf("%s", err) t.Fail()