mirror of
https://github.com/lingble/twenty.git
synced 2025-10-30 12:22:29 +00:00
Fix support custom object renaming (#8089)
In this PR 1) (FE) fixing update of name not taken into account if toggle value is changed in the same transaction 2) (BE) moving same nameSingular and namePlural validation before effective update
This commit is contained in:
@@ -102,6 +102,8 @@ const StyledLabel = styled.span`
|
||||
|
||||
const infoCircleElementId = 'info-circle-id';
|
||||
|
||||
export const IS_LABEL_SYNCED_WITH_NAME_LABEL = 'isLabelSyncedWithName';
|
||||
|
||||
export const SettingsDataModelObjectAboutForm = ({
|
||||
disabled,
|
||||
disableNameEdit,
|
||||
@@ -115,7 +117,7 @@ export const SettingsDataModelObjectAboutForm = ({
|
||||
isAdvancedModeEnabled,
|
||||
);
|
||||
|
||||
const isLabelSyncedWithName = watch('isLabelSyncedWithName');
|
||||
const isLabelSyncedWithName = watch(IS_LABEL_SYNCED_WITH_NAME_LABEL);
|
||||
const labelSingular = watch('labelSingular');
|
||||
const labelPlural = watch('labelPlural');
|
||||
const apiNameTooltipText = isLabelSyncedWithName
|
||||
@@ -318,7 +320,7 @@ export const SettingsDataModelObjectAboutForm = ({
|
||||
),
|
||||
)}
|
||||
<Controller
|
||||
name="isLabelSyncedWithName"
|
||||
name={IS_LABEL_SYNCED_WITH_NAME_LABEL}
|
||||
control={control}
|
||||
defaultValue={
|
||||
objectMetadataItem?.isLabelSyncedWithName ?? true
|
||||
|
||||
@@ -16,6 +16,7 @@ import { RecordFieldValueSelectorContextProvider } from '@/object-record/record-
|
||||
import { SaveAndCancelButtons } from '@/settings/components/SaveAndCancelButtons/SaveAndCancelButtons';
|
||||
import { SettingsPageContainer } from '@/settings/components/SettingsPageContainer';
|
||||
import {
|
||||
IS_LABEL_SYNCED_WITH_NAME_LABEL,
|
||||
SettingsDataModelObjectAboutForm,
|
||||
settingsDataModelObjectAboutFormSchema,
|
||||
} from '@/settings/data-model/objects/forms/components/SettingsDataModelObjectAboutForm';
|
||||
@@ -81,10 +82,16 @@ export const SettingsObjectEdit = () => {
|
||||
formValues: SettingsDataModelObjectEditFormValues,
|
||||
) => {
|
||||
let values = formValues;
|
||||
if (
|
||||
formValues.isLabelSyncedWithName === true ||
|
||||
activeObjectMetadataItem.isLabelSyncedWithName === true
|
||||
) {
|
||||
const dirtyFieldKeys = Object.keys(
|
||||
formConfig.formState.dirtyFields,
|
||||
) as (keyof SettingsDataModelObjectEditFormValues)[];
|
||||
const shouldComputeNamesFromLabels: boolean = dirtyFieldKeys.includes(
|
||||
IS_LABEL_SYNCED_WITH_NAME_LABEL,
|
||||
)
|
||||
? (formValues.isLabelSyncedWithName as boolean)
|
||||
: activeObjectMetadataItem.isLabelSyncedWithName;
|
||||
|
||||
if (shouldComputeNamesFromLabels) {
|
||||
values = {
|
||||
...values,
|
||||
...(values.labelSingular
|
||||
@@ -104,10 +111,6 @@ export const SettingsObjectEdit = () => {
|
||||
};
|
||||
}
|
||||
|
||||
const dirtyFieldKeys = Object.keys(
|
||||
formConfig.formState.dirtyFields,
|
||||
) as (keyof SettingsDataModelObjectEditFormValues)[];
|
||||
|
||||
return settingsUpdateObjectInputSchema.parse(
|
||||
pick(values, [
|
||||
...dirtyFieldKeys,
|
||||
|
||||
@@ -447,6 +447,15 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
isDefined(input.update.nameSingular) ||
|
||||
isDefined(input.update.namePlural)
|
||||
) {
|
||||
this.validateNameSingularAndNamePluralAreDifferentOrThrow(
|
||||
fullObjectMetadataAfterUpdate,
|
||||
);
|
||||
}
|
||||
|
||||
const updatedObject = await super.updateOne(input.id, input.update);
|
||||
|
||||
await this.handleObjectNameAndLabelUpdates(
|
||||
@@ -1416,21 +1425,6 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
objectMetadataForUpdate: ObjectMetadataEntity,
|
||||
input: UpdateOneObjectInput,
|
||||
) {
|
||||
if (
|
||||
isDefined(input.update.nameSingular) ||
|
||||
isDefined(input.update.namePlural)
|
||||
) {
|
||||
if (
|
||||
objectMetadataForUpdate.nameSingular ===
|
||||
objectMetadataForUpdate.namePlural
|
||||
) {
|
||||
throw new ObjectMetadataException(
|
||||
'The singular and plural name cannot be the same for an object',
|
||||
ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const newTargetTableName = computeObjectTargetTable(
|
||||
objectMetadataForUpdate,
|
||||
);
|
||||
@@ -1641,4 +1635,18 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
private validateNameSingularAndNamePluralAreDifferentOrThrow = (
|
||||
fullObjectMetadataAfterUpdate: ObjectMetadataEntity,
|
||||
) => {
|
||||
if (
|
||||
fullObjectMetadataAfterUpdate.nameSingular ===
|
||||
fullObjectMetadataAfterUpdate.namePlural
|
||||
) {
|
||||
throw new ObjectMetadataException(
|
||||
'The singular and plural name cannot be the same for an object',
|
||||
ObjectMetadataExceptionCode.INVALID_OBJECT_INPUT,
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user