Prechádzať zdrojové kódy

refactor(core): Split promotion conditions & actions into separate files

Michael Bromley 5 rokov pred
rodič
commit
2ec16fbe9c

+ 1 - 2
packages/core/src/config/default-config.ts

@@ -20,8 +20,7 @@ import { TypeOrmLogger } from './logger/typeorm-logger';
 import { DefaultPriceCalculationStrategy } from './order/default-price-calculation-strategy';
 import { MergeOrdersStrategy } from './order/merge-orders-strategy';
 import { UseGuestStrategy } from './order/use-guest-strategy';
-import { defaultPromotionActions } from './promotion/default-promotion-actions';
-import { defaultPromotionConditions } from './promotion/default-promotion-conditions';
+import { defaultPromotionActions, defaultPromotionConditions } from './promotion';
 import { InMemorySessionCacheStrategy } from './session-cache/in-memory-session-cache-strategy';
 import { defaultShippingCalculator } from './shipping-method/default-shipping-calculator';
 import { defaultShippingEligibilityChecker } from './shipping-method/default-shipping-eligibility-checker';

+ 1 - 4
packages/core/src/config/index.ts

@@ -22,10 +22,7 @@ export * from './order/order-merge-strategy';
 export * from './order/price-calculation-strategy';
 export * from './payment-method/example-payment-method-handler';
 export * from './payment-method/payment-method-handler';
-export * from './promotion/default-promotion-actions';
-export * from './promotion/default-promotion-conditions';
-export * from './promotion/promotion-action';
-export * from './promotion/promotion-condition';
+export * from './promotion';
 export * from './session-cache/session-cache-strategy';
 export * from './session-cache/noop-session-cache-strategy';
 export * from './session-cache/in-memory-session-cache-strategy';

+ 1 - 19
packages/core/src/config/promotion/default-promotion-actions.ts → packages/core/src/config/promotion/actions/facet-values-discount-action.ts

@@ -1,22 +1,6 @@
 import { LanguageCode } from '@vendure/common/lib/generated-types';
 
