Skip to content

The open alternative to Salesforce, designed for AI.

4008 dont create a contact company if it matches the persons domain (#4057)

bosiraphael09783912f363
diff --git a/packages/twenty-server/src/workspace/messaging/jobs/create-companies-and-contacts-after-sync.job.ts b/packages/twenty-server/src/workspace/messaging/jobs/create-companies-and-contacts-after-sync.job.ts
index 116c6d22858..a599969d817 100644
--- a/packages/twenty-server/src/workspace/messaging/jobs/create-companies-and-contacts-after-sync.job.ts
+++ b/packages/twenty-server/src/workspace/messaging/jobs/create-companies-and-contacts-after-sync.job.ts
@@ -32,11 +32,12 @@ export class CreateCompaniesAndContactsAfterSyncJob
);
const { workspaceId, messageChannelId } = data;
- const isContactAutoCreationEnabled =
- await this.messageChannelService.getIsContactAutoCreationEnabledByMessageChannelId(
- messageChannelId,
- workspaceId,
- );
+ const messageChannel = await this.messageChannelService.getByIds(
+ [messageChannelId],
+ workspaceId,
+ );
+
+ const { handle, isContactAutoCreationEnabled } = messageChannel[0];
if (!isContactAutoCreationEnabled) {
return;
@@ -49,6 +50,7 @@ export class CreateCompaniesAndContactsAfterSyncJob
);
await this.createCompaniesAndContactsService.createCompaniesAndContacts(
+ handle,
messageParticipantsWithoutPersonIdAndWorkspaceMemberId,
workspaceId,
);
diff --git a/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts b/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts
index 1fb4f6736eb..60e635a8b91 100644
--- a/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts
+++ b/packages/twenty-server/src/workspace/messaging/repositories/message-channel/message-channel.service.ts
@@ -56,23 +56,6 @@ export class MessageChannelService {
return messageChannel.isContactAutoCreationEnabled;
}
- public async getIsContactAutoCreationEnabledByMessageChannelId(
- messageChannelId: string,
- workspaceId: string,
- ): Promise<boolean> {
- const dataSourceSchema =
- this.workspaceDataSourceService.getSchemaName(workspaceId);
-
- const messageChannels =
- await this.workspaceDataSourceService.executeRawQuery(
- `SELECT * FROM ${dataSourceSchema}."messageChannel" WHERE "id" = $1 LIMIT 1`,
- [messageChannelId],
- workspaceId,
- );
-
- return messageChannels[0]?.isContactAutoCreationEnabled;
- }
-
public async getByIds(
ids: string[],
workspaceId: string,
diff --git a/packages/twenty-server/src/workspace/messaging/repositories/message/message.service.ts b/packages/twenty-server/src/workspace/messaging/repositories/message/message.service.ts
index 38e266cb017..0ecd39ba1bc 100644
--- a/packages/twenty-server/src/workspace/messaging/repositories/message/message.service.ts
+++ b/packages/twenty-server/src/workspace/messaging/repositories/message/message.service.ts
@@ -205,6 +205,7 @@ export class MessageService {
if (isContactAutoCreationEnabled && messageDirection === 'outgoing') {
await this.createCompaniesAndContactsService.createCompaniesAndContacts(
+ connectedAccount.handle,
message.participants,
workspaceId,
manager,
diff --git a/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.module.ts b/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.module.ts
index c997b155d78..36970fe7dfc 100644
--- a/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.module.ts
+++ b/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.module.ts
@@ -1,5 +1,6 @@
import { Module } from '@nestjs/common';
+import { PersonModule } from 'src/workspace/messaging/repositories/person/person.module';
import { CreateCompaniesAndContactsService } from 'src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.service';
import { CreateCompanyModule } from 'src/workspace/messaging/services/create-company/create-company.module';
import { CreateContactModule } from 'src/workspace/messaging/services/create-contact/create-contact.module';
@@ -10,6 +11,7 @@ import { WorkspaceDataSourceModule } from 'src/workspace/workspace-datasource/wo
WorkspaceDataSourceModule,
CreateContactModule,
CreateCompanyModule,
+ PersonModule,
],
providers: [CreateCompaniesAndContactsService],
exports: [CreateCompaniesAndContactsService],
diff --git a/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.service.ts b/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.service.ts
index 4ef1d2671d5..2757edd7081 100644
--- a/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.service.ts
+++ b/packages/twenty-server/src/workspace/messaging/services/create-companies-and-contacts/create-companies-and-contacts.service.ts
@@ -2,47 +2,56 @@ import { Injectable } from '@nestjs/common';
import { EntityManager } from 'typeorm';
-import { WorkspaceDataSourceService } from 'src/workspace/workspace-datasource/workspace-datasource.service';
import { Participant } from 'src/workspace/messaging/types/gmail-message';
import { getDomainNameFromHandle } from 'src/workspace/messaging/utils/get-domain-name-from-handle.util';
import { CreateCompanyService } from 'src/workspace/messaging/services/create-company/create-company.service';
import { CreateContactService } from 'src/workspace/messaging/services/create-contact/create-contact.service';
+import { PersonService } from 'src/workspace/messaging/repositories/person/person.service';
@Injectable()
export class CreateCompaniesAndContactsService {
constructor(
- private readonly workspaceDataSourceService: WorkspaceDataSourceService,
+ private readonly personService: PersonService,
private readonly createContactService: CreateContactService,
private readonly createCompaniesService: CreateCompanyService,
) {}
async createCompaniesAndContacts(
+ selfHandle: string,
participants: Participant[],
workspaceId: string,
transactionManager?: EntityManager,
) {
- const dataSourceSchema =
- this.workspaceDataSourceService.getSchemaName(workspaceId);
+ const selfDomainName = getDomainNameFromHandle(selfHandle);
+
+ // TODO: use isWorkEmail so we can create a contact even if the email is a personal email
+ const participantsFromOtherCompanies = participants.filter(
+ (participant) =>
+ getDomainNameFromHandle(participant.handle) !== selfDomainName,
+ );
+
+ if (!participantsFromOtherCompanies.length) {
+ return;
+ }
const uniqueHandles = Array.from(
- new Set(participants.map((participant) => participant.handle)),
+ new Set(
+ participantsFromOtherCompanies.map((participant) => participant.handle),
+ ),
);
const uniqueParticipants = uniqueHandles.map((handle) => {
- const participant = participants.find(
+ const participant = participantsFromOtherCompanies.find(
(participant) => participant.handle === handle,
);
return participant;
}) as Participant[];
- const alreadyCreatedContacts =
- await this.workspaceDataSourceService.executeRawQuery(
- `SELECT email FROM ${dataSourceSchema}."person" WHERE "email" = ANY($1)`,
- [uniqueParticipants.map((participant) => participant.handle)],
- workspaceId,
- transactionManager,
- );
+ const alreadyCreatedContacts = await this.personService.getByEmails(
+ uniqueHandles,
+ workspaceId,
+ );
const alreadyCreatedContactEmails: string[] = alreadyCreatedContacts?.map(
({ email }) => email,