mirror of
https://github.com/retailcrm/api-client-go.git
synced 2024-11-22 04:46:03 +03:00
add method of batch products operations
This commit is contained in:
parent
573cbb9679
commit
2f6ab28d85
45
client.go
45
client.go
@ -5643,3 +5643,48 @@ func (c *Client) AccountBonusOperations(id int, parameters AccountBonusOperation
|
||||
|
||||
return resp, status, nil
|
||||
}
|
||||
|
||||
func (c *Client) ProductsBatchEdit(products []ProductEdit) (ProductsBatchEditResponse, int, error) {
|
||||
var resp ProductsBatchEditResponse
|
||||
|
||||
productsEditJSON, _ := json.Marshal(products)
|
||||
p := url.Values{
|
||||
"products": {string(productsEditJSON)},
|
||||
}
|
||||
|
||||
data, status, err := c.PostRequest("/store/products/batch/edit", p)
|
||||
|
||||
if err != nil {
|
||||
return resp, status, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &resp)
|
||||
if err != nil {
|
||||
return resp, status, err
|
||||
}
|
||||
|
||||
return resp, status, nil
|
||||
}
|
||||
|
||||
func (c *Client) ProductsBatchCreate(products []ProductCreate) (ProductsBatchEditResponse, int, error) {
|
||||
var resp ProductsBatchEditResponse
|
||||
|
||||
productsEditJSON, _ := json.Marshal(products)
|
||||
p := url.Values{
|
||||
"products": {string(productsEditJSON)},
|
||||
}
|
||||
|
||||
data, status, err := c.PostRequest("/store/products/batch/create", p)
|
||||
|
||||
if err != nil {
|
||||
return resp, status, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &resp)
|
||||
if err != nil {
|
||||
return resp, status, err
|
||||
}
|
||||
|
||||
return resp, status, nil
|
||||
}
|
||||
|
||||
|
@ -6802,3 +6802,35 @@ func TestClient_AccountBonusOperations_Fail(t *testing.T) {
|
||||
t.Error(successFail)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Проверить реально запросы
|
||||
func TestClient_ProductsBatchCreate(t *testing.T) {
|
||||
defer gock.Off()
|
||||
|
||||
products := getProductsCreate()
|
||||
productsJSON, err := json.Marshal(getProductsCreate())
|
||||
assert.NoError(t, err)
|
||||
|
||||
p := url.Values{"products": {string(productsJSON)}}
|
||||
|
||||
gock.New(crmURL).
|
||||
Post("/store/products/batch/create").
|
||||
BodyString(p.Encode()).
|
||||
Reply(http.StatusOK).
|
||||
JSON(getProductsCreateResponse())
|
||||
|
||||
|
||||
resp, status, err := client().ProductsBatchCreate(products)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
if !statuses[status] {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
|
||||
if resp.Success != true {
|
||||
t.Errorf("%v", err)
|
||||
}
|
||||
}
|
||||
|
17
response.go
17
response.go
@ -370,6 +370,23 @@ type ProductsResponse struct {
|
||||
Products []Product `json:"products,omitempty"`
|
||||
}
|
||||
|
||||
type ProductEditNotFoundResponse struct {
|
||||
ID string `json:"id"`
|
||||
ExternalID string `json:"externalId,omitempty"`
|
||||
}
|
||||
|
||||
type ProductsBatchEditResponse struct {
|
||||
Success bool `json:"success"`
|
||||
ProcessedProductsCount int `json:"processedProductsCount,omitempty"`
|
||||
NotFoundProducts []ProductEditNotFoundResponse `json:"notFoundProducts,omitempty"`
|
||||
}
|
||||
|
||||
type ProductsBatchCreateResponse struct {
|
||||
Success bool `json:"success"`
|
||||
ProcessedProductsCount int `json:"processedProductsCount,omitempty"`
|
||||
AddedProducts []int `json:"addedProducts,omitempty"`
|
||||
}
|
||||
|
||||
// ProductsPropertiesResponse type.
|
||||
type ProductsPropertiesResponse struct {
|
||||
Success bool `json:"success"`
|
||||
|
55
testutils.go
Normal file
55
testutils.go
Normal file
@ -0,0 +1,55 @@
|
||||
//go:build testutils
|
||||
// +build testutils
|
||||
|
||||
package retailcrm
|
||||
|
||||
func getProductsCreate() []ProductCreate {
|
||||
products := []ProductCreate{
|
||||
{
|
||||
CatalogID: 123,
|
||||
BaseProduct: BaseProduct{
|
||||
Name: "Product 1",
|
||||
URL: "https://example.com/p/1",
|
||||
Article: "p1",
|
||||
ExternalID: "ext1",
|
||||
Manufacturer: "man1",
|
||||
Description: "Description 1",
|
||||
Popular: true,
|
||||
Stock: true,
|
||||
Novelty: true,
|
||||
Recommended: true,
|
||||
Active: true,
|
||||
Groups: []ProductGroup{{ID: 333}},
|
||||
Markable: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
CatalogID: 123,
|
||||
BaseProduct: BaseProduct{
|
||||
Name: "Product 2",
|
||||
URL: "https://example.com/p/2",
|
||||
Article: "p2",
|
||||
ExternalID: "ext2",
|
||||
Manufacturer: "man2",
|
||||
Description: "Description 2",
|
||||
Popular: true,
|
||||
Stock: true,
|
||||
Novelty: true,
|
||||
Recommended: true,
|
||||
Active: true,
|
||||
Groups: []ProductGroup{{ID: 444}},
|
||||
Markable: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
return products
|
||||
}
|
||||
|
||||
func getProductsCreateResponse() ProductsBatchCreateResponse {
|
||||
return ProductsBatchCreateResponse{
|
||||
Success: true,
|
||||
ProcessedProductsCount: 2,
|
||||
AddedProducts: []int{1, 2},
|
||||
}
|
||||
}
|
34
types.go
34
types.go
@ -924,29 +924,49 @@ type ProductGroup struct {
|
||||
Active bool `json:"active,omitempty"`
|
||||
}
|
||||
|
||||
// Product type.
|
||||
type Product struct {
|
||||
ID int `json:"id,omitempty"`
|
||||
MaxPrice float32 `json:"maxPrice,omitempty"`
|
||||
MinPrice float32 `json:"minPrice,omitempty"`
|
||||
// BaseProduct type.
|
||||
type BaseProduct struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
URL string `json:"url,omitempty"`
|
||||
Article string `json:"article,omitempty"`
|
||||
ExternalID string `json:"externalId,omitempty"`
|
||||
Manufacturer string `json:"manufacturer,omitempty"`
|
||||
ImageURL string `json:"imageUrl,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
Popular bool `json:"popular,omitempty"`
|
||||
Stock bool `json:"stock,omitempty"`
|
||||
Novelty bool `json:"novelty,omitempty"`
|
||||
Recommended bool `json:"recommended,omitempty"`
|
||||
Active bool `json:"active,omitempty"`
|
||||
Groups []ProductGroup `json:"groups,omitempty"`
|
||||
Markable bool `json:"markable,omitempty"`
|
||||
}
|
||||
|
||||
// Product type.
|
||||
type Product struct {
|
||||
BaseProduct
|
||||
ID int `json:"id,omitempty"`
|
||||
MaxPrice float32 `json:"maxPrice,omitempty"`
|
||||
MinPrice float32 `json:"minPrice,omitempty"`
|
||||
ImageURL string `json:"imageUrl,omitempty"`
|
||||
Quantity float32 `json:"quantity,omitempty"`
|
||||
Offers []Offer `json:"offers,omitempty"`
|
||||
Groups []ProductGroup `json:"groups,omitempty"`
|
||||
Properties StringMap `json:"properties,omitempty"`
|
||||
}
|
||||
|
||||
// ProductEdit type.
|
||||
type ProductEdit struct {
|
||||
BaseProduct
|
||||
ID int `json:"id,omitempty"`
|
||||
CatalogID int `json:"catalogId,omitempty"`
|
||||
Site string `json:"site,omitempty"`
|
||||
}
|
||||
|
||||
// ProductCreate type.
|
||||
type ProductCreate struct {
|
||||
BaseProduct
|
||||
CatalogID int `json:"catalogId,omitempty"`
|
||||
}
|
||||
|
||||
// DeliveryHistoryRecord type.
|
||||
type DeliveryHistoryRecord struct {
|
||||
Code string `json:"code,omitempty"`
|
||||
|
Loading…
Reference in New Issue
Block a user