diff --git a/.golangci.yml b/.golangci.yml index 93c1179..f383f9a 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -79,7 +79,6 @@ linters-settings: - unmarshal - unreachable - unsafeptr - - fieldalignment settings: printf: funcs: diff --git a/client.go b/client.go index 6c0cdc8..94a13eb 100644 --- a/client.go +++ b/client.go @@ -6907,3 +6907,26 @@ func (c *Client) EditMGChannelTemplate(req EditMGChannelTemplateRequest) (int, e return code, nil } + +func (c *Client) StoreOffers(req OffersRequest) (StoreOffersResponse, int, error) { + var result StoreOffersResponse + + filter, err := query.Values(req) + + if err != nil { + return StoreOffersResponse{}, 0, err + } + + resp, status, err := c.GetRequest(fmt.Sprintf("/store/offers?%s", filter.Encode())) + + if err != nil { + return StoreOffersResponse{}, status, err + } + + err = json.Unmarshal(resp, &result) + if err != nil { + return StoreOffersResponse{}, status, err + } + + return result, status, nil +} diff --git a/client_test.go b/client_test.go index f864b39..fdcdd30 100644 --- a/client_test.go +++ b/client_test.go @@ -8332,3 +8332,39 @@ func TestClient_EditMGChannelTemplate(t *testing.T) { assert.NoError(t, err) assert.True(t, statuses[code]) } + +func TestClient_StoreOffers(t *testing.T) { + cl := client() + + gock.New(cl.URL). + Get(prefix+"/store/offers"). + MatchParam("filter[active]", "1"). + MatchParam("filter[ids][]", "76"). + Reply(http.StatusOK). + JSON(getStoreOfferResponse()) + + a := 1 + f := OffersRequest{OffersFilter{Ids: []int{76}, Active: &a}} + + resp, status, err := cl.StoreOffers(f) + + if err != nil { + t.Errorf("%v", err) + } + + if !statuses[status] { + t.Errorf("%v", err) + } + + if resp.Success != true { + t.Errorf("%v", err) + } + + assert.Len(t, resp.Offers, 1) + assert.Equal(t, 76, resp.Offers[0].ID) + assert.Equal(t, "Название\nПеревод строки", resp.Offers[0].Name) + assert.Equal(t, 222, resp.Offers[0].Product.ID) + assert.Equal(t, "base", resp.Offers[0].Prices[0].PriceType) + assert.Equal(t, float32(10000), resp.Offers[0].Prices[0].Price) + assert.Equal(t, "RUB", resp.Offers[0].Prices[0].Currency) +} diff --git a/filters.go b/filters.go index 103ba8b..31e3669 100644 --- a/filters.go +++ b/filters.go @@ -474,3 +474,8 @@ type LoyaltyAPIFilter struct { Ids []int `url:"ids,omitempty,brackets"` Sites []string `url:"sites,omitempty,brackets"` } + +type OffersFilter struct { + Ids []int `url:"ids,omitempty,brackets"` + Active *int `url:"active,omitempty"` +} diff --git a/request.go b/request.go index 24ff2d0..47d25ba 100644 --- a/request.go +++ b/request.go @@ -310,3 +310,7 @@ func (r ConnectRequest) Verify(secret string) bool { } return hmac.Equal([]byte(r.Token), []byte(hex.EncodeToString(mac.Sum(nil)))) } + +type OffersRequest struct { + OffersFilter `url:"filter,omitempty"` +} diff --git a/response.go b/response.go index 748f4da..650d9c2 100644 --- a/response.go +++ b/response.go @@ -685,3 +685,9 @@ type MGChannelTemplatesResponse struct { Templates []MGChannelTemplate `json:"templates"` SuccessfulResponse } + +type StoreOffersResponse struct { + Pagination *Pagination `json:"pagination"` + SuccessfulResponse + Offers []Offer `json:"offers,omitempty"` +} diff --git a/testutils.go b/testutils.go index ffa945f..1ab6ee7 100644 --- a/testutils.go +++ b/testutils.go @@ -519,3 +519,47 @@ func getMGTemplatesResponse() string { func getMGTemplatesForEdit() string { return `[{"header":{"text":{"parts":["Hello,",{"var":"custom"}],"example":["Henry"]},"document":{"example":"https://example.com/file/123.pdf"},"image":{"example":"https://example.com/file/123.png"},"video":{"example":"https://example.com/file/123.mp4"}},"lang":"en","category":"test_0","code":"namespace#name_0#ru","name":"name_0","namespace":"namespace","footer":"footer_0","verificationStatus":"REJECTED","template":["Text_0",{"var":"custom"}],"buttons":[{"type":"PHONE_NUMBER","text":"your-phone-button-text","phoneNumber":"+79895553535"},{"type":"QUICK_REPLY","text":"Yes"},{"type":"URL","url":"https://example.com/file/{{1}}","text":"button","example":["https://www.website.com/dynamic-url-example"]}],"templateExample":["WIU"],"id":1,"externalId":10,"mgChannelId":110,"active":true}]` } + +func getStoreOfferResponse() string { + return `{ + "success": true, + "pagination": { + "limit": 20, + "totalCount": 1, + "currentPage": 1, + "totalPageCount": 1 + }, + "offers": [ + { + "images": [ + "https://s3-s1.retailcrm.tech/ru-central1/retailcrm/dev-vega-d32aea7f9a5bc26eba6ad986077cea03/product/65a92fa0bb737-test.jpeg" + ], + "id": 76, + "site": "main", + "name": "Название\nПеревод строки", + "article": "Артикул", + "product": { + "type": "product", + "catalogId": 2, + "id": 222 + }, + "prices": [ + { + "priceType": "base", + "price": 10000, + "ordering": 991, + "currency": "RUB" + } + ], + "purchasePrice": 10, + "quantity": 5, + "active": true, + "unit": { + "code": "pc", + "name": "Штука", + "sym": "шт." + } + } + ] +}` +} diff --git a/types.go b/types.go index 9aacad3..ccaf44e 100644 --- a/types.go +++ b/types.go @@ -598,6 +598,7 @@ type Offer struct { Prices []OfferPrice `json:"prices,omitempty"` Images []string `json:"images,omitempty"` Unit *Unit `json:"unit,omitempty"` + Product *Product `json:"product,omitempty"` } // Inventory type. @@ -1255,7 +1256,7 @@ type Action struct { // MgTransport type. type MgTransport struct { WebhookURL string `json:"webhookUrl,omitempty"` - RefreshToken bool `json:"refreshToken,omitempty"` + RefreshToken bool `json:"refreshToken,omitempty"` Actions *MgTransportActions `json:"actions,omitempty"` }