Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export interface CustomFields {
ProductOptionGroup?: CustomFieldConfig[];
ProductVariant?: CustomFieldConfig[];
User?: CustomFieldConfig[];
ShippingMethod?: CustomFieldConfig[];
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/config/default-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export const defaultConfig: RuntimeVendureConfig = {
ProductOptionGroup: [],
ProductVariant: [],
User: [],
ShippingMethod: [],
},
plugins: [],
};
1 change: 1 addition & 0 deletions packages/core/src/entity/custom-entity-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ export class CustomProductVariantFieldsTranslation {}
export class CustomUserFields {}
export class CustomGlobalSettingsFields {}
export class CustomOrderFields {}
export class CustomShippingMethodFields {}
6 changes: 3 additions & 3 deletions packages/core/src/entity/register-custom-entity-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
CustomProductOptionGroupFieldsTranslation,
CustomProductVariantFields,
CustomProductVariantFieldsTranslation,
CustomShippingMethodFields,
CustomUserFields,
} from './custom-entity-fields';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ 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,
} from '../../config/shipping-method/shipping-calculator';
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';

/**
Expand All @@ -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 } = {};

Expand All @@ -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) {
Expand Down