From 2f6ab28d85b781bee07074e11159ef0cd04622cc Mon Sep 17 00:00:00 2001 From: Ruslan Efanov Date: Wed, 23 Nov 2022 18:16:58 +0300 Subject: [PATCH] add method of batch products operations --- client.go | 45 +++++++++++++++++++++++++++++++++++++++++ client_test.go | 32 +++++++++++++++++++++++++++++ response.go | 17 ++++++++++++++++ testutils.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++ types.go | 38 +++++++++++++++++++++++++--------- 5 files changed, 178 insertions(+), 9 deletions(-) create mode 100644 testutils.go diff --git a/client.go b/client.go index cc60476..6df823d 100644 --- a/client.go +++ b/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 +} + diff --git a/client_test.go b/client_test.go index 317c30a..23b9ab6 100644 --- a/client_test.go +++ b/client_test.go @@ -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) + } +} diff --git a/response.go b/response.go index d81b82f..878f8ee 100644 --- a/response.go +++ b/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"` diff --git a/testutils.go b/testutils.go new file mode 100644 index 0000000..cb06737 --- /dev/null +++ b/testutils.go @@ -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}, + } +} diff --git a/types.go b/types.go index 4263439..90fe8cc 100644 --- a/types.go +++ b/types.go @@ -924,27 +924,47 @@ 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"` - Quantity float32 `json:"quantity,omitempty"` - Offers []Offer `json:"offers,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.