-import { PromotionItemAction, PromotionOrderAction } from './promotion-action';
-
-export const orderPercentageDiscount = new PromotionOrderAction({
-    code: 'order_percentage_discount',
-    args: {
-        discount: {
-            type: 'int',
-            config: {
-                inputType: 'percentage',
-            },
-        },
-    },
-    execute(order, args) {
-        return -order.subTotal * (args.discount / 100);
-    },
-    description: [{ languageCode: LanguageCode.en, value: 'Discount order by { discount }%' }],
-});
+import { PromotionItemAction } from '../promotion-action';
 
 export const discountOnItemWithFacets = new PromotionItemAction({
     code: 'facet_based_discount',
@@ -42,5 +26,3 @@ export const discountOnItemWithFacets = new PromotionItemAction({
         { languageCode: LanguageCode.en, value: 'Discount products with these facets by { discount }%' },
     ],
 });
-
-export const defaultPromotionActions = [orderPercentageDiscount, discountOnItemWithFacets];

+ 19 - 0
packages/core/src/config/promotion/actions/order-percentage-discount-action.ts

@@ -0,0 +1,19 @@
+import { LanguageCode } from '@vendure/common/lib/generated-types';
+
+import { PromotionOrderAction } from '../promotion-action';
+
+export const orderPercentageDiscount = new PromotionOrderAction({
+    code: 'order_percentage_discount',
+    args: {
+        discount: {
+            type: 'int',
+            config: {
+                inputType: 'percentage',
+            },
+        },
+    },
+    execute(order, args) {
+        return -order.subTotal * (args.discount / 100);
+    },
+    description: [{ languageCode: LanguageCode.en, value: 'Discount order by { discount }%' }],
+});

+ 26 - 0
packages/core/src/config/promotion/conditions/has-facet-values-condition.ts

@@ -0,0 +1,26 @@
+import { LanguageCode } from '@vendure/common/lib/generated-types';
+
+import { Order } from '../../../entity/order/order.entity';
+import { PromotionCondition } from '../promotion-condition';
+
+export const hasFacetValues = new PromotionCondition({
+    code: 'at_least_n_with_facets',
+    description: [
+        { languageCode: LanguageCode.en, value: 'Buy at least { minimum } products with the given facets' },
+    ],
+    args: {
+        minimum: { type: 'int' },
+        facets: { type: 'ID', list: true },
+    },
+
+    // tslint:disable-next-line:no-shadowed-variable
+    async check(order: Order, args, { hasFacetValues }) {
+        let matches = 0;
+        for (const line of order.lines) {
+            if (await hasFacetValues(line, args.facets)) {
+                matches += line.quantity;
+            }
+        }
+        return args.minimum <= matches;
+    },
+});

+ 21 - 0
packages/core/src/config/promotion/conditions/min-order-amount-condition.ts

@@ -0,0 +1,21 @@
+import { LanguageCode } from '@vendure/common/lib/generated-types';
+
+import { PromotionCondition } from '../promotion-condition';
+
+export const minimumOrderAmount = new PromotionCondition({
+    description: [{ languageCode: LanguageCode.en, value: 'If order total is greater than { amount }' }],
+    code: 'minimum_order_amount',
+    args: {
+        amount: { type: 'int', config: { inputType: 'money' } },
+        taxInclusive: { type: 'boolean' },
+        ids: { type: 'string', list: true },
+    },
+    check(order, args) {
+        if (args.taxInclusive) {
+            return order.subTotal >= args.amount;
+        } else {
+            return order.subTotalBeforeTax >= args.amount;
+        }
+    },
+    priorityValue: 10,
+});

+ 0 - 45
packages/core/src/config/promotion/default-promotion-conditions.ts

@@ -1,45 +0,0 @@
-import { LanguageCode } from '@vendure/common/lib/generated-types';
-
-import { Order } from '../../entity/order/order.entity';
-
-import { PromotionCondition } from './promotion-condition';
-
-export const minimumOrderAmount = new PromotionCondition({
-    description: [{ languageCode: LanguageCode.en, value: 'If order total is greater than { amount }' }],
-    code: 'minimum_order_amount',
-    args: {
-        amount: { type: 'int', config: { inputType: 'money' } },
-        taxInclusive: { type: 'boolean' },
-        ids: { type: 'string', list: true },
-    },
-    check(order, args) {
-        if (args.taxInclusive) {
-            return order.subTotal >= args.amount;
-        } else {
-            return order.subTotalBeforeTax >= args.amount;
-        }
-    },
-    priorityValue: 10,
-});
-
-export const atLeastNWithFacets = new PromotionCondition({
-    code: 'at_least_n_with_facets',
-    description: [
-        { languageCode: LanguageCode.en, value: 'Buy at least { minimum } products with the given facets' },
-    ],
-    args: {
-        minimum: { type: 'int' },
-        facets: { type: 'ID', list: true },
-    },
-    async check(order: Order, args, { hasFacetValues }) {
-        let matches = 0;
-        for (const line of order.lines) {
-            if (await hasFacetValues(line, args.facets)) {
-                matches += line.quantity;
-            }
-        }
-        return args.minimum <= matches;
-    },
-});
-
-export const defaultPromotionConditions = [minimumOrderAmount, atLeastNWithFacets];

+ 14 - 0
packages/core/src/config/promotion/index.ts

@@ -0,0 +1,14 @@
+import { discountOnItemWithFacets } from './actions/facet-values-discount-action';
+import { orderPercentageDiscount } from './actions/order-percentage-discount-action';
+import { hasFacetValues } from './conditions/has-facet-values-condition';
+import { minimumOrderAmount } from './conditions/min-order-amount-condition';
+
+export * from './promotion-action';
+export * from './promotion-condition';
+export * from './actions/facet-values-discount-action';
+export * from './actions/order-percentage-discount-action';
+export * from './conditions/has-facet-values-condition';
+export * from './conditions/min-order-amount-condition';
+
+export const defaultPromotionActions = [orderPercentageDiscount, discountOnItemWithFacets];
+export const defaultPromotionConditions = [minimumOrderAmount, hasFacetValues];