nopSolutions / nopCommerce

ASP.NET Core eCommerce software. nopCommerce is a free and open-source shopping cart.
https://www.nopcommerce.com
Other
9.19k stars 5.27k forks source link

Zettle Inventory doubling on initial synchronization when Inventory Tracking Enabled #7094

Closed coolio986 closed 5 months ago

coolio986 commented 6 months ago

nopCommerce version: 4.70

Using nop 4.70 with the zettle plugin.

When adding new inventory items (sync from Nop to zettle) with inventory tracking enabled, the inventory doubles due to the webhook being called to update inventory after creation.

I have added logging into the relevant sections to help me debug since this is an active site that I am unable to connect a debugger to.

This product I am trying to sync has an inventory level of 17.

Here is the data below:

Misc.Zettle information.
Synchronization started at 5:56:44 PM UTC
Add discounts...
Delete products...
  Also delete all existing library items before importing products
Change images...
Update inventory tracking...
Create and update products...
  Prepare 3 records to import
  Import 3 products (#7, #9, #10)
    Import (9164dece-da50-11ee-8f7f-3d465ce14ec0) created at 5:56:46 PM
Synchronization finished at 5:56:46 PM UTC

Then a product creation webhook occurs:


{
  "productUuid": "88398349-da50-11ee-aaa5-074994edb3be",
  "trackingStatusChange": "START_TRACKING",
  "variantChanges": [
    {
      "fromLocationUuid": "35c1f814-d8dd-11ee-80f2-39f9cbd7af56",
      "toLocationUuid": "35c1f856-d8dd-11ee-9912-4e467e438357",
      "variantUuid": "8839836c-da50-11ee-aaa5-074994edb3be",
      "change": 17
    }
  ]
}

UpdateInventoryBalanceAsync is called by webhook "ProductCreated"

{
  "productChanges": [
    {
      "productUuid": "88398349-da50-11ee-aaa5-074994edb3be",
      "trackingStatusChange": "START_TRACKING",
      "variantChanges": [
        {
          "fromLocationUuid": "35c1f814-d8dd-11ee-80f2-39f9cbd7af56",
          "toLocationUuid": "35c1f856-d8dd-11ee-9912-4e467e438357",
          "variantUuid": "8839836c-da50-11ee-aaa5-074994edb3be",
          "change": 17
        }
      ]
    }
  ],
  "returnBalanceForLocationUuid": "35c1f856-d8dd-11ee-9912-4e467e438357",
  "externalUuid": "925991aa-da50-11ee-aaa5-074994edb3be"
}

The problem appears to be the below item, webhook "InventoryBalanceChanged"

{
  "externalUuid": "925991aa-da50-11ee-aaa5-074994edb3be",
  "balanceBefore": [
    {
      "locationUuid": "35c1f856-d8dd-11ee-9912-4e467e438357",
      "productUuid": "88398349-da50-11ee-aaa5-074994edb3be",
      "variantUuid": "8839836c-da50-11ee-aaa5-074994edb3be",
      "balance": 0,
      "created": "2024-03-04T17:56:46.779+00:00"
    }
  ],
  "balanceAfter": [
    {
      "locationUuid": "35c1f856-d8dd-11ee-9912-4e467e438357",
      "productUuid": "88398349-da50-11ee-aaa5-074994edb3be",
      "variantUuid": "8839836c-da50-11ee-aaa5-074994edb3be",
      "balance": 17,
      "created": "2024-03-04T17:56:48.661+00:00"
    }
  ],
  "updated": {
    "timestamp": "2024-03-04T17:56:48.661+00:00"
  },
  "error": null,
  "error_description": null,
  "developerMessage": null
}

The issue: _zettleSettings.InventoryTrackingIds

This object is newed up each time a new thread gets spawned. This is supposed to be the repository for any external tracking GUID's that are transmitted from zettle to nop. Since there are multiple items synced, multiple threads get spawned, and as a result the memory space for each instance of _zettleSettings.InventoryTrackingIds is not shared across requests.

These id's are stored in the database under table setting (zettlesettings.inventorytrackingids) The issue here is that since the object is not shared across requests, this database location is overwritten constantly without concurrency.

If three products get added in bulk, three "InventoryBalanceChanged" webhook requests get fired, and each request only knows of it's own tracking id. not the other two leading to a loss of concurrent data.

I have re-worked this section to prevent this from occurring. I will submit a PR.

RomanovM commented 5 months ago

@coolio986 I've added a few changes on top of your pull request, could you please test this feature again with the latest branch version?

coolio986 commented 5 months ago

@coolio986 I've added a few changes on top of your pull request, could you please test this feature again with the latest branch version?

Will do. Thanks!

coolio986 commented 1 month ago

@coolio986 I've added a few changes on top of your pull request, could you please test this feature again with the latest branch version?

I can confirm that these changes are working properly. It took a few months to get around to it ;) Thanks for the excellent work you do!