Skip to content

Commit

Permalink
fix(core): Log error on misconfigured localized custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Nov 21, 2023
1 parent d665ec6 commit 5775447
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ export async function preBootstrapConfig(
});

let config = getConfig();
// The logger is set here so that we are able to log any messages prior to the final
// logger (which may depend on config coming from a plugin) being set.
Logger.useLogger(config.logger);
config = await runPluginConfigurations(config);
const entityIdStrategy = config.entityOptions.entityIdStrategy ?? config.entityIdStrategy;
setEntityIdStrategy(entityIdStrategy, entities);
Expand Down
55 changes: 55 additions & 0 deletions packages/core/src/entity/register-custom-entity-fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,52 +255,107 @@ function getDefault(customField: CustomFieldConfig, dbEngine: DataSourceOptions[
return type === 'datetime' ? formatDefaultDatetime(dbEngine, defaultValue) : defaultValue;
}

function assertLocaleFieldsNotSpecified(config: VendureConfig, entityName: keyof CustomFields) {
const customFields = config.customFields && config.customFields[entityName];
if (customFields) {
for (const customField of customFields) {
if (customField.type === 'localeString' || customField.type === 'localeText') {
Logger.error(
`Custom field "${customField.name}" on entity "${entityName}" cannot be of type "localeString" or "localeText". ` +
`This entity does not support localization.`,
);
}
}
}
}

/**
* Dynamically registers any custom fields with TypeORM. This function should be run at the bootstrap
* stage of the app lifecycle, before the AppModule is initialized.
*/
export function registerCustomEntityFields(config: VendureConfig) {
registerCustomFieldsForEntity(config, 'Address', CustomAddressFields);
assertLocaleFieldsNotSpecified(config, 'Address');

registerCustomFieldsForEntity(config, 'Administrator', CustomAdministratorFields);
assertLocaleFieldsNotSpecified(config, 'Administrator');

registerCustomFieldsForEntity(config, 'Asset', CustomAssetFields);
assertLocaleFieldsNotSpecified(config, 'Asset');

registerCustomFieldsForEntity(config, 'Collection', CustomCollectionFields);
registerCustomFieldsForEntity(config, 'Collection', CustomCollectionFieldsTranslation, true);

registerCustomFieldsForEntity(config, 'Channel', CustomChannelFields);
assertLocaleFieldsNotSpecified(config, 'Channel');

registerCustomFieldsForEntity(config, 'Customer', CustomCustomerFields);
assertLocaleFieldsNotSpecified(config, 'Customer');

registerCustomFieldsForEntity(config, 'CustomerGroup', CustomCustomerGroupFields);
assertLocaleFieldsNotSpecified(config, 'CustomerGroup');

registerCustomFieldsForEntity(config, 'Facet', CustomFacetFields);
registerCustomFieldsForEntity(config, 'Facet', CustomFacetFieldsTranslation, true);

registerCustomFieldsForEntity(config, 'FacetValue', CustomFacetValueFields);
registerCustomFieldsForEntity(config, 'FacetValue', CustomFacetValueFieldsTranslation, true);

registerCustomFieldsForEntity(config, 'Fulfillment', CustomFulfillmentFields);
assertLocaleFieldsNotSpecified(config, 'Fulfillment');

registerCustomFieldsForEntity(config, 'Order', CustomOrderFields);
assertLocaleFieldsNotSpecified(config, 'Order');

registerCustomFieldsForEntity(config, 'OrderLine', CustomOrderLineFields);
assertLocaleFieldsNotSpecified(config, 'OrderLine');

registerCustomFieldsForEntity(config, 'PaymentMethod', CustomPaymentMethodFields);
registerCustomFieldsForEntity(config, 'PaymentMethod', CustomPaymentMethodFieldsTranslation, true);

registerCustomFieldsForEntity(config, 'Product', CustomProductFields);
registerCustomFieldsForEntity(config, 'Product', CustomProductFieldsTranslation, true);

registerCustomFieldsForEntity(config, 'ProductOption', CustomProductOptionFields);
registerCustomFieldsForEntity(config, 'ProductOption', CustomProductOptionFieldsTranslation, true);

registerCustomFieldsForEntity(config, 'ProductOptionGroup', CustomProductOptionGroupFields);
registerCustomFieldsForEntity(
config,
'ProductOptionGroup',
CustomProductOptionGroupFieldsTranslation,
true,
);

registerCustomFieldsForEntity(config, 'ProductVariant', CustomProductVariantFields);
registerCustomFieldsForEntity(config, 'ProductVariant', CustomProductVariantFieldsTranslation, true);

registerCustomFieldsForEntity(config, 'Promotion', CustomPromotionFields);
registerCustomFieldsForEntity(config, 'Promotion', CustomPromotionFieldsTranslation, true);

registerCustomFieldsForEntity(config, 'TaxCategory', CustomTaxCategoryFields);
assertLocaleFieldsNotSpecified(config, 'TaxCategory');

registerCustomFieldsForEntity(config, 'TaxRate', CustomTaxRateFields);
assertLocaleFieldsNotSpecified(config, 'TaxRate');

registerCustomFieldsForEntity(config, 'User', CustomUserFields);
assertLocaleFieldsNotSpecified(config, 'User');
registerCustomFieldsForEntity(config, 'GlobalSettings', CustomGlobalSettingsFields);
assertLocaleFieldsNotSpecified(config, 'GlobalSettings');

registerCustomFieldsForEntity(config, 'Region', CustomRegionFields);
registerCustomFieldsForEntity(config, 'Region', CustomRegionFieldsTranslation, true);

registerCustomFieldsForEntity(config, 'Seller', CustomSellerFields);
assertLocaleFieldsNotSpecified(config, 'Seller');

registerCustomFieldsForEntity(config, 'ShippingMethod', CustomShippingMethodFields);
registerCustomFieldsForEntity(config, 'ShippingMethod', CustomShippingMethodFieldsTranslation, true);

registerCustomFieldsForEntity(config, 'StockLocation', CustomStockLocationFields);
assertLocaleFieldsNotSpecified(config, 'StockLocation');

registerCustomFieldsForEntity(config, 'Zone', CustomZoneFields);
assertLocaleFieldsNotSpecified(config, 'Zone');
}

0 comments on commit 5775447

Please sign in to comment.