From e0918c89c1a2ec0dcbb1d6c1c95774a1d3a418a6 Mon Sep 17 00:00:00 2001 From: Weiko Date: Fri, 5 Apr 2024 15:55:12 +0200 Subject: [PATCH] Fix contact creation when calendar is not enabled (#4843) ## Context Calendar tables are behind a featureFlag, they do not exist if the feature flag is off which means we should not use them for the same reason. I'm adding a check on the featureFlag before calling the repository. ``` Error executing raw query for workspace 20202020-1c25-4d02-bf25-6aeccf7ea419: relation "workspace_1wgvd1injqtife6y4rvfbu3h5.calendarEventParticipant" does not exist ``` ## Test locally with and without featureflag --- ...-companies-and-contacts-creation.module.ts | 3 +++ .../create-company-and-contact.service.ts | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module.ts b/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module.ts index 64a5d5b3e..9acc75978 100644 --- a/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module.ts +++ b/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/auto-companies-and-contacts-creation.module.ts @@ -1,4 +1,5 @@ import { Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; import { CreateCompanyAndContactService } from 'src/modules/connected-account/auto-companies-and-contacts-creation/services/create-company-and-contact.service'; import { CreateCompanyModule } from 'src/modules/connected-account/auto-companies-and-contacts-creation/create-company/create-company.module'; @@ -11,6 +12,7 @@ import { WorkspaceDataSourceModule } from 'src/engine/workspace-datasource/works import { CreateCompanyAndContactListener } from 'src/modules/connected-account/auto-companies-and-contacts-creation/listeners/create-company-and-contact.listener'; import { CalendarEventParticipantObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-participant.object-metadata'; import { CalendarEventParticipantModule } from 'src/modules/calendar/services/calendar-event-participant/calendar-event-participant.module'; +import { FeatureFlagEntity } from 'src/engine/core-modules/feature-flag/feature-flag.entity'; @Module({ imports: [ @@ -24,6 +26,7 @@ import { CalendarEventParticipantModule } from 'src/modules/calendar/services/ca MessageParticipantModule, WorkspaceDataSourceModule, CalendarEventParticipantModule, + TypeOrmModule.forFeature([FeatureFlagEntity], 'core'), ], providers: [CreateCompanyAndContactService, CreateCompanyAndContactListener], exports: [CreateCompanyAndContactService], diff --git a/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/services/create-company-and-contact.service.ts b/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/services/create-company-and-contact.service.ts index 09a4bc473..d6b172e96 100644 --- a/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/services/create-company-and-contact.service.ts +++ b/packages/twenty-server/src/modules/connected-account/auto-companies-and-contacts-creation/services/create-company-and-contact.service.ts @@ -1,6 +1,7 @@ import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; -import { EntityManager } from 'typeorm'; +import { EntityManager, Repository } from 'typeorm'; import compact from 'lodash/compact'; import { getDomainNameFromHandle } from 'src/modules/messaging/utils/get-domain-name-from-handle.util'; @@ -22,6 +23,10 @@ import { CalendarEventParticipantService } from 'src/modules/calendar/services/c import { CalendarEventParticipantRepository } from 'src/modules/calendar/repositories/calendar-event-participant.repository'; import { CalendarEventParticipantObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-participant.object-metadata'; import { filterOutContactsFromCompanyOrWorkspace } from 'src/modules/connected-account/auto-companies-and-contacts-creation/utils/filter-out-contacts-from-company-or-workspace.util'; +import { + FeatureFlagEntity, + FeatureFlagKeys, +} from 'src/engine/core-modules/feature-flag/feature-flag.entity'; @Injectable() export class CreateCompanyAndContactService { @@ -39,6 +44,8 @@ export class CreateCompanyAndContactService { @InjectObjectMetadataRepository(CalendarEventParticipantObjectMetadata) private readonly calendarEventParticipantRepository: CalendarEventParticipantRepository, private readonly calendarEventParticipantService: CalendarEventParticipantService, + @InjectRepository(FeatureFlagEntity, 'core') + private readonly featureFlagRepository: Repository, ) {} async createCompaniesAndContacts( @@ -162,6 +169,16 @@ export class CreateCompanyAndContactService { transactionManager, ); + const isCalendarEnabled = await this.featureFlagRepository.findOneBy({ + workspaceId, + key: FeatureFlagKeys.IsCalendarEnabled, + value: true, + }); + + if (!isCalendarEnabled || !isCalendarEnabled.value) { + return; + } + const calendarEventParticipantsWithoutPersonIdAndWorkspaceMemberId = await this.calendarEventParticipantRepository.getWithoutPersonIdAndWorkspaceMemberId( workspaceId,