Files
twenty_crm/server/src/metadata/object-metadata/hooks/before-delete-one-object.hook.ts
Jérémy M 474db1e142 fix: nested relations not working and relations not prefixed (#2782)
* fix: nested relations n+n

* fix: prefix custom relations

* fix: only apply targetColumnMap when it's a custom object

* fix: force workspaceId to be provided

* fix: toIsCustom -> isToCustom

* fix: remove console.log
2023-12-01 15:26:48 +01:00

50 lines
1.2 KiB
TypeScript

import {
BadRequestException,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import {
BeforeDeleteOneHook,
DeleteOneInputType,
} from '@ptc-org/nestjs-query-graphql';
import { ObjectMetadataService } from 'src/metadata/object-metadata/object-metadata.service';
@Injectable()
export class BeforeDeleteOneObject implements BeforeDeleteOneHook<any> {
constructor(readonly objectMetadataService: ObjectMetadataService) {}
async run(
instance: DeleteOneInputType,
context: any,
): Promise<DeleteOneInputType> {
const workspaceId = context?.req?.user?.workspace?.id;
if (!workspaceId) {
throw new UnauthorizedException();
}
const objectMetadata =
await this.objectMetadataService.findOneWithinWorkspace(workspaceId, {
where: {
id: instance.id.toString(),
},
});
if (!objectMetadata) {
throw new BadRequestException('Object does not exist');
}
if (!objectMetadata.isCustom) {
throw new BadRequestException("Standard Objects can't be deleted");
}
if (objectMetadata.isActive) {
throw new BadRequestException("Active objects can't be deleted");
}
return instance;
}
}