0d5fedfb0a
Fixes https://github.com/twentyhq/twenty/issues/13577. When naming or renaming an object, we are already checking that the name was available compared to other existing objects. But we also need to check that the relation fields that will be created or updated are available as well: we create relation fields on Attachment, Note, Favorite, Task and TimelineActivites, that bear the name of the object. Thus, it is not possible to create an object that has the same name as one of the fields on Attachment, Note etc: name, createdAt, ... are not valid names.
72 lines
2.4 KiB
TypeScript
72 lines
2.4 KiB
TypeScript
import { t } from '@lingui/core/macro';
|
|
import { FieldMetadataType } from 'twenty-shared/types';
|
|
|
|
import { compositeTypeDefinitions } from 'src/engine/metadata-modules/field-metadata/composite-types';
|
|
import { computeCompositeColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util';
|
|
import { isCompositeFieldMetadataType } from 'src/engine/metadata-modules/field-metadata/utils/is-composite-field-metadata-type.util';
|
|
import { type FieldMetadataMap } from 'src/engine/metadata-modules/types/field-metadata-map';
|
|
import {
|
|
InvalidMetadataException,
|
|
InvalidMetadataExceptionCode,
|
|
} from 'src/engine/metadata-modules/utils/exceptions/invalid-metadata.exception';
|
|
|
|
const getReservedCompositeFieldNames = (
|
|
fieldMetadataMapById: FieldMetadataMap,
|
|
) => {
|
|
const reservedCompositeFieldsNames: string[] = [];
|
|
|
|
for (const field of Object.values(fieldMetadataMapById)) {
|
|
if (isCompositeFieldMetadataType(field.type)) {
|
|
const base = field.name;
|
|
const compositeType = compositeTypeDefinitions.get(field.type);
|
|
|
|
compositeType?.properties.map((property) =>
|
|
reservedCompositeFieldsNames.push(
|
|
computeCompositeColumnName(base, property),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
return reservedCompositeFieldsNames;
|
|
};
|
|
|
|
type ValidateFieldNameAvailabilityOrThrowArgs = {
|
|
name: string;
|
|
fieldMetadataMapById: FieldMetadataMap;
|
|
};
|
|
export const validateFieldNameAvailabilityOrThrow = ({
|
|
name,
|
|
fieldMetadataMapById,
|
|
}: ValidateFieldNameAvailabilityOrThrowArgs) => {
|
|
const reservedCompositeFieldsNames =
|
|
getReservedCompositeFieldNames(fieldMetadataMapById);
|
|
|
|
if (
|
|
Object.values(fieldMetadataMapById).some(
|
|
(field) =>
|
|
field.name === name ||
|
|
(field.type === FieldMetadataType.RELATION &&
|
|
`${field.name}Id` === name),
|
|
)
|
|
) {
|
|
throw new InvalidMetadataException(
|
|
`Name "${name}" is not available as it is already used by another field`,
|
|
InvalidMetadataExceptionCode.NOT_AVAILABLE,
|
|
{
|
|
userFriendlyMessage: t`This name is not available as it is already used by another field.`,
|
|
},
|
|
);
|
|
}
|
|
|
|
if (reservedCompositeFieldsNames.includes(name)) {
|
|
throw new InvalidMetadataException(
|
|
`Name "${name}" is not available`,
|
|
InvalidMetadataExceptionCode.RESERVED_KEYWORD,
|
|
{
|
|
userFriendlyMessage: t`This name is not available.`,
|
|
},
|
|
);
|
|
}
|
|
};
|