Migrate domainName field from text type to links type (#6410)
Closes #5759.
This commit is contained in:
@ -0,0 +1,302 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import chalk from 'chalk';
|
||||
import { Command, CommandRunner, Option } from 'nest-commander';
|
||||
import { QueryRunner, Repository } from 'typeorm';
|
||||
|
||||
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
|
||||
import { DataSourceEntity } from 'src/engine/metadata-modules/data-source/data-source.entity';
|
||||
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
|
||||
import { CreateFieldInput } from 'src/engine/metadata-modules/field-metadata/dtos/create-field.input';
|
||||
import {
|
||||
FieldMetadataEntity,
|
||||
FieldMetadataType,
|
||||
} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata/field-metadata.service';
|
||||
import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager';
|
||||
import { WorkspaceStatusService } from 'src/engine/workspace-manager/workspace-status/services/workspace-status.service';
|
||||
import { COMPANY_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
|
||||
import { ViewService } from 'src/modules/view/services/view.service';
|
||||
import { ViewFieldWorkspaceEntity } from 'src/modules/view/standard-objects/view-field.workspace-entity';
|
||||
|
||||
interface MigrateDomainNameFromTextToLinksCommandOptions {
|
||||
workspaceId?: string;
|
||||
}
|
||||
|
||||
@Command({
|
||||
name: 'migrate-0.23:migrate-domain-standard-field-to-links',
|
||||
description:
|
||||
'Migrating field domainName of deprecated type TEXT to type LINKS',
|
||||
})
|
||||
export class MigrateDomainNameFromTextToLinksCommand extends CommandRunner {
|
||||
private readonly logger = new Logger(
|
||||
MigrateDomainNameFromTextToLinksCommand.name,
|
||||
);
|
||||
constructor(
|
||||
@InjectRepository(FieldMetadataEntity, 'metadata')
|
||||
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
|
||||
private readonly fieldMetadataService: FieldMetadataService,
|
||||
private readonly twentyORMGlobalManager: TwentyORMGlobalManager,
|
||||
private readonly typeORMService: TypeORMService,
|
||||
private readonly dataSourceService: DataSourceService,
|
||||
private readonly workspaceStatusService: WorkspaceStatusService,
|
||||
private readonly viewService: ViewService,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
@Option({
|
||||
flags: '-w, --workspace-id [workspace_id]',
|
||||
description:
|
||||
'workspace id. Command runs on all active workspaces if not provided',
|
||||
required: false,
|
||||
})
|
||||
parseWorkspaceId(value: string): string {
|
||||
return value;
|
||||
}
|
||||
|
||||
async run(
|
||||
_passedParam: string[],
|
||||
options: MigrateDomainNameFromTextToLinksCommandOptions,
|
||||
): Promise<void> {
|
||||
this.logger.log(
|
||||
'Running command to migrate standard field domainName from text to Link',
|
||||
);
|
||||
let workspaceIds: string[] = [];
|
||||
|
||||
if (options.workspaceId) {
|
||||
workspaceIds = [options.workspaceId];
|
||||
} else {
|
||||
const activeWorkspaceIds =
|
||||
await this.workspaceStatusService.getActiveWorkspaceIds();
|
||||
|
||||
workspaceIds = activeWorkspaceIds;
|
||||
}
|
||||
|
||||
if (!workspaceIds.length) {
|
||||
this.logger.log(chalk.yellow('No workspace found'));
|
||||
|
||||
return;
|
||||
} else {
|
||||
this.logger.log(
|
||||
chalk.green(`Running command on ${workspaceIds.length} workspaces`),
|
||||
);
|
||||
}
|
||||
|
||||
for (const workspaceId of workspaceIds) {
|
||||
this.logger.log(`Running command for workspace ${workspaceId}`);
|
||||
try {
|
||||
const dataSourceMetadata =
|
||||
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceId(
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
if (!dataSourceMetadata) {
|
||||
throw new Error(
|
||||
`Could not find dataSourceMetadata for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const workspaceDataSource =
|
||||
await this.typeORMService.connectToDataSource(dataSourceMetadata);
|
||||
|
||||
if (!workspaceDataSource) {
|
||||
throw new Error(
|
||||
`Could not connect to dataSource for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
const domainNameField = await this.fieldMetadataRepository.findOneBy({
|
||||
workspaceId,
|
||||
standardId: COMPANY_STANDARD_FIELD_IDS.domainName,
|
||||
});
|
||||
|
||||
if (!domainNameField) {
|
||||
throw new Error('Could not find domainName field');
|
||||
}
|
||||
|
||||
if (domainNameField.type === FieldMetadataType.LINKS) {
|
||||
this.logger.log(
|
||||
`Field domainName is already of type LINKS, skipping migration.`,
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
this.logger.log(`Attempting to migrate domainName field.`);
|
||||
|
||||
const workspaceQueryRunner = workspaceDataSource.createQueryRunner();
|
||||
|
||||
await workspaceQueryRunner.connect();
|
||||
|
||||
const fieldName = domainNameField.name;
|
||||
const { id: _id, ...domainNameFieldWithoutId } = domainNameField;
|
||||
|
||||
try {
|
||||
const tmpNewDomainLinksField =
|
||||
await this.fieldMetadataService.createOne({
|
||||
...domainNameFieldWithoutId,
|
||||
type: FieldMetadataType.LINKS,
|
||||
name: `${fieldName}Tmp`,
|
||||
defaultValue: {
|
||||
primaryLinkUrl: domainNameField.defaultValue,
|
||||
secondaryLinks: null,
|
||||
primaryLinkLabel: "''",
|
||||
},
|
||||
} satisfies CreateFieldInput);
|
||||
|
||||
// Migrate data from domainName to primaryLinkUrl
|
||||
await this.migrateDataWithinCompanyTable({
|
||||
sourceColumnName: `${domainNameField.name}`,
|
||||
targetColumnName: `${tmpNewDomainLinksField.name}PrimaryLinkUrl`,
|
||||
workspaceQueryRunner,
|
||||
dataSourceMetadata,
|
||||
});
|
||||
|
||||
// Duplicate initial domainName text field's views behaviour for new domainName field
|
||||
await this.viewService.removeFieldFromViews({
|
||||
workspaceId: workspaceId,
|
||||
fieldId: tmpNewDomainLinksField.id,
|
||||
});
|
||||
|
||||
const viewFieldRepository =
|
||||
await this.twentyORMGlobalManager.getRepositoryForWorkspace<ViewFieldWorkspaceEntity>(
|
||||
workspaceId,
|
||||
'viewField',
|
||||
);
|
||||
const viewFieldsWithDeprecatedField = await viewFieldRepository.find({
|
||||
where: {
|
||||
fieldMetadataId: domainNameField.id,
|
||||
isVisible: true,
|
||||
},
|
||||
});
|
||||
|
||||
await this.viewService.addFieldToViews({
|
||||
workspaceId: workspaceId,
|
||||
fieldId: tmpNewDomainLinksField.id,
|
||||
viewsIds: viewFieldsWithDeprecatedField
|
||||
.filter((viewField) => viewField.viewId !== null)
|
||||
.map((viewField) => viewField.viewId as string),
|
||||
positions: viewFieldsWithDeprecatedField.reduce(
|
||||
(acc, viewField) => {
|
||||
if (!viewField.viewId) {
|
||||
return acc;
|
||||
}
|
||||
acc[viewField.viewId] = viewField.position;
|
||||
|
||||
return acc;
|
||||
},
|
||||
[],
|
||||
),
|
||||
size: 150,
|
||||
});
|
||||
|
||||
// Delete initial domainName text field
|
||||
await this.fieldMetadataService.deleteOneField(
|
||||
{ id: domainNameField.id },
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
// Rename temporary domainName links field
|
||||
await this.fieldMetadataService.updateOne(tmpNewDomainLinksField.id, {
|
||||
id: tmpNewDomainLinksField.id,
|
||||
workspaceId: tmpNewDomainLinksField.workspaceId,
|
||||
name: `${fieldName}`,
|
||||
isCustom: false,
|
||||
});
|
||||
|
||||
this.logger.log(`Migration of domainName done!`);
|
||||
} catch (error) {
|
||||
this.logger.log(
|
||||
`Failed to migrate domainName ${domainNameField.id}, rolling back.`,
|
||||
);
|
||||
|
||||
// Re-create initial field if it was deleted
|
||||
const initialField =
|
||||
await this.fieldMetadataService.findOneWithinWorkspace(
|
||||
workspaceId,
|
||||
{
|
||||
where: {
|
||||
name: `${domainNameField.name}`,
|
||||
objectMetadataId: domainNameField.objectMetadataId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const tmpNewDomainLinksField =
|
||||
await this.fieldMetadataService.findOneWithinWorkspace(
|
||||
workspaceId,
|
||||
{
|
||||
where: {
|
||||
name: `${domainNameField.name}Tmp`,
|
||||
objectMetadataId: domainNameField.objectMetadataId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (!initialField) {
|
||||
this.logger.log(`Re-creating initial domainName field`);
|
||||
const restoredField = await this.fieldMetadataService.createOne({
|
||||
...domainNameField,
|
||||
});
|
||||
|
||||
if (tmpNewDomainLinksField) {
|
||||
this.logger.log(`Restoring data in domainName`);
|
||||
await this.migrateDataWithinCompanyTable({
|
||||
sourceColumnName: `${tmpNewDomainLinksField.name}PrimaryLinkLabel`,
|
||||
targetColumnName: `${restoredField.name}PrimaryLinkLabel`,
|
||||
workspaceQueryRunner,
|
||||
dataSourceMetadata,
|
||||
});
|
||||
|
||||
await this.migrateDataWithinCompanyTable({
|
||||
sourceColumnName: `${tmpNewDomainLinksField.name}PrimaryLinkUrl`,
|
||||
targetColumnName: `${restoredField.name}PrimaryLinkUrl`,
|
||||
workspaceQueryRunner,
|
||||
dataSourceMetadata,
|
||||
});
|
||||
} else {
|
||||
this.logger.log(
|
||||
`Failed to restore data in domainName field ${domainNameField.id}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (tmpNewDomainLinksField) {
|
||||
await this.fieldMetadataService.deleteOneField(
|
||||
{ id: tmpNewDomainLinksField.id },
|
||||
workspaceId,
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
await workspaceQueryRunner.release();
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.log(
|
||||
chalk.red(
|
||||
`Running command on workspace ${workspaceId} failed with error: ${error}`,
|
||||
),
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
this.logger.log(chalk.green(`Command completed!`));
|
||||
}
|
||||
}
|
||||
|
||||
private async migrateDataWithinCompanyTable({
|
||||
sourceColumnName,
|
||||
targetColumnName,
|
||||
workspaceQueryRunner,
|
||||
dataSourceMetadata,
|
||||
}: {
|
||||
sourceColumnName: string;
|
||||
targetColumnName: string;
|
||||
workspaceQueryRunner: QueryRunner;
|
||||
dataSourceMetadata: DataSourceEntity;
|
||||
}) {
|
||||
await workspaceQueryRunner.query(
|
||||
`UPDATE "${dataSourceMetadata.schema}"."company" SET "${targetColumnName}" = CASE WHEN "${sourceColumnName}" LIKE 'http%' THEN "${sourceColumnName}" ELSE 'https://' || "${sourceColumnName}" END;`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
import { Command, CommandRunner, Option } from 'nest-commander';
|
||||
|
||||
import { MigrateDomainNameFromTextToLinksCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-domain-to-links.command';
|
||||
import { MigrateLinkFieldsToLinksCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-link-fields-to-links.command';
|
||||
import { MigrateMessageChannelSyncStatusEnumCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-message-channel-sync-status-enum.command';
|
||||
|
||||
@ -14,6 +15,7 @@ interface Options {
|
||||
export class UpgradeTo0_23Command extends CommandRunner {
|
||||
constructor(
|
||||
private readonly migrateLinkFieldsToLinks: MigrateLinkFieldsToLinksCommand,
|
||||
private readonly migrateDomainNameFromTextToLinks: MigrateDomainNameFromTextToLinksCommand,
|
||||
private readonly migrateMessageChannelSyncStatusEnumCommand: MigrateMessageChannelSyncStatusEnumCommand,
|
||||
) {
|
||||
super();
|
||||
@ -31,6 +33,7 @@ export class UpgradeTo0_23Command extends CommandRunner {
|
||||
|
||||
async run(_passedParam: string[], options: Options): Promise<void> {
|
||||
await this.migrateLinkFieldsToLinks.run(_passedParam, options);
|
||||
await this.migrateDomainNameFromTextToLinks.run(_passedParam, options);
|
||||
await this.migrateMessageChannelSyncStatusEnumCommand.run(
|
||||
_passedParam,
|
||||
options,
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { MigrateDomainNameFromTextToLinksCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-domain-to-links.command';
|
||||
import { MigrateLinkFieldsToLinksCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-link-fields-to-links.command';
|
||||
import { MigrateMessageChannelSyncStatusEnumCommand } from 'src/database/commands/upgrade-version/0-23/0-23-migrate-message-channel-sync-status-enum.command';
|
||||
import { UpgradeTo0_23Command } from 'src/database/commands/upgrade-version/0-23/0-23-upgrade-version.command';
|
||||
@ -28,6 +29,7 @@ import { ViewModule } from 'src/modules/view/view.module';
|
||||
],
|
||||
providers: [
|
||||
MigrateLinkFieldsToLinksCommand,
|
||||
MigrateDomainNameFromTextToLinksCommand,
|
||||
MigrateMessageChannelSyncStatusEnumCommand,
|
||||
UpgradeTo0_23Command,
|
||||
],
|
||||
|
||||
@ -28,7 +28,7 @@ export const seedCompanies = async (
|
||||
.into(`${schemaName}.${tableName}`, [
|
||||
'id',
|
||||
'name',
|
||||
'domainName',
|
||||
'domainNamePrimaryLinkUrl',
|
||||
'addressAddressStreet1',
|
||||
'addressAddressStreet2',
|
||||
'addressAddressCity',
|
||||
@ -42,7 +42,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.LINKEDIN,
|
||||
name: 'Linkedin',
|
||||
domainName: 'linkedin.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://linkedin.com' },
|
||||
addressAddressStreet1: 'Eutaw Street',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'Dublin',
|
||||
@ -54,7 +54,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.FACEBOOK,
|
||||
name: 'Facebook',
|
||||
domainName: 'facebook.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://facebook.com' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -66,7 +66,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.QONTO,
|
||||
name: 'Qonto',
|
||||
domainName: 'qonto.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://qonto.com' },
|
||||
addressAddressStreet1: '18 rue de navarrin',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'Paris',
|
||||
@ -78,7 +78,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.MICROSOFT,
|
||||
name: 'Microsoft',
|
||||
domainName: 'microsoft.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://microsoft.com' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -90,7 +90,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.AIRBNB,
|
||||
name: 'Airbnb',
|
||||
domainName: 'airbnb.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://airbnb.com' },
|
||||
addressAddressStreet1: '888 Brannan St',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'San Francisco',
|
||||
@ -102,7 +102,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.GOOGLE,
|
||||
name: 'Google',
|
||||
domainName: 'google.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://google.com' },
|
||||
addressAddressStreet1: '760 Market St',
|
||||
addressAddressStreet2: 'Floor 10',
|
||||
addressAddressCity: 'San Francisco',
|
||||
@ -114,7 +114,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.NETFLIX,
|
||||
name: 'Netflix',
|
||||
domainName: 'netflix.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://netflix.com' },
|
||||
addressAddressStreet1: '2300 Harrison St',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'San Francisco',
|
||||
@ -126,7 +126,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.LIBEO,
|
||||
name: 'Libeo',
|
||||
domainName: 'libeo.io',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://libeo.io' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -138,7 +138,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.CLAAP,
|
||||
name: 'Claap',
|
||||
domainName: 'claap.io',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://claap.io' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -150,7 +150,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.HASURA,
|
||||
name: 'Hasura',
|
||||
domainName: 'hasura.io',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://hasura.io' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -162,7 +162,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.WEWORK,
|
||||
name: 'Wework',
|
||||
domainName: 'wework.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://wework.com' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -174,7 +174,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.SAMSUNG,
|
||||
name: 'Samsung',
|
||||
domainName: 'samsung.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://samsung.com' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
@ -186,7 +186,7 @@ export const seedCompanies = async (
|
||||
{
|
||||
id: DEV_SEED_COMPANY_IDS.ALGOLIA,
|
||||
name: 'Algolia',
|
||||
domainName: 'algolia.com',
|
||||
domainNamePrimaryLinkUrl: { primarlyLinkUrl: 'https://algolia.com' },
|
||||
addressAddressStreet1: null,
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: null,
|
||||
|
||||
@ -8,7 +8,9 @@ import { ObjectMetadataInterface } from 'src/engine/metadata-modules/field-metad
|
||||
import { stringifyWithoutKeyQuote } from 'src/engine/api/graphql/workspace-query-builder/utils/stringify-without-key-quote.util';
|
||||
import { WorkspaceQueryRunnerService } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-runner.service';
|
||||
import { IntelligenceService } from 'src/engine/core-modules/quick-actions/intelligence.service';
|
||||
import { getCompanyNameFromDomainName } from 'src/modules/contact-creation-manager/utils/get-company-name-from-domain-name.util';
|
||||
import { capitalize } from 'src/utils/capitalize';
|
||||
import { getCompanyDomainName } from 'src/utils/getCompanyDomainName';
|
||||
import { isWorkEmail } from 'src/utils/is-work-email';
|
||||
|
||||
@Injectable()
|
||||
@ -159,7 +161,7 @@ export class QuickActionsService {
|
||||
}
|
||||
|
||||
const enrichedData = await this.intelligenceService.enrichCompany(
|
||||
company.domainName,
|
||||
getCompanyNameFromDomainName(getCompanyDomainName(company)),
|
||||
);
|
||||
|
||||
await this.workspaceQueryRunnunerService.execute(
|
||||
|
||||
@ -1,11 +1,12 @@
|
||||
import { FieldMetadataDefaultValue } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata-default-value.interface';
|
||||
import { FieldMetadataOptions } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata-options.interface';
|
||||
import { FieldMetadataSettings } from 'src/engine/metadata-modules/field-metadata/interfaces/field-metadata-settings.interface';
|
||||
|
||||
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { generateDefaultValue } from 'src/engine/metadata-modules/field-metadata/utils/generate-default-value';
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
import { metadataArgsStorage } from 'src/engine/twenty-orm/storage/metadata-args.storage';
|
||||
import { TypedReflect } from 'src/utils/typed-reflect';
|
||||
import { generateDefaultValue } from 'src/engine/metadata-modules/field-metadata/utils/generate-default-value';
|
||||
|
||||
export interface WorkspaceFieldOptions<
|
||||
T extends FieldMetadataType | 'default',
|
||||
@ -17,6 +18,7 @@ export interface WorkspaceFieldOptions<
|
||||
icon?: string;
|
||||
defaultValue?: FieldMetadataDefaultValue<T>;
|
||||
options?: FieldMetadataOptions<T>;
|
||||
settings?: FieldMetadataSettings<T>;
|
||||
}
|
||||
|
||||
export function WorkspaceField<T extends FieldMetadataType>(
|
||||
|
||||
@ -11,7 +11,7 @@ export const companyPrefillDemoData = async (
|
||||
.insert()
|
||||
.into(`${schemaName}.company`, [
|
||||
'name',
|
||||
'domainName',
|
||||
'domainNamePrimaryLinkUrl',
|
||||
'addressAddressCity',
|
||||
'employees',
|
||||
'linkedinLinkPrimaryLinkUrl',
|
||||
|
||||
@ -9,7 +9,7 @@ export const companyPrefillData = async (
|
||||
.insert()
|
||||
.into(`${schemaName}.company`, [
|
||||
'name',
|
||||
'domainName',
|
||||
'domainNamePrimaryLinkUrl',
|
||||
'addressAddressStreet1',
|
||||
'addressAddressStreet2',
|
||||
'addressAddressCity',
|
||||
@ -23,7 +23,7 @@ export const companyPrefillData = async (
|
||||
.values([
|
||||
{
|
||||
name: 'Airbnb',
|
||||
domainName: 'airbnb.com',
|
||||
domainNamePrimaryLinkUrl: 'https://airbnb.com',
|
||||
addressAddressStreet1: '888 Brannan St',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'San Francisco',
|
||||
@ -35,7 +35,7 @@ export const companyPrefillData = async (
|
||||
},
|
||||
{
|
||||
name: 'Qonto',
|
||||
domainName: 'qonto.com',
|
||||
domainNamePrimaryLinkUrl: 'https://qonto.com',
|
||||
addressAddressStreet1: '18 rue de navarrin',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'Paris',
|
||||
@ -47,7 +47,7 @@ export const companyPrefillData = async (
|
||||
},
|
||||
{
|
||||
name: 'Stripe',
|
||||
domainName: 'stripe.com',
|
||||
domainNamePrimaryLinkUrl: 'https://stripe.com',
|
||||
addressAddressStreet1: 'Eutaw Street',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'Dublin',
|
||||
@ -59,7 +59,7 @@ export const companyPrefillData = async (
|
||||
},
|
||||
{
|
||||
name: 'Figma',
|
||||
domainName: 'figma.com',
|
||||
domainNamePrimaryLinkUrl: 'https://figma.com',
|
||||
addressAddressStreet1: '760 Market St',
|
||||
addressAddressStreet2: 'Floor 10',
|
||||
addressAddressCity: 'San Francisco',
|
||||
@ -71,7 +71,7 @@ export const companyPrefillData = async (
|
||||
},
|
||||
{
|
||||
name: 'Notion',
|
||||
domainName: 'notion.com',
|
||||
domainNamePrimaryLinkUrl: 'https://notion.com',
|
||||
addressAddressStreet1: '2300 Harrison St',
|
||||
addressAddressStreet2: null,
|
||||
addressAddressCity: 'San Francisco',
|
||||
|
||||
@ -51,10 +51,15 @@ export class SyncWorkspaceMetadataCommand extends CommandRunner {
|
||||
);
|
||||
}
|
||||
|
||||
let count = 1;
|
||||
|
||||
const errorsDuringSync: string[] = [];
|
||||
|
||||
for (const workspaceId of workspaceIds) {
|
||||
this.logger.log(`Running workspace sync for workspace: ${workspaceId}`);
|
||||
this.logger.log(
|
||||
`Running workspace sync for workspace: ${workspaceId} (${count} out of ${workspaceIds.length})`,
|
||||
);
|
||||
count++;
|
||||
try {
|
||||
const issues =
|
||||
await this.workspaceHealthService.healthCheck(workspaceId);
|
||||
|
||||
@ -20,6 +20,7 @@ export class CompanyRepository {
|
||||
public async getExistingCompaniesByDomainNames(
|
||||
domainNames: string[],
|
||||
workspaceId: string,
|
||||
companyDomainNameColumnName: string,
|
||||
transactionManager?: EntityManager,
|
||||
): Promise<{ id: string; domainName: string }[]> {
|
||||
const dataSourceSchema =
|
||||
@ -27,7 +28,7 @@ export class CompanyRepository {
|
||||
|
||||
const existingCompanies =
|
||||
await this.workspaceDataSourceService.executeRawQuery(
|
||||
`SELECT id, "domainName" FROM ${dataSourceSchema}.company WHERE "domainName" = ANY($1)`,
|
||||
`SELECT id, "${companyDomainNameColumnName}" AS "domainName" FROM ${dataSourceSchema}.company WHERE REGEXP_REPLACE("${companyDomainNameColumnName}", '^https?://', '') = ANY($1)`,
|
||||
[domainNames],
|
||||
workspaceId,
|
||||
transactionManager,
|
||||
@ -56,6 +57,7 @@ export class CompanyRepository {
|
||||
public async createCompany(
|
||||
workspaceId: string,
|
||||
companyToCreate: CompanyToCreate,
|
||||
companyDomainNameColumnName,
|
||||
transactionManager?: EntityManager,
|
||||
): Promise<void> {
|
||||
const dataSourceSchema =
|
||||
@ -67,11 +69,11 @@ export class CompanyRepository {
|
||||
);
|
||||
|
||||
await this.workspaceDataSourceService.executeRawQuery(
|
||||
`INSERT INTO ${dataSourceSchema}.company (id, "domainName", name, "addressAddressCity", position)
|
||||
`INSERT INTO ${dataSourceSchema}.company (id, "${companyDomainNameColumnName}", name, "addressAddressCity", position)
|
||||
VALUES ($1, $2, $3, $4, $5)`,
|
||||
[
|
||||
companyToCreate.id,
|
||||
companyToCreate.domainName,
|
||||
'https://' + companyToCreate.domainName,
|
||||
companyToCreate.name ?? '',
|
||||
companyToCreate.city ?? '',
|
||||
lastCompanyPosition + 1,
|
||||
|
||||
@ -48,7 +48,7 @@ export class CompanyWorkspaceEntity extends BaseWorkspaceEntity {
|
||||
|
||||
@WorkspaceField({
|
||||
standardId: COMPANY_STANDARD_FIELD_IDS.domainName,
|
||||
type: FieldMetadataType.TEXT,
|
||||
type: FieldMetadataType.LINKS,
|
||||
label: 'Domain Name',
|
||||
description:
|
||||
'The company website URL. We use this url to fetch the company icon',
|
||||
|
||||
@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity';
|
||||
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 { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
|
||||
import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/workspace-datasource.module';
|
||||
@ -24,6 +25,7 @@ import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/sta
|
||||
WorkspaceDataSourceModule,
|
||||
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
|
||||
TypeOrmModule.forFeature([ObjectMetadataEntity], 'metadata'),
|
||||
TypeOrmModule.forFeature([FieldMetadataEntity], 'metadata'),
|
||||
],
|
||||
providers: [
|
||||
CreateCompanyService,
|
||||
|
||||
@ -7,8 +7,13 @@ import compact from 'lodash.compact';
|
||||
import { EntityManager, Repository } from 'typeorm';
|
||||
|
||||
import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event';
|
||||
import {
|
||||
FieldMetadataEntity,
|
||||
FieldMetadataType,
|
||||
} from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
|
||||
import { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
|
||||
import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
|
||||
import { COMPANY_STANDARD_FIELD_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-field-ids';
|
||||
import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-ids';
|
||||
import { ConnectedAccountWorkspaceEntity } from 'src/modules/connected-account/standard-objects/connected-account.workspace-entity';
|
||||
import { CONTACTS_CREATION_BATCH_SIZE } from 'src/modules/contact-creation-manager/constants/contacts-creation-batch-size.constant';
|
||||
@ -36,12 +41,15 @@ export class CreateCompanyAndContactService {
|
||||
private readonly eventEmitter: EventEmitter2,
|
||||
@InjectRepository(ObjectMetadataEntity, 'metadata')
|
||||
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>,
|
||||
@InjectRepository(FieldMetadataEntity, 'metadata')
|
||||
private readonly fieldMetadataRepository: Repository<FieldMetadataEntity>,
|
||||
) {}
|
||||
|
||||
private async createCompaniesAndPeople(
|
||||
connectedAccount: ConnectedAccountWorkspaceEntity,
|
||||
contactsToCreate: Contact[],
|
||||
workspaceId: string,
|
||||
companyDomainNameColumnName: string,
|
||||
transactionManager?: EntityManager,
|
||||
): Promise<PersonWorkspaceEntity[]> {
|
||||
if (!contactsToCreate || contactsToCreate.length === 0) {
|
||||
@ -103,6 +111,7 @@ export class CreateCompanyAndContactService {
|
||||
const companiesObject = await this.createCompaniesService.createCompanies(
|
||||
domainNamesToCreate,
|
||||
workspaceId,
|
||||
companyDomainNameColumnName,
|
||||
transactionManager,
|
||||
);
|
||||
|
||||
@ -146,11 +155,24 @@ export class CreateCompanyAndContactService {
|
||||
throw new Error('Object metadata not found');
|
||||
}
|
||||
|
||||
const domainNameFieldMetadata = await this.fieldMetadataRepository.findOne({
|
||||
where: {
|
||||
workspaceId: workspaceId,
|
||||
standardId: COMPANY_STANDARD_FIELD_IDS.domainName,
|
||||
},
|
||||
});
|
||||
|
||||
const companyDomainNameColumnName =
|
||||
domainNameFieldMetadata?.type === FieldMetadataType.LINKS
|
||||
? 'domainNamePrimaryLinkUrl'
|
||||
: 'domainName';
|
||||
|
||||
for (const contactsBatch of contactsBatches) {
|
||||
const createdPeople = await this.createCompaniesAndPeople(
|
||||
connectedAccount,
|
||||
contactsBatch,
|
||||
workspaceId,
|
||||
companyDomainNameColumnName,
|
||||
);
|
||||
|
||||
for (const createdPerson of createdPeople) {
|
||||
|
||||
@ -8,6 +8,7 @@ import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repos
|
||||
import { CompanyRepository } from 'src/modules/company/repositories/company.repository';
|
||||
import { CompanyWorkspaceEntity } from 'src/modules/company/standard-objects/company.workspace-entity';
|
||||
import { getCompanyNameFromDomainName } from 'src/modules/contact-creation-manager/utils/get-company-name-from-domain-name.util';
|
||||
import { getCompanyDomainName } from 'src/utils/getCompanyDomainName';
|
||||
@Injectable()
|
||||
export class CreateCompanyService {
|
||||
private readonly httpService: AxiosInstance;
|
||||
@ -24,6 +25,7 @@ export class CreateCompanyService {
|
||||
async createCompanies(
|
||||
domainNames: string[],
|
||||
workspaceId: string,
|
||||
companyDomainNameColumnName: string,
|
||||
transactionManager?: EntityManager,
|
||||
): Promise<{
|
||||
[domainName: string]: string;
|
||||
@ -38,6 +40,7 @@ export class CreateCompanyService {
|
||||
await this.companyRepository.getExistingCompaniesByDomainNames(
|
||||
uniqueDomainNames,
|
||||
workspaceId,
|
||||
companyDomainNameColumnName,
|
||||
transactionManager,
|
||||
);
|
||||
|
||||
@ -61,7 +64,7 @@ export class CreateCompanyService {
|
||||
(domainName) =>
|
||||
!existingCompanies.some(
|
||||
(company: { domainName: string }) =>
|
||||
company.domainName === domainName,
|
||||
getCompanyDomainName(company) === domainName,
|
||||
),
|
||||
);
|
||||
|
||||
@ -69,6 +72,7 @@ export class CreateCompanyService {
|
||||
companiesObject[domainName] = await this.createCompany(
|
||||
domainName,
|
||||
workspaceId,
|
||||
companyDomainNameColumnName,
|
||||
transactionManager,
|
||||
);
|
||||
}
|
||||
@ -79,6 +83,7 @@ export class CreateCompanyService {
|
||||
private async createCompany(
|
||||
domainName: string,
|
||||
workspaceId: string,
|
||||
companyDomainNameColumnName,
|
||||
transactionManager?: EntityManager,
|
||||
): Promise<string> {
|
||||
const companyId = v4();
|
||||
@ -93,6 +98,7 @@ export class CreateCompanyService {
|
||||
name,
|
||||
city,
|
||||
},
|
||||
companyDomainNameColumnName,
|
||||
transactionManager,
|
||||
);
|
||||
|
||||
|
||||
@ -17,6 +17,7 @@ export class ViewService {
|
||||
fieldId,
|
||||
viewsIds,
|
||||
positions,
|
||||
size,
|
||||
}: {
|
||||
workspaceId: string;
|
||||
fieldId: string;
|
||||
@ -24,6 +25,7 @@ export class ViewService {
|
||||
positions?: {
|
||||
[key: string]: number;
|
||||
}[];
|
||||
size?: number;
|
||||
}) {
|
||||
const viewFieldRepository =
|
||||
await this.twentyORMGlobalManager.getRepositoryForWorkspace(
|
||||
@ -51,6 +53,7 @@ export class ViewService {
|
||||
fieldMetadataId: fieldId,
|
||||
isVisible: true,
|
||||
...(isDefined(position) && { position: position }),
|
||||
...(isDefined(size) && { size: size }),
|
||||
});
|
||||
|
||||
await viewFieldRepository.save(newViewField);
|
||||
|
||||
13
packages/twenty-server/src/utils/getCompanyDomainName.ts
Normal file
13
packages/twenty-server/src/utils/getCompanyDomainName.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { isDefined } from 'class-validator';
|
||||
|
||||
// temporary, to remove once domainName has been fully migrated to Links type
|
||||
export const getCompanyDomainName = (company: any) => {
|
||||
if (!isDefined(company.domainName)) {
|
||||
return company.domainName;
|
||||
}
|
||||
if (typeof company.domainName === 'string') {
|
||||
return company.domainName;
|
||||
} else {
|
||||
return company.domainName.primaryLinkUrl;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user