From 173e9a70cc1116d38e03f269f779fd0ff1d51794 Mon Sep 17 00:00:00 2001 From: diPhantxm Date: Thu, 31 Oct 2024 15:54:47 +0300 Subject: [PATCH] method for listing products turnovers --- ozon/analytics.go | 51 +++++++++++++++++++++++++++++++++ ozon/analytics_test.go | 64 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/ozon/analytics.go b/ozon/analytics.go index b43747d..c251a36 100644 --- a/ozon/analytics.go +++ b/ozon/analytics.go @@ -219,3 +219,54 @@ func (c Analytics) GetStocksOnWarehouses(ctx context.Context, params *GetStocksO return resp, nil } + +type GetProductTurnoverParams struct { + // Number of values in the response + Limit int64 `json:"limit"` + + // Number of elements to skip in the response. + // + // For example, if offset = 10, the response starts with the 11th element found + Offset int32 `json:"offset"` + + // Product identifiers in the Ozon system, SKU + SKU []string `json:"sku"` +} + +type GetProductTurnoverResponse struct { + core.CommonResponse + + // Products + Items []ProductTurnoverItem `json:"items"` +} + +type ProductTurnoverItem struct { + // Average daily number of product items sold over the last 60 days + Ads float64 `json:"ads"` + + // Product stock, pcs + CurrentStock int64 `json:"current_stock"` + + // Number of days the stock will last based on your average daily sales + IDC float64 `json:"idc"` + + // Product stock level + IDCGrade string `json:"idc_grade"` +} + +// Use the method to get the product turnover rate and the number of days the current stock will last. +// +// If you request a list of products by sku, the limit and offset parameters are optional. +func (c Analytics) GetProductTurnover(ctx context.Context, params *GetProductTurnoverParams) (*GetProductTurnoverResponse, error) { + url := "/v1/analytics/turnover/stocks" + + resp := &GetProductTurnoverResponse{} + + response, err := c.client.Request(ctx, http.MethodPost, url, params, resp, nil) + if err != nil { + return nil, err + } + response.CopyCommonResponse(&resp.CommonResponse) + + return resp, nil +} diff --git a/ozon/analytics_test.go b/ozon/analytics_test.go index a269aa7..3be5d75 100644 --- a/ozon/analytics_test.go +++ b/ozon/analytics_test.go @@ -145,3 +145,67 @@ func TestGetStocksOnWarehouses(t *testing.T) { } } } + +func TestGetProductTurnover(t *testing.T) { + t.Parallel() + + tests := []struct { + statusCode int + headers map[string]string + params *GetProductTurnoverParams + response string + }{ + // Test Ok + { + http.StatusOK, + map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, + &GetProductTurnoverParams{ + Limit: 1, + SKU: []string{"string"}, + }, + `{ + "items": [ + { + "ads": 0, + "current_stock": 0, + "idc": 0, + "idc_grade": "GRADES_NONE" + } + ] + }`, + }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &GetProductTurnoverParams{}, + `{ + "code": 16, + "message": "Client-Id and Api-Key headers are required" + }`, + }, + } + + for _, test := range tests { + c := NewMockClient(core.NewMockHttpHandler(test.statusCode, test.response, test.headers)) + + ctx, _ := context.WithTimeout(context.Background(), testTimeout) + resp, err := c.Analytics().GetProductTurnover(ctx, test.params) + if err != nil { + t.Error(err) + continue + } + + compareJsonResponse(t, test.response, &GetProductTurnoverResponse{}) + + if resp.StatusCode != test.statusCode { + t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode) + } + + if resp.StatusCode == http.StatusOK { + if len(resp.Items) > int(test.params.Limit) { + t.Errorf("Length of items is bigger than limit") + } + } + } +}