-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add label identifier to object decorator (#6227)
## Context LabelIdentifier and ImageIdentifier are metadata info attached to objectMetadata that are used to display a record in a more readable way. Those columns point to existing fields that are part of the object. For example, for a relation picker of a person, we will show a record using the "name" labelIdentifier and the "avatarUrl" imageIdentifier. <img width="215" alt="Screenshot 2024-07-11 at 18 45 51" src="https://github.com/twentyhq/twenty/assets/1834158/488f8294-0d7c-4209-b763-2499716ef29d"> Currently, the FE has a specific logic for company and people objects and we have a way to update this value via the API for custom objects, but the code is not flexible enough to change other standard objects. This PR updates the WorkspaceEntity API so we can now provide the labelIdentifier and imageIdentifier in the WorkspaceEntity decorator. Example: ```typescript @WorkspaceEntity({ standardId: STANDARD_OBJECT_IDS.activity, namePlural: 'activities', labelSingular: 'Activity', labelPlural: 'Activities', description: 'An activity', icon: 'IconCheckbox', labelIdentifierStandardId: ACTIVITY_STANDARD_FIELD_IDS.title, }) @WorkspaceIsSystem() export class ActivityWorkspaceEntity extends BaseWorkspaceEntity { @WorkspaceField({ standardId: ACTIVITY_STANDARD_FIELD_IDS.title, type: FieldMetadataType.TEXT, label: 'Title', description: 'Activity title', icon: 'IconNotes', }) title: string; ... ```
- Loading branch information
Showing
29 changed files
with
351 additions
and
106 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...wenty-server/src/database/typeorm/metadata/migrations/1721057142509-fixIdentifierTypes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class FixIdentifierTypes1721057142509 implements MigrationInterface { | ||
name = 'FixIdentifierTypes1721057142509'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "metadata"."objectMetadata" ALTER COLUMN "labelIdentifierFieldMetadataId" TYPE uuid USING "labelIdentifierFieldMetadataId"::uuid`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "metadata"."objectMetadata" ALTER COLUMN "imageIdentifierFieldMetadataId" TYPE uuid USING "imageIdentifierFieldMetadataId"::uuid`, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "metadata"."objectMetadata" ALTER COLUMN "labelIdentifierFieldMetadataId" TYPE text USING "labelIdentifierFieldMetadataId"::text`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "metadata"."objectMetadata" ALTER COLUMN "imageIdentifierFieldMetadataId" TYPE text USING "imageIdentifierFieldMetadataId"::text`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
...er/workspace-sync-metadata/services/workspace-sync-object-metadata-identifiers.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
|
||
import { EntityManager, Repository } from 'typeorm'; | ||
|
||
import { FeatureFlagMap } from 'src/engine/core-modules/feature-flag/interfaces/feature-flag-map.interface'; | ||
import { WorkspaceSyncContext } from 'src/engine/workspace-manager/workspace-sync-metadata/interfaces/workspace-sync-context.interface'; | ||
|
||
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 { 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 { WorkspaceSyncStorage } from 'src/engine/workspace-manager/workspace-sync-metadata/storage/workspace-sync.storage'; | ||
import { mapObjectMetadataByUniqueIdentifier } from 'src/engine/workspace-manager/workspace-sync-metadata/utils/sync-metadata.util'; | ||
|
||
@Injectable() | ||
export class WorkspaceSyncObjectMetadataIdentifiersService { | ||
private readonly logger = new Logger( | ||
WorkspaceSyncObjectMetadataIdentifiersService.name, | ||
); | ||
|
||
constructor(private readonly standardObjectFactory: StandardObjectFactory) {} | ||
|
||
async synchronize( | ||
context: WorkspaceSyncContext, | ||
manager: EntityManager, | ||
_storage: WorkspaceSyncStorage, | ||
workspaceFeatureFlagsMap: FeatureFlagMap, | ||
): Promise<void> { | ||
const objectMetadataRepository = | ||
manager.getRepository(ObjectMetadataEntity); | ||
|
||
const originalObjectMetadataCollection = | ||
await this.getOriginalObjectMetadataCollection( | ||
context.workspaceId, | ||
objectMetadataRepository, | ||
); | ||
|
||
const standardObjectMetadataMap = this.createStandardObjectMetadataMap( | ||
context, | ||
workspaceFeatureFlagsMap, | ||
); | ||
|
||
await this.processObjectMetadataCollection( | ||
originalObjectMetadataCollection, | ||
standardObjectMetadataMap, | ||
objectMetadataRepository, | ||
); | ||
} | ||
|
||
private async getOriginalObjectMetadataCollection( | ||
workspaceId: string, | ||
objectMetadataRepository: Repository<ObjectMetadataEntity>, | ||
): Promise<ObjectMetadataEntity[]> { | ||
return await objectMetadataRepository.find({ | ||
where: { workspaceId, isCustom: false }, | ||
relations: ['fields'], | ||
}); | ||
} | ||
|
||
private createStandardObjectMetadataMap( | ||
context: WorkspaceSyncContext, | ||
workspaceFeatureFlagsMap: FeatureFlagMap, | ||
): Record<string, any> { | ||
const standardObjectMetadataCollection = this.standardObjectFactory.create( | ||
standardObjectMetadataDefinitions, | ||
context, | ||
workspaceFeatureFlagsMap, | ||
); | ||
|
||
return mapObjectMetadataByUniqueIdentifier( | ||
standardObjectMetadataCollection, | ||
); | ||
} | ||
|
||
private async processObjectMetadataCollection( | ||
originalObjectMetadataCollection: ObjectMetadataEntity[], | ||
standardObjectMetadataMap: Record<string, any>, | ||
objectMetadataRepository: Repository<ObjectMetadataEntity>, | ||
): Promise<void> { | ||
for (const objectMetadata of originalObjectMetadataCollection) { | ||
const objectStandardId = objectMetadata.standardId; | ||
|
||
if (!objectStandardId) { | ||
throw new Error( | ||
`Object ${objectMetadata.nameSingular} is missing standardId`, | ||
); | ||
} | ||
|
||
const labelIdentifierFieldMetadata = this.findIdentifierFieldMetadata( | ||
objectMetadata, | ||
objectStandardId, | ||
standardObjectMetadataMap, | ||
'labelIdentifierStandardId', | ||
); | ||
|
||
const imageIdentifierFieldMetadata = this.findIdentifierFieldMetadata( | ||
objectMetadata, | ||
objectStandardId, | ||
standardObjectMetadataMap, | ||
'imageIdentifierStandardId', | ||
); | ||
|
||
this.validateFieldMetadata( | ||
objectMetadata, | ||
labelIdentifierFieldMetadata, | ||
imageIdentifierFieldMetadata, | ||
); | ||
|
||
// TODO: Add image identifier field metadata | ||
await objectMetadataRepository.save({ | ||
...objectMetadata, | ||
labelIdentifierFieldMetadataId: | ||
labelIdentifierFieldMetadata?.id ?? null, | ||
}); | ||
} | ||
} | ||
|
||
private findIdentifierFieldMetadata( | ||
objectMetadata: ObjectMetadataEntity, | ||
objectStandardId: string, | ||
standardObjectMetadataMap: Record<string, any>, | ||
standardIdFieldName: string, | ||
): FieldMetadataEntity | undefined { | ||
const identifierFieldMetadata = objectMetadata.fields.find( | ||
(field) => | ||
field.standardId === | ||
standardObjectMetadataMap[objectStandardId][standardIdFieldName], | ||
); | ||
|
||
if ( | ||
!identifierFieldMetadata && | ||
standardObjectMetadataMap[objectStandardId][standardIdFieldName] | ||
) { | ||
throw new Error( | ||
`Identifier field for object ${objectMetadata.nameSingular} does not exist`, | ||
); | ||
} | ||
|
||
return identifierFieldMetadata; | ||
} | ||
|
||
private validateFieldMetadata( | ||
objectMetadata: ObjectMetadataEntity, | ||
labelIdentifierFieldMetadata: FieldMetadataEntity | undefined, | ||
imageIdentifierFieldMetadata: FieldMetadataEntity | undefined, | ||
): void { | ||
if ( | ||
labelIdentifierFieldMetadata && | ||
![ | ||
FieldMetadataType.UUID, | ||
FieldMetadataType.TEXT, | ||
FieldMetadataType.FULL_NAME, | ||
].includes(labelIdentifierFieldMetadata.type) | ||
) { | ||
throw new Error( | ||
`Label identifier field for object ${objectMetadata.nameSingular} has invalid type ${labelIdentifierFieldMetadata.type}`, | ||
); | ||
} | ||
|
||
if (imageIdentifierFieldMetadata) { | ||
throw new Error( | ||
`Image identifier field for object ${objectMetadata.nameSingular} are not supported yet.`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.