add method of batch products operations

This commit is contained in:
Ruslan Efanov 2022-11-23 18:16:58 +03:00
parent 573cbb9679
commit 2f6ab28d85
5 changed files with 178 additions and 9 deletions

View File

@ -5643,3 +5643,48 @@ func (c *Client) AccountBonusOperations(id int, parameters AccountBonusOperation
return resp, status, nil 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
}

View File

@ -6802,3 +6802,35 @@ func TestClient_AccountBonusOperations_Fail(t *testing.T) {
t.Error(successFail) 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)
}
}

View File

@ -370,6 +370,23 @@ type ProductsResponse struct {
Products []Product `json:"products,omitempty"` 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. // ProductsPropertiesResponse type.
type ProductsPropertiesResponse struct { type ProductsPropertiesResponse struct {
Success bool `json:"success"` Success bool `json:"success"`

55
testutils.go Normal file
View 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},
}
}

View File

@ -924,27 +924,47 @@ type ProductGroup struct {
Active bool `json:"active,omitempty"` Active bool `json:"active,omitempty"`
} }
// Product type. // BaseProduct type.
type Product struct { type BaseProduct struct {
ID int `json:"id,omitempty"`
MaxPrice float32 `json:"maxPrice,omitempty"`
MinPrice float32 `json:"minPrice,omitempty"`
Name string `json:"name,omitempty"` Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"` URL string `json:"url,omitempty"`
Article string `json:"article,omitempty"` Article string `json:"article,omitempty"`
ExternalID string `json:"externalId,omitempty"` ExternalID string `json:"externalId,omitempty"`
Manufacturer string `json:"manufacturer,omitempty"` Manufacturer string `json:"manufacturer,omitempty"`
ImageURL string `json:"imageUrl,omitempty"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
Popular bool `json:"popular,omitempty"` Popular bool `json:"popular,omitempty"`
Stock bool `json:"stock,omitempty"` Stock bool `json:"stock,omitempty"`
Novelty bool `json:"novelty,omitempty"` Novelty bool `json:"novelty,omitempty"`
Recommended bool `json:"recommended,omitempty"` Recommended bool `json:"recommended,omitempty"`
Active bool `json:"active,omitempty"` Active bool `json:"active,omitempty"`
Quantity float32 `json:"quantity,omitempty"`
Offers []Offer `json:"offers,omitempty"`
Groups []ProductGroup `json:"groups,omitempty"` Groups []ProductGroup `json:"groups,omitempty"`
Properties StringMap `json:"properties,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"`
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. // DeliveryHistoryRecord type.