From b3ed6cb9030148441327c9899af1437fa7559530 Mon Sep 17 00:00:00 2001 From: "gitstart-app[bot]" <57568882+gitstart-app[bot]@users.noreply.github.com> Date: Thu, 19 Sep 2024 14:05:28 +0200 Subject: [PATCH] =?UTF-8?q?[Bug]=C2=A0Select=20options=20names=20can't=20s?= =?UTF-8?q?tart=20with=20a=20number=20(#7079)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR was created by [GitStart](https://gitstart.com/) to address the requirements from this ticket: [TWNTY-6980](https://clients.gitstart.com/twenty/5449/tickets/TWNTY-6980). This ticket was imported from: [TWNTY-6980](https://github.com/twentyhq/twenty/issues/6980) --- ### Description - **fix**: added a transformation step that prefixes the newly added option with an underscore before the Graphql enum is generated so it saves successfully and passes the default GraphQL validation. ### Demo - ### Refs #6980 Fixes #6980 Co-authored-by: gitstart-twenty Co-authored-by: Weiko --- .../validation-schemas/selectOptionsSchema.ts | 4 ---- .../factories/enum-type-definition.factory.ts | 7 ++++--- .../field-metadata/dtos/field-metadata.dto.ts | 6 ++++++ .../src/engine/utils/transform-enum-value.ts | 14 ++++++++++++++ 4 files changed, 24 insertions(+), 7 deletions(-) create mode 100644 packages/twenty-server/src/engine/utils/transform-enum-value.ts diff --git a/packages/twenty-front/src/modules/object-metadata/validation-schemas/selectOptionsSchema.ts b/packages/twenty-front/src/modules/object-metadata/validation-schemas/selectOptionsSchema.ts index ff767cdd5493..3893306c87ce 100644 --- a/packages/twenty-front/src/modules/object-metadata/validation-schemas/selectOptionsSchema.ts +++ b/packages/twenty-front/src/modules/object-metadata/validation-schemas/selectOptionsSchema.ts @@ -2,7 +2,6 @@ import { themeColorSchema } from 'twenty-ui'; import { z } from 'zod'; import { FieldMetadataItemOption } from '@/object-metadata/types/FieldMetadataItem'; -import { getOptionValueFromLabel } from '@/settings/data-model/fields/forms/select/utils/getOptionValueFromLabel'; import { computeOptionValueFromLabelOrThrow } from '~/pages/settings/data-model/utils/compute-option-value-from-label.utils'; const selectOptionSchema = z @@ -13,9 +12,6 @@ const selectOptionSchema = z position: z.number(), value: z.string(), }) - .refine((option) => option.value === getOptionValueFromLabel(option.label), { - message: 'Value does not match label', - }) .refine( (option) => { try { diff --git a/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/factories/enum-type-definition.factory.ts b/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/factories/enum-type-definition.factory.ts index de77733d6151..aefd82efc995 100644 --- a/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/factories/enum-type-definition.factory.ts +++ b/packages/twenty-server/src/engine/api/graphql/workspace-schema-builder/factories/enum-type-definition.factory.ts @@ -3,15 +3,16 @@ import { Injectable, Logger } from '@nestjs/common'; import { GraphQLEnumType } from 'graphql'; import { WorkspaceBuildSchemaOptions } from 'src/engine/api/graphql/workspace-schema-builder/interfaces/workspace-build-schema-optionts.interface'; -import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface'; import { FieldMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata.interface'; +import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/object-metadata.interface'; -import { pascalCase } from 'src/utils/pascal-case'; import { FieldMetadataComplexOption, FieldMetadataDefaultOption, } from 'src/engine/metadata-modules/field-metadata/dtos/options.input'; import { isEnumFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/utils/is-enum-field-metadata-type.util'; +import { transformEnumValue } from 'src/engine/utils/transform-enum-value'; +import { pascalCase } from 'src/utils/pascal-case'; export interface EnumTypeDefinition { target: string; @@ -53,7 +54,7 @@ export class EnumTypeDefinitionFactory { ): GraphQLEnumType { // FixMe: It's a hack until Typescript get fixed on union types for reduce function // https://github.com/microsoft/TypeScript/issues/36390 - const enumOptions = fieldMetadata.options as Array< + const enumOptions = transformEnumValue(fieldMetadata.options) as Array< FieldMetadataDefaultOption | FieldMetadataComplexOption >; diff --git a/packages/twenty-server/src/engine/metadata-modules/field-metadata/dtos/field-metadata.dto.ts b/packages/twenty-server/src/engine/metadata-modules/field-metadata/dtos/field-metadata.dto.ts index 576ce0b64824..b81cf7a76b8b 100644 --- a/packages/twenty-server/src/engine/metadata-modules/field-metadata/dtos/field-metadata.dto.ts +++ b/packages/twenty-server/src/engine/metadata-modules/field-metadata/dtos/field-metadata.dto.ts @@ -12,6 +12,7 @@ import { QueryOptions, Relation, } from '@ptc-org/nestjs-query-graphql'; +import { Transform } from 'class-transformer'; import { IsBoolean, IsDateString, @@ -30,11 +31,13 @@ import { FieldMetadataSettings } from 'src/engine/metadata-modules/field-metadat import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars'; import { IsValidMetadataName } from 'src/engine/decorators/metadata/is-valid-metadata-name.decorator'; +import { FieldMetadataDefaultOption } from 'src/engine/metadata-modules/field-metadata/dtos/options.input'; import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity'; import { IsFieldMetadataDefaultValue } from 'src/engine/metadata-modules/field-metadata/validators/is-field-metadata-default-value.validator'; import { IsFieldMetadataOptions } from 'src/engine/metadata-modules/field-metadata/validators/is-field-metadata-options.validator'; import { ObjectMetadataDTO } from 'src/engine/metadata-modules/object-metadata/dtos/object-metadata.dto'; import { RelationMetadataDTO } from 'src/engine/metadata-modules/relation-metadata/dtos/relation-metadata.dto'; +import { transformEnumValue } from 'src/engine/utils/transform-enum-value'; registerEnumType(FieldMetadataType, { name: 'FieldMetadataType', @@ -120,6 +123,9 @@ export class FieldMetadataDTO< @Field(() => GraphQLJSON, { nullable: true }) defaultValue?: FieldMetadataDefaultValue; + @Transform(({ value }) => + transformEnumValue(value as FieldMetadataDefaultOption[]), + ) @Validate(IsFieldMetadataOptions) @IsOptional() @Field(() => GraphQLJSON, { nullable: true }) diff --git a/packages/twenty-server/src/engine/utils/transform-enum-value.ts b/packages/twenty-server/src/engine/utils/transform-enum-value.ts new file mode 100644 index 000000000000..20eb24e49309 --- /dev/null +++ b/packages/twenty-server/src/engine/utils/transform-enum-value.ts @@ -0,0 +1,14 @@ +import { FieldMetadataDefaultOption } from 'src/engine/metadata-modules/field-metadata/dtos/options.input'; + +export function transformEnumValue(options?: FieldMetadataDefaultOption[]) { + return options?.map((option) => { + if (/^\d/.test(option.value)) { + return { + ...option, + value: `_${option.value}`, + }; + } + + return option; + }); +}