Remove some dead code (#6611)
We could remove a lot more than this, this is just a start. There are various tools to help with this, knip is a good one
This commit is contained in:
@ -1,183 +0,0 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { InjectDataSource } from '@nestjs/typeorm';
|
||||
|
||||
import { Command, CommandRunner, Option } from 'nest-commander';
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
import { FieldMetadataEntity } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
import { CustomWorkspaceEntity } from 'src/engine/twenty-orm/custom.workspace-entity';
|
||||
import { StandardFieldFactory } from 'src/engine/workspace-manager/workspace-sync-metadata/factories/standard-field.factory';
|
||||
import { StandardObjectFactory } from 'src/engine/workspace-manager/workspace-sync-metadata/factories/standard-object.factory';
|
||||
import { standardObjectMetadataDefinitions } from 'src/engine/workspace-manager/workspace-sync-metadata/standard-objects';
|
||||
import { computeStandardFields } from 'src/engine/workspace-manager/workspace-sync-metadata/utils/compute-standard-fields.util';
|
||||
|
||||
interface RunCommandOptions {
|
||||
workspaceId?: string;
|
||||
}
|
||||
|
||||
@Command({
|
||||
name: 'workspace:add-standard-id',
|
||||
description: 'Add standard id to all metadata objects and fields',
|
||||
})
|
||||
export class AddStandardIdCommand extends CommandRunner {
|
||||
private readonly logger = new Logger(AddStandardIdCommand.name);
|
||||
|
||||
constructor(
|
||||
@InjectDataSource('metadata')
|
||||
private readonly metadataDataSource: DataSource,
|
||||
private readonly standardObjectFactory: StandardObjectFactory,
|
||||
private readonly standardFieldFactory: StandardFieldFactory,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async run(_passedParam: string[], options: RunCommandOptions): Promise<void> {
|
||||
const queryRunner = this.metadataDataSource.createQueryRunner();
|
||||
const workspaceId = options.workspaceId;
|
||||
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
const manager = queryRunner.manager;
|
||||
|
||||
this.logger.log('Adding standardId to metadata objects and fields');
|
||||
|
||||
try {
|
||||
const standardObjectMetadataCollection =
|
||||
this.standardObjectFactory.create(
|
||||
standardObjectMetadataDefinitions,
|
||||
{
|
||||
// We don't need to provide the workspace id and data source id as we're only adding standardId
|
||||
workspaceId: '',
|
||||
dataSourceId: '',
|
||||
},
|
||||
{
|
||||
IS_BLOCKLIST_ENABLED: true,
|
||||
IS_EVENT_OBJECT_ENABLED: true,
|
||||
IS_AIRTABLE_INTEGRATION_ENABLED: true,
|
||||
IS_POSTGRESQL_INTEGRATION_ENABLED: true,
|
||||
IS_STRIPE_INTEGRATION_ENABLED: false,
|
||||
IS_COPILOT_ENABLED: false,
|
||||
IS_MESSAGING_ALIAS_FETCHING_ENABLED: true,
|
||||
IS_GOOGLE_CALENDAR_SYNC_V2_ENABLED: true,
|
||||
IS_FREE_ACCESS_ENABLED: false,
|
||||
IS_FUNCTION_SETTINGS_ENABLED: false,
|
||||
IS_WORKFLOW_ENABLED: false,
|
||||
IS_MESSAGE_THREAD_SUBSCRIBER_ENABLED: false,
|
||||
},
|
||||
);
|
||||
const standardFieldMetadataCollection = this.standardFieldFactory.create(
|
||||
CustomWorkspaceEntity,
|
||||
{
|
||||
workspaceId: '',
|
||||
dataSourceId: '',
|
||||
},
|
||||
{
|
||||
IS_BLOCKLIST_ENABLED: true,
|
||||
IS_EVENT_OBJECT_ENABLED: true,
|
||||
IS_AIRTABLE_INTEGRATION_ENABLED: true,
|
||||
IS_POSTGRESQL_INTEGRATION_ENABLED: true,
|
||||
IS_STRIPE_INTEGRATION_ENABLED: false,
|
||||
IS_COPILOT_ENABLED: false,
|
||||
IS_MESSAGING_ALIAS_FETCHING_ENABLED: true,
|
||||
IS_GOOGLE_CALENDAR_SYNC_V2_ENABLED: true,
|
||||
IS_FREE_ACCESS_ENABLED: false,
|
||||
IS_FUNCTION_SETTINGS_ENABLED: false,
|
||||
IS_WORKFLOW_ENABLED: false,
|
||||
IS_MESSAGE_THREAD_SUBSCRIBER_ENABLED: false,
|
||||
},
|
||||
);
|
||||
|
||||
const objectMetadataRepository =
|
||||
manager.getRepository(ObjectMetadataEntity);
|
||||
const fieldMetadataRepository =
|
||||
manager.getRepository(FieldMetadataEntity);
|
||||
|
||||
/**
|
||||
* Update all object metadata with standard id
|
||||
*/
|
||||
const updateObjectMetadataCollection: Partial<ObjectMetadataEntity>[] =
|
||||
[];
|
||||
const updateFieldMetadataCollection: Partial<FieldMetadataEntity>[] = [];
|
||||
const originalObjectMetadataCollection =
|
||||
await objectMetadataRepository.find({
|
||||
where: {
|
||||
fields: { isCustom: false },
|
||||
workspaceId: workspaceId,
|
||||
},
|
||||
relations: ['fields'],
|
||||
});
|
||||
const customObjectMetadataCollection =
|
||||
originalObjectMetadataCollection.filter(
|
||||
(metadata) => metadata.isCustom,
|
||||
);
|
||||
|
||||
const standardObjectMetadataMap = new Map(
|
||||
standardObjectMetadataCollection.map((metadata) => [
|
||||
metadata.nameSingular,
|
||||
metadata,
|
||||
]),
|
||||
);
|
||||
|
||||
for (const originalObjectMetadata of originalObjectMetadataCollection) {
|
||||
const standardObjectMetadata = standardObjectMetadataMap.get(
|
||||
originalObjectMetadata.nameSingular,
|
||||
);
|
||||
|
||||
if (!standardObjectMetadata && !originalObjectMetadata.isCustom) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const computedStandardFieldMetadataCollection = computeStandardFields(
|
||||
standardFieldMetadataCollection,
|
||||
originalObjectMetadata,
|
||||
customObjectMetadataCollection,
|
||||
);
|
||||
|
||||
if (!originalObjectMetadata.isCustom) {
|
||||
updateObjectMetadataCollection.push({
|
||||
id: originalObjectMetadata.id,
|
||||
standardId: originalObjectMetadata.standardId,
|
||||
});
|
||||
}
|
||||
|
||||
for (const fieldMetadata of originalObjectMetadata.fields) {
|
||||
const standardFieldMetadata =
|
||||
computedStandardFieldMetadataCollection.find(
|
||||
(field) => field.name === fieldMetadata.name && !field.isCustom,
|
||||
);
|
||||
|
||||
if (!standardFieldMetadata) {
|
||||
continue;
|
||||
}
|
||||
|
||||
updateFieldMetadataCollection.push({
|
||||
id: fieldMetadata.id,
|
||||
standardId: standardFieldMetadata.standardId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await objectMetadataRepository.save(updateObjectMetadataCollection);
|
||||
|
||||
await fieldMetadataRepository.save(updateFieldMetadataCollection);
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
this.logger.error('Error adding standard id to metadata', error);
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
|
||||
@Option({
|
||||
flags: '-w, --workspace-id [workspace_id]',
|
||||
description: 'workspace id',
|
||||
required: false,
|
||||
})
|
||||
parseWorkspaceId(value: string): string {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@ -7,7 +7,6 @@ import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-s
|
||||
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
||||
import { WorkspaceHealthModule } from 'src/engine/workspace-manager/workspace-health/workspace-health.module';
|
||||
import { WorkspaceStatusModule } from 'src/engine/workspace-manager/workspace-status/workspace-manager.module';
|
||||
import { AddStandardIdCommand } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/add-standard-id.command';
|
||||
import { ConvertRecordPositionsToIntegers } from 'src/engine/workspace-manager/workspace-sync-metadata/commands/convert-record-positions-to-integers.command';
|
||||
import { WorkspaceSyncMetadataModule } from 'src/engine/workspace-manager/workspace-sync-metadata/workspace-sync-metadata.module';
|
||||
|
||||
@ -27,7 +26,6 @@ import { SyncWorkspaceLoggerService } from './services/sync-workspace-logger.ser
|
||||
],
|
||||
providers: [
|
||||
SyncWorkspaceMetadataCommand,
|
||||
AddStandardIdCommand,
|
||||
ConvertRecordPositionsToIntegers,
|
||||
SyncWorkspaceLoggerService,
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user