Files
twenty/packages/twenty-server/src/engine-metadata/relation-metadata/hooks/before-delete-one-field.hook.ts
Jérémy M 94487f6737 feat: refactor folder structure (#4498)
* feat: wip refactor folder structure

* Fix

* fix position

---------

Co-authored-by: Charles Bochet <charles@twenty.com>
2024-03-15 14:40:58 +01:00

56 lines
1.4 KiB
TypeScript

import {
BadRequestException,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import {
BeforeDeleteOneHook,
DeleteOneInputType,
} from '@ptc-org/nestjs-query-graphql';
import { RelationMetadataService } from 'src/engine-metadata/relation-metadata/relation-metadata.service';
@Injectable()
export class BeforeDeleteOneRelation implements BeforeDeleteOneHook<any> {
constructor(readonly relationMetadataService: RelationMetadataService) {}
async run(
instance: DeleteOneInputType,
context: any,
): Promise<DeleteOneInputType> {
const workspaceId = context?.req?.user?.workspace?.id;
if (!workspaceId) {
throw new UnauthorizedException();
}
const relationMetadata =
await this.relationMetadataService.findOneWithinWorkspace(workspaceId, {
where: {
id: instance.id.toString(),
},
});
if (!relationMetadata) {
throw new BadRequestException('Relation does not exist');
}
if (
!relationMetadata.toFieldMetadata.isCustom ||
!relationMetadata.fromFieldMetadata.isCustom
) {
throw new BadRequestException("Standard Relations can't be deleted");
}
if (
relationMetadata.toFieldMetadata.isActive ||
relationMetadata.fromFieldMetadata.isActive
) {
throw new BadRequestException("Active relations can't be deleted");
}
return instance;
}
}