Skip to content

Wrong total_count on GET /V1/inventory/export-stock-salable-qty/{salesChannelType}/{salesChannelCode} API #3416

Open
@lamasfoker

Description

Preconditions

  1. MSI tagged with 1.2.7-beta3, but also previous versions have the same bug;
  2. A fixed number of products, in this example 95486.

Steps to reproduce

Call the GET /V1/inventory/export-stock-salable-qty/{salesChannelType}/{salesChannelCode} API

curl --location --globoff 'https://127.0.0.1:8080/rest/V1/inventory/export-stock-salable-qty/{salesChannelType}/{salesChannelCode}?searchCriteria[currentPage]=5&searchCriteria[pageSize]=1000' \
--header 'Authorization: Bearer TOKEN' 

Expected result

total_count should return the number of product with stock item in the select sales channel:

{
    "items": [
        {
            "sku": "SKU_1",
            "qty": 0,
            "is_salable": false
        },
        [...]
        {
            "sku": "SKU_1000",
            "qty": 0,
            "is_salable": false
        }
    ],
    "search_criteria": {
        "filter_groups": [],
        "page_size": 1000,
        "current_page": 5
    },
    "total_count": 95486
}

Actual result

total_count return instead the number of items in the current response:

{
    "items": [
        {
            "sku": "SKU_1",
            "qty": 0,
            "is_salable": false
        },
        [...]
        {
            "sku": "SKU_1000",
            "qty": 0,
            "is_salable": false
        }
    ],
    "search_criteria": {
        "filter_groups": [],
        "page_size": 1000,
        "current_page": 5
    },
    "total_count": 1000
}

You can crearly see the bug here. Proposed solution:

    public function execute(
        \Magento\InventorySalesApi\Api\Data\SalesChannelInterface $salesChannel,
        \Magento\Framework\Api\SearchCriteriaInterface $searchCriteria
    ): ExportStockSalableQtySearchResultInterface {
        $stock = $this->getStockBySalesChannel->execute($salesChannel);
        $productSearchResult = $this->getProducts($searchCriteria);
        $items = $this->preciseExportStockProcessor->execute($productSearchResult->getItems(), $stock->getStockId());
        /** @var ExportStockSalableQtySearchResultInterface $searchResult */
        $searchResult = $this->exportStockSalableQtySearchResultFactory->create();
        $searchResult->setSearchCriteria($productSearchResult->getSearchCriteria());
        $searchResult->setItems($items);
-        $searchResult->setTotalCount(count($items));
+        $searchResult->setTotalCount($productSearchResult->getTotalCount());

        return $searchResult;
    }

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions