Skip to content

The open alternative to Salesforce, designed for AI.

9.9 KB
Raw Blame
import { Command } from 'nest-commander';
import { STANDARD_OBJECTS } from 'twenty-shared/metadata';
import { isDefined } from 'twenty-shared/utils';

import { ProvisionedWorkspaceCommandRunner } from 'src/database/commands/command-runners/provisioned-workspace.command-runner';
import { WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service';
import { type RunOnWorkspaceArgs } from 'src/database/commands/command-runners/workspace.command-runner';
import { ApplicationService } from 'src/engine/core-modules/application/application.service';
import { RegisteredWorkspaceCommand } from 'src/engine/core-modules/upgrade/decorators/registered-workspace-command.decorator';
import { findFlatEntityByUniversalIdentifier } from 'src/engine/metadata-modules/flat-entity/utils/find-flat-entity-by-universal-identifier.util';
import { type FlatFieldMetadata } from 'src/engine/metadata-modules/flat-field-metadata/types/flat-field-metadata.type';
import { type FlatObjectMetadata } from 'src/engine/metadata-modules/flat-object-metadata/types/flat-object-metadata.type';
import { type FlatViewField } from 'src/engine/metadata-modules/flat-view-field/types/flat-view-field.type';
import { computeTwentyStandardApplicationAllFlatEntityMaps } from 'src/engine/workspace-manager/twenty-standard-application/utils/twenty-standard-application-all-flat-entity-maps.constant';
import { WorkspaceCacheService } from 'src/engine/workspace-cache/services/workspace-cache.service';
import { WorkspaceMigrationBuilderException } from 'src/engine/workspace-manager/workspace-migration/exceptions/workspace-migration-builder-exception';
import { WorkspaceMigrationValidateBuildAndRunService } from 'src/engine/workspace-manager/workspace-migration/services/workspace-migration-validate-build-and-run-service';

const CAMPAIGN = STANDARD_OBJECTS.messageCampaign;

const ENGAGEMENT_FIELD_UNIVERSAL_IDENTIFIERS = [
  CAMPAIGN.fields.uniqueClickCount.universalIdentifier,
  CAMPAIGN.fields.clickRate.universalIdentifier,
  CAMPAIGN.fields.uniqueOpenCount.universalIdentifier,
  CAMPAIGN.fields.openRate.universalIdentifier,
];

const ENGAGEMENT_VIEW_FIELD_UNIVERSAL_IDENTIFIERS = [
  CAMPAIGN.views.messageCampaignRecordPageFields.viewFields.uniqueClickCount
    .universalIdentifier,
  CAMPAIGN.views.messageCampaignRecordPageFields.viewFields.clickRate
    .universalIdentifier,
  CAMPAIGN.views.messageCampaignRecordPageFields.viewFields.uniqueOpenCount
    .universalIdentifier,
  CAMPAIGN.views.messageCampaignRecordPageFields.viewFields.openRate
    .universalIdentifier,
];

@RegisteredWorkspaceCommand('2.47.0', 1791811197174)
@Command({
  name: 'upgrade:2-47:sync-campaign-engagement-fields',
  description:
    'Add the click and open fields to campaigns, and their columns, in existing workspaces',
})
export class SyncCampaignEngagementFieldsCommand extends ProvisionedWorkspaceCommandRunner {
  constructor(
    protected readonly workspaceIteratorService: WorkspaceIteratorService,
    private readonly applicationService: ApplicationService,
    private readonly workspaceCacheService: WorkspaceCacheService,
    private readonly workspaceMigrationValidateBuildAndRunService: WorkspaceMigrationValidateBuildAndRunService,
  ) {
    super(workspaceIteratorService);
  }

  override async runOnWorkspace(args: RunOnWorkspaceArgs): Promise<void> {
    await this.up(args);
  }

  async up({ workspaceId, options }: RunOnWorkspaceArgs): Promise<void> {
    const {
      flatObjectMetadataMaps,
      flatFieldMetadataMaps,
      flatViewMaps,
      flatViewFieldMaps,
    } = await this.workspaceCacheService.getOrRecompute(workspaceId, [
      'flatObjectMetadataMaps',
      'flatFieldMetadataMaps',
      'flatViewMaps',
      'flatViewFieldMaps',
    ]);

    const hasCampaignObject = isDefined(
      findFlatEntityByUniversalIdentifier<FlatObjectMetadata>({
        flatEntityMaps: flatObjectMetadataMaps,
        universalIdentifier: CAMPAIGN.universalIdentifier,
      }),
    );

    if (!hasCampaignObject) {
      this.logger.log(
        `Campaign object does not exist for workspace ${workspaceId}, skipping`,
      );

      return;
    }

    const { twentyStandardFlatApplication } =
      await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
        { workspaceId },
      );

    const { allFlatEntityMaps: standardAllFlatEntityMaps } =
      computeTwentyStandardApplicationAllFlatEntityMaps({
        now: new Date().toISOString(),
        workspaceId,
        twentyStandardApplicationId: twentyStandardFlatApplication.id,
      });

    const fieldsToCreate = ENGAGEMENT_FIELD_UNIVERSAL_IDENTIFIERS.filter(
      (universalIdentifier) =>
        !isDefined(
          flatFieldMetadataMaps.byUniversalIdentifier[universalIdentifier],
        ),
    ).map((universalIdentifier) => {
      const standardField =
        findFlatEntityByUniversalIdentifier<FlatFieldMetadata>({
          flatEntityMaps: standardAllFlatEntityMaps.flatFieldMetadataMaps,
          universalIdentifier,
        });

      if (!isDefined(standardField)) {
        throw new Error(
          `Standard application is missing engagement field ${universalIdentifier}`,
        );
      }

      return standardField;
    });

    // A workspace that never got the campaign views has no place for the
    // columns; the view backfill command owns those workspaces.
    const viewFieldsToCreate =
      ENGAGEMENT_VIEW_FIELD_UNIVERSAL_IDENTIFIERS.filter(
        (universalIdentifier) =>
          !isDefined(
            flatViewFieldMaps.byUniversalIdentifier[universalIdentifier],
          ),
      ).flatMap((universalIdentifier) => {
        const standardViewField =
          findFlatEntityByUniversalIdentifier<FlatViewField>({
            flatEntityMaps: standardAllFlatEntityMaps.flatViewFieldMaps,
            universalIdentifier,
          });

        if (!isDefined(standardViewField)) {
          throw new Error(
            `Standard application is missing engagement view column ${universalIdentifier}`,
          );
        }

        const viewUniversalIdentifier =
          standardAllFlatEntityMaps.flatViewMaps.universalIdentifierById[
            standardViewField.viewId
          ];
        const hasView =
          isDefined(viewUniversalIdentifier) &&
          isDefined(
            flatViewMaps.byUniversalIdentifier[viewUniversalIdentifier],
          );

        return hasView ? [standardViewField] : [];
      });

    if (fieldsToCreate.length === 0 && viewFieldsToCreate.length === 0) {
      return;
    }

    if (options.dryRun) {
      this.logger.log(
        `[DRY RUN] Workspace ${workspaceId}: ${fieldsToCreate.length} engagement field(s), ${viewFieldsToCreate.length} view column(s)`,
      );

      return;
    }

    const result =
      await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunLegacyWorkspaceMigration(
        {
          shouldDeferActions: false,
          isSystemBuild: true,
          workspaceId,
          applicationUniversalIdentifier:
            twentyStandardFlatApplication.universalIdentifier,
          allFlatEntityOperationByMetadataName: {
            fieldMetadata: {
              flatEntityToCreate: fieldsToCreate,
              flatEntityToDelete: [],
              flatEntityToUpdate: [],
            },
            viewField: {
              flatEntityToCreate: viewFieldsToCreate,
              flatEntityToDelete: [],
              flatEntityToUpdate: [],
            },
          },
        },
      );

    if (result.status === 'fail') {
      throw new WorkspaceMigrationBuilderException(
        result,
        `Failed to add the campaign engagement fields for workspace ${workspaceId}`,
      );
    }
  }

  async down({ workspaceId, options }: RunOnWorkspaceArgs): Promise<void> {
    const { flatFieldMetadataMaps, flatViewFieldMaps } =
      await this.workspaceCacheService.getOrRecompute(workspaceId, [
        'flatFieldMetadataMaps',
        'flatViewFieldMaps',
      ]);

    const fieldsToDelete = ENGAGEMENT_FIELD_UNIVERSAL_IDENTIFIERS.map(
      (universalIdentifier) =>
        findFlatEntityByUniversalIdentifier<FlatFieldMetadata>({
          flatEntityMaps: flatFieldMetadataMaps,
          universalIdentifier,
        }),
    ).filter(isDefined);

    const viewFieldsToDelete = ENGAGEMENT_VIEW_FIELD_UNIVERSAL_IDENTIFIERS.map(
      (universalIdentifier) =>
        findFlatEntityByUniversalIdentifier<FlatViewField>({
          flatEntityMaps: flatViewFieldMaps,
          universalIdentifier,
        }),
    ).filter(isDefined);

    if (fieldsToDelete.length === 0 && viewFieldsToDelete.length === 0) {
      return;
    }

    if (options.dryRun) {
      this.logger.log(
        `[DRY RUN] Workspace ${workspaceId}: would remove ${fieldsToDelete.length} engagement field(s), ${viewFieldsToDelete.length} view column(s)`,
      );

      return;
    }

    const { twentyStandardFlatApplication } =
      await this.applicationService.findWorkspaceTwentyStandardAndCustomApplicationOrThrow(
        { workspaceId },
      );

    const result =
      await this.workspaceMigrationValidateBuildAndRunService.validateBuildAndRunLegacyWorkspaceMigration(
        {
          shouldDeferActions: false,
          isSystemBuild: true,
          workspaceId,
          applicationUniversalIdentifier:
            twentyStandardFlatApplication.universalIdentifier,
          allFlatEntityOperationByMetadataName: {
            viewField: {
              flatEntityToCreate: [],
              flatEntityToDelete: viewFieldsToDelete,
              flatEntityToUpdate: [],
            },
            fieldMetadata: {
              flatEntityToCreate: [],
              flatEntityToDelete: fieldsToDelete,
              flatEntityToUpdate: [],
            },
          },
        },
      );

    if (result.status === 'fail') {
      throw new WorkspaceMigrationBuilderException(
        result,
        `Failed to remove the campaign engagement fields for workspace ${workspaceId}`,
      );
    }
  }
}