better handling the function argument without touching it (#11614)
leave intact the `input` argument to avoid side effects on the parent caller --------- Co-authored-by: Charles Bochet <charlesBochet@users.noreply.github.com>
This commit is contained in:
@ -16,7 +16,10 @@ import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/
|
||||
import { IndexMetadataService } from 'src/engine/metadata-modules/index-metadata/index-metadata.service';
|
||||
import { DeleteOneObjectInput } from 'src/engine/metadata-modules/object-metadata/dtos/delete-object.input';
|
||||
import { ObjectMetadataDTO } from 'src/engine/metadata-modules/object-metadata/dtos/object-metadata.dto';
|
||||
import { UpdateOneObjectInput } from 'src/engine/metadata-modules/object-metadata/dtos/update-object.input';
|
||||
import {
|
||||
UpdateObjectPayload,
|
||||
UpdateOneObjectInput,
|
||||
} from 'src/engine/metadata-modules/object-metadata/dtos/update-object.input';
|
||||
import {
|
||||
ObjectMetadataException,
|
||||
ObjectMetadataExceptionCode,
|
||||
@ -219,22 +222,22 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
input: UpdateOneObjectInput,
|
||||
workspaceId: string,
|
||||
): Promise<ObjectMetadataEntity> {
|
||||
const { update } = input;
|
||||
const inputId = input.id;
|
||||
|
||||
if (isDefined(update.labelSingular)) {
|
||||
update.labelSingular = capitalize(update.labelSingular);
|
||||
}
|
||||
const inputPayload = {
|
||||
...input.update,
|
||||
...(isDefined(input.update.labelSingular)
|
||||
? { labelSingular: capitalize(input.update.labelSingular) }
|
||||
: {}),
|
||||
...(isDefined(input.update.labelPlural)
|
||||
? { labelPlural: capitalize(input.update.labelPlural) }
|
||||
: {}),
|
||||
};
|
||||
|
||||
if (isDefined(update.labelPlural)) {
|
||||
update.labelPlural = capitalize(update.labelPlural);
|
||||
}
|
||||
|
||||
input.update = update;
|
||||
|
||||
validateObjectMetadataInputNamesOrThrow(input.update);
|
||||
validateObjectMetadataInputNamesOrThrow(inputPayload);
|
||||
|
||||
const existingObjectMetadata = await this.objectMetadataRepository.findOne({
|
||||
where: { id: input.id, workspaceId: workspaceId },
|
||||
where: { id: inputId, workspaceId: workspaceId },
|
||||
});
|
||||
|
||||
if (!existingObjectMetadata) {
|
||||
@ -246,7 +249,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
|
||||
const existingObjectMetadataCombinedWithUpdateInput = {
|
||||
...existingObjectMetadata,
|
||||
...input.update,
|
||||
...inputPayload,
|
||||
};
|
||||
|
||||
await this.validatesNoOtherObjectWithSameNameExistsOrThrows({
|
||||
@ -271,8 +274,8 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
}
|
||||
|
||||
if (
|
||||
isDefined(input.update.nameSingular) ||
|
||||
isDefined(input.update.namePlural)
|
||||
isDefined(inputPayload.nameSingular) ||
|
||||
isDefined(inputPayload.namePlural)
|
||||
) {
|
||||
validateLowerCasedAndTrimmedStringsAreDifferentOrThrow({
|
||||
inputs: [
|
||||
@ -284,35 +287,35 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
});
|
||||
}
|
||||
|
||||
const updatedObject = await super.updateOne(input.id, input.update);
|
||||
const updatedObject = await super.updateOne(inputId, inputPayload);
|
||||
|
||||
await this.handleObjectNameAndLabelUpdates(
|
||||
existingObjectMetadata,
|
||||
existingObjectMetadataCombinedWithUpdateInput,
|
||||
input,
|
||||
inputPayload,
|
||||
);
|
||||
|
||||
if (input.update.isActive !== undefined) {
|
||||
if (inputPayload.isActive !== undefined) {
|
||||
await this.objectMetadataRelationService.updateObjectRelationshipsActivationStatus(
|
||||
input.id,
|
||||
input.update.isActive,
|
||||
inputId,
|
||||
inputPayload.isActive,
|
||||
);
|
||||
}
|
||||
|
||||
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
|
||||
workspaceId,
|
||||
);
|
||||
if (input.update.labelIdentifierFieldMetadataId) {
|
||||
if (inputPayload.labelIdentifierFieldMetadataId) {
|
||||
const labelIdentifierFieldMetadata =
|
||||
await this.fieldMetadataRepository.findOneByOrFail({
|
||||
id: input.update.labelIdentifierFieldMetadataId,
|
||||
objectMetadataId: input.id,
|
||||
id: inputPayload.labelIdentifierFieldMetadataId,
|
||||
objectMetadataId: inputId,
|
||||
workspaceId: workspaceId,
|
||||
});
|
||||
|
||||
if (isSearchableFieldType(labelIdentifierFieldMetadata.type)) {
|
||||
await this.searchVectorService.updateSearchVector(
|
||||
input.id,
|
||||
inputId,
|
||||
[
|
||||
{
|
||||
name: labelIdentifierFieldMetadata.name,
|
||||
@ -473,7 +476,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
private async handleObjectNameAndLabelUpdates(
|
||||
existingObjectMetadata: ObjectMetadataEntity,
|
||||
objectMetadataForUpdate: ObjectMetadataEntity,
|
||||
input: UpdateOneObjectInput,
|
||||
inputPayload: UpdateObjectPayload,
|
||||
) {
|
||||
const newTargetTableName = computeObjectTargetTable(
|
||||
objectMetadataForUpdate,
|
||||
@ -520,9 +523,9 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
);
|
||||
|
||||
if (
|
||||
(input.update.labelPlural || input.update.icon) &&
|
||||
(input.update.labelPlural !== existingObjectMetadata.labelPlural ||
|
||||
input.update.icon !== existingObjectMetadata.icon)
|
||||
(inputPayload.labelPlural || inputPayload.icon) &&
|
||||
(inputPayload.labelPlural !== existingObjectMetadata.labelPlural ||
|
||||
inputPayload.icon !== existingObjectMetadata.icon)
|
||||
) {
|
||||
await this.objectMetadataRelatedRecordsService.updateObjectViews(
|
||||
objectMetadataForUpdate,
|
||||
|
||||
Reference in New Issue
Block a user