feat: manually implement joinColumn (#6022)

This PR introduce a new decorator named `@WorkspaceJoinColumn`, the goal
of this one is to manually declare the join columns inside the workspace
entities, so we don't have to rely on `ObjectRecord` type.

This decorator can be used that way:

```typescript
  @WorkspaceRelation({
    standardId: ACTIVITY_TARGET_STANDARD_FIELD_IDS.company,
    type: RelationMetadataType.MANY_TO_ONE,
    label: 'Company',
    description: 'ActivityTarget company',
    icon: 'IconBuildingSkyscraper',
    inverseSideTarget: () => CompanyWorkspaceEntity,
    inverseSideFieldKey: 'activityTargets',
  })
  @WorkspaceIsNullable()
  company: Relation<CompanyWorkspaceEntity> | null;

  // The argument is the name of the relation above
  @WorkspaceJoinColumn('company')
  companyId: string | null;
```
This commit is contained in:
Jérémy M
2024-06-27 11:41:22 +02:00
committed by GitHub
parent 7eb69a78ef
commit 95c5602a4e
64 changed files with 427 additions and 243 deletions

View File

@ -70,12 +70,9 @@ export class BlocklistItemDeleteCalendarEventsJob {
const calendarChannels = await this.calendarChannelRepository.find({
where: {
connectedAccount: {
accountOwner: {
id: workspaceMemberId,
},
accountOwnerId: workspaceMemberId,
},
},
relations: ['connectedAccount.accountOwner'],
});
const calendarChannelIds = calendarChannels.map(({ id }) => id);
@ -88,9 +85,7 @@ export class BlocklistItemDeleteCalendarEventsJob {
handle: isHandleDomain ? ILike(`%${handle}`) : handle,
},
calendarChannelEventAssociations: {
calendarChannel: {
id: Any(calendarChannelIds),
},
calendarChannelId: Any(calendarChannelIds),
},
},
});

View File

