Comments (6)
I think this could be handled with a strategy-based approach. This would be the most flexible I think. Something like:
export interface ProductVariantPriceUpdateStrategy extends InjectableStrategy { /** * This would be invoked whenever a price is changed. */ onPriceUpdate( ctx: RequestContect, updatedPrice: ProductVariantPrice, allPrices: ProductVariantPrice[]): Promise<ProductVariantPrice[]> { // For example, let's get all the other prices with the same currencyCode const pricesWithSameCurrency = allPrices.filter(p => p.currencyCode === updatedPrice.currencyCode); // we can now sync the prices pricesWithSameCurrency.forEach(p => p.price = updatedPrice.price); // any returned ProductVariantPrice entities will have the new price // persisted to the DB. return pricesWithSameCurrency; } }I quite like this approach because:
* It requires zero changes to the data model * It can handle different sync directions. E.g. one-way sync only from default channel to child channels, or two-way sync as in this example. * It can be used for other use cases. For instance, let's say you wanted to have automatic currency conversion. This mechanism could be used to do a lookup on exchange rates and update other currencies accordingly.
Very nice approach! This way will be much better
from vendure.
I think this could be handled with a strategy-based approach. This would be the most flexible I think. Something like:
export interface ProductVariantPriceUpdateStrategy extends InjectableStrategy {
/**
* This would be invoked whenever a price is changed.
*/
onPriceUpdate(
ctx: RequestContect,
updatedPrice: ProductVariantPrice,
allPrices: ProductVariantPrice[]): Promise<ProductVariantPrice[]> {
// For example, let's get all the other prices with the same currencyCode
const pricesWithSameCurrency = allPrices.filter(p => p.currencyCode === updatedPrice.currencyCode);
// we can now sync the prices
pricesWithSameCurrency.forEach(p => p.price = updatedPrice.price);
// any returned ProductVariantPrice entities will have the new price
// persisted to the DB.
return pricesWithSameCurrency;
}
}
I quite like this approach because:
- It requires zero changes to the data model
- It can handle different sync directions. E.g. one-way sync only from default channel to child channels, or two-way sync as in this example.
- It can be used for other use cases. For instance, let's say you wanted to have automatic currency conversion. This mechanism could be used to do a lookup on exchange rates and update other currencies accordingly.
from vendure.
@NoahPerez It's not available yet - it is going to be released in v2.2. You can try it now by installing the pre-release v2.2.0-next.3
from vendure.
Within the next 2-3 weeks is very likely.
from vendure.
@dylviz Where should I add ProductVariantPriceUpdateStrategy strategy-based approach or export it as a plugin?
or should I wait for Vendure 2.2 ?
from vendure.
@michaelbromley okay thanks, I will install the pre-release "@vendure/core": "2.2.0"
Do you know when you will release V2.2 ? i saw some nice improvements
from vendure.
Related Issues (20)
- Custom Fields Permission in Admin UI affect the shop-api HOT 2
- As a Vendor, I am unable to edit a Customer. Additionally, the Customer section is empty. The error message displayed is: `Variable "$id" of required type "ID!" was not provided.` HOT 2
- Add Payment To Order in a different channel than the default HOT 4
- Facets codes are globally unique instead of channel unique HOT 1
- Default value in RichTextEditor
- Style RichTextEditor doesn't work
- CustomField datetime limited on dates (cannot set a year before 2014)
- Readonly, Nullable and Unique when adding CustomFields HOT 1
- UI folder not compile on production
- Facet values don't update in admin ui when switching locales
- RoleService findAll list display/pagination is broken due to post query filtering
- Setting a relational customField on Asset with relation Asset results in Errors HOT 2
- Changed option name is not reflected in ProductVariant name
- Extend detail page layout sidebar
- customer group page has incorrect locationId
- Variant creation problem in Generate Product Variants component
- Admin UI: Metrics widget doesn't show numbers on hover: `Invalid language tag: en-`
- Listing options do not show up when selecting facets, and scrolls multiply when this functionality is found HOT 2
- EntityHydrator does not shorten jointable alias names anymore
- RFC: Migration of the Admin UI from Clarity to Tailwind
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vendure.