vendure-ecommerce / vendure

The commerce platform with customization in its DNA.
https://www.vendure.io
Other
5.74k stars 1.02k forks source link

Stripe Plugin - Product variant stock level not updated in the ElasticSearch index after a successful transaction #3066

Open LeftoversTodayAppAdmin opened 1 month ago

LeftoversTodayAppAdmin commented 1 month ago

Describe the bug I am using ElasticSearch as the search backend and when I use Stripe Plugin to successfully complete a purchase, the search index for that product variant is not updated. As a result, the search results still have the inStock value set to true and shows an out of stock item in the search results. The products API shows the correct stock level immediately after the transaction and when the stock is allocated.

To Reproduce Steps to reproduce the behavior:

  1. Use the Stripe Plugin to complete a purchase of a variant that has only 1 item in stock
  2. Call the searchProducts API
  3. The product variant that just went out of stock still shows as inStock=true

Expected behavior

Environment (please complete the following information):

Additional context Looks like DefaultSearchPluginInitOptions has a parameter called indexStockStatus, what is a similar option for ElasticSearch or is it enabled by default? Also is it the responsibility of the Stripe plugin to trigger a reindex for each variant in the order?

image bug5 bug6 bug7

michaelbromley commented 1 month ago

Thanks for the report.

This should be a relatively straightforward one to solve. The problem is that currently neither the DefaultSearchPlugin nor the ElasticSearchPlugin are listening for the StockMovementEvent, which is the event that gets published whenever the stock level is being changed for a ProductVariant.

In the case of the DefaultSearchPlugin, the event subscribers are defined here.

For the ElasticSearchPlugin they are defined here.

LeftoversTodayAppAdmin commented 1 month ago

So if I just add something like this, it should be enough to keep the product variant stock level up to date in ElasticSearch?

this.eventBus.ofType(StockMovementEvent).subscribe(event => {
            return this.elasticsearchIndexService.updateVariants(event.ctx, event.variants);
        });
dlhck commented 1 month ago

@LeftoversTodayAppAdmin yes, that seems like a good fix!

LeftoversTodayAppAdmin commented 1 month ago

Pull Request: https://github.com/vendure-ecommerce/vendure/pull/3112