From d6be0230e891c2a2b0c39dcffd061ac84165eab6 Mon Sep 17 00:00:00 2001 From: Alex Lushpai Date: Mon, 29 Jan 2018 14:19:51 +0300 Subject: [PATCH] users & tasks methods --- v5/client.go | 148 +++++++++++++++++++++++++++++++++++++ v5/filters.go | 25 +++++++ v5/request.go | 20 +++++ v5/response.go | 39 ++++++++++ v5/types.go | 136 +++++++++++++++++++++++++--------- v5_tests/customers_test.go | 2 +- v5_tests/orders_test.go | 4 +- v5_tests/tasks_test.go | 101 +++++++++++++++++++++++++ v5_tests/users_test.go | 97 ++++++++++++++++++++++++ 9 files changed, 533 insertions(+), 39 deletions(-) create mode 100644 v5_tests/tasks_test.go create mode 100644 v5_tests/users_test.go diff --git a/v5/client.go b/v5/client.go index ee1b717..934306f 100644 --- a/v5/client.go +++ b/v5/client.go @@ -342,3 +342,151 @@ func (c *Client) OrdersHistory(parameters OrdersHistoryRequest) (*CustomersHisto return &resp, status, err } + +// User get method +func (c *Client) User(id int) (*UserResponse, int, error) { + var resp UserResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/users/%d", versionedPrefix, id)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// Users list method +func (c *Client) Users(parameters UsersRequest) (*UsersResponse, int, error) { + var resp UsersResponse + + params, _ := query.Values(parameters) + + data, status, err := c.GetRequest(fmt.Sprintf("%s/users?%s", versionedPrefix, params.Encode())) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// UserGroups list method +func (c *Client) UserGroups(parameters UserGroupsRequest) (*UserGroupsResponse, int, error) { + var resp UserGroupsResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/user-groups", versionedPrefix)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// UserStatus update method +func (c *Client) UserStatus(id int, status string) (*SucessfulResponse, int, error) { + var resp SucessfulResponse + + p := url.Values{ + "status": {string(status)}, + } + + data, st, err := c.PostRequest(fmt.Sprintf("%s/users/%d/status", versionedPrefix, id), p) + if err != nil { + return &resp, st, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, st, err +} + +// Task get method +func (c *Client) Task(id int) (*TaskResponse, int, error) { + var resp TaskResponse + + data, status, err := c.GetRequest(fmt.Sprintf("%s/tasks/%d", versionedPrefix, id)) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// Tasks list method +func (c *Client) Tasks(parameters TasksRequest) (*TasksResponse, int, error) { + var resp TasksResponse + + params, _ := query.Values(parameters) + + data, status, err := c.GetRequest(fmt.Sprintf("%s/tasks?%s", versionedPrefix, params.Encode())) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// TaskCreate method +func (c *Client) TaskCreate(task Task, site ...string) (*TaskChangeResponse, int, error) { + var resp TaskChangeResponse + taskJson, _ := json.Marshal(&task) + + p := url.Values{ + "task": {string(taskJson[:])}, + } + + if len(site) > 0 { + s := site[0] + + if s != "" { + p.Add("site", s) + } + } + + data, status, err := c.PostRequest(fmt.Sprintf("%s/tasks/create", versionedPrefix), p) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} + +// TaskEdit method +func (c *Client) TaskEdit(task Task, site ...string) (*SucessfulResponse, int, error) { + var resp SucessfulResponse + var uid = strconv.Itoa(task.Id) + + taskJson, _ := json.Marshal(&task) + + p := url.Values{ + "task": {string(taskJson[:])}, + } + + if len(site) > 0 { + s := site[0] + + if s != "" { + p.Add("site", s) + } + } + + data, status, err := c.PostRequest(fmt.Sprintf("%s/tasks/%s/edit", versionedPrefix, uid), p) + if err != nil { + return &resp, status, err + } + + err = json.Unmarshal(data, &resp) + + return &resp, status, err +} diff --git a/v5/filters.go b/v5/filters.go index 233a8f9..42f9906 100644 --- a/v5/filters.go +++ b/v5/filters.go @@ -164,3 +164,28 @@ type OrdersHistoryFilter struct { StartDate string `url:"startDate,omitempty"` EndDate string `url:"endDate,omitempty"` } + +// UsersFilter type +type UsersFilter struct { + Email string `url:"email,omitempty"` + Status string `url:"status,omitempty"` + Online int `url:"online,omitempty"` + Active int `url:"active,omitempty"` + IsManager int `url:"isManager,omitempty"` + IsAdmin int `url:"isAdmin,omitempty"` + CreatedAtFrom string `url:"createdAtFrom,omitempty"` + CreatedAtTo string `url:"createdAtTo,omitempty"` + Groups []string `url:"groups,omitempty"` +} + +// TasksFilter type +type TasksFilter struct { + OrderNumber string `url:"orderNumber,omitempty"` + Status string `url:"status,omitempty"` + Customer string `url:"customer,omitempty"` + Text string `url:"text,omitempty"` + DateFrom string `url:"dateFrom,omitempty"` + DateTo string `url:"dateTo,omitempty"` + Creators []int `url:"creators,omitempty"` + Performers []int `url:"performers,omitempty"` +} diff --git a/v5/request.go b/v5/request.go index 915ce69..857a7a3 100644 --- a/v5/request.go +++ b/v5/request.go @@ -51,3 +51,23 @@ type OrdersHistoryRequest struct { Limit int `url:"limit,omitempty"` Page int `url:"page,omitempty"` } + +// UsersRequest type +type UsersRequest struct { + Filter UsersFilter `url:"filter,omitempty"` + Limit int `url:"limit,omitempty"` + Page int `url:"page,omitempty"` +} + +// UserGroupsRequest type +type UserGroupsRequest struct { + Limit int `url:"limit,omitempty"` + Page int `url:"page,omitempty"` +} + +// TasksRequest type +type TasksRequest struct { + Filter TasksFilter `url:"filter,omitempty"` + Limit int `url:"limit,omitempty"` + Page int `url:"page,omitempty"` +} diff --git a/v5/response.go b/v5/response.go index 1e556e7..b4c3e24 100644 --- a/v5/response.go +++ b/v5/response.go @@ -89,3 +89,42 @@ type OrdersHistoryResponse struct { History []OrdersHistoryRecord `json:"history,omitempty,brackets"` Pagination *Pagination `json:"pagination,omitempty"` } + +// UserResponse type +type UserResponse struct { + Success bool `json:"success"` + User *User `json:"user,omitempty,brackets"` +} + +// UsersResponse type +type UsersResponse struct { + Success bool `json:"success"` + Pagination *Pagination `json:"pagination,omitempty"` + Users []User `json:"users,omitempty,brackets"` +} + +// UserGroupsResponse type +type UserGroupsResponse struct { + Success bool `json:"success"` + Pagination *Pagination `json:"pagination,omitempty"` + Groups []UserGroup `json:"groups,omitempty,brackets"` +} + +// TaskResponse type +type TaskResponse struct { + Success bool `json:"success"` + Task *Task `json:"task,omitempty,brackets"` +} + +// TaskChangeResponse type +type TaskChangeResponse struct { + Success bool `json:"success"` + Id int `json:"id,omitempty"` +} + +// TasksResponse type +type TasksResponse struct { + Success bool `json:"success"` + Pagination *Pagination `json:"pagination,omitempty"` + Tasks []Task `json:"tasks,omitempty,brackets"` +} diff --git a/v5/types.go b/v5/types.go index 52066a8..7ec2c3c 100644 --- a/v5/types.go +++ b/v5/types.go @@ -78,40 +78,44 @@ type Property struct { Value string `json:"value,omitempty"` } +/** +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"` - CustomFields []map[string]string `json:"customFields,omitempty,brackets"` + 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"` + //CustomFields []map[string]string `json:"customFields,omitempty,brackets"` } // CustomerPhone type @@ -138,6 +142,10 @@ type CustomerHistoryRecord struct { Customer *Customer `json:"customer,omitempty,brackets"` } +/** +Order related types +*/ + // Order type type Order struct { Id int `json:"id,omitempty"` @@ -270,6 +278,7 @@ type OrdersHistoryRecord struct { Order *Order `json:"order,omitempty,brackets"` } +// Offer type type Offer struct { Id int `json:"id,omitempty"` ExternalId string `json:"externalId,omitempty"` @@ -277,13 +286,47 @@ type Offer struct { VatRate string `json:"vatRate,omitempty"` } +/** +User related types +*/ + +// User type type User struct { - Id int `json:"id,omitempty"` - FirstName string `json:"firstName,omitempty"` - LastName string `json:"lastName,omitempty"` - Patronymic string `json:"patronymic,omitempty"` + Id int `json:"id,omitempty"` + FirstName string `json:"firstName,omitempty"` + LastName string `json:"lastName,omitempty"` + Patronymic string `json:"patronymic,omitempty"` + CreatedAt string `json:"createdAt,omitempty"` + Active bool `json:"active,omitempty"` + Online bool `json:"online,omitempty"` + IsAdmin bool `json:"isAdmin,omitempty"` + IsManager bool `json:"isManager,omitempty"` + Email string `json:"email,omitempty"` + Phone string `json:"phone,omitempty"` + Status string `json:"status,omitempty"` + Groups []UserGroup `json:"groups,omitempty,brackets"` } +// UserGroup type +type UserGroup struct { + Name string `json:"name,omitempty"` + Code string `json:"code,omitempty"` + SignatureTemplate string `json:"signatureTemplate,omitempty"` + IsManager bool `json:"isManager,omitempty"` + IsDeliveryMen bool `json:"isDeliveryMen,omitempty"` + DeliveryTypes []string `json:"deliveryTypes,omitempty,brackets"` + BreakdownOrderTypes []string `json:"breakdownOrderTypes,omitempty,brackets"` + BreakdownSites []string `json:"breakdownSites,omitempty,brackets"` + BreakdownOrderMethods []string `json:"breakdownOrderMethods,omitempty,brackets"` + GrantedOrderTypes []string `json:"grantedOrderTypes,omitempty,brackets"` + GrantedSites []string `json:"grantedSites,omitempty,brackets"` +} + +/** +Reference related types +*/ + +// PriceType type type PriceType struct { Name string `json:"name,omitempty"` Code string `json:"code,omitempty"` @@ -292,3 +335,24 @@ type PriceType struct { Active bool `json:"active,omitempty"` Ordering int `json:"ordering,omitempty"` } + +/** +Task related types +*/ + +// Task type +type Task struct { + Id int `json:"id,omitempty"` + PerformerId int `json:"performerId,omitempty"` + Text string `json:"text,omitempty"` + Commentary string `json:"commentary,omitempty"` + Datetime string `json:"datetime,omitempty"` + Complete bool `json:"complete,omitempty"` + CreatedAt string `json:"createdAt,omitempty"` + Creator int `json:"creator,omitempty"` + Performer int `json:"performer,omitempty"` + Phone string `json:"phone,omitempty"` + PhoneSite string `json:"phoneSite,omitempty"` + Customer *Customer `json:"customer,omitempty"` + Order *Order `json:"order,omitempty"` +} diff --git a/v5_tests/customers_test.go b/v5_tests/customers_test.go index 6e2f1d1..ff802ce 100644 --- a/v5_tests/customers_test.go +++ b/v5_tests/customers_test.go @@ -185,7 +185,7 @@ func TestClient_CustomersHistory(t *testing.T) { c := client() f := v5.CustomersHistoryRequest{ Filter: v5.CustomersHistoryFilter{ - SinceId: 100, + SinceId: 20, }, } diff --git a/v5_tests/orders_test.go b/v5_tests/orders_test.go index 8979636..3f82dee 100644 --- a/v5_tests/orders_test.go +++ b/v5_tests/orders_test.go @@ -36,7 +36,7 @@ func TestClient_OrdersOrders(t *testing.T) { func TestClient_OrdersOrder(t *testing.T) { c := client() - data, status, err := c.Order("upload-b-1480333204", "externalId", "") + data, status, err := c.Order("asdf1510920687", "externalId", "") if err != nil { t.Errorf("%s", err) t.Fail() @@ -57,7 +57,7 @@ func TestClient_OrdersHistory(t *testing.T) { c := client() f := v5.OrdersHistoryRequest{ Filter: v5.OrdersHistoryFilter{ - SinceId: 100, + SinceId: 20, }, } diff --git a/v5_tests/tasks_test.go b/v5_tests/tasks_test.go new file mode 100644 index 0000000..537d81f --- /dev/null +++ b/v5_tests/tasks_test.go @@ -0,0 +1,101 @@ +package v5_tests + +import ( + "net/http" + "testing" + + "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{ + Filter: v5.TasksFilter{ + Creators: []int{6}, + }, + 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() + + random1 := RandomString(15) + random2 := RandomString(20) + + f := v5.Task{ + Text: random1, + PerformerId: 6, + } + + 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 = random2 + + 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 new file mode 100644 index 0000000..6578ab5 --- /dev/null +++ b/v5_tests/users_test.go @@ -0,0 +1,97 @@ +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(6) + 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(6, "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() + } +}