nopSolutions / zettle-plugin-for-nopcommerce

nopCommerce plugin for Zettle (POS)
https://www.nopcommerce.com/
GNU General Public License v3.0
0 stars 3 forks source link

Inventory doubling on initial synchronization when Inventory Tracking Enabled #1

Open coolio986 opened 6 months ago

coolio986 commented 6 months ago

Using nop 4.70 with the zettle 4.50 plugin ported to 4.70.

When adding new inventory items 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
}

During the inventory update, "balanceBefore" is 0, and "balanceAfter" is 17.

This looks incorrect to me. The ProductCreated webhook clearly has "change" 17.

coolio986 commented 6 months ago

I may have found what looks like it could be the issue. The Inventory Tracking GUIDS are stored as a single array in settings (zettlesettings.inventorytrackinggids). When multiple items are added and sync'd, multiple webhook requests come in, and multiple writes occur to that array out of order. This causes the data to be overwritten from whichever requests happens first (or if multiple happen at the same time) those don't get written.

The tracking gids should be stored as multiple line items in the DB, not as a single object array