From ae6777fab87aac4e3f83f00fd64a432f6f3aa8d6 Mon Sep 17 00:00:00 2001 From: Weiko Date: Fri, 27 Sep 2024 19:10:18 +0200 Subject: [PATCH] Fix viewFilter operand for dateTime fields (#7306) --- ...ew-filter-operand-for-date-time.command.ts | 110 ++++++++++++++++++ .../0-30/0-30-upgrade-version.command.ts | 7 ++ .../0-30/0-30-upgrade-version.module.ts | 2 + 3 files changed, 119 insertions(+) create mode 100644 packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-fix-view-filter-operand-for-date-time.command.ts diff --git a/packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-fix-view-filter-operand-for-date-time.command.ts b/packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-fix-view-filter-operand-for-date-time.command.ts new file mode 100644 index 0000000000..f2e98d6600 --- /dev/null +++ b/packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-fix-view-filter-operand-for-date-time.command.ts @@ -0,0 +1,110 @@ +import { InjectRepository } from '@nestjs/typeorm'; + +import chalk from 'chalk'; +import { Command } from 'nest-commander'; +import { Any, Repository } from 'typeorm'; + +import { + ActiveWorkspacesCommandOptions, + ActiveWorkspacesCommandRunner, +} from 'src/database/commands/active-workspaces.command'; +import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity'; +import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service'; +import { + FieldMetadataEntity, + FieldMetadataType, +} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity'; +import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; +import { ViewFilterWorkspaceEntity } from 'src/modules/view/standard-objects/view-filter.workspace-entity'; + +@Command({ + name: 'upgrade-0.30:fix-view-filter-operand-for-date-time', + description: 'Fix the view filter operand for date time fields', +}) +export class FixViewFilterOperandForDateTimeCommand extends ActiveWorkspacesCommandRunner { + constructor( + @InjectRepository(Workspace, 'core') + protected readonly workspaceRepository: Repository, + @InjectRepository(FieldMetadataEntity, 'metadata') + private readonly fieldMetadataRepository: Repository, + private readonly dataSourceService: DataSourceService, + private readonly twentyORMGlobalManager: TwentyORMGlobalManager, + ) { + super(workspaceRepository); + } + + async executeActiveWorkspacesCommand( + _passedParam: string[], + _options: ActiveWorkspacesCommandOptions, + workspaceIds: string[], + ): Promise { + for (const workspaceId of workspaceIds) { + try { + const dataSourceMetadata = + await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceId( + workspaceId, + ); + + if (!dataSourceMetadata) { + throw new Error( + `Could not find dataSourceMetadata for workspace ${workspaceId}`, + ); + } + + const viewFilterRepository = + await this.twentyORMGlobalManager.getRepositoryForWorkspace( + workspaceId, + 'viewFilter', + ); + + const dateTimeFieldMetadata = await this.fieldMetadataRepository.find({ + where: { + workspaceId, + type: FieldMetadataType.DATE_TIME, + }, + }); + + const dateTimeFieldMetadataIds = dateTimeFieldMetadata.map( + (fieldMetadata) => fieldMetadata.id, + ); + + const lessThanUpdatedResult = await viewFilterRepository.update( + { + operand: 'lessThan', + fieldMetadataId: Any(dateTimeFieldMetadataIds), + }, + { + operand: 'isBefore', + }, + ); + + const greaterThanUpdatedResult = await viewFilterRepository.update( + { + operand: 'greaterThan', + fieldMetadataId: Any(dateTimeFieldMetadataIds), + }, + { + operand: 'isAfter', + }, + ); + + this.logger.log( + `Updated ${(lessThanUpdatedResult.affected ?? 0) + (greaterThanUpdatedResult.affected ?? 0)} view filters for workspace ${workspaceId}`, + ); + } catch (error) { + this.logger.log( + chalk.red( + `Error running command for workspace ${workspaceId}: ${error}`, + ), + ); + continue; + } finally { + this.logger.log( + chalk.green(`Finished running command for workspace ${workspaceId}.`), + ); + } + + this.logger.log(chalk.green(`Command completed!`)); + } + } +} diff --git a/packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-upgrade-version.command.ts b/packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-upgrade-version.command.ts index 144746f829..25ff077ca5 100644 --- a/packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-upgrade-version.command.ts +++ b/packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-upgrade-version.command.ts @@ -5,6 +5,7 @@ import { Repository } from 'typeorm'; import { ActiveWorkspacesCommandRunner } from 'src/database/commands/active-workspaces.command'; import { FixEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-email-field-migration.command'; +import { FixViewFilterOperandForDateTimeCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-view-filter-operand-for-date-time.command'; import { MigrateEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-email-fields-to-emails.command'; import { MigratePhoneFieldsToPhonesCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-phone-fields-to-phones.command'; import { SetStaleMessageSyncBackToPendingCommand } from 'src/database/commands/upgrade-version/0-30/0-30-set-stale-message-sync-back-to-pending'; @@ -28,6 +29,7 @@ export class UpgradeTo0_30Command extends ActiveWorkspacesCommandRunner { private readonly setStaleMessageSyncBackToPendingCommand: SetStaleMessageSyncBackToPendingCommand, private readonly fixEmailFieldsToEmailsCommand: FixEmailFieldsToEmailsCommand, private readonly migratePhoneFieldsToPhones: MigratePhoneFieldsToPhonesCommand, + private readonly fixViewFilterOperandForDateTimeCommand: FixViewFilterOperandForDateTimeCommand, ) { super(workspaceRepository); } @@ -65,5 +67,10 @@ export class UpgradeTo0_30Command extends ActiveWorkspacesCommandRunner { options, workspaceIds, ); + await this.fixViewFilterOperandForDateTimeCommand.executeActiveWorkspacesCommand( + passedParam, + options, + workspaceIds, + ); } } diff --git a/packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-upgrade-version.module.ts b/packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-upgrade-version.module.ts index 3a7c51c8df..191bad9352 100644 --- a/packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-upgrade-version.module.ts +++ b/packages/twenty-server/src/database/commands/upgrade-version/0-30/0-30-upgrade-version.module.ts @@ -2,6 +2,7 @@ import { Module } from '@nestjs/common'; import { TypeOrmModule } from '@nestjs/typeorm'; import { FixEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-email-field-migration.command'; +import { FixViewFilterOperandForDateTimeCommand } from 'src/database/commands/upgrade-version/0-30/0-30-fix-view-filter-operand-for-date-time.command'; import { MigrateEmailFieldsToEmailsCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-email-fields-to-emails.command'; import { MigratePhoneFieldsToPhonesCommand } from 'src/database/commands/upgrade-version/0-30/0-30-migrate-phone-fields-to-phones.command'; import { SetStaleMessageSyncBackToPendingCommand } from 'src/database/commands/upgrade-version/0-30/0-30-set-stale-message-sync-back-to-pending'; @@ -36,6 +37,7 @@ import { ViewModule } from 'src/modules/view/view.module'; SetStaleMessageSyncBackToPendingCommand, FixEmailFieldsToEmailsCommand, MigratePhoneFieldsToPhonesCommand, + FixViewFilterOperandForDateTimeCommand, ], }) export class UpgradeTo0_30CommandModule {}