Files
twenty/packages/twenty-front/src/modules/object-metadata/utils/formatRelationMetadataInput.ts
T
Weiko 3c7805c6d0 Add field isLabelSyncedWithName (#8829)
## Context
The recent addition of object renaming introduced issues with enum
names. Enum names should follow the pattern
`${schemaName}.${tableName}_${columnName}_enum`. To address this, and to
allow users to customize the API name (which is included in the enum
name, columnName), this PR implements behavior similar to object
renaming by introducing a `isLabelSyncedWithName` boolean.

<img width="624" alt="Screenshot 2024-12-02 at 11 58 49"
src="https://github.com/user-attachments/assets/690fb71c-83f0-4922-80c0-946c92dacc30">
<img width="596" alt="Screenshot 2024-12-02 at 11 58 39"
src="https://github.com/user-attachments/assets/af9a0037-7cf5-40c3-9ed5-d51b340c8087">
2024-12-03 13:22:12 +01:00

63 lines
1.7 KiB
TypeScript

import { RelationType } from '@/settings/data-model/types/RelationType';
import {
CreateRelationInput,
Field,
RelationDefinitionType,
RelationMetadataType,
} from '~/generated-metadata/graphql';
import { formatFieldMetadataItemInput } from './formatFieldMetadataItemInput';
export type FormatRelationMetadataInputParams = {
relationType: RelationType;
field: Pick<Field, 'label' | 'icon' | 'description' | 'name'>;
objectMetadataId: string;
connect: {
field: Pick<Field, 'label' | 'icon' | 'name'>;
objectMetadataId: string;
};
};
export const formatRelationMetadataInput = (
input: FormatRelationMetadataInputParams,
): CreateRelationInput => {
// /!\ MANY_TO_ONE does not exist on backend.
// => Transform into ONE_TO_MANY and invert "from" and "to" data.
const isManyToOne = input.relationType === 'MANY_TO_ONE';
const relationType = isManyToOne
? RelationDefinitionType.OneToMany
: (input.relationType as RelationDefinitionType);
const { field: fromField, objectMetadataId: fromObjectMetadataId } =
isManyToOne ? input.connect : input;
const { field: toField, objectMetadataId: toObjectMetadataId } = isManyToOne
? input
: input.connect;
const {
description: fromDescription,
icon: fromIcon,
label: fromLabel = '',
name: fromName = '',
} = formatFieldMetadataItemInput(fromField);
const {
description: toDescription,
icon: toIcon,
label: toLabel = '',
name: toName = '',
} = formatFieldMetadataItemInput(toField);
return {
fromDescription,
fromIcon,
fromLabel,
fromName,
fromObjectMetadataId,
relationType: relationType as unknown as RelationMetadataType,
toDescription,
toIcon,
toLabel,
toName,
toObjectMetadataId,
};
};