Files
twenty/packages/twenty-server/src/modules/connected-account/standard-objects/connected-account.workspace-entity.ts
T
Jérémy M 8b5f79ddbf fix: multiple twenty orm issues & show an example of use (#5439)
This PR is fixing some issues and adding enhancement in TwentyORM:

- [x] Composite fields in nested relations are not formatted properly
- [x] Passing operators like `Any` in `where` condition is breaking the
query
- [x] Ability to auto load workspace-entities based on a regex path

I've also introduced an example of use for `CalendarEventService`:


https://github.com/twentyhq/twenty/pull/5439/files#diff-3a7dffc0dea57345d10e70c648e911f98fe237248bcea124dafa9c8deb1db748R15
2024-05-20 11:01:47 +02:00

125 lines
4.8 KiB
TypeScript

import { Relation } from 'src/engine/workspace-manager/workspace-sync-metadata/interfaces/relation.interface';
import { FieldMetadataType } from 'src/engine/metadata-modules/field-metadata/field-metadata.entity';
import {
RelationMetadataType,
RelationOnDeleteAction,
} from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
import { CONNECTED_ACCOUNT_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 { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-channel.workspace-entity';
import { MessageChannelWorkspaceEntity } from 'src/modules/messaging/standard-objects/message-channel.workspace-entity';
import { WorkspaceMemberWorkspaceEntity } from 'src/modules/workspace-member/standard-objects/workspace-member.workspace-entity';
import { BaseWorkspaceEntity } from 'src/engine/twenty-orm/base.workspace-entity';
import { WorkspaceEntity } from 'src/engine/twenty-orm/decorators/workspace-entity.decorator';
import { WorkspaceIsSystem } from 'src/engine/twenty-orm/decorators/workspace-is-system.decorator';
import { WorkspaceIsNotAuditLogged } from 'src/engine/twenty-orm/decorators/workspace-is-not-audit-logged.decorator';
import { WorkspaceField } from 'src/engine/twenty-orm/decorators/workspace-field.decorator';
import { WorkspaceRelation } from 'src/engine/twenty-orm/decorators/workspace-relation.decorator';
import { WorkspaceIsNullable } from 'src/engine/twenty-orm/decorators/workspace-is-nullable.decorator';
export enum ConnectedAccountProvider {
GOOGLE = 'google',
}
@WorkspaceEntity({
standardId: STANDARD_OBJECT_IDS.connectedAccount,
namePlural: 'connectedAccounts',
labelSingular: 'Connected Account',
labelPlural: 'Connected Accounts',
description: 'A connected account',
icon: 'IconAt',
})
@WorkspaceIsSystem()
@WorkspaceIsNotAuditLogged()
export class ConnectedAccountWorkspaceEntity extends BaseWorkspaceEntity {
@WorkspaceField({
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.handle,
type: FieldMetadataType.TEXT,
label: 'handle',
description: 'The account handle (email, username, phone number, etc.)',
icon: 'IconMail',
})
handle: string;
@WorkspaceField({
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.provider,
type: FieldMetadataType.TEXT,
label: 'provider',
description: 'The account provider',
icon: 'IconSettings',
})
provider: ConnectedAccountProvider; // field metadata should be a SELECT
@WorkspaceField({
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.accessToken,
type: FieldMetadataType.TEXT,
label: 'Access Token',
description: 'Messaging provider access token',
icon: 'IconKey',
})
accessToken: string;
@WorkspaceField({
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.refreshToken,
type: FieldMetadataType.TEXT,
label: 'Refresh Token',
description: 'Messaging provider refresh token',
icon: 'IconKey',
})
refreshToken: string;
@WorkspaceField({
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.lastSyncHistoryId,
type: FieldMetadataType.TEXT,
label: 'Last sync history ID',
description: 'Last sync history ID',
icon: 'IconHistory',
})
lastSyncHistoryId: string;
@WorkspaceField({
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.authFailedAt,
type: FieldMetadataType.DATE_TIME,
label: 'Auth failed at',
description: 'Auth failed at',
icon: 'IconX',
})
@WorkspaceIsNullable()
authFailedAt: Date;
@WorkspaceRelation({
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.accountOwner,
type: RelationMetadataType.MANY_TO_ONE,
label: 'Account Owner',
description: 'Account Owner',
icon: 'IconUserCircle',
joinColumn: 'accountOwnerId',
inverseSideTarget: () => WorkspaceMemberWorkspaceEntity,
inverseSideFieldKey: 'connectedAccounts',
})
accountOwner: Relation<WorkspaceMemberWorkspaceEntity>;
@WorkspaceRelation({
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.messageChannels,
type: RelationMetadataType.ONE_TO_MANY,
label: 'Message Channels',
description: 'Message Channels',
icon: 'IconMessage',
inverseSideTarget: () => MessageChannelWorkspaceEntity,
onDelete: RelationOnDeleteAction.CASCADE,
})
messageChannels: Relation<MessageChannelWorkspaceEntity[]>;
@WorkspaceRelation({
standardId: CONNECTED_ACCOUNT_STANDARD_FIELD_IDS.calendarChannels,
type: RelationMetadataType.ONE_TO_MANY,
label: 'Calendar Channels',
description: 'Calendar Channels',
icon: 'IconCalendar',
inverseSideTarget: () => CalendarChannelWorkspaceEntity,
onDelete: RelationOnDeleteAction.CASCADE,
})
calendarChannels: Relation<CalendarChannelWorkspaceEntity[]>;
}