From 9402aab917f1a49fbcba984477da7524d3f14d19 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Thu, 1 Feb 2018 00:23:19 +0300 Subject: [PATCH] Implode files into single subpackage, add references methods (lists) --- README.md | 48 ++ v5/client.go | 258 +++++++- v5/client_test.go | 1179 ++++++++++++++++++++++++++++++++++++ v5/filters.go | 28 +- v5/request.go | 7 + v5/response.go | 109 ++++ v5/types.go | 306 ++++++++-- v5_tests/base_test.go | 103 ---- v5_tests/customers_test.go | 212 ------- v5_tests/notes_test.go | 100 --- v5_tests/orders_test.go | 212 ------- v5_tests/payments_test.go | 98 --- v5_tests/tasks_test.go | 95 --- v5_tests/users_test.go | 97 --- 14 files changed, 1878 insertions(+), 974 deletions(-) create mode 100644 v5/client_test.go delete mode 100644 v5_tests/base_test.go delete mode 100644 v5_tests/customers_test.go delete mode 100644 v5_tests/notes_test.go delete mode 100644 v5_tests/orders_test.go delete mode 100644 v5_tests/payments_test.go delete mode 100644 v5_tests/tasks_test.go delete mode 100644 v5_tests/users_test.go diff --git a/README.md b/README.md index a9a5b8b..6f9e615 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,51 @@ # retailCRM API Go client Go client for [retailCRM API](http://www.retailcrm.pro/docs/Developers/ApiVersion5). + +## Installation + +```bash +go get -x github.com/retailcrm/api-client-go +``` + +## Usage + +```golang +import ( + c "github.com/retailcrm/api-client-go" +) + +var client = c.Version5("https://demo.retailcrm.ru", "09jIJ09j0JKhgyfvyuUIKhiugF") + +data, status, err := c.Customers(CustomersRequest{ + Filter: CustomersFilter{ + MinCostSumm: 500, + }, + Page: 2, +}) + +if err != nil { + t.Errorf("%s", err) + t.Fail() +} + +if status >= http.StatusBadRequest { + t.Errorf("%s", err) + t.Fail() +} + +var email = data.Customers[0].Email +``` + +## Testing + +```bash +export RETAILCRM_URL="https://demo.retailcrm.ru" +export RETAILCRM_KEY="09jIJ09j0JKhgyfvyuUIKhiugF" +export RETAILCRM_USER="1" + +cd $GOPATH/src/github.com/retailcrm/api-client-go + +go test -v ./... + +``` \ No newline at end of file diff --git a/v5/client.go b/v5/client.go index 379b96d..2f6ef4a 100644 --- a/v5/client.go +++ b/v5/client.go @@ -151,8 +151,8 @@ func (c *Client) ApiCredentials() (*CredentialResponse, int, error) { } // StaticticUpdate update statistic -func (c *Client) StaticticUpdate() (*VersionResponse, int, error) { - var resp VersionResponse +func (c *Client) StaticticUpdate() (*SucessfulResponse, int, error) { + var resp SucessfulResponse data, status, err := c.GetRequest(fmt.Sprintf("%s/statistic/update", versionedPrefix)) if err != nil { @@ -164,6 +164,22 @@ func (c *Client) StaticticUpdate() (*VersionResponse, int, error) { return &resp, status, err } +// Segments get segments +func (c *Client) Segments(parameters SegmentsRequest) (*SegmentsResponse, int, error) { + var resp SegmentsResponse + + params, _ := query.Values(parameters) + + data, status, err := c.GetRequest(fmt.Sprintf("%s/segments?%s", versionedPrefix, params.Encode())) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + // Customer get method func (c *Client) Customer(id, by, site string) (*CustomerResponse, int, error) { var resp CustomerResponse @@ -708,3 +724,241 @@ func (c *Client) PaymentEdit(payment Payment, by string, site ...string) (*Suces return &resp, status, err } + +// Countries method +func (c *Client) Countries() (*CountriesResponse, int, error) { + var resp CountriesResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/countries", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// CostGroups method +func (c *Client) CostGroups() (*CostGroupsResponse, int, error) { + var resp CostGroupsResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/cost-groups", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// CostItems method +func (c *Client) CostItems() (*CostItemsResponse, int, error) { + var resp CostItemsResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/cost-items", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// Couriers method +func (c *Client) Couriers() (*CouriersResponse, int, error) { + var resp CouriersResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/couriers", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// DeliveryService method +func (c *Client) DeliveryService() (*DeliveryServiceResponse, int, error) { + var resp DeliveryServiceResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/delivery-services", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// DeliveryTypes method +func (c *Client) DeliveryTypes() (*DeliveryTypesResponse, int, error) { + var resp DeliveryTypesResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/delivery-types", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// LegalEntities method +func (c *Client) LegalEntities() (*LegalEntitiesResponse, int, error) { + var resp LegalEntitiesResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/legal-entities", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// OrderMethods method +func (c *Client) OrderMethods() (*OrderMethodsResponse, int, error) { + var resp OrderMethodsResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/order-methods", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// OrderTypes method +func (c *Client) OrderTypes() (*OrderTypesResponse, int, error) { + var resp OrderTypesResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/order-types", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// PaymentStatuses method +func (c *Client) PaymentStatuses() (*PaymentStatusesResponse, int, error) { + var resp PaymentStatusesResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/payment-statuses", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// PaymentTypes method +func (c *Client) PaymentTypes() (*PaymentTypesResponse, int, error) { + var resp PaymentTypesResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/payment-types", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// PriceTypes method +func (c *Client) PriceTypes() (*PriceTypesResponse, int, error) { + var resp PriceTypesResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/price-types", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// ProductStatuses method +func (c *Client) ProductStatuses() (*ProductStatusesResponse, int, error) { + var resp ProductStatusesResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/product-statuses", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// Statuses method +func (c *Client) Statuses() (*StatusesResponse, int, error) { + var resp StatusesResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/statuses", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// StatusGroups method +func (c *Client) StatusGroups() (*StatusGroupsResponse, int, error) { + var resp StatusGroupsResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/status-groups", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// Sites method +func (c *Client) Sites() (*SitesResponse, int, error) { + var resp SitesResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/sites", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// Stores method +func (c *Client) Stores() (*StoresResponse, int, error) { + var resp StoresResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/reference/stores", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} diff --git a/v5/client_test.go b/v5/client_test.go new file mode 100644 index 0000000..88185d8 --- /dev/null +++ b/v5/client_test.go @@ -0,0 +1,1179 @@ +package v5 + +import ( + "fmt" + "math/rand" + "net/http" + "net/url" + "os" + "strconv" + "testing" + "time" +) + +var r *rand.Rand // Rand for this package. +var user, _ = strconv.Atoi(os.Getenv("RETAILCRM_USER")) + +func init() { + r = rand.New(rand.NewSource(time.Now().UnixNano())) +} + +func RandomString(strlen int) string { + const chars = "abcdefghijklmnopqrstuvwxyz0123456789" + result := make([]byte, strlen) + + for i := range result { + result[i] = chars[r.Intn(len(chars))] + } + + return string(result) +} + +func client() *Client { + return New(os.Getenv("RETAILCRM_URL"), os.Getenv("RETAILCRM_KEY")) +} + +func TestGetRequest(t *testing.T) { + c := client() + _, status, _ := c.GetRequest("/fake-method") + + if status != http.StatusNotFound { + t.Fail() + } +} + +func TestPostRequest(t *testing.T) { + c := client() + _, status, _ := c.PostRequest("/fake-method", url.Values{}) + + if status != http.StatusNotFound { + t.Fail() + } +} + +func TestClient_ApiVersionsVersions(t *testing.T) { + c := client() + + data, status, err := c.ApiVersions() + if err != nil { + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Fail() + } + + if data.Success != true { + t.Fail() + } +} + +func TestClient_ApiCredentialsCredentials(t *testing.T) { + c := client() + + data, status, err := c.ApiCredentials() + if err != nil { + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Fail() + } + + if data.Success != true { + t.Fail() + } +} + +func TestClient_CustomersCustomers(t *testing.T) { + c := client() + + data, status, err := c.Customers(CustomersRequest{ + Filter: CustomersFilter{ + City: "Москва", + }, + Page: 3, + }) + + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_CustomerChange(t *testing.T) { + c := client() + + f := Customer{ + FirstName: "Понтелей", + LastName: "Турбин", + Patronymic: "Аристархович", + ExternalId: RandomString(8), + Email: fmt.Sprintf("%s@example.com", RandomString(8)), + } + + cr, sc, err := c.CustomerCreate(f) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if sc != http.StatusCreated { + t.Errorf("%v", err) + t.Fail() + } + + if cr.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + f.Id = cr.Id + f.Vip = true + + ed, se, err := c.CustomerEdit(f, "id") + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if se != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if ed.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + data, status, err := c.Customer(f.ExternalId, "externalId", "") + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + if data.Customer.ExternalId != f.ExternalId { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_CustomersUpload(t *testing.T) { + c := client() + customers := make([]Customer, 3) + + for i := range customers { + customers[i] = Customer{ + 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.CustomersUpload(customers) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_CustomersFixExternalIds(t *testing.T) { + c := client() + f := Customer{ + 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.CustomerCreate(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() + } + + customers := []IdentifiersPair{{ + Id: cr.Id, + ExternalId: RandomString(8), + }} + + fx, fe, err := c.CustomersFixExternalIds(customers) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if fe != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if fx.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_CustomersHistory(t *testing.T) { + c := client() + f := CustomersHistoryRequest{ + Filter: CustomersHistoryFilter{ + SinceId: 20, + }, + } + + data, status, err := c.CustomersHistory(f) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + if len(data.History) == 0 { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_NotesNotes(t *testing.T) { + c := client() + + data, status, err := c.Notes(NotesRequest{Page: 1}) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_NotesCreateDelete(t *testing.T) { + c := client() + + createCustomerResponse, createCustomerStatus, err := c.CustomerCreate(Customer{ + FirstName: "Понтелей", + LastName: "Турбин", + Patronymic: "Аристархович", + ExternalId: RandomString(8), + Email: fmt.Sprintf("%s@example.com", RandomString(8)), + }) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if createCustomerStatus != http.StatusCreated { + t.Errorf("%v", err) + t.Fail() + } + + if createCustomerResponse.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + noteCreateResponse, noteCreateStatus, err := c.NoteCreate(Note{ + Text: "some text", + ManagerId: user, + Customer: &Customer{ + Id: createCustomerResponse.Id, + }, + }) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if noteCreateStatus != http.StatusCreated { + t.Errorf("%v", err) + t.Fail() + } + + if noteCreateResponse.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + noteDeleteResponse, noteDeleteStatus, err := c.NoteDelete(noteCreateResponse.Id) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if noteDeleteStatus != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if noteDeleteResponse.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_OrdersOrders(t *testing.T) { + c := client() + + data, status, err := c.Orders(OrdersRequest{Filter: OrdersFilter{City: "Москва"}, Page: 1}) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_OrderChange(t *testing.T) { + c := client() + + random := RandomString(8) + + f := Order{ + FirstName: "Понтелей", + LastName: "Турбин", + Patronymic: "Аристархович", + ExternalId: random, + Email: fmt.Sprintf("%s@example.com", random), + } + + cr, sc, err := c.OrderCreate(f) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if sc != http.StatusCreated { + t.Errorf("%v", err) + t.Fail() + } + + if cr.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + f.Id = cr.Id + f.CustomerComment = "test comment" + + ed, se, err := c.OrderEdit(f, "id") + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if se != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if ed.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + data, status, err := c.Order(f.ExternalId, "externalId", "") + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + if data.Order.ExternalId != f.ExternalId { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_OrdersUpload(t *testing.T) { + c := client() + orders := make([]Order, 3) + + for i := range orders { + orders[i] = 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("%v", err) + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_OrdersFixExternalIds(t *testing.T) { + c := client() + f := 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 := []IdentifiersPair{{ + Id: cr.Id, + ExternalId: RandomString(8), + }} + + fx, fe, err := c.OrdersFixExternalIds(orders) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if fe != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if fx.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_OrdersHistory(t *testing.T) { + c := client() + + data, status, err := c.OrdersHistory(OrdersHistoryRequest{Filter: OrdersHistoryFilter{SinceId: 20}}) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + if len(data.History) == 0 { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_PaymentCreateEditDelete(t *testing.T) { + c := client() + + order := Order{ + FirstName: "Понтелей", + LastName: "Турбин", + Patronymic: "Аристархович", + ExternalId: RandomString(8), + Email: fmt.Sprintf("%s@example.com", RandomString(8)), + } + + createOrderResponse, status, err := c.OrderCreate(order) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status != http.StatusCreated { + t.Errorf("%v", err) + t.Fail() + } + + if createOrderResponse.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + f := Payment{ + Order: &Order{ + Id: createOrderResponse.Id, + }, + Amount: 300, + Type: "cash", + } + + paymentCreateResponse, status, err := c.PaymentCreate(f) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status != http.StatusCreated { + t.Errorf("%v", err) + t.Fail() + } + + if paymentCreateResponse.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + k := Payment{ + Id: paymentCreateResponse.Id, + Amount: 500, + } + + paymentEditResponse, status, err := c.PaymentEdit(k, "id") + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if paymentEditResponse.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + paymentDeleteResponse, status, err := c.PaymentDelete(paymentCreateResponse.Id) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if paymentDeleteResponse.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_TasksTasks(t *testing.T) { + c := client() + + f := TasksRequest{ + Filter: TasksFilter{ + Creators: []int{user}, + }, + Page: 1, + } + + data, status, err := c.Tasks(f) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_TaskChange(t *testing.T) { + c := client() + + f := Task{ + Text: RandomString(15), + PerformerId: user, + } + + cr, sc, err := c.TaskCreate(f) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if sc != http.StatusCreated { + t.Errorf("%v", err) + t.Fail() + } + + if cr.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + f.Id = cr.Id + f.Commentary = RandomString(20) + + gt, sg, err := c.Task(f.Id) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if sg != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if gt.Success != true { + t.Errorf("%v", err) + t.Fail() + } + + data, status, err := c.TaskEdit(f) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_UsersUsers(t *testing.T) { + c := client() + + data, status, err := c.Users(UsersRequest{Filter: UsersFilter{Active: 1}, Page: 1}) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_UsersUser(t *testing.T) { + c := client() + + data, st, err := c.User(user) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_UsersGroups(t *testing.T) { + c := client() + + data, status, err := c.UserGroups(UserGroupsRequest{Page: 1}) + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if status >= http.StatusBadRequest { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_UsersUpdate(t *testing.T) { + c := client() + + data, st, err := c.UserStatus(user, "busy") + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_StaticticUpdate(t *testing.T) { + c := client() + + data, st, err := c.StaticticUpdate() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_Countries(t *testing.T) { + c := client() + + data, st, err := c.Couriers() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_CostGroups(t *testing.T) { + c := client() + + data, st, err := c.CostGroups() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_CostItems(t *testing.T) { + c := client() + + data, st, err := c.CostItems() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_Couriers(t *testing.T) { + c := client() + + data, st, err := c.Couriers() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_DeliveryService(t *testing.T) { + c := client() + + data, st, err := c.DeliveryService() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_DeliveryTypes(t *testing.T) { + c := client() + + data, st, err := c.DeliveryTypes() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_LegalEntities(t *testing.T) { + c := client() + + data, st, err := c.LegalEntities() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_OrderMethods(t *testing.T) { + c := client() + + data, st, err := c.OrderMethods() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_OrderTypes(t *testing.T) { + c := client() + + data, st, err := c.OrderTypes() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_PaymentStatuses(t *testing.T) { + c := client() + + data, st, err := c.PaymentStatuses() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_PaymentTypes(t *testing.T) { + c := client() + + data, st, err := c.PaymentTypes() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_PriceTypes(t *testing.T) { + c := client() + + data, st, err := c.PriceTypes() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_ProductStatuses(t *testing.T) { + c := client() + + data, st, err := c.ProductStatuses() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_Statuses(t *testing.T) { + c := client() + + data, st, err := c.Statuses() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_StatusGroups(t *testing.T) { + c := client() + + data, st, err := c.StatusGroups() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_Sites(t *testing.T) { + c := client() + + data, st, err := c.Sites() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} + +func TestClient_Stores(t *testing.T) { + c := client() + + data, st, err := c.Stores() + if err != nil { + t.Errorf("%v", err) + t.Fail() + } + + if st != http.StatusOK { + t.Errorf("%v", err) + t.Fail() + } + + if data.Success != true { + t.Errorf("%v", err) + t.Fail() + } +} diff --git a/v5/filters.go b/v5/filters.go index 3a6afde..3d1868d 100644 --- a/v5/filters.go +++ b/v5/filters.go @@ -9,14 +9,14 @@ type CustomersFilter struct { Name string `url:"name,omitempty"` Email string `url:"email,omitempty"` Notes string `url:"notes,omitempty"` - MinOrdersCount string `url:"minOrdersCount,omitempty"` - MaxOrdersCount string `url:"maxOrdersCount,omitempty"` - MinAverageSumm string `url:"minAverageSumm,omitempty"` - MaxAverageSumm string `url:"maxAverageSumm,omitempty"` - MinTotalSumm string `url:"minTotalSumm,omitempty"` - MaxTotalSumm string `url:"maxTotalSumm,omitempty"` - MinCostSumm string `url:"minCostSumm,omitempty"` - MaxCostSumm string `url:"maxCostSumm,omitempty"` + MinOrdersCount int `url:"minOrdersCount,omitempty"` + MaxOrdersCount int `url:"maxOrdersCount,omitempty"` + MinAverageSumm float32 `url:"minAverageSumm,omitempty"` + MaxAverageSumm float32 `url:"maxAverageSumm,omitempty"` + MinTotalSumm float32 `url:"minTotalSumm,omitempty"` + MaxTotalSumm float32 `url:"maxTotalSumm,omitempty"` + MinCostSumm float32 `url:"minCostSumm,omitempty"` + MaxCostSumm float32 `url:"maxCostSumm,omitempty"` ClassSegment string `url:"classSegment,omitempty"` Vip int `url:"vip,omitempty"` Bad int `url:"bad,omitempty"` @@ -200,3 +200,15 @@ type NotesFilter struct { CreatedAtFrom string `url:"createdAtFrom,omitempty"` CreatedAtTo string `url:"createdAtTo,omitempty"` } + +// SegmentsFilter type +type SegmentsFilter struct { + Ids []int `url:"ids,omitempty,brackets"` + Active int `url:"active,omitempty,brackets"` + Name string `url:"name,omitempty,brackets"` + Type string `url:"type,omitempty,brackets"` + MinCustomersCount int `url:"minCustomersCount,omitempty,brackets"` + MaxCustomersCount int `url:"maxCustomersCount,omitempty,brackets"` + DateFrom string `url:"dateFrom,omitempty"` + DateTo string `url:"dateTo,omitempty"` +} diff --git a/v5/request.go b/v5/request.go index ee4e3c5..74c727d 100644 --- a/v5/request.go +++ b/v5/request.go @@ -78,3 +78,10 @@ type NotesRequest struct { Limit int `url:"limit,omitempty"` Page int `url:"page,omitempty"` } + +// SegmentsRequest type +type SegmentsRequest struct { + Filter SegmentsFilter `url:"filter,omitempty"` + Limit int `url:"limit,omitempty"` + Page int `url:"page,omitempty"` +} diff --git a/v5/response.go b/v5/response.go index f01a333..a6ac635 100644 --- a/v5/response.go +++ b/v5/response.go @@ -141,3 +141,112 @@ type NotesResponse struct { Pagination *Pagination `json:"pagination,omitempty"` Notes []Note `json:"notes,omitempty,brackets"` } + +// SegmentsResponse type +type SegmentsResponse struct { + Success bool `json:"success"` + Pagination *Pagination `json:"pagination,omitempty"` + Segments []Segment `json:"segments,omitempty,brackets"` +} + +// CountriesResponse type +type CountriesResponse struct { + Success bool `json:"success"` + CountriesIso []string `json:"countriesIso,omitempty,brackets"` +} + +// CostGroupsResponse type +type CostGroupsResponse struct { + Success bool `json:"success"` + CostGroups []CostGroup `json:"costGroups,omitempty,brackets"` +} + +// CostItemsResponse type +type CostItemsResponse struct { + Success bool `json:"success"` + CostItems []CostItem `json:"costItems,omitempty,brackets"` +} + +// CouriersResponse type +type CouriersResponse struct { + Success bool `json:"success"` + Couriers []Courier `json:"couriers,omitempty,brackets"` +} + +// DeliveryServiceResponse type +type DeliveryServiceResponse struct { + Success bool `json:"success"` + DeliveryServices map[string]DeliveryService `json:"deliveryServices,omitempty,brackets"` +} + +// DeliveryTypesResponse type +type DeliveryTypesResponse struct { + Success bool `json:"success"` + DeliveryTypes map[string]DeliveryType `json:"deliveryTypes,omitempty,brackets"` +} + +// LegalEntitiesResponse type +type LegalEntitiesResponse struct { + Success bool `json:"success"` + LegalEntities []LegalEntity `json:"legalEntities,omitempty,brackets"` +} + +// OrderMethodsResponse type +type OrderMethodsResponse struct { + Success bool `json:"success"` + OrderMethods map[string]OrderMethod `json:"orderMethods,omitempty,brackets"` +} + +// OrderTypesResponse type +type OrderTypesResponse struct { + Success bool `json:"success"` + OrderTypes map[string]OrderType `json:"orderTypes,omitempty,brackets"` +} + +// PaymentStatusesResponse type +type PaymentStatusesResponse struct { + Success bool `json:"success"` + PaymentStatuses map[string]PaymentStatus `json:"paymentStatuses,omitempty,brackets"` +} + +// PaymentTypesResponse type +type PaymentTypesResponse struct { + Success bool `json:"success"` + PaymentTypes map[string]PaymentType `json:"paymentTypes,omitempty,brackets"` +} + +// PriceTypesResponse type +type PriceTypesResponse struct { + Success bool `json:"success"` + PriceTypes []PriceType `json:"priceTypes,omitempty,brackets"` +} + +// ProductStatusesResponse type +type ProductStatusesResponse struct { + Success bool `json:"success"` + ProductStatuses map[string]ProductStatus `json:"productStatuses,omitempty,brackets"` +} + +// StatusesResponse type +type StatusesResponse struct { + Success bool `json:"success"` + Statuses map[string]Status `json:"statuses,omitempty,brackets"` +} + +// StatusGroupsResponse type +type StatusGroupsResponse struct { + Success bool `json:"success"` + StatusGroups map[string]StatusGroup `json:"statusGroups,omitempty,brackets"` +} + +// SitesResponse type +type SitesResponse struct { + Success bool `json:"success"` + Sites map[string]Site `json:"sites,omitempty,brackets"` +} + +// StoresResponse type +type StoresResponse struct { + Success bool `json:"success"` + Stores []Store `json:"stores,omitempty,brackets"` +} diff --git a/v5/types.go b/v5/types.go index 6f7a9f3..4578b4b 100644 --- a/v5/types.go +++ b/v5/types.go @@ -40,6 +40,15 @@ type Address struct { Text string `json:"text,omitempty"` } +// GeoHierarchyRow type +type GeoHierarchyRow struct { + Country string `json:"country,omitempty"` + Region string `json:"region,omitempty"` + RegionId int `json:"regionId,omitempty"` + City string `json:"city,omitempty"` + CityId int `json:"cityId,omitempty"` +} + // Source type type Source struct { Source string `json:"source,omitempty"` @@ -92,42 +101,42 @@ Customer related types // Customer type type Customer struct { - Id int `json:"id,omitempty"` - ExternalId string `json:"externalId,omitempty"` - FirstName string `json:"firstName,omitempty"` - LastName string `json:"lastName,omitempty"` - Patronymic string `json:"patronymic,omitempty"` - Sex string `json:"sex,omitempty"` - Email string `json:"email,omitempty"` - Phones []CustomerPhone `json:"phones,brackets,omitempty"` - Address *Address `json:"address,omitempty"` - CreatedAt string `json:"createdAt,omitempty"` - Birthday string `json:"birthday,omitempty"` - ManagerId int `json:"managerId,omitempty"` - Vip bool `json:"vip,omitempty"` - Bad bool `json:"bad,omitempty"` - Site string `json:"site,omitempty"` - Source *Source `json:"source,omitempty"` - Contragent *Contragent `json:"contragent,omitempty"` - PersonalDiscount float32 `json:"personalDiscount,omitempty"` - CumulativeDiscount float32 `json:"cumulativeDiscount,omitempty"` - DiscountCardNumber string `json:"discountCardNumber,omitempty"` - EmailMarketingUnsubscribedAt string `json:"emailMarketingUnsubscribedAt,omitempty"` - AvgMarginSumm float32 `json:"avgMarginSumm,omitempty"` - MarginSumm float32 `json:"marginSumm,omitempty"` - TotalSumm float32 `json:"totalSumm,omitempty"` - AverageSumm float32 `json:"averageSumm,omitempty"` - OrdersCount int `json:"ordersCount,omitempty"` - CostSumm float32 `json:"costSumm,omitempty"` - MaturationTime int `json:"maturationTime,omitempty"` - FirstClientId string `json:"firstClientId,omitempty"` - LastClientId string `json:"lastClientId,omitempty"` - BrowserId string `json:"browserId,omitempty"` + Id int `json:"id,omitempty"` + ExternalId string `json:"externalId,omitempty"` + FirstName string `json:"firstName,omitempty"` + LastName string `json:"lastName,omitempty"` + Patronymic string `json:"patronymic,omitempty"` + Sex string `json:"sex,omitempty"` + Email string `json:"email,omitempty"` + Phones []Phone `json:"phones,brackets,omitempty"` + Address *Address `json:"address,omitempty"` + CreatedAt string `json:"createdAt,omitempty"` + Birthday string `json:"birthday,omitempty"` + ManagerId int `json:"managerId,omitempty"` + Vip bool `json:"vip,omitempty"` + Bad bool `json:"bad,omitempty"` + Site string `json:"site,omitempty"` + Source *Source `json:"source,omitempty"` + Contragent *Contragent `json:"contragent,omitempty"` + PersonalDiscount float32 `json:"personalDiscount,omitempty"` + CumulativeDiscount float32 `json:"cumulativeDiscount,omitempty"` + DiscountCardNumber string `json:"discountCardNumber,omitempty"` + EmailMarketingUnsubscribedAt string `json:"emailMarketingUnsubscribedAt,omitempty"` + AvgMarginSumm float32 `json:"avgMarginSumm,omitempty"` + MarginSumm float32 `json:"marginSumm,omitempty"` + TotalSumm float32 `json:"totalSumm,omitempty"` + AverageSumm float32 `json:"averageSumm,omitempty"` + OrdersCount int `json:"ordersCount,omitempty"` + CostSumm float32 `json:"costSumm,omitempty"` + MaturationTime int `json:"maturationTime,omitempty"` + FirstClientId string `json:"firstClientId,omitempty"` + LastClientId string `json:"lastClientId,omitempty"` + BrowserId string `json:"browserId,omitempty"` // CustomFields []map[string]string `json:"customFields,omitempty,brackets"` } -// CustomerPhone type -type CustomerPhone struct { +// Phone type +type Phone struct { Number string `json:"number,omitempty"` } @@ -331,20 +340,6 @@ type UserGroup struct { GrantedSites []string `json:"grantedSites,omitempty,brackets"` } -/** -Reference related types -*/ - -// PriceType type -type PriceType struct { - Name string `json:"name,omitempty"` - Code string `json:"code,omitempty"` - Description string `json:"description,omitempty"` - FilterExpression string `json:"filterExpression,omitempty"` - Active bool `json:"active,omitempty"` - Ordering int `json:"ordering,omitempty"` -} - /** Task related types */ @@ -394,3 +389,220 @@ type Payment struct { Type string `json:"type,omitempty"` Order *Order `json:"order,omitempty"` } + +/* + Segment related types +*/ + +// Segment type +type Segment struct { + Id int `json:"id,omitempty"` + Code string `json:"code,omitempty"` + Name string `json:"name,omitempty"` + CreatedAt string `json:"createdAt,omitempty"` + CustomersCount int `json:"customersCount,omitempty"` + IsDynamic bool `json:"isDynamic,omitempty"` + Active bool `json:"active,omitempty"` +} + +/** +Reference related types +*/ + +// CostGroup type +type CostGroup struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Color string `json:"color,omitempty"` + Active bool `json:"active,omitempty"` + Ordering int `json:"ordering,omitempty"` +} + +// CostItem type +type CostItem struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Group string `json:"group,omitempty"` + Type string `json:"type,omitempty"` + Active bool `json:"active,omitempty"` + AppliesToOrders bool `json:"appliesToOrders,omitempty"` + AppliesToUsers bool `json:"appliesToUsers,omitempty"` + Ordering int `json:"ordering,omitempty"` + Source *Source `json:"source,omitempty"` +} + +// Courier type +type Courier struct { + Id int `json:"id,omitempty"` + FirstName string `json:"firstName,omitempty"` + LastName string `json:"lastName,omitempty"` + Patronymic string `json:"patronymic,omitempty"` + Email string `json:"email,omitempty"` + Description string `json:"description,omitempty"` + Active bool `json:"active,omitempty"` + Phone *Phone `json:"phone,omitempty"` +} + +// DeliveryService type +type DeliveryService struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Active bool `json:"active,omitempty"` +} + +// DeliveryType type +type DeliveryType struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Active bool `json:"active,omitempty"` + DefaultCost float32 `json:"defaultCost,omitempty"` + DefaultNetCost float32 `json:"defaultNetCost,omitempty"` + Description string `json:"description,omitempty"` + IntegrationCode string `json:"integrationCode,omitempty"` + VatRate string `json:"vatRate,omitempty"` + DefaultForCrm bool `json:"defaultForCrm,omitempty"` + DeliveryServices []string `json:"deliveryServices,omitempty"` + PaymentTypes []string `json:"paymentTypes,omitempty"` +} + +// LegalEntity type +type LegalEntity struct { + Code string `json:"code,omitempty"` + VatRate string `json:"vatRate,omitempty"` + CountryIso string `json:"countryIso,omitempty"` + ContragentType string `json:"contragentType,omitempty"` + LegalName string `json:"legalName,omitempty"` + LegalAddress string `json:"legalAddress,omitempty"` + INN string `json:"INN,omitempty"` + OKPO string `json:"OKPO,omitempty"` + KPP string `json:"KPP,omitempty"` + OGRN string `json:"OGRN,omitempty"` + OGRNIP string `json:"OGRNIP,omitempty"` + CertificateNumber string `json:"certificateNumber,omitempty"` + CertificateDate string `json:"certificateDate,omitempty"` + BIK string `json:"BIK,omitempty"` + Bank string `json:"bank,omitempty"` + BankAddress string `json:"bankAddress,omitempty"` + CorrAccount string `json:"corrAccount,omitempty"` + BankAccount string `json:"bankAccount,omitempty"` +} + +// OrderMethod type +type OrderMethod struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Active bool `json:"active,omitempty"` + DefaultForCrm bool `json:"defaultForCrm,omitempty"` + DefaultForApi bool `json:"defaultForApi,omitempty"` +} + +// OrderType type +type OrderType struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Active bool `json:"active,omitempty"` + DefaultForCrm bool `json:"defaultForCrm,omitempty"` + DefaultForApi bool `json:"defaultForApi,omitempty"` +} + +// PaymentStatus type +type PaymentStatus struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Active bool `json:"active,omitempty"` + DefaultForCrm bool `json:"defaultForCrm,omitempty"` + DefaultForApi bool `json:"defaultForApi,omitempty"` + PaymentComplete bool `json:"paymentComplete,omitempty"` + Description string `json:"description,omitempty"` + Ordering int `json:"ordering,omitempty"` + PaymentTypes []string `json:"paymentTypes,omitempty,brackets"` +} + +// PaymentType type +type PaymentType struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Active bool `json:"active,omitempty"` + DefaultForCrm bool `json:"defaultForCrm,omitempty"` + DefaultForApi bool `json:"defaultForApi,omitempty"` + Description string `json:"description,omitempty"` + DeliveryTypes []string `json:"deliveryTypes,omitempty,brackets"` + PaymentStatuses []string `json:"PaymentStatuses,omitempty,brackets"` +} + +// PriceType type +type PriceType struct { + Id int `json:"id,omitempty"` + Code string `json:"code,omitempty"` + Name string `json:"name,omitempty"` + Active bool `json:"active,omitempty"` + Default bool `json:"default,omitempty"` + Description string `json:"description,omitempty"` + FilterExpression string `json:"filterExpression,omitempty"` + Ordering int `json:"ordering,omitempty"` + Groups []string `json:"groups,omitempty,brackets"` + Geo []GeoHierarchyRow `json:"geo,omitempty,brackets"` +} + +// ProductStatus type +type ProductStatus struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Active bool `json:"active,omitempty"` + Ordering int `json:"ordering,omitempty"` + CreatedAt string `json:"createdAt,omitempty"` + CancelStatus bool `json:"cancelStatus,omitempty"` + OrderStatusByProductStatus string `json:"orderStatusByProductStatus,omitempty"` + OrderStatusForProductStatus string `json:"orderStatusForProductStatus,omitempty"` +} + +// Status type +type Status struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Active bool `json:"active,omitempty"` + Ordering int `json:"ordering,omitempty"` + Group string `json:"group,omitempty"` +} + +// StatusGroup type +type StatusGroup struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Active bool `json:"active,omitempty"` + Ordering int `json:"ordering,omitempty"` + Process bool `json:"process,omitempty"` + Statuses []string `json:"statuses,omitempty,brackets"` +} + +// Site type +type Site struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + Url string `json:"url,omitempty"` + Description string `json:"description,omitempty"` + Phones string `json:"phones,omitempty"` + Zip string `json:"zip,omitempty"` + Address string `json:"address,omitempty"` + CountryIso string `json:"countryIso,omitempty"` + YmlUrl string `json:"ymlUrl,omitempty"` + LoadFromYml bool `json:"loadFromYml,omitempty"` + CatalogUpdatedAt string `json:"catalogUpdatedAt,omitempty"` + CatalogLoadingAt string `json:"catalogLoadingAt,omitempty"` + Contragent *LegalEntity `json:"contragent,omitempty"` +} + +// Store type +type Store struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + ExternalId string `json:"externalId,omitempty"` + Description string `json:"description,omitempty"` + XmlId string `json:"xmlId,omitempty"` + Email string `json:"email,omitempty"` + Type string `json:"type,omitempty"` + InventoryType string `json:"inventoryType,omitempty"` + Active bool `json:"active,omitempty"` + Phone *Phone `json:"phone,omitempty"` + Address *Address `json:"address,omitempty"` +} diff --git a/v5_tests/base_test.go b/v5_tests/base_test.go deleted file mode 100644 index 4059ac4..0000000 --- a/v5_tests/base_test.go +++ /dev/null @@ -1,103 +0,0 @@ -package v5_tests - -import ( - "math/rand" - "net/http" - "net/url" - "os" - "strconv" - "testing" - "time" - - "github.com/retailcrm/api-client-go/v5" -) - -type Configuration struct { - Url string `json:"url"` - Key string `json:"key"` -} - -func buildConfiguration() *Configuration { - return &Configuration{ - os.Getenv("RETAILCRM_URL"), - os.Getenv("RETAILCRM_KEY"), - } -} - -var r *rand.Rand // Rand for this package. - -func init() { - r = rand.New(rand.NewSource(time.Now().UnixNano())) -} - -func GetUser() int { - uid, _ := strconv.Atoi(os.Getenv("RETAILCRM_USER")) - - return uid -} - -func RandomString(strlen int) string { - const chars = "abcdefghijklmnopqrstuvwxyz0123456789" - result := make([]byte, strlen) - - for i := range result { - result[i] = chars[r.Intn(len(chars))] - } - - return string(result) -} - -func client() *v5.Client { - configuration := buildConfiguration() - return v5.New(configuration.Url, configuration.Key) -} - -func TestGetRequest(t *testing.T) { - c := client() - _, status, _ := c.GetRequest("/fake-method") - - if status != http.StatusNotFound { - t.Fail() - } -} - -func TestPostRequest(t *testing.T) { - c := client() - _, status, _ := c.PostRequest("/fake-method", url.Values{}) - - if status != http.StatusNotFound { - t.Fail() - } -} - -func TestClient_ApiVersionsVersions(t *testing.T) { - c := client() - data, status, err := c.ApiVersions() - if err != nil { - t.Fail() - } - - if status >= http.StatusBadRequest { - t.Fail() - } - - if data.Success != true { - t.Fail() - } -} - -func TestClient_ApiCredentialsCredentials(t *testing.T) { - c := client() - data, status, err := c.ApiCredentials() - if err != nil { - t.Fail() - } - - if status >= http.StatusBadRequest { - t.Fail() - } - - if data.Success != true { - t.Fail() - } -} diff --git a/v5_tests/customers_test.go b/v5_tests/customers_test.go deleted file mode 100644 index 18a589b..0000000 --- a/v5_tests/customers_test.go +++ /dev/null @@ -1,212 +0,0 @@ -package v5_tests - -import ( - "fmt" - "net/http" - "testing" - - "github.com/retailcrm/api-client-go/v5" -) - -func TestClient_CustomersCustomers(t *testing.T) { - c := client() - f := v5.CustomersRequest{ - Filter: v5.CustomersFilter{ - City: "Москва", - }, - Page: 3, - } - - data, status, err := c.Customers(f) - 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_CustomerChange(t *testing.T) { - c := client() - - random := RandomString(8) - - f := v5.Customer{ - FirstName: "Понтелей", - LastName: "Турбин", - Patronymic: "Аристархович", - ExternalId: random, - Email: fmt.Sprintf("%s@example.com", random), - } - - cr, sc, err := c.CustomerCreate(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.Vip = true - - ed, se, err := c.CustomerEdit(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.Customer(f.ExternalId, "externalId", "") - 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() - } - - if data.Customer.ExternalId != f.ExternalId { - t.Errorf("%s", err) - t.Fail() - } -} - -func TestClient_CustomersUpload(t *testing.T) { - c := client() - customers := make([]v5.Customer, 3) - - for i := range customers { - customers[i] = v5.Customer{ - 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.CustomersUpload(customers) - 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_CustomersFixExternalIds(t *testing.T) { - c := client() - f := v5.Customer{ - 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.CustomerCreate(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() - } - - customers := []v5.IdentifiersPair{{ - Id: cr.Id, - ExternalId: RandomString(8), - }} - - fx, fe, err := c.CustomersFixExternalIds(customers) - 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_CustomersHistory(t *testing.T) { - c := client() - f := v5.CustomersHistoryRequest{ - Filter: v5.CustomersHistoryFilter{ - SinceId: 20, - }, - } - - data, status, err := c.CustomersHistory(f) - 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() - } - - if len(data.History) == 0 { - t.Errorf("%s", err) - t.Fail() - } -} diff --git a/v5_tests/notes_test.go b/v5_tests/notes_test.go deleted file mode 100644 index a4f946e..0000000 --- a/v5_tests/notes_test.go +++ /dev/null @@ -1,100 +0,0 @@ -package v5_tests - -import ( - "fmt" - "net/http" - "testing" - - "github.com/retailcrm/api-client-go/v5" -) - -func TestClient_NotesNotes(t *testing.T) { - c := client() - f := v5.NotesRequest{ - Page: 1, - } - - data, status, err := c.Notes(f) - if err != nil { - t.Errorf("%s", err) - t.Fail() - } - - if status != http.StatusOK { - t.Errorf("%s", err) - t.Fail() - } - - if data.Success != true { - t.Errorf("%s", err) - t.Fail() - } -} - -func TestClient_NotesCreateDelete(t *testing.T) { - c := client() - - customer := v5.Customer{ - FirstName: "Понтелей", - LastName: "Турбин", - Patronymic: "Аристархович", - ExternalId: RandomString(8), - Email: fmt.Sprintf("%s@example.com", RandomString(8)), - } - - createCustomerResponse, createCustomerStatus, err := c.CustomerCreate(customer) - if err != nil { - t.Errorf("%s", err) - t.Fail() - } - - if createCustomerStatus != http.StatusCreated { - t.Errorf("%s", err) - t.Fail() - } - - if createCustomerResponse.Success != true { - t.Errorf("%s", err) - t.Fail() - } - - f := v5.Note{ - Text: "some text", - ManagerId: GetUser(), - Customer: &v5.Customer{ - Id: createCustomerResponse.Id, - }, - } - - noteCreateResponse, noteCreateStatus, err := c.NoteCreate(f) - if err != nil { - t.Errorf("%s", err) - t.Fail() - } - - if noteCreateStatus != http.StatusCreated { - t.Errorf("%s", err) - t.Fail() - } - - if noteCreateResponse.Success != true { - t.Errorf("%s", err) - t.Fail() - } - - noteDeleteResponse, noteDeleteStatus, err := c.NoteDelete(noteCreateResponse.Id) - if err != nil { - t.Errorf("%s", err) - t.Fail() - } - - if noteDeleteStatus != http.StatusOK { - t.Errorf("%s", err) - t.Fail() - } - - if noteDeleteResponse.Success != true { - t.Errorf("%s", err) - t.Fail() - } -} diff --git a/v5_tests/orders_test.go b/v5_tests/orders_test.go deleted file mode 100644 index 3158eb4..0000000 --- a/v5_tests/orders_test.go +++ /dev/null @@ -1,212 +0,0 @@ -package v5_tests - -import ( - "fmt" - "net/http" - "testing" - - "github.com/retailcrm/api-client-go/v5" -) - -func TestClient_OrdersOrders(t *testing.T) { - c := client() - f := v5.OrdersRequest{ - Filter: v5.OrdersFilter{ - City: "Москва", - }, - Page: 1, - } - - data, status, err := c.Orders(f) - 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_OrderChange(t *testing.T) { - c := client() - - 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() - } - - if status >= http.StatusBadRequest { - t.Errorf("%s", err) - t.Fail() - } - - if data.Success != true { - 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) { - c := client() - f := v5.OrdersHistoryRequest{ - Filter: v5.OrdersHistoryFilter{ - SinceId: 20, - }, - } - - data, status, err := c.OrdersHistory(f) - 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() - } - - if len(data.History) == 0 { - t.Errorf("%s", err) - t.Fail() - } -} diff --git a/v5_tests/payments_test.go b/v5_tests/payments_test.go deleted file mode 100644 index ad13eaf..0000000 --- a/v5_tests/payments_test.go +++ /dev/null @@ -1,98 +0,0 @@ -package v5_tests - -import ( - "fmt" - "net/http" - "testing" - - "github.com/retailcrm/api-client-go/v5" -) - -func TestClient_PaymentCreateEditDelete(t *testing.T) { - c := client() - - order := v5.Order{ - FirstName: "Понтелей", - LastName: "Турбин", - Patronymic: "Аристархович", - ExternalId: RandomString(8), - Email: fmt.Sprintf("%s@example.com", RandomString(8)), - } - - createOrderResponse, status, err := c.OrderCreate(order) - if err != nil { - t.Errorf("%s", err) - t.Fail() - } - - if status != http.StatusCreated { - t.Errorf("%s", err) - t.Fail() - } - - if createOrderResponse.Success != true { - t.Errorf("%s", err) - t.Fail() - } - - f := v5.Payment{ - Order: &v5.Order{ - Id: createOrderResponse.Id, - }, - Amount: 300, - Type: "cash", - } - - paymentCreateResponse, status, err := c.PaymentCreate(f) - if err != nil { - t.Errorf("%s", err) - t.Fail() - } - - if status != http.StatusCreated { - t.Errorf("%s", err) - t.Fail() - } - - if paymentCreateResponse.Success != true { - t.Errorf("%s", err) - t.Fail() - } - - k := v5.Payment{ - Id: paymentCreateResponse.Id, - Amount: 500, - } - - paymentEditResponse, status, err := c.PaymentEdit(k, "id") - if err != nil { - t.Errorf("%s", err) - t.Fail() - } - - if status != http.StatusOK { - t.Errorf("%s", err) - t.Fail() - } - - if paymentEditResponse.Success != true { - t.Errorf("%s", err) - t.Fail() - } - - paymentDeleteResponse, status, err := c.PaymentDelete(paymentCreateResponse.Id) - if err != nil { - t.Errorf("%s", err) - t.Fail() - } - - if status != http.StatusOK { - t.Errorf("%s", err) - t.Fail() - } - - if paymentDeleteResponse.Success != true { - t.Errorf("%s", err) - t.Fail() - } -} diff --git a/v5_tests/tasks_test.go b/v5_tests/tasks_test.go deleted file mode 100644 index d635a97..0000000 --- a/v5_tests/tasks_test.go +++ /dev/null @@ -1,95 +0,0 @@ -package v5_tests - -import ( - "net/http" - "testing" - - "github.com/retailcrm/api-client-go/v5" -) - -func TestClient_TasksTasks(t *testing.T) { - c := client() - - f := v5.TasksRequest{ - Filter: v5.TasksFilter{ - Creators: []int{GetUser()}, - }, - Page: 1, - } - - data, status, err := c.Tasks(f) - if err != nil { - t.Errorf("%s", err) - t.Fail() - } - - if status != http.StatusOK { - t.Errorf("%s", err) - t.Fail() - } - - if data.Success != true { - t.Errorf("%s", err) - t.Fail() - } -} - -func TestClient_TaskChange(t *testing.T) { - c := client() - - f := v5.Task{ - Text: RandomString(15), - PerformerId: GetUser(), - } - - cr, sc, err := c.TaskCreate(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.Commentary = RandomString(20) - - 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) - t.Fail() - } - - if status != http.StatusOK { - t.Errorf("%s", err) - t.Fail() - } - - if data.Success != true { - t.Errorf("%s", err) - t.Fail() - } -} diff --git a/v5_tests/users_test.go b/v5_tests/users_test.go deleted file mode 100644 index 3b48a94..0000000 --- a/v5_tests/users_test.go +++ /dev/null @@ -1,97 +0,0 @@ -package v5_tests - -import ( - "net/http" - "testing" - - "github.com/retailcrm/api-client-go/v5" -) - -func TestClient_UsersUsers(t *testing.T) { - c := client() - f := v5.UsersRequest{ - Filter: v5.UsersFilter{ - Active: 1, - }, - Page: 1, - } - - data, status, err := c.Users(f) - 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_UsersUser(t *testing.T) { - c := client() - - data, st, err := c.User(GetUser()) - 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_UsersGroups(t *testing.T) { - c := client() - f := v5.UserGroupsRequest{ - Page: 1, - } - - data, status, err := c.UserGroups(f) - 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_UsersUpdate(t *testing.T) { - c := client() - - data, st, err := c.UserStatus(GetUser(), "busy") - 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() - } -}