feat(medusa): invalidate price selection caching within update request (#3553)

* feat: invalidate price selection caching on update

* feat: add `onVariantsPricesUpdate` to PriceSelectionStrategy

* fix: update units

* fix: import

* Create .changeset/tame-pillows-heal.md

* fix: address feedback

* refactor: make `onVariantsPricesUpdate` optional

---------

Co-authored-by: fPolic <frane@medusajs.com>
Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Frane Polić
2023-03-28 11:18:13 +02:00
committed by GitHub
co-authored by fPolic Oliver Windall Juhl
parent 455c56c4b3
commit 1ce3cc5ae4
10 changed files with 53 additions and 47 deletions
@@ -7,7 +7,7 @@ export interface IPriceSelectionStrategy {
/**
* Instantiate a new price selection strategy with the active transaction in
* order to ensure reads are accurate.
* @param manager EntityManager with the queryrunner of the active transaction
* @param manager EntityManager with the query runner of the active transaction
* @returns a new price selection strategy
*/
withTransaction(manager: EntityManager): IPriceSelectionStrategy
@@ -15,15 +15,21 @@ export interface IPriceSelectionStrategy {
/**
* Calculate the original and discount price for a given variant in a set of
* circumstances described in the context.
* @param variant The variant id of the variant for which to retrieve prices
* @param variantId The variant id of the variant for which to retrieve prices
* @param context Details relevant to determine the correct pricing of the variant
* @return pricing details in an object containing the calculated lowest price,
* the default price an all valid prices for the given variant
*/
calculateVariantPrice(
variant_id: string,
variantId: string,
context: PriceSelectionContext
): Promise<PriceSelectionResult>
/**
* Notify price selection strategy that variants prices have been updated.
* @param variantIds The ids of the updated variants
*/
onVariantsPricesUpdate(variantIds: string[]): Promise<void>
}
export abstract class AbstractPriceSelectionStrategy
@@ -34,9 +40,13 @@ export abstract class AbstractPriceSelectionStrategy
): IPriceSelectionStrategy
public abstract calculateVariantPrice(
variant_id: string,
variantId: string,
context: PriceSelectionContext
): Promise<PriceSelectionResult>
public async onVariantsPricesUpdate(variantIds: string[]): Promise<void> {
return void 0
}
}
export function isPriceSelectionStrategy(
@@ -57,6 +67,7 @@ export type PriceSelectionContext = {
currency_code?: string
include_discount_prices?: boolean
tax_rates?: TaxServiceRate[]
ignore_cache?: boolean
}
enum DefaultPriceType {