فهرست منبع

feat(server): Create required adjustment operations & actions

Michael Bromley 7 سال پیش
والد
کامیت
8cf4107ecd

+ 16 - 0
server/src/config/adjustment/required-adjustment-actions.ts

@@ -0,0 +1,16 @@
+import { AdjustmentType } from 'shared/generated-types';
+
+import { AdjustmentActionDefinition } from './adjustment-types';
+
+export const taxAction: AdjustmentActionDefinition = {
+    type: AdjustmentType.TAX,
+    code: 'tax_action',
+    args: [{ name: 'discount', type: 'percentage' }],
+    calculate(order, args) {
+        return order.items.map(item => ({
+            orderItemId: item.id,
+            amount: -(item.totalPrice * args.discount) / 100,
+        }));
+    },
+    description: 'Apply tax of { discount }%',
+};

+ 15 - 0
server/src/config/adjustment/required-adjustment-conditions.ts

@@ -0,0 +1,15 @@
+import { AdjustmentType } from 'shared/generated-types';
+
+import { Order } from '../../entity/order/order.entity';
+
+import { AdjustmentConditionDefinition } from './adjustment-types';
+
+export const taxCondition: AdjustmentConditionDefinition = {
+    type: AdjustmentType.TAX,
+    code: 'tax_condition',
+    args: [],
+    predicate(order: Order, args) {
+        return true;
+    },
+    description: 'Apply tax to all orders',
+};

+ 19 - 0
server/src/config/merge-config.ts

@@ -1,5 +1,7 @@
 import { DeepPartial } from 'shared/shared-types';
 
+import { taxAction } from './adjustment/required-adjustment-actions';
+import { taxCondition } from './adjustment/required-adjustment-conditions';
 import { VendureConfig } from './vendure-config';
 
 /**
@@ -41,5 +43,22 @@ export function mergeConfig<T extends VendureConfig>(target: T, source: DeepPart
         }
     }
 
+    // Always include the required adjustment operations
+    const requiredAdjustmentActions = [taxAction];
+    const requiredAdjustmentConditions = [taxCondition];
+    for (const requiredAction of requiredAdjustmentActions) {
+        if (target.adjustmentActions && !target.adjustmentActions.find(a => a.code === requiredAction.code)) {
+            target.adjustmentActions.push(requiredAction);
+        }
+    }
+    for (const requiredCondition of requiredAdjustmentConditions) {
+        if (
+            target.adjustmentConditions &&
+            !target.adjustmentConditions.find(c => c.code === requiredCondition.code)
+        ) {
+            target.adjustmentConditions.push(requiredCondition);
+        }
+    }
+
     return target;
 }

+ 4 - 0
server/src/entity/adjustment-source/adjustment-source.entity.ts

@@ -27,6 +27,10 @@ export class AdjustmentSource extends VendureEntity implements ChannelAware {
     @Column('simple-json') actions: AdjustmentOperation[];
 }
 
+/**
+ * When an AdjustmentSource is applied to an OrderItem or Order, an Adjustment is
+ * generated based on the actions assigned to the AdjustmentSource.
+ */
 export interface Adjustment {
     adjustmentSourceId: ID;
     description: string;

+ 1 - 1
server/src/service/providers/adjustment-source.service.ts

@@ -156,7 +156,7 @@ export class AdjustmentSourceService {
             type === 'condition' ? this.availableConditions : this.availableActions;
         const match = available.find(a => a.code === code);
         if (!match) {
-            throw new I18nError(`error.adjustment-source-with-code-not-found`, { code });
+            throw new I18nError(`error.adjustment-operation-with-code-not-found`, { code });
         }
         return match;
     }