[feat] Enable deletion of custom fields in workspace (#4780)
**Context** cf. feature request [#4597](https://github.com/twentyhq/twenty/issues/4597) Enables deletion of custom fields that aren't active nor of type relation Also 1. renamed a misnamed file 2. deleted redundant hook BeforeDeleteOneField as it seemed best to move the logic to the resolver instead **How was it tested?** Did not write unit tests as code is to be migrated (discussed with @Weiko). Locally tested. --------- Co-authored-by: Marie Stoppa <mariestoppa@MacBook-Pro-de-Marie.local>
This commit is contained in:
@ -1,4 +1,8 @@
|
||||
import { UseGuards } from '@nestjs/common';
|
||||
import {
|
||||
BadRequestException,
|
||||
UnauthorizedException,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import {
|
||||
Args,
|
||||
Mutation,
|
||||
@ -11,9 +15,11 @@ import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator';
|
||||
import { JwtAuthGuard } from 'src/engine/guards/jwt.auth.guard';
|
||||
import { CreateOneFieldMetadataInput } from 'src/engine/metadata-modules/field-metadata/dtos/create-field.input';
|
||||
import { DeleteOneFieldInput } from 'src/engine/metadata-modules/field-metadata/dtos/delete-field.input';
|
||||
import { FieldMetadataDTO } from 'src/engine/metadata-modules/field-metadata/dtos/field-metadata.dto';
|
||||
import { RelationDefinitionDTO } from 'src/engine/metadata-modules/field-metadata/dtos/relation-definition.dto';
|
||||
import { UpdateOneFieldMetadataInput } from 'src/engine/metadata-modules/field-metadata/dtos/update-field.input';
|
||||
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata/field-metadata.service';
|
||||
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@ -43,6 +49,43 @@ export class FieldMetadataResolver {
|
||||
});
|
||||
}
|
||||
|
||||
@Mutation(() => FieldMetadataDTO)
|
||||
async deleteOneField(
|
||||
@Args('input') input: DeleteOneFieldInput,
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
) {
|
||||
if (!workspaceId) {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
|
||||
const fieldMetadata =
|
||||
await this.fieldMetadataService.findOneWithinWorkspace(workspaceId, {
|
||||
where: {
|
||||
id: input.id.toString(),
|
||||
},
|
||||
});
|
||||
|
||||
if (!fieldMetadata) {
|
||||
throw new BadRequestException('Field does not exist');
|
||||
}
|
||||
|
||||
if (!fieldMetadata.isCustom) {
|
||||
throw new BadRequestException("Standard Fields can't be deleted");
|
||||
}
|
||||
|
||||
if (fieldMetadata.isActive) {
|
||||
throw new BadRequestException("Active fields can't be deleted");
|
||||
}
|
||||
|
||||
if (fieldMetadata.type === FieldMetadataType.RELATION) {
|
||||
throw new BadRequestException(
|
||||
"Relation fields can't be deleted, you need to delete the RelationMetadata instead",
|
||||
);
|
||||
}
|
||||
|
||||
return this.fieldMetadataService.deleteOneField(input, workspaceId);
|
||||
}
|
||||
|
||||
@ResolveField(() => RelationDefinitionDTO, { nullable: true })
|
||||
async relationDefinition(
|
||||
@Parent() fieldMetadata: FieldMetadataDTO,
|
||||
|
||||
Reference in New Issue
Block a user