Deprecate old relations completely (#12482)
# What Fully deprecate old relations because we have one bug tied to it and it make the codebase complex # How I've made this PR: 1. remove metadata datasource (we only keep 'core') => this was causing extra complexity in the refactor + flaky reset 2. merge dev and demo datasets => as I needed to update the tests which is very painful, I don't want to do it twice 3. remove all code tied to RELATION_METADATA / relation-metadata.resolver, or anything tied to the old relation system 4. Remove ONE_TO_ONE and MANY_TO_MANY that are not supported 5. fix impacts on the different areas : see functional testing below # Functional testing ## Functional testing from the front-end: 1. Database Reset ✅ 2. Sign In ✅ 3. Workspace sign-up ✅ 5. Browsing table / kanban / show ✅ 6. Assigning a record in a one to many / in a many to one ✅ 7. Deleting a record involved in a relation ✅ => broken but not tied to this PR 8. "Add new" from relation picker ✅ => broken but not tied to this PR 9. Creating a Task / Note, Updating a Task / Note relations, Deleting a Task / Note (from table, show page, right drawer) ✅ => broken but not tied to this PR 10. creating a relation from settings (custom / standard x oneToMany / manyToOne) ✅ 11. updating a relation from settings should not be possible ✅ 12. deleting a relation from settings (custom / standard x oneToMany / manyToOne) ✅ 13. Make sure timeline activity still work (relation were involved there), espacially with Task / Note => to be double checked ✅ => Cannot convert undefined or null to object 14. Workspace deletion / User deletion ✅ 15. CSV Import should keep working ✅ 16. Permissions: I have tested without permissions V2 as it's still hard to test v2 work and it's not in prod yet ✅ 17. Workflows global test ✅ ## From the API: 1. Review open-api documentation (REST) ✅ 2. Make sure REST Api are still able to fetch relations ==> won't do, we have a coupling Get/Update/Create there, this requires refactoring 3. Make sure REST Api is still able to update / remove relation => won't do same ## Automated tests 1. lint + typescript ✅ 2. front unit tests: ✅ 3. server unit tests 2 ✅ 4. front stories: ✅ 5. server integration: ✅ 6. chromatic check : expected 0 7. e2e check : expected no more that current failures ## Remove // Todos 1. All are captured by functional tests above, nothing additional to do ## (Un)related regressions 1. Table loading state is not working anymore, we see the empty state before table content 2. Filtering by Creator Tim Ap return empty results 3. Not possible to add Tasks / Notes / Files from show page # Result ## New seeds that can be easily extended <img width="1920" alt="image" src="https://github.com/user-attachments/assets/d290d130-2a5f-44e6-b419-7e42a89eec4b" /> ## -5k lines of code ## No more 'metadata' dataSource (we only have 'core) ## No more relationMetadata (I haven't drop the table yet it's not referenced in the code anymore) ## We are ready to fix the 6 months lag between current API results and our mocked tests ## No more bug on relation creation / deletion --------- Co-authored-by: Weiko <corentin@twenty.com> Co-authored-by: Félix Malfait <felix@twenty.com>
This commit is contained in:
@ -1,80 +0,0 @@
|
||||
import { Field, HideField, InputType } from '@nestjs/graphql';
|
||||
|
||||
import { BeforeCreateOne } from '@ptc-org/nestjs-query-graphql';
|
||||
import {
|
||||
IsEnum,
|
||||
IsNotEmpty,
|
||||
IsOptional,
|
||||
IsString,
|
||||
IsUUID,
|
||||
} from 'class-validator';
|
||||
|
||||
import { BeforeCreateOneRelation } from 'src/engine/metadata-modules/relation-metadata/hooks/before-create-one-relation.hook';
|
||||
import { RelationMetadataType } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
|
||||
|
||||
@InputType()
|
||||
@BeforeCreateOne(BeforeCreateOneRelation)
|
||||
export class CreateRelationInput {
|
||||
@IsEnum(RelationMetadataType)
|
||||
@IsNotEmpty()
|
||||
@Field(() => RelationMetadataType)
|
||||
relationType: RelationMetadataType;
|
||||
|
||||
@IsUUID()
|
||||
@IsNotEmpty()
|
||||
@Field()
|
||||
fromObjectMetadataId: string;
|
||||
|
||||
@IsUUID()
|
||||
@IsNotEmpty()
|
||||
@Field()
|
||||
toObjectMetadataId: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Field()
|
||||
fromName: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Field()
|
||||
toName: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Field()
|
||||
fromLabel: string;
|
||||
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
@Field()
|
||||
toLabel: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
fromIcon?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
toIcon?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true, deprecationReason: 'Use fromDescription instead' })
|
||||
description?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
fromDescription?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@Field({ nullable: true })
|
||||
toDescription?: string;
|
||||
|
||||
@HideField()
|
||||
workspaceId: string;
|
||||
}
|
||||
@ -1,13 +0,0 @@
|
||||
import { InputType } from '@nestjs/graphql';
|
||||
|
||||
import { IDField } from '@ptc-org/nestjs-query-graphql';
|
||||
|
||||
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
|
||||
|
||||
@InputType()
|
||||
export class DeleteOneRelationInput {
|
||||
@IDField(() => UUIDScalarType, {
|
||||
description: 'The id of the relation to delete.',
|
||||
})
|
||||
id!: string;
|
||||
}
|
||||
@ -1,72 +0,0 @@
|
||||
import {
|
||||
Field,
|
||||
HideField,
|
||||
ObjectType,
|
||||
registerEnumType,
|
||||
} from '@nestjs/graphql';
|
||||
|
||||
import {
|
||||
Authorize,
|
||||
BeforeDeleteOne,
|
||||
IDField,
|
||||
QueryOptions,
|
||||
Relation,
|
||||
} from '@ptc-org/nestjs-query-graphql';
|
||||
import { CreateDateColumn, UpdateDateColumn } from 'typeorm';
|
||||
|
||||
import { UUIDScalarType } from 'src/engine/api/graphql/workspace-schema-builder/graphql-types/scalars';
|
||||
import { ObjectMetadataDTO } from 'src/engine/metadata-modules/object-metadata/dtos/object-metadata.dto';
|
||||
import { BeforeDeleteOneRelation } from 'src/engine/metadata-modules/relation-metadata/hooks/before-delete-one-relation.hook';
|
||||
import { RelationMetadataType } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
|
||||
|
||||
registerEnumType(RelationMetadataType, {
|
||||
name: 'RelationMetadataType',
|
||||
description: 'Type of the relation',
|
||||
});
|
||||
|
||||
@ObjectType('RelationMetadata')
|
||||
@Authorize({
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
authorize: (context: any) => ({
|
||||
workspaceId: { eq: context?.req?.workspace?.id },
|
||||
}),
|
||||
})
|
||||
@QueryOptions({
|
||||
defaultResultSize: 10,
|
||||
disableFilter: true,
|
||||
disableSort: true,
|
||||
maxResultsSize: 1000,
|
||||
})
|
||||
@BeforeDeleteOne(BeforeDeleteOneRelation)
|
||||
@Relation('fromObjectMetadata', () => ObjectMetadataDTO)
|
||||
@Relation('toObjectMetadata', () => ObjectMetadataDTO)
|
||||
export class RelationMetadataDTO {
|
||||
@IDField(() => UUIDScalarType)
|
||||
id: string;
|
||||
|
||||
@Field(() => RelationMetadataType)
|
||||
relationType: RelationMetadataType;
|
||||
|
||||
@Field()
|
||||
fromObjectMetadataId: string;
|
||||
|
||||
@Field()
|
||||
toObjectMetadataId: string;
|
||||
|
||||
@Field()
|
||||
fromFieldMetadataId: string;
|
||||
|
||||
@Field()
|
||||
toFieldMetadataId: string;
|
||||
|
||||
@HideField()
|
||||
workspaceId: string;
|
||||
|
||||
@Field()
|
||||
@CreateDateColumn()
|
||||
createdAt: Date;
|
||||
|
||||
@Field()
|
||||
@UpdateDateColumn()
|
||||
updatedAt: Date;
|
||||
}
|
||||
@ -1,29 +0,0 @@
|
||||
import { Injectable, UnauthorizedException } from '@nestjs/common';
|
||||
|
||||
import {
|
||||
BeforeCreateOneHook,
|
||||
CreateOneInputType,
|
||||
} from '@ptc-org/nestjs-query-graphql';
|
||||
|
||||
import { CreateRelationInput } from 'src/engine/metadata-modules/relation-metadata/dtos/create-relation.input';
|
||||
|
||||
@Injectable()
|
||||
export class BeforeCreateOneRelation<T extends CreateRelationInput>
|
||||
implements BeforeCreateOneHook<T>
|
||||
{
|
||||
async run(
|
||||
instance: CreateOneInputType<T>,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
context: any,
|
||||
): Promise<CreateOneInputType<T>> {
|
||||
const workspaceId = context?.req?.workspace?.id;
|
||||
|
||||
if (!workspaceId) {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
|
||||
instance.input.workspaceId = workspaceId;
|
||||
|
||||
return instance;
|
||||
}
|
||||
}
|
||||
@ -1,62 +0,0 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Injectable,
|
||||
UnauthorizedException,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import {
|
||||
BeforeDeleteOneHook,
|
||||
DeleteOneInputType,
|
||||
} from '@ptc-org/nestjs-query-graphql';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import { RelationMetadataEntity } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
|
||||
|
||||
@Injectable()
|
||||
export class BeforeDeleteOneRelation implements BeforeDeleteOneHook {
|
||||
constructor(
|
||||
@InjectRepository(RelationMetadataEntity, 'metadata')
|
||||
private readonly relationMetadataRepository: Repository<RelationMetadataEntity>,
|
||||
) {}
|
||||
|
||||
async run(
|
||||
instance: DeleteOneInputType,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
context: any,
|
||||
): Promise<DeleteOneInputType> {
|
||||
const workspaceId = context?.req?.workspace?.id;
|
||||
|
||||
if (!workspaceId) {
|
||||
throw new UnauthorizedException();
|
||||
}
|
||||
|
||||
const relationMetadata = await this.relationMetadataRepository.findOne({
|
||||
where: {
|
||||
workspaceId,
|
||||
id: instance.id.toString(),
|
||||
},
|
||||
relations: ['fromFieldMetadata', 'toFieldMetadata'],
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -1,18 +0,0 @@
|
||||
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
|
||||
|
||||
import { Observable, catchError } from 'rxjs';
|
||||
|
||||
import { relationMetadataGraphqlApiExceptionHandler } from 'src/engine/metadata-modules/relation-metadata/utils/relation-metadata-graphql-api-exception-handler.util';
|
||||
|
||||
export class RelationMetadataGraphqlApiExceptionInterceptor
|
||||
implements NestInterceptor
|
||||
{
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
intercept(_: ExecutionContext, next: CallHandler): Observable<any> {
|
||||
return next
|
||||
.handle()
|
||||
.pipe(
|
||||
catchError((err) => relationMetadataGraphqlApiExceptionHandler(err)),
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,100 +0,0 @@
|
||||
import {
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
Entity,
|
||||
JoinColumn,
|
||||
ManyToOne,
|
||||
OneToOne,
|
||||
PrimaryGeneratedColumn,
|
||||
Relation,
|
||||
UpdateDateColumn,
|
||||
} from 'typeorm';
|
||||
|
||||
import { RelationMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-metadata.interface';
|
||||
|
||||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
|
||||
export enum RelationMetadataType {
|
||||
ONE_TO_ONE = 'ONE_TO_ONE',
|
||||
ONE_TO_MANY = 'ONE_TO_MANY',
|
||||
MANY_TO_ONE = 'MANY_TO_ONE',
|
||||
MANY_TO_MANY = 'MANY_TO_MANY',
|
||||
}
|
||||
|
||||
export enum RelationOnDeleteAction {
|
||||
CASCADE = 'CASCADE',
|
||||
RESTRICT = 'RESTRICT',
|
||||
SET_NULL = 'SET_NULL',
|
||||
NO_ACTION = 'NO_ACTION',
|
||||
}
|
||||
|
||||
@Entity('relationMetadata')
|
||||
export class RelationMetadataEntity implements RelationMetadataInterface {
|
||||
@PrimaryGeneratedColumn('uuid')
|
||||
id: string;
|
||||
|
||||
@Column({ nullable: false })
|
||||
relationType: RelationMetadataType;
|
||||
|
||||
@Column({
|
||||
nullable: false,
|
||||
default: RelationOnDeleteAction.SET_NULL,
|
||||
type: 'enum',
|
||||
enum: RelationOnDeleteAction,
|
||||
})
|
||||
onDeleteAction: RelationOnDeleteAction;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
fromObjectMetadataId: string;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
toObjectMetadataId: string;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
fromFieldMetadataId: string;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
toFieldMetadataId: string;
|
||||
|
||||
@Column({ nullable: false, type: 'uuid' })
|
||||
workspaceId: string;
|
||||
|
||||
@ManyToOne(
|
||||
() => ObjectMetadataEntity,
|
||||
(object: ObjectMetadataEntity) => object.fromRelations,
|
||||
{
|
||||
onDelete: 'CASCADE',
|
||||
},
|
||||
)
|
||||
fromObjectMetadata: Relation<ObjectMetadataEntity>;
|
||||
|
||||
@ManyToOne(
|
||||
() => ObjectMetadataEntity,
|
||||
(object: ObjectMetadataEntity) => object.toRelations,
|
||||
{
|
||||
onDelete: 'CASCADE',
|
||||
},
|
||||
)
|
||||
toObjectMetadata: Relation<ObjectMetadataEntity>;
|
||||
|
||||
@OneToOne(
|
||||
() => FieldMetadataEntity,
|
||||
(field: FieldMetadataEntity) => field.fromRelationMetadata,
|
||||
)
|
||||
@JoinColumn()
|
||||
fromFieldMetadata: Relation<FieldMetadataEntity>;
|
||||
|
||||
@OneToOne(
|
||||
() => FieldMetadataEntity,
|
||||
(field: FieldMetadataEntity) => field.toRelationMetadata,
|
||||
)
|
||||
@JoinColumn()
|
||||
toFieldMetadata: Relation<FieldMetadataEntity>;
|
||||
|
||||
@CreateDateColumn({ type: 'timestamptz' })
|
||||
createdAt: Date;
|
||||
|
||||
@UpdateDateColumn({ type: 'timestamptz' })
|
||||
updatedAt: Date;
|
||||
}
|
||||
@ -1,15 +0,0 @@
|
||||
import { CustomException } from 'src/utils/custom-exception';
|
||||
|
||||
export class RelationMetadataException extends CustomException {
|
||||
declare code: RelationMetadataExceptionCode;
|
||||
constructor(message: string, code: RelationMetadataExceptionCode) {
|
||||
super(message, code);
|
||||
}
|
||||
}
|
||||
|
||||
export enum RelationMetadataExceptionCode {
|
||||
RELATION_METADATA_NOT_FOUND = 'RELATION_METADATA_NOT_FOUND',
|
||||
INVALID_RELATION_INPUT = 'INVALID_RELATION_INPUT',
|
||||
RELATION_ALREADY_EXISTS = 'RELATION_ALREADY_EXISTS',
|
||||
FOREIGN_KEY_NOT_FOUND = 'FOREIGN_KEY_NOT_FOUND',
|
||||
}
|
||||
@ -1,76 +0,0 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import {
|
||||
NestjsQueryGraphQLModule,
|
||||
PagingStrategies,
|
||||
} from '@ptc-org/nestjs-query-graphql';
|
||||
import { NestjsQueryTypeOrmModule } from '@ptc-org/nestjs-query-typeorm';
|
||||
|
||||
import { FeatureFlagModule } from 'src/engine/core-modules/feature-flag/feature-flag.module';
|
||||
import { SettingsPermissionsGuard } from 'src/engine/guards/settings-permissions.guard';
|
||||
import { WorkspaceAuthGuard } from 'src/engine/guards/workspace-auth.guard';
|
||||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { FieldMetadataModule } from 'src/engine/metadata-modules/field-metadata/field-metadata.module';
|
||||
import { IndexMetadataModule } from 'src/engine/metadata-modules/index-metadata/index-metadata.module';
|
||||
import { ObjectMetadataModule } from 'src/engine/metadata-modules/object-metadata/object-metadata.module';
|
||||
import { SettingPermissionType } from 'src/engine/metadata-modules/permissions/constants/setting-permission-type.constants';
|
||||
import { PermissionsModule } from 'src/engine/metadata-modules/permissions/permissions.module';
|
||||
import { PermissionsGraphqlApiExceptionFilter } from 'src/engine/metadata-modules/permissions/utils/permissions-graphql-api-exception.filter';
|
||||
import { RelationMetadataGraphqlApiExceptionInterceptor } from 'src/engine/metadata-modules/relation-metadata/interceptors/relation-metadata-graphql-api-exception.interceptor';
|
||||
import { RelationMetadataResolver } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.resolver';
|
||||
import { WorkspaceMetadataVersionModule } from 'src/engine/metadata-modules/workspace-metadata-version/workspace-metadata-version.module';
|
||||
import { WorkspaceMigrationModule } from 'src/engine/metadata-modules/workspace-migration/workspace-migration.module';
|
||||
import { WorkspaceCacheStorageModule } from 'src/engine/workspace-cache-storage/workspace-cache-storage.module';
|
||||
import { WorkspaceMigrationRunnerModule } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.module';
|
||||
|
||||
import { RelationMetadataEntity } from './relation-metadata.entity';
|
||||
import { RelationMetadataService } from './relation-metadata.service';
|
||||
|
||||
import { CreateRelationInput } from './dtos/create-relation.input';
|
||||
import { RelationMetadataDTO } from './dtos/relation-metadata.dto';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
NestjsQueryGraphQLModule.forFeature({
|
||||
imports: [
|
||||
NestjsQueryTypeOrmModule.forFeature(
|
||||
[RelationMetadataEntity, FieldMetadataEntity],
|
||||
'metadata',
|
||||
),
|
||||
ObjectMetadataModule,
|
||||
FieldMetadataModule,
|
||||
IndexMetadataModule,
|
||||
WorkspaceMigrationRunnerModule,
|
||||
WorkspaceMigrationModule,
|
||||
WorkspaceCacheStorageModule,
|
||||
WorkspaceMetadataVersionModule,
|
||||
FeatureFlagModule,
|
||||
PermissionsModule,
|
||||
],
|
||||
services: [RelationMetadataService],
|
||||
resolvers: [
|
||||
{
|
||||
EntityClass: RelationMetadataEntity,
|
||||
DTOClass: RelationMetadataDTO,
|
||||
ServiceClass: RelationMetadataService,
|
||||
CreateDTOClass: CreateRelationInput,
|
||||
pagingStrategy: PagingStrategies.CURSOR,
|
||||
create: {
|
||||
many: { disabled: true },
|
||||
guards: [
|
||||
SettingsPermissionsGuard(SettingPermissionType.DATA_MODEL),
|
||||
],
|
||||
},
|
||||
update: { disabled: true },
|
||||
delete: { disabled: true },
|
||||
guards: [WorkspaceAuthGuard],
|
||||
interceptors: [RelationMetadataGraphqlApiExceptionInterceptor],
|
||||
filters: [PermissionsGraphqlApiExceptionFilter],
|
||||
},
|
||||
],
|
||||
}),
|
||||
],
|
||||
providers: [RelationMetadataService, RelationMetadataResolver],
|
||||
exports: [RelationMetadataService],
|
||||
})
|
||||
export class RelationMetadataModule {}
|
||||
@ -1,38 +0,0 @@
|
||||
import { UseFilters, UseGuards } from '@nestjs/common';
|
||||
import { Args, Mutation, Resolver } from '@nestjs/graphql';
|
||||
|
||||
import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
|
||||
import { AuthWorkspace } from 'src/engine/decorators/auth/auth-workspace.decorator';
|
||||
import { SettingsPermissionsGuard } from 'src/engine/guards/settings-permissions.guard';
|
||||
import { WorkspaceAuthGuard } from 'src/engine/guards/workspace-auth.guard';
|
||||
import { SettingPermissionType } from 'src/engine/metadata-modules/permissions/constants/setting-permission-type.constants';
|
||||
import { PermissionsGraphqlApiExceptionFilter } from 'src/engine/metadata-modules/permissions/utils/permissions-graphql-api-exception.filter';
|
||||
import { DeleteOneRelationInput } from 'src/engine/metadata-modules/relation-metadata/dtos/delete-relation.input';
|
||||
import { RelationMetadataDTO } from 'src/engine/metadata-modules/relation-metadata/dtos/relation-metadata.dto';
|
||||
import { RelationMetadataService } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.service';
|
||||
import { relationMetadataGraphqlApiExceptionHandler } from 'src/engine/metadata-modules/relation-metadata/utils/relation-metadata-graphql-api-exception-handler.util';
|
||||
|
||||
@UseGuards(WorkspaceAuthGuard)
|
||||
@Resolver()
|
||||
@UseFilters(PermissionsGraphqlApiExceptionFilter)
|
||||
export class RelationMetadataResolver {
|
||||
constructor(
|
||||
private readonly relationMetadataService: RelationMetadataService,
|
||||
) {}
|
||||
|
||||
@UseGuards(SettingsPermissionsGuard(SettingPermissionType.DATA_MODEL))
|
||||
@Mutation(() => RelationMetadataDTO)
|
||||
async deleteOneRelation(
|
||||
@Args('input') input: DeleteOneRelationInput,
|
||||
@AuthWorkspace() { id: workspaceId }: Workspace,
|
||||
) {
|
||||
try {
|
||||
return await this.relationMetadataService.deleteOneRelation(
|
||||
input.id,
|
||||
workspaceId,
|
||||
);
|
||||
} catch (error) {
|
||||
relationMetadataGraphqlApiExceptionHandler(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,586 +0,0 @@
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
|
||||
import camelCase from 'lodash.camelcase';
|
||||
import { FieldMetadataType } from 'twenty-shared/types';
|
||||
import { isDefined } from 'twenty-shared/utils';
|
||||
import { FindOneOptions, In, Repository } from 'typeorm';
|
||||
import { v4 as uuidV4 } from 'uuid';
|
||||
|
||||
import { FieldMetadataDefaultSettings } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata-settings.interface';
|
||||
import { FieldMetadataInterface } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata.interface';
|
||||
import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface';
|
||||
|
||||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata/field-metadata.service';
|
||||
import { IndexMetadataService } from 'src/engine/metadata-modules/index-metadata/index-metadata.service';
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
import { ObjectMetadataService } from 'src/engine/metadata-modules/object-metadata/object-metadata.service';
|
||||
import { CreateRelationInput } from 'src/engine/metadata-modules/relation-metadata/dtos/create-relation.input';
|
||||
import {
|
||||
RelationMetadataException,
|
||||
RelationMetadataExceptionCode,
|
||||
} from 'src/engine/metadata-modules/relation-metadata/relation-metadata.exception';
|
||||
import { InvalidMetadataException } from 'src/engine/metadata-modules/utils/exceptions/invalid-metadata.exception';
|
||||
import { validateFieldNameAvailabilityOrThrow } from 'src/engine/metadata-modules/utils/validate-field-name-availability.utils';
|
||||
import { validateMetadataNameOrThrow } from 'src/engine/metadata-modules/utils/validate-metadata-name.utils';
|
||||
import { WorkspaceMetadataVersionService } from 'src/engine/metadata-modules/workspace-metadata-version/services/workspace-metadata-version.service';
|
||||
import { generateMigrationName } from 'src/engine/metadata-modules/workspace-migration/utils/generate-migration-name.util';
|
||||
import {
|
||||
WorkspaceMigrationColumnActionType,
|
||||
WorkspaceMigrationColumnDrop,
|
||||
WorkspaceMigrationTableActionType,
|
||||
} from 'src/engine/metadata-modules/workspace-migration/workspace-migration.entity';
|
||||
import { WorkspaceMigrationService } from 'src/engine/metadata-modules/workspace-migration/workspace-migration.service';
|
||||
import { computeObjectTargetTable } from 'src/engine/utils/compute-object-target-table.util';
|
||||
import { WorkspaceCacheStorageService } from 'src/engine/workspace-cache-storage/workspace-cache-storage.service';
|
||||
import { WorkspaceMigrationRunnerService } from 'src/engine/workspace-manager/workspace-migration-runner/workspace-migration-runner.service';
|
||||
import { BASE_OBJECT_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
|
||||
|
||||
import {
|
||||
RelationMetadataEntity,
|
||||
RelationMetadataType,
|
||||
RelationOnDeleteAction,
|
||||
} from './relation-metadata.entity';
|
||||
|
||||
@Injectable()
|
||||
export class RelationMetadataService extends TypeOrmQueryService<RelationMetadataEntity> {
|
||||
constructor(
|
||||
@InjectRepository(RelationMetadataEntity, 'metadata')
|
||||
private readonly relationMetadataRepository: Repository<RelationMetadataEntity>,
|
||||
@InjectRepository(FieldMetadataEntity, 'metadata')
|
||||
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
|
||||
private readonly objectMetadataService: ObjectMetadataService,
|
||||
private readonly fieldMetadataService: FieldMetadataService,
|
||||
private readonly workspaceMigrationService: WorkspaceMigrationService,
|
||||
private readonly workspaceMigrationRunnerService: WorkspaceMigrationRunnerService,
|
||||
private readonly workspaceMetadataVersionService: WorkspaceMetadataVersionService,
|
||||
private readonly indexMetadataService: IndexMetadataService,
|
||||
private readonly workspaceCacheStorageService: WorkspaceCacheStorageService,
|
||||
) {
|
||||
super(relationMetadataRepository);
|
||||
}
|
||||
|
||||
override async createOne(
|
||||
relationMetadataInput: CreateRelationInput,
|
||||
): Promise<RelationMetadataEntity> {
|
||||
const objectMetadataMap = await this.getObjectMetadataMap(
|
||||
relationMetadataInput,
|
||||
);
|
||||
|
||||
try {
|
||||
validateMetadataNameOrThrow(relationMetadataInput.fromName);
|
||||
validateMetadataNameOrThrow(relationMetadataInput.toName);
|
||||
} catch (error) {
|
||||
if (error instanceof InvalidMetadataException)
|
||||
throw new RelationMetadataException(
|
||||
error.message,
|
||||
RelationMetadataExceptionCode.INVALID_RELATION_INPUT,
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
|
||||
await this.validateCreateRelationMetadataInput(
|
||||
relationMetadataInput,
|
||||
objectMetadataMap,
|
||||
);
|
||||
|
||||
// NOTE: this logic is called to create relation through metadata graphql endpoint (so only for custom field relations)
|
||||
const isCustom = true;
|
||||
const columnName = `${camelCase(relationMetadataInput.toName)}Id`;
|
||||
|
||||
const fromId = uuidV4();
|
||||
const toId = uuidV4();
|
||||
|
||||
const createdRelationFieldsMetadata =
|
||||
await this.fieldMetadataRepository.save(
|
||||
[
|
||||
this.createFieldMetadataForRelationMetadata(
|
||||
relationMetadataInput,
|
||||
'from',
|
||||
isCustom,
|
||||
fromId,
|
||||
),
|
||||
this.createFieldMetadataForRelationMetadata(
|
||||
relationMetadataInput,
|
||||
'to',
|
||||
isCustom,
|
||||
toId,
|
||||
),
|
||||
].flat(),
|
||||
);
|
||||
|
||||
const createdRelationMetadata = await super.createOne({
|
||||
...relationMetadataInput,
|
||||
fromFieldMetadataId: fromId,
|
||||
toFieldMetadataId: toId,
|
||||
});
|
||||
|
||||
const fromFieldMetadata = createdRelationFieldsMetadata.find(
|
||||
(fieldMetadata) => fieldMetadata.id === fromId,
|
||||
);
|
||||
|
||||
if (!fromFieldMetadata) {
|
||||
throw new RelationMetadataException(
|
||||
`From field metadata not found`,
|
||||
RelationMetadataExceptionCode.RELATION_METADATA_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const toFieldMetadata = createdRelationFieldsMetadata.find(
|
||||
(fieldMetadata) => fieldMetadata.id === toId,
|
||||
);
|
||||
|
||||
if (!toFieldMetadata) {
|
||||
throw new RelationMetadataException(
|
||||
`To field metadata not found`,
|
||||
RelationMetadataExceptionCode.RELATION_METADATA_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
await this.fieldMetadataRepository.update(fromId, {
|
||||
settings: {
|
||||
relationType: RelationType.ONE_TO_MANY,
|
||||
} as Partial<FieldMetadataDefaultSettings>,
|
||||
relationTargetFieldMetadataId: toId,
|
||||
relationTargetObjectMetadataId: relationMetadataInput.toObjectMetadataId,
|
||||
});
|
||||
|
||||
await this.fieldMetadataRepository.update(toId, {
|
||||
settings: {
|
||||
relationType: RelationType.MANY_TO_ONE,
|
||||
joinColumnName: `${relationMetadataInput.toName}Id`,
|
||||
} as Partial<FieldMetadataDefaultSettings>,
|
||||
relationTargetFieldMetadataId: fromId,
|
||||
relationTargetObjectMetadataId:
|
||||
relationMetadataInput.fromObjectMetadataId,
|
||||
});
|
||||
|
||||
await this.createWorkspaceCustomMigration(
|
||||
relationMetadataInput,
|
||||
objectMetadataMap,
|
||||
columnName,
|
||||
);
|
||||
|
||||
const toObjectMetadata =
|
||||
objectMetadataMap[relationMetadataInput.toObjectMetadataId];
|
||||
|
||||
const deletedAtFieldMetadata = toObjectMetadata.fields.find(
|
||||
(fieldMetadata) =>
|
||||
fieldMetadata.standardId === BASE_OBJECT_STANDARD_FIELD_IDS.deletedAt,
|
||||
);
|
||||
|
||||
this.throwIfDeletedAtFieldMetadataNotFound(deletedAtFieldMetadata);
|
||||
|
||||
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
|
||||
relationMetadataInput.workspaceId,
|
||||
);
|
||||
|
||||
await this.workspaceMetadataVersionService.incrementMetadataVersion(
|
||||
relationMetadataInput.workspaceId,
|
||||
);
|
||||
|
||||
return createdRelationMetadata;
|
||||
}
|
||||
|
||||
private async validateCreateRelationMetadataInput(
|
||||
relationMetadataInput: CreateRelationInput,
|
||||
objectMetadataMap: { [key: string]: ObjectMetadataEntity },
|
||||
) {
|
||||
if (
|
||||
relationMetadataInput.relationType === RelationMetadataType.MANY_TO_MANY
|
||||
) {
|
||||
throw new RelationMetadataException(
|
||||
'Many to many relations are not supported yet',
|
||||
RelationMetadataExceptionCode.INVALID_RELATION_INPUT,
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
objectMetadataMap[relationMetadataInput.fromObjectMetadataId] ===
|
||||
undefined ||
|
||||
objectMetadataMap[relationMetadataInput.toObjectMetadataId] === undefined
|
||||
) {
|
||||
throw new RelationMetadataException(
|
||||
"Can't find an existing object matching with fromObjectMetadataId or toObjectMetadataId",
|
||||
RelationMetadataExceptionCode.RELATION_METADATA_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
await this.checkIfFieldMetadataRelationNameExists(
|
||||
relationMetadataInput,
|
||||
objectMetadataMap,
|
||||
'from',
|
||||
);
|
||||
await this.checkIfFieldMetadataRelationNameExists(
|
||||
relationMetadataInput,
|
||||
objectMetadataMap,
|
||||
'to',
|
||||
);
|
||||
|
||||
validateFieldNameAvailabilityOrThrow(
|
||||
relationMetadataInput.fromName,
|
||||
objectMetadataMap[relationMetadataInput.fromObjectMetadataId],
|
||||
);
|
||||
validateFieldNameAvailabilityOrThrow(
|
||||
relationMetadataInput.toName,
|
||||
objectMetadataMap[relationMetadataInput.toObjectMetadataId],
|
||||
);
|
||||
}
|
||||
|
||||
private async checkIfFieldMetadataRelationNameExists(
|
||||
relationMetadataInput: CreateRelationInput,
|
||||
objectMetadataMap: { [key: string]: ObjectMetadataEntity },
|
||||
relationDirection: 'from' | 'to',
|
||||
) {
|
||||
const fieldAlreadyExists =
|
||||
await this.fieldMetadataService.findOneWithinWorkspace(
|
||||
relationMetadataInput.workspaceId,
|
||||
{
|
||||
where: {
|
||||
name: relationMetadataInput[`${relationDirection}Name`],
|
||||
objectMetadataId:
|
||||
relationMetadataInput[`${relationDirection}ObjectMetadataId`],
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (fieldAlreadyExists) {
|
||||
throw new RelationMetadataException(
|
||||
`Field on ${
|
||||
objectMetadataMap[
|
||||
relationMetadataInput[`${relationDirection}ObjectMetadataId`]
|
||||
].nameSingular
|
||||
} already exists`,
|
||||
RelationMetadataExceptionCode.RELATION_ALREADY_EXISTS,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async createWorkspaceCustomMigration(
|
||||
relationMetadataInput: CreateRelationInput,
|
||||
objectMetadataMap: { [key: string]: ObjectMetadataEntity },
|
||||
columnName: string,
|
||||
) {
|
||||
await this.workspaceMigrationService.createCustomMigration(
|
||||
generateMigrationName(`create-${relationMetadataInput.fromName}`),
|
||||
relationMetadataInput.workspaceId,
|
||||
[
|
||||
// Create the column
|
||||
{
|
||||
name: computeObjectTargetTable(
|
||||
objectMetadataMap[relationMetadataInput.toObjectMetadataId],
|
||||
),
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
action: WorkspaceMigrationColumnActionType.CREATE,
|
||||
columnName,
|
||||
columnType: 'uuid',
|
||||
isNullable: true,
|
||||
defaultValue: null,
|
||||
},
|
||||
],
|
||||
},
|
||||
// Create the foreignKey
|
||||
{
|
||||
name: computeObjectTargetTable(
|
||||
objectMetadataMap[relationMetadataInput.toObjectMetadataId],
|
||||
),
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
action: WorkspaceMigrationColumnActionType.CREATE_FOREIGN_KEY,
|
||||
columnName,
|
||||
referencedTableName: computeObjectTargetTable(
|
||||
objectMetadataMap[relationMetadataInput.fromObjectMetadataId],
|
||||
),
|
||||
referencedTableColumnName: 'id',
|
||||
isUnique:
|
||||
relationMetadataInput.relationType ===
|
||||
RelationMetadataType.ONE_TO_ONE,
|
||||
onDelete: RelationOnDeleteAction.SET_NULL,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
private createFieldMetadataForRelationMetadata(
|
||||
relationMetadataInput: CreateRelationInput,
|
||||
relationDirection: 'from' | 'to',
|
||||
isCustom: boolean,
|
||||
id?: string,
|
||||
) {
|
||||
return {
|
||||
...(id && { id: id }),
|
||||
name: relationMetadataInput[`${relationDirection}Name`],
|
||||
label: relationMetadataInput[`${relationDirection}Label`],
|
||||
description: relationMetadataInput[`${relationDirection}Description`],
|
||||
icon: relationMetadataInput[`${relationDirection}Icon`],
|
||||
isCustom,
|
||||
isActive: true,
|
||||
isNullable: true,
|
||||
type: FieldMetadataType.RELATION,
|
||||
objectMetadataId:
|
||||
relationMetadataInput[`${relationDirection}ObjectMetadataId`],
|
||||
workspaceId: relationMetadataInput.workspaceId,
|
||||
};
|
||||
}
|
||||
|
||||
private createForeignKeyFieldMetadata(
|
||||
relationMetadataInput: CreateRelationInput,
|
||||
columnName: string,
|
||||
) {
|
||||
return {
|
||||
name: columnName,
|
||||
label: `${relationMetadataInput.toLabel} Foreign Key`,
|
||||
description: relationMetadataInput.toDescription
|
||||
? `${relationMetadataInput.toDescription} Foreign Key`
|
||||
: undefined,
|
||||
icon: undefined,
|
||||
isCustom: true,
|
||||
isActive: true,
|
||||
isNullable: true,
|
||||
isSystem: true,
|
||||
type: FieldMetadataType.UUID,
|
||||
objectMetadataId: relationMetadataInput.toObjectMetadataId,
|
||||
workspaceId: relationMetadataInput.workspaceId,
|
||||
settings: { isForeignKey: true },
|
||||
};
|
||||
}
|
||||
|
||||
private async getObjectMetadataMap(
|
||||
relationMetadataInput: CreateRelationInput,
|
||||
): Promise<{ [key: string]: ObjectMetadataEntity }> {
|
||||
const objectMetadataEntries =
|
||||
await this.objectMetadataService.findManyWithinWorkspace(
|
||||
relationMetadataInput.workspaceId,
|
||||
{
|
||||
where: {
|
||||
id: In([
|
||||
relationMetadataInput.fromObjectMetadataId,
|
||||
relationMetadataInput.toObjectMetadataId,
|
||||
]),
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return objectMetadataEntries.reduce(
|
||||
(acc, curr) => {
|
||||
acc[curr.id] = curr;
|
||||
|
||||
return acc;
|
||||
},
|
||||
{} as { [key: string]: ObjectMetadataEntity },
|
||||
);
|
||||
}
|
||||
|
||||
public async findOneWithinWorkspace(
|
||||
workspaceId: string,
|
||||
options: FindOneOptions<RelationMetadataEntity>,
|
||||
) {
|
||||
return this.relationMetadataRepository.findOne({
|
||||
...options,
|
||||
where: {
|
||||
...options.where,
|
||||
workspaceId,
|
||||
},
|
||||
relations: ['fromFieldMetadata', 'toFieldMetadata'],
|
||||
});
|
||||
}
|
||||
|
||||
public async deleteOneRelation(
|
||||
id: string,
|
||||
workspaceId: string,
|
||||
): Promise<RelationMetadataEntity> {
|
||||
// TODO: This logic is duplicated with the BeforeDeleteOneRelation hook
|
||||
const relationMetadata = await this.relationMetadataRepository.findOne({
|
||||
where: { id },
|
||||
relations: [
|
||||
'fromFieldMetadata',
|
||||
'toFieldMetadata',
|
||||
'fromObjectMetadata',
|
||||
'toObjectMetadata',
|
||||
],
|
||||
});
|
||||
|
||||
if (!relationMetadata) {
|
||||
throw new RelationMetadataException(
|
||||
'Relation does not exist',
|
||||
RelationMetadataExceptionCode.RELATION_METADATA_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
const foreignKeyFieldMetadataName = `${camelCase(
|
||||
relationMetadata.toFieldMetadata.name,
|
||||
)}Id`;
|
||||
|
||||
const foreignKeyFieldMetadata = await this.fieldMetadataRepository.findOne({
|
||||
where: {
|
||||
name: foreignKeyFieldMetadataName,
|
||||
objectMetadataId: relationMetadata.toObjectMetadataId,
|
||||
workspaceId: relationMetadata.workspaceId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!foreignKeyFieldMetadata) {
|
||||
throw new RelationMetadataException(
|
||||
`Foreign key fieldMetadata not found (${foreignKeyFieldMetadataName}) for relation ${relationMetadata.id}`,
|
||||
RelationMetadataExceptionCode.FOREIGN_KEY_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
|
||||
await super.deleteOne(id);
|
||||
|
||||
// TODO: Move to a cdc scheduler
|
||||
await this.fieldMetadataService.deleteMany({
|
||||
id: {
|
||||
in: [
|
||||
relationMetadata.fromFieldMetadataId,
|
||||
relationMetadata.toFieldMetadataId,
|
||||
foreignKeyFieldMetadata.id,
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
const columnName = `${camelCase(relationMetadata.toFieldMetadata.name)}Id`;
|
||||
const objectTargetTable = computeObjectTargetTable(
|
||||
relationMetadata.toObjectMetadata,
|
||||
);
|
||||
|
||||
await this.deleteRelationWorkspaceCustomMigration(
|
||||
relationMetadata,
|
||||
objectTargetTable,
|
||||
columnName,
|
||||
);
|
||||
|
||||
const deletedAtFieldMetadata = await this.fieldMetadataRepository.findOneBy(
|
||||
{
|
||||
objectMetadataId: relationMetadata.toObjectMetadataId,
|
||||
name: 'deletedAt',
|
||||
},
|
||||
);
|
||||
|
||||
this.throwIfDeletedAtFieldMetadataNotFound(deletedAtFieldMetadata);
|
||||
|
||||
await this.indexMetadataService.deleteIndexMetadata(
|
||||
workspaceId,
|
||||
relationMetadata.toObjectMetadata,
|
||||
[
|
||||
foreignKeyFieldMetadata,
|
||||
deletedAtFieldMetadata as FieldMetadataEntity<FieldMetadataType>,
|
||||
],
|
||||
);
|
||||
|
||||
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
|
||||
relationMetadata.workspaceId,
|
||||
);
|
||||
|
||||
await this.workspaceMetadataVersionService.incrementMetadataVersion(
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
// TODO: Return id for delete endpoints
|
||||
return relationMetadata;
|
||||
}
|
||||
|
||||
async findManyRelationMetadataByFieldMetadataIds(
|
||||
fieldMetadataItems: Array<
|
||||
Pick<FieldMetadataInterface, 'id' | 'type' | 'objectMetadataId'>
|
||||
>,
|
||||
workspaceId: string,
|
||||
): Promise<(RelationMetadataEntity | NotFoundException)[]> {
|
||||
const objectMetadataMaps =
|
||||
await this.workspaceCacheStorageService.getObjectMetadataMapsOrThrow(
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
const mappedResult = fieldMetadataItems.map((fieldMetadataItem) => {
|
||||
const objectMetadata =
|
||||
objectMetadataMaps.byId[fieldMetadataItem.objectMetadataId];
|
||||
|
||||
if (!objectMetadata) {
|
||||
return new NotFoundException(
|
||||
`Object metadata not found for field ${fieldMetadataItem.id}`,
|
||||
);
|
||||
}
|
||||
|
||||
const fieldMetadata = objectMetadata.fieldsById[fieldMetadataItem.id];
|
||||
|
||||
const relationMetadata =
|
||||
fieldMetadata.fromRelationMetadata ?? fieldMetadata.toRelationMetadata;
|
||||
|
||||
if (!relationMetadata) {
|
||||
return new NotFoundException(
|
||||
`From object metadata not found for relation ${fieldMetadata?.id}`,
|
||||
);
|
||||
}
|
||||
|
||||
const fromObjectMetadata =
|
||||
objectMetadataMaps.byId[relationMetadata.fromObjectMetadataId];
|
||||
|
||||
const toObjectMetadata =
|
||||
objectMetadataMaps.byId[relationMetadata.toObjectMetadataId];
|
||||
|
||||
const fromFieldMetadata =
|
||||
objectMetadataMaps.byId[fromObjectMetadata.id].fieldsById[
|
||||
relationMetadata.fromFieldMetadataId
|
||||
];
|
||||
|
||||
const toFieldMetadata =
|
||||
objectMetadataMaps.byId[toObjectMetadata.id].fieldsById[
|
||||
relationMetadata.toFieldMetadataId
|
||||
];
|
||||
|
||||
return {
|
||||
...relationMetadata,
|
||||
fromObjectMetadata,
|
||||
toObjectMetadata,
|
||||
fromFieldMetadata,
|
||||
toFieldMetadata,
|
||||
};
|
||||
});
|
||||
|
||||
return mappedResult as (RelationMetadataEntity | NotFoundException)[];
|
||||
}
|
||||
|
||||
private async deleteRelationWorkspaceCustomMigration(
|
||||
relationMetadata: RelationMetadataEntity,
|
||||
objectTargetTable: string,
|
||||
columnName: string,
|
||||
) {
|
||||
await this.workspaceMigrationService.createCustomMigration(
|
||||
generateMigrationName(
|
||||
`delete-relation-from-${relationMetadata.fromObjectMetadata.nameSingular}-to-${relationMetadata.toObjectMetadata.nameSingular}`,
|
||||
),
|
||||
relationMetadata.workspaceId,
|
||||
[
|
||||
// Delete the column
|
||||
{
|
||||
name: objectTargetTable,
|
||||
action: WorkspaceMigrationTableActionType.ALTER,
|
||||
columns: [
|
||||
{
|
||||
action: WorkspaceMigrationColumnActionType.DROP,
|
||||
columnName,
|
||||
} satisfies WorkspaceMigrationColumnDrop,
|
||||
],
|
||||
},
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
private throwIfDeletedAtFieldMetadataNotFound(
|
||||
deletedAtFieldMetadata?: FieldMetadataEntity<FieldMetadataType> | null,
|
||||
) {
|
||||
if (!isDefined(deletedAtFieldMetadata)) {
|
||||
throw new RelationMetadataException(
|
||||
`Deleted field metadata not found`,
|
||||
RelationMetadataExceptionCode.RELATION_METADATA_NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
export enum RelationOnDeleteAction {
|
||||
CASCADE = 'CASCADE',
|
||||
RESTRICT = 'RESTRICT',
|
||||
SET_NULL = 'SET_NULL',
|
||||
NO_ACTION = 'NO_ACTION',
|
||||
}
|
||||
@ -1,14 +0,0 @@
|
||||
export type RelationToDelete = {
|
||||
id: string;
|
||||
fromFieldMetadataId: string;
|
||||
toFieldMetadataId: string;
|
||||
fromFieldMetadataName: string;
|
||||
toFieldMetadataName: string;
|
||||
fromObjectMetadataId: string;
|
||||
toObjectMetadataId: string;
|
||||
fromObjectName: string;
|
||||
toObjectName: string;
|
||||
toFieldMetadataIsCustom: boolean;
|
||||
toObjectMetadataIsCustom: boolean;
|
||||
direction: string;
|
||||
};
|
||||
@ -1,5 +0,0 @@
|
||||
import { camelCase } from 'src/utils/camel-case';
|
||||
|
||||
export const createRelationForeignKeyFieldMetadataName = (name: string) => {
|
||||
return `${camelCase(name)}Id`;
|
||||
};
|
||||
@ -1,34 +0,0 @@
|
||||
import {
|
||||
ConflictError,
|
||||
UserInputError,
|
||||
} from 'src/engine/core-modules/graphql/utils/graphql-errors.util';
|
||||
import {
|
||||
RelationMetadataException,
|
||||
RelationMetadataExceptionCode,
|
||||
} from 'src/engine/metadata-modules/relation-metadata/relation-metadata.exception';
|
||||
import { InvalidMetadataException } from 'src/engine/metadata-modules/utils/exceptions/invalid-metadata.exception';
|
||||
|
||||
export const relationMetadataGraphqlApiExceptionHandler = (error: Error) => {
|
||||
if (error instanceof InvalidMetadataException) {
|
||||
throw new UserInputError(error.message);
|
||||
}
|
||||
|
||||
if (error instanceof RelationMetadataException) {
|
||||
switch (error.code) {
|
||||
case RelationMetadataExceptionCode.INVALID_RELATION_INPUT:
|
||||
throw new UserInputError(error.message);
|
||||
case RelationMetadataExceptionCode.RELATION_ALREADY_EXISTS:
|
||||
throw new ConflictError(error.message);
|
||||
case RelationMetadataExceptionCode.FOREIGN_KEY_NOT_FOUND:
|
||||
case RelationMetadataExceptionCode.RELATION_METADATA_NOT_FOUND:
|
||||
throw error;
|
||||
default: {
|
||||
const _exhaustiveCheck: never = error.code;
|
||||
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw error;
|
||||
};
|
||||
Reference in New Issue
Block a user