This commit is contained in:
Alex Lushpai 2018-01-31 16:46:02 +03:00
parent d763ae9329
commit c21caef055
9 changed files with 309 additions and 86 deletions

View file

@ -28,6 +28,7 @@ func New(url string, key string) *Client {
}
}
// GetRequest implements GET Request
func (c *Client) GetRequest(urlWithParameters string) ([]byte, int, error) {
var res []byte
@ -55,6 +56,7 @@ func (c *Client) GetRequest(urlWithParameters string) ([]byte, int, error) {
return res, resp.StatusCode, nil
}
// PostRequest implements POST Request
func (c *Client) PostRequest(url string, postParams url.Values) ([]byte, int, error) {
var res []byte
@ -148,6 +150,20 @@ func (c *Client) ApiCredentials() (*CredentialResponse, int, error) {
return &resp, status, err
}
// StaticticUpdate update statistic
func (c *Client) StaticticUpdate() (*VersionResponse, int, error) {
var resp VersionResponse
data, status, err := c.GetRequest(fmt.Sprintf("%s/statistic/update", versionedPrefix))
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
@ -326,8 +342,8 @@ func (c *Client) Orders(parameters OrdersRequest) (*OrdersResponse, int, error)
}
// OrderCreate method
func (c *Client) OrderCreate(order Order, site ...string) (*OrderChangeResponse, int, error) {
var resp OrderChangeResponse
func (c *Client) OrderCreate(order Order, site ...string) (*CreateResponse, int, error) {
var resp CreateResponse
orderJson, _ := json.Marshal(&order)
p := url.Values{
@ -346,9 +362,9 @@ func (c *Client) OrderCreate(order Order, site ...string) (*OrderChangeResponse,
return &resp, status, err
}
// CustomerEdit method
func (c *Client) OrderEdit(order Order, by string, site ...string) (*OrderChangeResponse, int, error) {
var resp OrderChangeResponse
// OrderEdit method
func (c *Client) OrderEdit(order Order, by string, site ...string) (*CreateResponse, int, error) {
var resp CreateResponse
var uid = strconv.Itoa(order.Id)
var context = checkBy(by)
@ -568,3 +584,59 @@ func (c *Client) TaskEdit(task Task, site ...string) (*SucessfulResponse, int, e
return &resp, status, err
}
// Notes list method
func (c *Client) Notes(parameters NotesRequest) (*NotesResponse, int, error) {
var resp NotesResponse
params, _ := query.Values(parameters)
data, status, err := c.GetRequest(fmt.Sprintf("%s/customers/notes?%s", versionedPrefix, params.Encode()))
if err != nil {
return &resp, status, err
}
err = json.Unmarshal(data, &resp)
return &resp, status, err
}
// NoteCreate method
func (c *Client) NoteCreate(note Note, site ...string) (*CreateResponse, int, error) {
var resp CreateResponse
noteJson, _ := json.Marshal(&note)
p := url.Values{
"note": {string(noteJson[:])},
}
fillSite(&p, site)
data, status, err := c.PostRequest(fmt.Sprintf("%s/customers/notes/create", versionedPrefix), p)
if err != nil {
return &resp, status, err
}
err = json.Unmarshal(data, &resp)
return &resp, status, err
}
// NoteDelete method
func (c *Client) NoteDelete(id int) (*SucessfulResponse, int, error) {
var resp SucessfulResponse
p := url.Values{
"id": {string(id)},
}
data, status, err := c.PostRequest(fmt.Sprintf("%s/customers/notes/%d/delete", versionedPrefix, id), p)
if err != nil {
return &resp, status, err
}
err = json.Unmarshal(data, &resp)
return &resp, status, err
}

View file

@ -67,7 +67,7 @@ type CustomersHistoryFilter struct {
// OrdersFilter type
type OrdersFilter struct {
Ids []string `url:"ids,omitempty,brackets"`
Ids []int `url:"ids,omitempty,brackets"`
ExternalIds []string `url:"externalIds,omitempty,brackets"`
Numbers []string `url:"numbers,omitempty,brackets"`
Customer string `url:"customer,omitempty"`
@ -189,3 +189,14 @@ type TasksFilter struct {
Creators []int `url:"creators,omitempty"`
Performers []int `url:"performers,omitempty"`
}
// NotesFilter type
type NotesFilter struct {
Ids []int `url:"ids,omitempty,brackets"`
CustomerIds []int `url:"customerIds,omitempty,brackets"`
CustomerExternalIds []string `url:"customerExternalIds,omitempty,brackets"`
ManagerIds []int `url:"managerIds,omitempty,brackets"`
Text string `url:"text,omitempty"`
CreatedAtFrom string `url:"createdAtFrom,omitempty"`
CreatedAtTo string `url:"createdAtTo,omitempty"`
}

View file

@ -39,7 +39,7 @@ type OrdersRequest struct {
Page int `url:"page,omitempty"`
}
// CustomersUploadRequest type
// OrdersUploadRequest type
type OrdersUploadRequest struct {
Customers []Order `url:"orders,omitempty,brackets"`
Site string `url:"site,omitempty"`
@ -71,3 +71,10 @@ type TasksRequest struct {
Limit int `url:"limit,omitempty"`
Page int `url:"page,omitempty"`
}
// NotesRequest type
type NotesRequest struct {
Filter TasksFilter `url:"filter,omitempty"`
Limit int `url:"limit,omitempty"`
Page int `url:"page,omitempty"`
}

View file

@ -21,6 +21,12 @@ type SucessfulResponse struct {
Success bool `json:"success,omitempty"`
}
// CreateResponse type
type CreateResponse struct {
Success bool `json:"success"`
Id int `json:"id,omitempty"`
}
// VersionResponse return available API versions
type VersionResponse struct {
Success bool `json:"success,omitempty"`
@ -82,12 +88,6 @@ 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"`
@ -140,3 +140,10 @@ type TasksResponse struct {
Pagination *Pagination `json:"pagination,omitempty"`
Tasks []Task `json:"tasks,omitempty,brackets"`
}
// NotesResponse type
type NotesResponse struct {
Success bool `json:"success"`
Pagination *Pagination `json:"pagination,omitempty"`
Notes []Note `json:"notes,omitempty,brackets"`
}

View file

@ -68,10 +68,12 @@ type Contragent struct {
BankAccount string `json:"bankAccount,omitempty"`
}
// ApiKey type
type ApiKey struct {
Current bool `json:"current,omitempty"`
}
// Property type
type Property struct {
Code string `json:"code,omitempty"`
Name string `json:"name,omitempty"`
@ -98,7 +100,7 @@ type Customer struct {
Sex string `json:"sex,omitempty"`
Email string `json:"email,omitempty"`
Phones []CustomerPhone `json:"phones,brackets,omitempty"`
Address *Address `json:"address,omitempty"`
Address Address `json:"address,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
Birthday string `json:"birthday,omitempty"`
ManagerId int `json:"managerId,omitempty"`
@ -121,7 +123,7 @@ type Customer struct {
FirstClientId string `json:"firstClientId,omitempty"`
LastClientId string `json:"lastClientId,omitempty"`
BrowserId string `json:"browserId,omitempty"`
//CustomFields []map[string]string `json:"customFields,omitempty,brackets"`
// CustomFields []map[string]string `json:"customFields,omitempty,brackets"`
}
// CustomerPhone type
@ -131,15 +133,15 @@ type CustomerPhone struct {
// CustomerHistoryRecord type
type CustomerHistoryRecord struct {
Id int `json:"id,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
Created bool `json:"created,omitempty"`
Deleted bool `json:"deleted,omitempty"`
Source string `json:"source,omitempty"`
Field string `json:"field,omitempty"`
User *User `json:"user,omitempty,brackets"`
ApiKey *ApiKey `json:"apiKey,omitempty,brackets"`
Customer *Customer `json:"customer,omitempty,brackets"`
Id int `json:"id,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
Created bool `json:"created,omitempty"`
Deleted bool `json:"deleted,omitempty"`
Source string `json:"source,omitempty"`
Field string `json:"field,omitempty"`
User User `json:"user,omitempty,brackets"`
ApiKey ApiKey `json:"apiKey,omitempty,brackets"`
Customer Customer `json:"customer,omitempty,brackets"`
}
/**
@ -189,41 +191,45 @@ type Order struct {
ClientId string `json:"clientId,omitempty"`
Shipped bool `json:"shipped,omitempty"`
UploadedToExternalStoreSystem bool `json:"uploadedToExternalStoreSystem,omitempty"`
Source *Source `json:"source,omitempty"`
Contragent *Contragent `json:"contragent,omitempty"`
Customer *Customer `json:"customer,omitempty"`
Delivery *OrderDelivery `json:"delivery,omitempty"`
Marketplace *OrderMarketplace `json:"marketplace,omitempty"`
Source Source `json:"source,omitempty"`
Contragent Contragent `json:"contragent,omitempty"`
Customer Customer `json:"customer,omitempty"`
Delivery OrderDelivery `json:"delivery,omitempty"`
Marketplace OrderMarketplace `json:"marketplace,omitempty"`
Items []OrderItem `json:"items,omitempty,brackets"`
CustomFields []map[string]string `json:"customFields,omitempty,brackets"`
//Payments []OrderPayment `json:"payments,omitempty,brackets"`
// Payments []OrderPayment `json:"payments,omitempty,brackets"`
}
// OrderDelivery type
type OrderDelivery struct {
Code string `json:"code,omitempty"`
IntegrationCode string `json:"integrationCode,omitempty"`
Cost float32 `json:"cost,omitempty"`
NetCost float32 `json:"netCost,omitempty"`
VatRate string `json:"vatRate,omitempty"`
Date string `json:"date,omitempty"`
Time *OrderDeliveryTime `json:"time,omitempty"`
Address *Address `json:"address,omitempty"`
Service *OrderDeliveryService `json:"service,omitempty"`
Data *OrderDeliveryData `json:"data,omitempty"`
Code string `json:"code,omitempty"`
IntegrationCode string `json:"integrationCode,omitempty"`
Cost float32 `json:"cost,omitempty"`
NetCost float32 `json:"netCost,omitempty"`
VatRate string `json:"vatRate,omitempty"`
Date string `json:"date,omitempty"`
Time OrderDeliveryTime `json:"time,omitempty"`
Address Address `json:"address,omitempty"`
Service OrderDeliveryService `json:"service,omitempty"`
Data OrderDeliveryData `json:"data,omitempty"`
}
// OrderDeliveryTime type
type OrderDeliveryTime struct {
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
Custom string `json:"custom,omitempty"`
}
// OrderDeliveryService type
type OrderDeliveryService struct {
Name string `json:"name,omitempty"`
Code string `json:"code,omitempty"`
Active bool `json:"active,omitempty"`
}
// OrderDeliveryData type
type OrderDeliveryData struct {
TrackNumber string `json:"trackNumber,omitempty"`
Status string `json:"status,omitempty"`
@ -231,11 +237,13 @@ type OrderDeliveryData struct {
PayerType string `json:"payerType,omitempty"`
}
// OrderMarketplace type
type OrderMarketplace struct {
Code string `json:"code,omitempty"`
OrderId string `json:"orderId,omitempty"`
}
// OrderPayment type
type OrderPayment struct {
Id int `json:"id,omitempty"`
ExternalId string `json:"externalId,omitempty"`
@ -246,36 +254,37 @@ type OrderPayment struct {
Comment string `json:"comment,omitempty"`
}
// OrderItem type
type OrderItem struct {
Id int `json:"id,omitempty"`
InitialPrice float32 `json:"initialPrice,omitempty"`
PurchasePrice float32 `json:"purchasePrice,omitempty"`
DiscountTotal float32 `json:"discountTotal,omitempty"`
DiscountManualAmount float32 `json:"discountManualAmount,omitempty"`
DiscountManualPercent float32 `json:"discountManualPercent,omitempty"`
ProductName string `json:"productName,omitempty"`
VatRate string `json:"vatRate,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
Quantity float32 `json:"quantity,omitempty"`
Status string `json:"status,omitempty"`
Comment string `json:"comment,omitempty"`
IsCanceled bool `json:"isCanceled,omitempty"`
Offer *Offer `json:"offer,omitempty"`
Properties []*Property `json:"properties,omitempty,brackets"`
PriceType *PriceType `json:"priceType,omitempty"`
Id int `json:"id,omitempty"`
InitialPrice float32 `json:"initialPrice,omitempty"`
PurchasePrice float32 `json:"purchasePrice,omitempty"`
DiscountTotal float32 `json:"discountTotal,omitempty"`
DiscountManualAmount float32 `json:"discountManualAmount,omitempty"`
DiscountManualPercent float32 `json:"discountManualPercent,omitempty"`
ProductName string `json:"productName,omitempty"`
VatRate string `json:"vatRate,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
Quantity float32 `json:"quantity,omitempty"`
Status string `json:"status,omitempty"`
Comment string `json:"comment,omitempty"`
IsCanceled bool `json:"isCanceled,omitempty"`
Offer Offer `json:"offer,omitempty"`
Properties []Property `json:"properties,omitempty,brackets"`
PriceType PriceType `json:"priceType,omitempty"`
}
// OrdersHistoryRecord type
type OrdersHistoryRecord struct {
Id int `json:"id,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
Created bool `json:"created,omitempty"`
Deleted bool `json:"deleted,omitempty"`
Source string `json:"source,omitempty"`
Field string `json:"field,omitempty"`
User *User `json:"user,omitempty,brackets"`
ApiKey *ApiKey `json:"apiKey,omitempty,brackets"`
Order *Order `json:"order,omitempty,brackets"`
Id int `json:"id,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
Created bool `json:"created,omitempty"`
Deleted bool `json:"deleted,omitempty"`
Source string `json:"source,omitempty"`
Field string `json:"field,omitempty"`
User User `json:"user,omitempty,brackets"`
ApiKey ApiKey `json:"apiKey,omitempty,brackets"`
Order Order `json:"order,omitempty,brackets"`
}
// Offer type
@ -342,17 +351,30 @@ 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"`
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"`
}
/*
Notes related types
*/
// Note type
type Note struct {
Id int `json:"id,omitempty"`
ManagerId int `json:"managerId,omitempty"`
Text string `json:"text,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
Customer Customer `json:"customer,omitempty"`
}

View file

@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"os"
"strconv"
"testing"
"time"
@ -29,6 +30,12 @@ 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)

100
v5_tests/notes_test.go Normal file
View file

@ -0,0 +1,100 @@
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()
}
}

View file

@ -9,9 +9,10 @@ import (
func TestClient_TasksTasks(t *testing.T) {
c := client()
f := v5.TasksRequest{
Filter: v5.TasksFilter{
Creators: []int{6},
Creators: []int{GetUser()},
},
Page: 1,
}
@ -41,7 +42,7 @@ func TestClient_TaskChange(t *testing.T) {
f := v5.Task{
Text: random1,
PerformerId: 6,
PerformerId: GetUser(),
}
cr, sc, err := c.TaskCreate(f)

View file

@ -2,15 +2,11 @@ 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{
@ -40,7 +36,7 @@ func TestClient_UsersUsers(t *testing.T) {
func TestClient_UsersUser(t *testing.T) {
c := client()
data, st, err := c.User(user)
data, st, err := c.User(GetUser())
if err != nil {
t.Errorf("%s", err)
t.Fail()
@ -83,7 +79,7 @@ func TestClient_UsersGroups(t *testing.T) {
func TestClient_UsersUpdate(t *testing.T) {
c := client()
data, st, err := c.UserStatus(user, "busy")
data, st, err := c.UserStatus(GetUser(), "busy")
if err != nil {
t.Errorf("%s", err)
t.Fail()