-
-
Notifications
You must be signed in to change notification settings - Fork 932
/
default-product-variant-price-update-strategy.ts
79 lines (73 loc) · 2.8 KB
/
default-product-variant-price-update-strategy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { RequestContext } from '../../api/common/request-context';
import { ProductVariantPrice } from '../../entity/product-variant/product-variant-price.entity';
import { ProductVariantPriceUpdateStrategy } from './product-variant-price-update-strategy';
/**
* @description
* The options available to the {@link DefaultProductVariantPriceUpdateStrategy}.
*
* @docsCategory configuration
* @docsPage ProductVariantPriceUpdateStrategy
* @since 2.2.0
*/
export interface DefaultProductVariantPriceUpdateStrategyOptions {
/**
* @description
* When `true`, any price changes to a ProductVariant in one Channel will update any other
* prices of the same currencyCode in other Channels. Note that if there are different
* tax settings across the channels, these will not be taken into account. To handle this
* case, a custom strategy should be implemented.
*/
syncPricesAcrossChannels: boolean;
}
/**
* @description
* The default {@link ProductVariantPriceUpdateStrategy} which by default will not update any other
* prices when a price is created, updated or deleted.
*
* If the `syncPricesAcrossChannels` option is set to `true`, then when a price is updated in one Channel,
* the price of the same currencyCode in other Channels will be updated to match. Note that if there are different
* tax settings across the channels, these will not be taken into account. To handle this
* case, a custom strategy should be implemented.
*
* @example
* ```ts
* import { DefaultProductVariantPriceUpdateStrategy, VendureConfig } from '\@vendure/core';
*
* export const config: VendureConfig = {
* // ...
* catalogOptions: {
* // highlight-start
* productVariantPriceUpdateStrategy: new DefaultProductVariantPriceUpdateStrategy({
* syncPricesAcrossChannels: true,
* }),
* // highlight-end
* },
* // ...
* };
* ```
*
* @docsCategory configuration
* @docsPage ProductVariantPriceUpdateStrategy
* @since 2.2.0
*/
export class DefaultProductVariantPriceUpdateStrategy implements ProductVariantPriceUpdateStrategy {
constructor(private options: DefaultProductVariantPriceUpdateStrategyOptions) {}
onPriceCreated(ctx: RequestContext, price: ProductVariantPrice) {
return [];
}
onPriceUpdated(ctx: RequestContext, updatedPrice: ProductVariantPrice, prices: ProductVariantPrice[]) {
if (this.options.syncPricesAcrossChannels) {
return prices
.filter(p => p.currencyCode === updatedPrice.currencyCode)
.map(p => ({
id: p.id,
price: updatedPrice.price,
}));
} else {
return [];
}
}
onPriceDeleted(ctx: RequestContext, deletedPrice: ProductVariantPrice, prices: ProductVariantPrice[]) {
return [];
}
}