From 0fe3d86c48ca6d1630b46b4033e319f2ffc3046a Mon Sep 17 00:00:00 2001 From: diPhantxm Date: Sat, 18 Mar 2023 16:20:51 +0300 Subject: [PATCH] add method for getting shipment data by barcode for fbs --- ENDPOINTS.md | 2 +- ozon/analytics.go | 2 +- ozon/fbs.go | 102 ++++++++++++++++++++++++++++++++++++++++++ ozon/fbs_test.go | 111 ++++++++++++++++++++++++++++++++++++++++++++++ ozon/products.go | 2 +- ozon/reports.go | 4 +- 6 files changed, 218 insertions(+), 5 deletions(-) diff --git a/ENDPOINTS.md b/ENDPOINTS.md index fa36dac..cf18873 100644 --- a/ENDPOINTS.md +++ b/ENDPOINTS.md @@ -92,7 +92,7 @@ - [x] List of unprocessed shipments (version 3) - [x] Shipments list (version 3) - [ ] Get shipment details by identifier (version 3) -- [ ] Get shipment data by barcode +- [x] Get shipment data by barcode - [ ] List of manufacturing countries - [ ] Set the manufacturing country - [ ] Specify number of boxes for multi-box shipments diff --git a/ozon/analytics.go b/ozon/analytics.go index dc6bdc5..574dbfc 100644 --- a/ozon/analytics.go +++ b/ozon/analytics.go @@ -112,7 +112,7 @@ type GetStocksOnWarehousesParams struct { // Number of values per page. // // Default is 100 - Limit int64 `json:"limit"` + Limit int64 `json:"limit" default:"100"` // Number of elements that will be skipped in the response. For example, if `offset=10`, the response will start with the 11th element found Offset int64 `json:"offset"` diff --git a/ozon/fbs.go b/ozon/fbs.go index e26c91a..373e807 100644 --- a/ozon/fbs.go +++ b/ozon/fbs.go @@ -456,3 +456,105 @@ func (c FBS) ValidateLabelingCodes(params *ValidateLabelingCodesParams) (*Valida return resp, nil } + +type GetShipmentDataByBarcodeParams struct { + // Shipment barcode + Barcode string `json:"barcode"` +} + +type GetShipmentDataByBarcodeResponse struct { + core.CommonResponse + + // Method result + Result struct { + // Analytical data + AnalyticsData struct { + // Delivery city + City string `json:"city"` + + // Delivery method + DeliveryType string `json:"delivery_type"` + + // Indication that the recipient is a legal entity: + // - true — a legal entity + // - false — a natural person + IsLegal bool `json:"is_legal"` + + // Premium subscription availability + IsPremium bool `json:"is_premium"` + + // Payment method + PaymentTypeGroupName string `json:"payment_type_group_name"` + + // Delivery region + Region string `json:"region"` + } `json:"analytics_data"` + + // Shipment barcodes + Barcodes struct { + // Lower barcode on the shipment label + Lower string `json:"lower_barcode"` + + // Upper barcode on the shipment label + Upper string `json:"upper_barcode"` + } `json:"barcodes"` + + // Cancellation reason identifier + CancelReasonId int64 `json:"cancel_reason_id"` + + // Date and time when the shipment was created + CreatedAt time.Time `json:"created_at"` + + // Financial data + FinancialData struct { + // Identifier of the cluster, where the shipment is sent from + ClusterFrom string `json:"cluster_from"` + + // Identifier of the cluster, where the shipment is delivered to + ClusterTo string `json:"cluster_to"` + + // Services + PostingServices []MarketplaceServices `json:"posting_services"` + + // Products list + Products []FinancialDataProduct `json:"products"` + } `json:"financial_data"` + + // Start date and time of shipment processing + InProcessAt time.Time `json:"in_process_at"` + + // Order identifier to which the shipment belongs + OrderId int64 `json:"order_id"` + + // Order number to which the shipment belongs + OrderNumber string `json:"order_number"` + + // Shipment number + PostingNumber string `json:"posting_number"` + + // List of products in the shipment + Products []PostingProduct `json:"products"` + + // Date and time before which the shipment must be packaged. + // If the shipment is not packaged by this date, it will be canceled automatically + ShipmentDate time.Time `json:"shipment_date"` + + // Shipment status + Status string `json:"status"` + } `json:"result"` +} + +// Methof for getting shipments data by barcode +func (c FBS) GetShipmentDataByBarcode(params *GetShipmentDataByBarcodeParams) (*GetShipmentDataByBarcodeResponse, error) { + url := "/v2/posting/fbs/get-by-barcode" + + resp := &GetShipmentDataByBarcodeResponse{} + + response, err := c.client.Request(http.MethodPost, url, params, resp) + if err != nil { + return nil, err + } + response.CopyCommonResponse(&resp.CommonResponse) + + return resp, nil +} diff --git a/ozon/fbs_test.go b/ozon/fbs_test.go index 804bf67..f2ebb2d 100644 --- a/ozon/fbs_test.go +++ b/ozon/fbs_test.go @@ -437,3 +437,114 @@ func TestValidateLabelingCodes(t *testing.T) { } } } + +func TestGetShipmentDataByBarcode(t *testing.T) { + t.Parallel() + + tests := []struct { + statusCode int + headers map[string]string + params *GetShipmentDataByBarcodeParams + response string + }{ + // Test Ok + { + http.StatusOK, + map[string]string{"Client-Id": "my-client-id", "Api-Key": "my-api-key"}, + &GetShipmentDataByBarcodeParams{ + Barcode: "20325804886000", + }, + `{ + "result": { + "posting_number": "57195475-0050-3", + "order_id": 438764970, + "order_number": "57195475-0050", + "status": "awaiting_packaging", + "delivery_method": { + "id": 18114520187000, + "name": "Ozon Логистика самостоятельно, Москва", + "warehouse_id": 18114520187000, + "warehouse": "Москва основной", + "tpl_provider_id": 24, + "tpl_provider": "Ozon Логистика" + }, + "tracking_number": "", + "tpl_integration_type": "ozon", + "in_process_at": "2021-11-20T09:14:16Z", + "shipment_date": "2021-11-23T10:00:00Z", + "delivering_date": null, + "provider_status": "", + "delivery_price": "", + "cancellation": { + "cancel_reason_id": 0, + "cancel_reason": "", + "cancellation_type": "", + "cancelled_after_ship": false, + "affect_cancellation_rating": false, + "cancellation_initiator": "" + }, + "customer": null, + "addressee": null, + "products": [ + { + "price": "279.0000", + "offer_id": "250-7898-1", + "name": "Кофе ароматизированный \"Шоколадный апельсин\" 250 гр", + "sku": 180550365, + "quantity": 1, + "mandatory_mark": [], + "dimensions": { + "height": "40.00", + "length": "240.00", + "weight": "260", + "width": "140.00" + } + } + ], + "barcodes": null, + "analytics_data": null, + "financial_data": null, + "additional_data": [], + "is_express": false, + "requirements": { + "products_requiring_gtd": [], + "products_requiring_country": [] + }, + "product_exemplars": null + } + }`, + }, + // Test No Client-Id or Api-Key + { + http.StatusUnauthorized, + map[string]string{}, + &GetShipmentDataByBarcodeParams{}, + `{ + "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)) + + resp, err := c.FBS().GetShipmentDataByBarcode(test.params) + if err != nil { + t.Error(err) + } + + 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 resp.Result.OrderId == 0 { + t.Errorf("Order id cannot be 0") + } + if resp.Result.Status == "" { + t.Errorf("Status cannot be empty") + } + } + } +} diff --git a/ozon/products.go b/ozon/products.go index 4d9192f..63a63a3 100644 --- a/ozon/products.go +++ b/ozon/products.go @@ -708,7 +708,7 @@ type CreateOrUpdateProductItem struct { // Service type. Pass one of the values in upper case: // - IS_CODE_SERVICE, // - IS_NO_CODE_SERVICE - ServiceType string `json:"service_type"` + ServiceType string `json:"service_type" default:"IS_CODE_SERVICE"` // VAT rate for the product: // - 0 — not subject to VAT, diff --git a/ozon/reports.go b/ozon/reports.go index 4b9f30d..f1e920e 100644 --- a/ozon/reports.go +++ b/ozon/reports.go @@ -31,7 +31,7 @@ type GetReportsListParams struct { // - SELLER_RETURNS — returns report, // - SELLER_POSTINGS — shipments report, // - SELLER_FINANCE — financial report - ReportType string `json:"report_type"` + ReportType string `json:"report_type" default:"ALL"` } type GetReportsListResponse struct { @@ -408,7 +408,7 @@ type GetShipmentReportParams struct { // Response language: // - RU — Russian // - EN — English - Language string `json:"language"` + Language string `json:"language" default:"DEFAULT"` } type GetShipmentReportFilter struct {