@ -72,9 +72,7 @@ export class CalendarCreateCompanyAndContactAfterSyncJob {
where: {
calendarEvent: {
calendarChannelEventAssociations: {
calendarChannel: {
id: calendarChannelId,
},
calendarChannelId,
},
calendarEventParticipants: {
person: IsNull(),

View File

@ -10,7 +10,6 @@ import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/work
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-event-participant.workspace-entity';
import { TimelineActivityRepository } from 'src/modules/timeline/repositiories/timeline-activity.repository';
import { TimelineActivityWorkspaceEntity } from 'src/modules/timeline/standard-objects/timeline-activity.workspace-entity';
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
@Injectable()
export class CalendarEventParticipantListener {
@ -26,7 +25,7 @@ export class CalendarEventParticipantListener {
public async handleCalendarEventParticipantMatchedEvent(payload: {
workspaceId: string;
workspaceMemberId: string;
calendarEventParticipants: ObjectRecord<CalendarEventParticipantWorkspaceEntity>[];
calendarEventParticipants: CalendarEventParticipantWorkspaceEntity[];
}): Promise<void> {
const calendarEventParticipants = payload.calendarEventParticipants ?? [];

View File

@ -34,9 +34,7 @@ export class CalendarEventFindManyPreQueryHook
const calendarChannelCalendarEventAssociations =
await this.calendarChannelEventAssociationRepository.find({
where: {
calendarEvent: {
id: payload?.filter?.id?.eq,
},
calendarEventId: payload?.filter?.id?.eq,
},
relations: ['calendarChannel.connectedAccount'],
});

View File

@ -35,9 +35,7 @@ export class CalendarEventFindOnePreQueryHook
const calendarChannelCalendarEventAssociations =
await this.calendarChannelEventAssociationRepository.find({
where: {
calendarEvent: {
id: payload?.filter?.id?.eq,
},
calendarEventId: payload?.filter?.id?.eq,
},
relations: ['calendarChannel.connectedAccount'],
});

View File

@ -11,7 +11,6 @@ import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/c
import { CalendarEventParticipant } from 'src/modules/calendar/types/calendar-event';
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-event-participant.workspace-entity';
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
@ -28,10 +27,10 @@ export class CalendarEventParticipantService {
) {}
public async updateCalendarEventParticipantsAfterPeopleCreation(
createdPeople: ObjectRecord<PersonWorkspaceEntity>[],
createdPeople: PersonWorkspaceEntity[],
workspaceId: string,
transactionManager?: EntityManager,
): Promise<ObjectRecord<CalendarEventParticipantWorkspaceEntity>[]> {
): Promise<CalendarEventParticipantWorkspaceEntity[]> {
const participants = await this.calendarEventParticipantRepository.find({
where: {
handle: Any(createdPeople.map((person) => person.email)),
@ -88,7 +87,7 @@ export class CalendarEventParticipantService {
calendarEventParticipants: CalendarEventParticipant[],
workspaceId: string,
transactionManager?: EntityManager,
): Promise<ObjectRecord<CalendarEventParticipantWorkspaceEntity>[]> {
): Promise<CalendarEventParticipantWorkspaceEntity[]> {
if (calendarEventParticipants.length === 0) {
return [];
}

View File

@ -37,7 +37,6 @@ import {
} from 'src/modules/connected-account/auto-companies-and-contacts-creation/jobs/create-company-and-contact.job';
import { InjectWorkspaceRepository } from 'src/engine/twenty-orm/decorators/inject-workspace-repository.decorator';
import { WorkspaceRepository } from 'src/engine/twenty-orm/repository/workspace.repository';
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
import { isDefined } from 'src/utils/is-defined';
import { WorkspaceDataSource } from 'src/engine/twenty-orm/datasource/workspace.datasource';
import { InjectWorkspaceDatasource } from 'src/engine/twenty-orm/decorators/inject-workspace-datasource.decorator';
@ -416,7 +415,7 @@ export class GoogleCalendarSyncService {
eventExternalId: string;
calendarChannelId: string;
}[],
connectedAccount: ObjectRecord<ConnectedAccountWorkspaceEntity>,
connectedAccount: ConnectedAccountWorkspaceEntity,
calendarChannel: CalendarChannelWorkspaceEntity,
workspaceId: string,
): Promise<void> {
@ -431,7 +430,7 @@ export class GoogleCalendarSyncService {
let startTime: number;
let endTime: number;
const savedCalendarEventParticipantsToEmit: ObjectRecord<CalendarEventParticipantWorkspaceEntity>[] =
const savedCalendarEventParticipantsToEmit: CalendarEventParticipantWorkspaceEntity[] =
[];
try {
@ -496,13 +495,11 @@ export class GoogleCalendarSyncService {
const existingCalendarEventParticipants =
await this.calendarEventParticipantsRepository.find({
where: {
calendarEvent: {
id: Any(
participantsToUpdate
.map((participant) => participant.calendarEventId)
.filter(isDefined),
),
},
calendarEventId: Any(
participantsToUpdate
.map((participant) => participant.calendarEventId)
.filter(isDefined),
),
},
});

View File

@ -12,6 +12,7 @@ import { RelationMetadataType } from 'src/engine/metadata-modules/relation-metad
import { WorkspaceField } from 'src/engine/twenty-orm/decorators/workspace-field.decorator';
import { BaseWorkspaceEntity } from 'src/engine/twenty-orm/base.workspace-entity';
import { CalendarChannelWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-channel.workspace-entity';
import { WorkspaceJoinColumn } from 'src/engine/twenty-orm/decorators/workspace-join-column.decorator';
@WorkspaceEntity({
standardId: STANDARD_OBJECT_IDS.calendarChannelEventAssociation,
@ -41,12 +42,14 @@ export class CalendarChannelEventAssociationWorkspaceEntity extends BaseWorkspac
label: 'Channel ID',
description: 'Channel ID',
icon: 'IconCalendar',
joinColumn: 'calendarChannelId',
inverseSideTarget: () => CalendarChannelWorkspaceEntity,
inverseSideFieldKey: 'calendarChannelEventAssociations',
})
calendarChannel: Relation<CalendarChannelWorkspaceEntity>;
@WorkspaceJoinColumn('calendarChannel')
calendarChannelId: string;
@WorkspaceRelation({
standardId:
CALENDAR_CHANNEL_EVENT_ASSOCIATION_STANDARD_FIELD_IDS.calendarEvent,
@ -54,9 +57,11 @@ export class CalendarChannelEventAssociationWorkspaceEntity extends BaseWorkspac
label: 'Event ID',
description: 'Event ID',
icon: 'IconCalendar',
joinColumn: 'calendarEventId',
inverseSideTarget: () => CalendarEventWorkspaceEntity,
inverseSideFieldKey: 'calendarChannelEventAssociations',
})
calendarEvent: Relation<CalendarEventWorkspaceEntity>;
@WorkspaceJoinColumn('calendarEvent')
calendarEventId: string;
}

View File

@ -16,6 +16,7 @@ import { WorkspaceIsNotAuditLogged } from 'src/engine/twenty-orm/decorators/work
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';
import { WorkspaceJoinColumn } from 'src/engine/twenty-orm/decorators/workspace-join-column.decorator';
export enum CalendarChannelVisibility {
METADATA = 'METADATA',
@ -231,12 +232,12 @@ export class CalendarChannelWorkspaceEntity extends BaseWorkspaceEntity {
label: 'Connected Account',
description: 'Connected Account',
icon: 'IconUserCircle',
joinColumn: 'connectedAccountId',
inverseSideTarget: () => ConnectedAccountWorkspaceEntity,
inverseSideFieldKey: 'calendarChannels',
})
connectedAccount: Relation<ConnectedAccountWorkspaceEntity>;
@WorkspaceJoinColumn('connectedAccount')
connectedAccountId: string;
@WorkspaceRelation({

View File

@ -14,6 +14,7 @@ import { WorkspaceField } from 'src/engine/twenty-orm/decorators/workspace-field
import { WorkspaceRelation } from 'src/engine/twenty-orm/decorators/workspace-relation.decorator';
import { RelationMetadataType } from 'src/engine/metadata-modules/relation-metadata/relation-metadata.entity';
import { WorkspaceIsNullable } from 'src/engine/twenty-orm/decorators/workspace-is-nullable.decorator';
import { WorkspaceJoinColumn } from 'src/engine/twenty-orm/decorators/workspace-join-column.decorator';
export enum CalendarEventParticipantResponseStatus {
NEEDS_ACTION = 'NEEDS_ACTION',
@ -103,35 +104,41 @@ export class CalendarEventParticipantWorkspaceEntity extends BaseWorkspaceEntity
label: 'Event ID',
description: 'Event ID',
icon: 'IconCalendar',
joinColumn: 'calendarEventId',
inverseSideTarget: () => CalendarEventWorkspaceEntity,
inverseSideFieldKey: 'calendarEventParticipants',
})
calendarEvent: Relation<CalendarEventWorkspaceEntity>;
@WorkspaceJoinColumn('calendarEvent')
calendarEventId: string;
@WorkspaceRelation({
standardId: CALENDAR_EVENT_PARTICIPANT_STANDARD_FIELD_IDS.person,
type: RelationMetadataType.MANY_TO_ONE,
label: 'Person',
description: 'Person',
icon: 'IconUser',
joinColumn: 'personId',
inverseSideTarget: () => PersonWorkspaceEntity,
inverseSideFieldKey: 'calendarEventParticipants',
})
@WorkspaceIsNullable()
person: Relation<PersonWorkspaceEntity> | null;
@WorkspaceJoinColumn('person')
personId: string | null;
@WorkspaceRelation({
standardId: CALENDAR_EVENT_PARTICIPANT_STANDARD_FIELD_IDS.workspaceMember,
type: RelationMetadataType.MANY_TO_ONE,
label: 'Workspace Member',
description: 'Workspace Member',
icon: 'IconUser',
joinColumn: 'workspaceMemberId',
inverseSideTarget: () => WorkspaceMemberWorkspaceEntity,
inverseSideFieldKey: 'calendarEventParticipants',
})
@WorkspaceIsNullable()
workspaceMember: Relation<WorkspaceMemberWorkspaceEntity> | null;
@WorkspaceJoinColumn('workspaceMember')
workspaceMemberId: string | null;
}

View File

@ -1,9 +1,8 @@
import { CalendarEventParticipantWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-event-participant.workspace-entity';
import { CalendarEventWorkspaceEntity } from 'src/modules/calendar/standard-objects/calendar-event.workspace-entity';
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
export type CalendarEvent = Omit<
ObjectRecord<CalendarEventWorkspaceEntity>,
CalendarEventWorkspaceEntity,
| 'createdAt'
| 'updatedAt'
| 'calendarChannelEventAssociations'
@ -15,7 +14,7 @@ export type CalendarEvent = Omit<
};
export type CalendarEventParticipant = Omit<
ObjectRecord<CalendarEventParticipantWorkspaceEntity>,
CalendarEventParticipantWorkspaceEntity,
| 'id'
| 'createdAt'
| 'updatedAt'