commercetools / commercetools-project-sync

Dockerized CLI application which allows to automatically sync different resources between commercetools projects
44 stars 23 forks source link

Product Sync erroring when using product discounts #363

Closed dazlious closed 2 years ago

dazlious commented 2 years ago

In our project, we are experiencing issues when syncing our products.

The main reason for that is that the prices include the discount which is not synced by this tool.

This command is being used:

docker run \                                                                   
-e SOURCE_PROJECT_KEY=XXXXX \
-e SOURCE_CLIENT_ID=XXXX\
-e SOURCE_CLIENT_SECRET=XXX\
-e SOURCE_SCOPES=XXXX \
-e TARGET_PROJECT_KEY=XXXX\
-e TARGET_CLIENT_ID=XXXX \
-e TARGET_CLIENT_SECRET=XXXX \
commercetools/commercetools-project-sync:5.1.0 -s products

This is the error we are running into.

{"timestamp":"2022-03-01T17:54:37.717Z","severity":"ERROR","loggerName":"com.commercetools.project.sync.product.ProductSyncer","message":"Error when trying to sync product. Existing key: <<not present>>. Update actions: []","stackHash":"xxxxx","stackTrace":"i.s.s.c.ErrorResponseException: detailMessage: The referenced object of type 'product-discount' with identifier 'XXXXX' was not found. It either doesn't exist, or it can't be accessed from this endpoint (e.g., if the endpoint filters by store or customer account).\nsummary: POST https://api.europe-west1.gcp.commercetools.com/xxxxxxx/products failed  with response code 400 with X-Correlation-ID `xxxxx/xxxxxxx` on 2022-03-01T17:54:33.728622Z\nhttp response formatted body: {\n  \"statusCode\" : 400,\n  \"message\" : \"The referenced object of type 'product-discount' with identifier 'xxxxxx' was not found. It either doesn't exist, or it can't be accessed from this endpoint (e.g., if the endpoint filters by store or customer account).\"
...
...
...
praveenkumarct commented 2 years ago

Hello @dazlious,

Thanks for writing us. Yes, This project doesn't support syncing Product-discounts yet. Can you try copying Product-discounts manually and then perform sync using this project?

Thanks.

dazlious commented 2 years ago

@praveenkumarct: I can sync the product-discounts manually. I can also create them manually. The issue here is that the uuid of the source project product discount needs to be found in the source project.

AFAIK I cannot create a product discount with a specific id, or can I?

Additionally, as the tool is not able to sync product-discounts, why doesn't it skip this field for now and syncs without the discount?

butenkor commented 2 years ago

One possibility would be to fork the repo and either modify the way we build the drafts from fetched data of the source project (be ignoring of the price discounts) or by the usage of the beforeCreate and beforeUpdate callbacks which can be defined here. Example of the callbacks can be found here. In the beforeCreate callback you can then return a new draft without the discount and in the beforeUpdate callback you can filter for price change update actions and remove from those the discounts too.

Additionally, as the tool is not able to sync product-discounts, why doesn't it skip this field for now and syncs without the discount?

We will check if we can support that case.

jrabepixelart commented 2 years ago

Additionally, as the tool is not able to sync product-discounts, why doesn't it skip this field for now and syncs without the discount?

Upvote for a fix on that. It is very tedious for product syncs to fail on the Product Discounts. Its not needed to to have the Product Discount Reference on the target project as the products would evaluate against the active product discounts nevertheless after product creation/update

butenkor commented 2 years ago

We will be looking into this issue in the upcoming 3-4 weeks. If you mind this fix earlier you can create a PR.

The issue might be fixed in project-sync if we iterate over the returned drafts here: https://github.com/commercetools/commercetools-project-sync/blob/883f28dc55a00e66cabcf0d84a9e20f725819e43/src/main/java/com/commercetools/project/sync/product/ProductSyncer.java#L111

and set the discount for each of the prices to null with that: https://github.com/commercetools/commercetools-jvm-sdk/blob/3ddea540456425553739869d7ac0c3c53bf16d51/commercetools-models/src/main/java/io/sphere/sdk/products/PriceDraftBuilder.java#L119