Create record page layout after custom object creation (#17923)
## Context Creating a new object should now also create its record page layout, tabs and widgets, including fields widget with its associated views/view fields. Custom objects record page layout fields widgets don't have section per default Note: I had to enable some widget creation through the custom API but we should now implement proper validation (which should be minimal since there is usually only the configuration type in the configuration (except for FIELDS widget which contains a viewId) Next step: Create view field should also create a viewField for the FIELDS_WIDGET view (we should also add in the FIELDS widget configuration a newFieldDefaults which will contain default visibility and position to apply to the new view field) The feature is still gated behind an env variable (this was necessary for workspace creation, not so much here in this case but I prefer to keep the same path for consistence)
This commit is contained in:
+59
@@ -0,0 +1,59 @@
|
||||
import { msg, t } from '@lingui/core/macro';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import { validate as uuidValidate } from 'uuid';
|
||||
|
||||
import { type ValidateFlatPageLayoutWidgetTypeSpecificitiesForCreationArgs } from 'src/engine/metadata-modules/flat-page-layout-widget/services/flat-page-layout-widget-type-validator.service';
|
||||
import { type FlatPageLayoutWidgetValidationError } from 'src/engine/metadata-modules/flat-page-layout-widget/types/flat-page-layout-widget-validation-error.type';
|
||||
import { WidgetConfigurationType } from 'src/engine/metadata-modules/page-layout-widget/enums/widget-configuration-type.type';
|
||||
import { PageLayoutWidgetExceptionCode } from 'src/engine/metadata-modules/page-layout-widget/exceptions/page-layout-widget.exception';
|
||||
|
||||
export const validateFieldsFlatPageLayoutWidgetForCreation = (
|
||||
args: ValidateFlatPageLayoutWidgetTypeSpecificitiesForCreationArgs,
|
||||
): FlatPageLayoutWidgetValidationError[] => {
|
||||
const { flatEntityToValidate } = args;
|
||||
const { universalConfiguration, title: widgetTitle } = flatEntityToValidate;
|
||||
const errors: FlatPageLayoutWidgetValidationError[] = [];
|
||||
|
||||
if (!isDefined(universalConfiguration)) {
|
||||
errors.push({
|
||||
code: PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
|
||||
message: t`Configuration is required for fields widget "${widgetTitle}"`,
|
||||
userFriendlyMessage: msg`Configuration is required for fields widget`,
|
||||
});
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
if (
|
||||
universalConfiguration.configurationType !== WidgetConfigurationType.FIELDS
|
||||
) {
|
||||
const actualString =
|
||||
universalConfiguration.configurationType?.toString() ?? 'undefined';
|
||||
|
||||
errors.push({
|
||||
code: PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
|
||||
message: t`Invalid configuration type for fields widget "${widgetTitle}". Expected FIELDS, got ${actualString}`,
|
||||
userFriendlyMessage: msg`Invalid configuration type for fields widget`,
|
||||
value: universalConfiguration.configurationType,
|
||||
});
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
const viewId = (
|
||||
universalConfiguration as { configurationType: string; viewId?: unknown }
|
||||
).viewId;
|
||||
|
||||
if (isDefined(viewId) && viewId !== null) {
|
||||
if (typeof viewId !== 'string' || !uuidValidate(viewId)) {
|
||||
errors.push({
|
||||
code: PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
|
||||
message: t`Invalid viewId for fields widget "${widgetTitle}". Expected a valid UUID`,
|
||||
userFriendlyMessage: msg`Invalid viewId for fields widget`,
|
||||
value: viewId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
};
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
import { msg, t } from '@lingui/core/macro';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
|
||||
import { type ValidateFlatPageLayoutWidgetTypeSpecificitiesForCreationArgs } from 'src/engine/metadata-modules/flat-page-layout-widget/services/flat-page-layout-widget-type-validator.service';
|
||||
import { type FlatPageLayoutWidgetValidationError } from 'src/engine/metadata-modules/flat-page-layout-widget/types/flat-page-layout-widget-validation-error.type';
|
||||
import { type WidgetConfigurationType } from 'src/engine/metadata-modules/page-layout-widget/enums/widget-configuration-type.type';
|
||||
import { PageLayoutWidgetExceptionCode } from 'src/engine/metadata-modules/page-layout-widget/exceptions/page-layout-widget.exception';
|
||||
|
||||
export const validateSimpleRecordPageWidgetForCreation =
|
||||
(expectedConfigurationType: WidgetConfigurationType) =>
|
||||
(
|
||||
args: ValidateFlatPageLayoutWidgetTypeSpecificitiesForCreationArgs,
|
||||
): FlatPageLayoutWidgetValidationError[] => {
|
||||
const { flatEntityToValidate } = args;
|
||||
const { universalConfiguration, title: widgetTitle } = flatEntityToValidate;
|
||||
const errors: FlatPageLayoutWidgetValidationError[] = [];
|
||||
|
||||
if (!isDefined(universalConfiguration)) {
|
||||
errors.push({
|
||||
code: PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
|
||||
message: t`Configuration is required for widget "${widgetTitle}"`,
|
||||
userFriendlyMessage: msg`Configuration is required for widget`,
|
||||
});
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
if (
|
||||
universalConfiguration.configurationType !== expectedConfigurationType
|
||||
) {
|
||||
const expectedString = expectedConfigurationType.toString();
|
||||
const actualString =
|
||||
universalConfiguration.configurationType?.toString() ?? 'undefined';
|
||||
|
||||
errors.push({
|
||||
code: PageLayoutWidgetExceptionCode.INVALID_PAGE_LAYOUT_WIDGET_DATA,
|
||||
message: t`Invalid configuration type for widget "${widgetTitle}". Expected ${expectedString}, got ${actualString}`,
|
||||
userFriendlyMessage: msg`Invalid configuration type for widget`,
|
||||
value: universalConfiguration.configurationType,
|
||||
});
|
||||
}
|
||||
|
||||
return errors;
|
||||
};
|
||||
Reference in New Issue
Block a user