Skip to content

Commit

Permalink
Add custom field types on seed (twentyhq#6505)
Browse files Browse the repository at this point in the history
Fixes twentyhq#6364

---------

Co-authored-by: Weiko <[email protected]>
  • Loading branch information
prateekj117 and Weiko authored Aug 5, 2024
1 parent ed0102e commit 48f4e41
Show file tree
Hide file tree
Showing 5 changed files with 292 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';

import { Command, CommandRunner } from 'nest-commander';
import { EntityManager } from 'typeorm';
import { EntityManager, Repository } from 'typeorm';

import { seedCoreSchema } from 'src/database/typeorm-seeds/core';
import {
SEED_APPLE_WORKSPACE_ID,
SEED_TWENTY_WORKSPACE_ID,
} from 'src/database/typeorm-seeds/core/workspaces';
import {
getDevSeedCompanyCustomFields,
getDevSeedPeopleCustomFields,
} from 'src/database/typeorm-seeds/metadata/fieldsMetadata';
import { seedCalendarChannels } from 'src/database/typeorm-seeds/workspace/calendar-channel';
import { seedCalendarChannelEventAssociations } from 'src/database/typeorm-seeds/workspace/calendar-channel-event-association';
import { seedCalendarEventParticipants } from 'src/database/typeorm-seeds/workspace/calendar-event-participants';
Expand All @@ -31,10 +36,13 @@ import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache
import { InjectCacheStorage } from 'src/engine/integrations/cache-storage/decorators/cache-storage.decorator';
import { CacheStorageNamespace } from 'src/engine/integrations/cache-storage/types/cache-storage-namespace.enum';
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
import { FieldMetadataService } from 'src/engine/metadata-modules/field-metadata/field-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 { WorkspaceCacheVersionService } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.service';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { viewPrefillData } from 'src/engine/workspace-manager/standard-objects-prefill-data/view';
import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync-metadata/constants/standard-object-ids';
import { WorkspaceSyncMetadataService } from 'src/engine/workspace-manager/workspace-sync-metadata/workspace-sync-metadata.service';

// TODO: implement dry-run
Expand All @@ -52,6 +60,9 @@ export class DataSeedWorkspaceCommand extends CommandRunner {
private readonly typeORMService: TypeORMService,
private readonly workspaceSyncMetadataService: WorkspaceSyncMetadataService,
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
private readonly fieldMetadataService: FieldMetadataService,
@InjectRepository(ObjectMetadataEntity, 'metadata')
private readonly objectMetadataRepository: Repository<ObjectMetadataEntity>,
private readonly objectMetadataService: ObjectMetadataService,
@InjectCacheStorage(CacheStorageNamespace.WorkspaceSchema)
private readonly workspaceSchemaCache: CacheStorageService,
Expand Down Expand Up @@ -128,6 +139,15 @@ export class DataSeedWorkspaceCommand extends CommandRunner {

const featureFlags = await featureFlagRepository.find({});

await this.seedCompanyCustomFields(
objectMetadataMap[STANDARD_OBJECT_IDS.company],
workspaceId,
);
await this.seedPeopleCustomFields(
objectMetadataMap[STANDARD_OBJECT_IDS.person],
workspaceId,
);

await workspaceDataSource.transaction(
async (entityManager: EntityManager) => {
await seedCompanies(entityManager, dataSourceMetadata.schema);
Expand Down Expand Up @@ -206,4 +226,55 @@ export class DataSeedWorkspaceCommand extends CommandRunner {
await this.typeORMService.disconnectFromDataSource(dataSourceMetadata.id);
}
}

async seedCompanyCustomFields(
companyObjectMetadata: ObjectMetadataEntity,
workspaceId: string,
) {
const companyObjectMetadataId = companyObjectMetadata?.id;

if (!companyObjectMetadataId) {
throw new Error(
`Company object metadata not found for workspace ${workspaceId}, can't seed custom fields`,
);
}

const DEV_SEED_COMPANY_CUSTOM_FIELDS = getDevSeedCompanyCustomFields(
companyObjectMetadataId,
workspaceId,
);

for (const customField of DEV_SEED_COMPANY_CUSTOM_FIELDS) {
// TODO: Use createMany once implemented for better performances
await this.fieldMetadataService.createOne({
...customField,
isCustom: true,
});
}
}

async seedPeopleCustomFields(
personObjectMetadata: ObjectMetadataEntity,
workspaceId: string,
) {
const personObjectMetadataId = personObjectMetadata?.id;

if (!personObjectMetadataId) {
throw new Error(
`Person object metadata not found for workspace ${workspaceId}, can't seed custom fields`,
);
}

const DEV_SEED_PERSON_CUSTOM_FIELDS = getDevSeedPeopleCustomFields(
personObjectMetadataId,
workspaceId,
);

for (const customField of DEV_SEED_PERSON_CUSTOM_FIELDS) {
await this.fieldMetadataService.createOne({
...customField,
isCustom: true,
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Workspace } from 'src/engine/core-modules/workspace/workspace.entity';
import { WorkspaceModule } from 'src/engine/core-modules/workspace/workspace.module';
import { DataSourceModule } from 'src/engine/metadata-modules/data-source/data-source.module';
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 { ObjectMetadataEntity } from 'src/engine/metadata-modules/object-metadata/object-metadata.entity';
import { ObjectMetadataModule } from 'src/engine/metadata-modules/object-metadata/object-metadata.module';
import { WorkspaceCacheVersionModule } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.module';
Expand All @@ -42,6 +43,7 @@ import { WorkspaceSyncMetadataModule } from 'src/engine/workspace-manager/worksp
WorkspaceSyncMetadataModule,
WorkspaceStatusModule,
ObjectMetadataModule,
FieldMetadataModule,
DataSeedDemoWorkspaceModule,
WorkspaceCacheVersionModule,
UpgradeTo0_23CommandModule,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
import { CreateFieldInput } from 'src/engine/metadata-modules/field-metadata/dtos/create-field.input';
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';

export const getDevSeedCompanyCustomFields = (
objectMetadataId: string,
workspaceId: string,
): CreateFieldInput[] => {
return [
{
workspaceId,
type: FieldMetadataType.TEXT,
name: 'tagline',
label: 'Tagline',
description: "Company's Tagline",
icon: 'IconAdCircle',
isActive: true,
isNullable: false,
objectMetadataId,
},
{
workspaceId,
type: FieldMetadataType.LINKS,
name: 'introVideo',
label: 'Intro Video',
description: "Company's Intro Video",
icon: 'IconVideo',
isActive: true,
isNullable: true,
objectMetadataId,
},
{
workspaceId,
type: FieldMetadataType.MULTI_SELECT,
name: 'workPolicy',
label: 'Work Policy',
description: "Company's Work Policy",
icon: 'IconHome',
isActive: true,
isNullable: true,
objectMetadataId,
options: [
{
color: 'green',
label: 'On-Site',
position: 0,
value: 'ON_SITE',
},
{
color: 'turquoise',
label: 'Hybrid',
position: 1,
value: 'HYBRID',
},
{
color: 'sky',
label: 'Remote Work',
position: 2,
value: 'REMOTE_WORK',
},
],
},
{
workspaceId,
type: FieldMetadataType.BOOLEAN,
name: 'visaSponsorship',
label: 'Visa Sponsorship',
description: "Company's Visa Sponsorship Policy",
icon: 'IconBrandVisa',
isActive: true,
isNullable: true,
objectMetadataId,
defaultValue: false,
},
];
};

export const getDevSeedPeopleCustomFields = (
objectMetadataId: string,
workspaceId: string,
): CreateFieldInput[] => {
return [
{
workspaceId,
type: FieldMetadataType.TEXT,
name: 'intro',
label: 'Intro',
description: "Contact's Intro",
icon: 'IconNote',
isActive: true,
isNullable: true,
objectMetadataId,
},
{
workspaceId,
type: FieldMetadataType.PHONE,
name: 'whatsapp',
label: 'Whatsapp',
description: "Contact's Whatsapp Number",
icon: 'IconBrandWhatsapp',
isActive: true,
isNullable: false,
objectMetadataId,
},
{
workspaceId,
type: FieldMetadataType.MULTI_SELECT,
name: 'workPrefereance',
label: 'Work Preference',
description: "Person's Work Preference",
icon: 'IconHome',
isActive: true,
isNullable: true,
objectMetadataId,
options: [
{
color: 'green',
label: 'On-Site',
position: 0,
value: 'ON_SITE',
},
{
color: 'turquoise',
label: 'Hybrid',
position: 1,
value: 'HYBRID',
},
{
color: 'sky',
label: 'Remote Work',
position: 2,
value: 'REMOTE_WORK',
},
],
},
{
workspaceId,
type: FieldMetadataType.RATING,
name: 'performanceRating',
label: 'Performance Rating',
description: "Person's Performance Rating",
icon: 'IconStars',
isActive: true,
isNullable: true,
objectMetadataId,
options: [
{
label: '1',
value: 'RATING_1',
position: 0,
},
{
label: '2',
value: 'RATING_2',
position: 1,
},
{
label: '3',
value: 'RATING_3',
position: 2,
},
{
label: '4',
value: 'RATING_4',
position: 3,
},
{
label: '5',
value: 'RATING_5',
position: 4,
},
],
},
];
};
Loading

0 comments on commit 48f4e41

Please sign in to comment.