Zend certified PHP/Magento developer

Magento 2 after programetically cancel the order stock restored but item showing as out of stock message in reorder process

I am facing a problem with resume order process.when customer click resume order I am cancelling the order programatically and creating new order with the same qty. In this process the stock is restoring immediatly after cancel step but while adding the same qty product to cart there are 2 scenarios.

  1. if the ordered item > 0 stock remains (after my order) there is not problem with reorder the same item
  2. If I order the item with total available stock (for example item has 4 qty and I ordered all 4 qty with my order means after my order that item qty is zero) and resume the order the stock is restoring (as per my script after cancel the order) but while adding the same qty to cart the system showing “product not available- out of stock” message.

Why this is happening I am not able to identify. I am using magento default cancel function for item cancellation. your help will save my time and get me out from this stuggle 🙂