From bc5f0e52a5a2933df9bf4547dadc610525286c7e Mon Sep 17 00:00:00 2001 From: Kirill Date: Fri, 27 Dec 2024 21:50:21 +0300 Subject: [PATCH] Update November 18, 2024 (#121) --- ozon/analytics.go | 70 ++++++++++++++++++++++++++++++++++++++++++ ozon/analytics_test.go | 68 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/ozon/analytics.go b/ozon/analytics.go index c251a36..88e269d 100644 --- a/ozon/analytics.go +++ b/ozon/analytics.go @@ -270,3 +270,73 @@ func (c Analytics) GetProductTurnover(ctx context.Context, params *GetProductTur return resp, nil } + +type GetStockManagementParams struct { + // GetStockManagementFilter + Filter GetStockManagementFilter `json:"filter"` + + // Number of values in the response + Limit int32 `json:"limit,omitempty"` + + // Number of elements to skip in the response + Offset int32 `json:"offset,omitempty"` +} + +type GetStockManagementFilter struct { + // Product identifiers in the Ozon system, SKU + SKUs []string `json:"skus"` + + // The type of item in stock + StockTypes string `json:"stock_types"` + + // Warehouse identifiers + WarehouseIds []string `json:"warehouse_ids"` +} + +type GetStockManagementResponse struct { + core.CommonResponse + + // Products + Items []StockItem `json:"items"` +} + +type StockItem struct { + // Stock of defective products, pcs + DefectCount int64 `json:"defect_stock_count"` + + // Stock of near-expired products, pcs + ExpiringCount int64 `json:"expiring_stock_count"` + + // Product name + ProductName string `json:"name"` + + // Product identifier in the seller's system + OfferId string `json:"offer_id"` + + // Product identifier in the Ozon system, SKU + SKU int64 `json:"sku"` + + // Stock of valid products + ValidCount int64 `json:"valid_stock_count"` + + // Stock of products that waiting for documents + WaitingDocsCount int64 `json:"waitingdocs_stock_count"` + + // Warehouse name + WarehouseName string `json:"warehouse_name"` +} + +// Use the method to find out how many product items are left in stock +func (c Analytics) Stock(ctx context.Context, params *GetStockManagementParams) (*GetStockManagementResponse, error) { + url := "/v1/analytics/manage/stocks" + + resp := &GetStockManagementResponse{} + + 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 3be5d75..78607b4 100644 --- a/ozon/analytics_test.go +++ b/ozon/analytics_test.go @@ -209,3 +209,71 @@ func TestGetProductTurnover(t *testing.T) { } } } + +func TestGetStock(t *testing.T) { + t.Parallel() + + tests := []struct { + statusCode int + headers map[string]string + params *GetStockManagementParams + response string + }{ + // Test Ok + { + http.StatusOK, + map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, + &GetStockManagementParams{ + Limit: 1, + Offset: 0, + Filter: GetStockManagementFilter{ + StockTypes: "STOCK_TYPE_VALID", + SKUs: []string{ + "string", + }, + }, + }, + `{ + "items": [ + { + "defect_stock_count": 0, + "expiring_stock_count": 0, + "name": "string", + "offer_id": "string", + "sku": 0, + "valid_stock_count": 0, + "waitingdocs_stock_count": 0, + "warehouse_name": "string" + } + ] + }`, + }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &GetStockManagementParams{}, + `{ + "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().Stock(ctx, test.params) + if err != nil { + t.Error(err) + continue + } + + compareJsonResponse(t, test.response, &GetStockManagementResponse{}) + + if resp.StatusCode != test.statusCode { + t.Errorf("got wrong status code: got: %d, expected: %d", resp.StatusCode, test.statusCode) + } + } +}