Переглянути джерело

feat(core): Enable custom fields on ShippingMethod entity (#406)

Co-authored-by: Hendrik Depauw <hendrik@advantitge.com>

Closes #402
hendrikdepauw 5 роки тому
батько
коміт
fbc36ab999

+ 1 - 0
packages/core/src/config/custom-field/custom-field-types.ts

@@ -149,6 +149,7 @@ export interface CustomFields {
     ProductOptionGroup?: CustomFieldConfig[];
     ProductVariant?: CustomFieldConfig[];
     User?: CustomFieldConfig[];
+    ShippingMethod?: CustomFieldConfig[];
 }
 
 /**

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

@@ -138,6 +138,7 @@ export const defaultConfig: RuntimeVendureConfig = {
         ProductOptionGroup: [],
         ProductVariant: [],
         User: [],
+        ShippingMethod: [],
     },
     plugins: [],
 };

+ 1 - 0
packages/core/src/entity/custom-entity-fields.ts

@@ -18,3 +18,4 @@ export class CustomProductVariantFieldsTranslation {}
 export class CustomUserFields {}
 export class CustomGlobalSettingsFields {}
 export class CustomOrderFields {}
+export class CustomShippingMethodFields {}

+ 3 - 3
packages/core/src/entity/register-custom-entity-fields.ts

@@ -27,6 +27,7 @@ import {
     CustomProductOptionGroupFieldsTranslation,
     CustomProductVariantFields,
     CustomProductVariantFieldsTranslation,
+    CustomShippingMethodFields,
     CustomUserFields,
 } from './custom-entity-fields';
 
@@ -62,9 +63,7 @@ function registerCustomFieldsForEntity(
                     const length = customField.length || 255;
                     if (MAX_STRING_LENGTH < length) {
                         throw new Error(
-                            `ERROR: The "length" property of the custom field "${
-                                customField.name
-                            }" is greater than the maximum allowed value of ${MAX_STRING_LENGTH}`,
+                            `ERROR: The "length" property of the custom field "${customField.name}" is greater than the maximum allowed value of ${MAX_STRING_LENGTH}`,
                         );
                     }
                     options.length = length;
@@ -177,4 +176,5 @@ export function registerCustomEntityFields(config: VendureConfig) {
     registerCustomFieldsForEntity(config, 'ProductVariant', CustomProductVariantFieldsTranslation, true);
     registerCustomFieldsForEntity(config, 'User', CustomUserFields);
     registerCustomFieldsForEntity(config, 'GlobalSettings', CustomGlobalSettingsFields);
+    registerCustomFieldsForEntity(config, 'ShippingMethod', CustomShippingMethodFields);
 }

+ 7 - 2
packages/core/src/entity/shipping-method/shipping-method.entity.ts

@@ -4,6 +4,7 @@ import { Column, Entity, JoinTable, ManyToMany } from 'typeorm';
 
 import { ChannelAware, SoftDeletable } from '../../common/types/common-types';
 import { getConfig } from '../../config/config-helpers';
+import { HasCustomFields } from '../../config/custom-field/custom-field-types';
 import {
     ShippingCalculationResult,
     ShippingCalculator,
@@ -11,6 +12,7 @@ import {
 import { ShippingEligibilityChecker } from '../../config/shipping-method/shipping-eligibility-checker';
 import { VendureEntity } from '../base/base.entity';
 import { Channel } from '../channel/channel.entity';
+import { CustomShippingMethodFields } from '../custom-entity-fields';
 import { Order } from '../order/order.entity';
 
 /**
@@ -24,7 +26,7 @@ import { Order } from '../order/order.entity';
  * @docsCategory entities
  */
 @Entity()
-export class ShippingMethod extends VendureEntity implements ChannelAware, SoftDeletable {
+export class ShippingMethod extends VendureEntity implements ChannelAware, SoftDeletable, HasCustomFields {
     private readonly allCheckers: { [code: string]: ShippingEligibilityChecker } = {};
     private readonly allCalculators: { [code: string]: ShippingCalculator } = {};
 
@@ -47,10 +49,13 @@ export class ShippingMethod extends VendureEntity implements ChannelAware, SoftD
 
     @Column('simple-json') calculator: ConfigurableOperation;
 
-    @ManyToMany(type => Channel)
+    @ManyToMany((type) => Channel)
     @JoinTable()
     channels: Channel[];
 
+    @Column((type) => CustomShippingMethodFields)
+    customFields: CustomShippingMethodFields;
+
     async apply(order: Order): Promise<ShippingCalculationResult | undefined> {
         const calculator = this.allCalculators[this.calculator.code];
         if (calculator) {