[flexible-backend] handle object already exists error (#5225)

## Context
Currently we have an unicity constraint in the DB but we don't return a
clear error to the frontend before reaching the DB (which then throws a
500). This PR adds a validation check similar to what we have with field
creation
This commit is contained in:
Weiko 2024-04-30 16:36:02 +02:00 committed by GitHub
parent c193663a71
commit bc0d30f28b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,6 @@
import {
BadRequestException,
ConflictException,
Injectable,
NotFoundException,
} from '@nestjs/common';
@ -56,7 +57,6 @@ import {
import { createWorkspaceMigrationsForCustomObject } from 'src/engine/metadata-modules/object-metadata/utils/create-workspace-migrations-for-custom-object.util';
import { createWorkspaceMigrationsForRemoteObject } from 'src/engine/metadata-modules/object-metadata/utils/create-workspace-migrations-for-remote-object.util';
import { computeColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-column-name.util';
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity';
import { validateObjectMetadataInput } from 'src/engine/metadata-modules/object-metadata/utils/validate-object-metadata-input.util';
import { mapUdtNameToFieldType } from 'src/engine/metadata-modules/remote-server/remote-table/utils/udt-name-mapper.util';
@ -81,8 +81,6 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
private readonly typeORMService: TypeORMService,
private readonly workspaceMigrationService: WorkspaceMigrationService,
private readonly workspaceMigrationRunnerService: WorkspaceMigrationRunnerService,
@InjectRepository(FeatureFlagEntity, 'core')
private readonly featureFlagRepository: Repository<FeatureFlagEntity>,
) {
super(objectMetadataRepository);
}
@ -249,6 +247,23 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
);
}
const objectAlreadyExists = await this.objectMetadataRepository.findOne({
where: [
{
nameSingular: objectMetadataInput.nameSingular,
workspaceId: objectMetadataInput.workspaceId,
},
{
namePlural: objectMetadataInput.namePlural,
workspaceId: objectMetadataInput.workspaceId,
},
],
});
if (objectAlreadyExists) {
throw new ConflictException('Object already exists');
}
const isCustom = !objectMetadataInput.isRemote;
const createdObjectMetadata = await super.createOne({