3808 auto creation of company when importing emails (#3881)

* create service

* wip

* use raw queries

* creating companies is working

* Fix participant handle domain name extraction

* Add HTTP service to fetch company info from domain name

* Handle 404

* Fix missing parameter in INSERT query

* wip

* renaming

* Add typing
This commit is contained in:
bosiraphael 2024-02-09 15:28:35 +01:00 committed by GitHub
parent a8cb4dc2f4
commit 11d1c4c161
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 82 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import { EnvironmentModule } from 'src/integrations/environment/environment.modu
import { MessagingPersonListener } from 'src/workspace/messaging/listeners/messaging-person.listener';
import { MessageModule } from 'src/workspace/messaging/message/message.module';
import { GmailClientProvider } from 'src/workspace/messaging/providers/gmail/gmail-client.provider';
import { CreateCompanyService } from 'src/workspace/messaging/services/create-company.service';
import { FetchMessagesByBatchesService } from 'src/workspace/messaging/services/fetch-messages-by-batches.service';
import { GmailFullSyncService } from 'src/workspace/messaging/services/gmail-full-sync.service';
import { GmailPartialSyncService } from 'src/workspace/messaging/services/gmail-partial-sync.service';
@ -35,6 +36,7 @@ import { MessagingWorkspaceMemberListener } from 'src/workspace/messaging/listen
GmailRefreshAccessTokenService,
MessagingUtilsService,
GmailClientProvider,
CreateCompanyService,
MessagingPersonListener,
MessagingWorkspaceMemberListener,
],

View File

@ -0,0 +1,65 @@
import { Injectable } from '@nestjs/common';
import { EntityManager } from 'typeorm';
import axios, { AxiosInstance } from 'axios';
import { v4 } from 'uuid';
import { DataSourceEntity } from 'src/metadata/data-source/data-source.entity';
import { capitalize } from 'src/utils/capitalize';
@Injectable()
export class CreateCompanyService {
private readonly httpService: AxiosInstance;
constructor() {
this.httpService = axios.create({
baseURL: 'https://companies.twenty.com',
});
}
async createCompanyFromDomainName(
domainName: string,
dataSourceMetadata: DataSourceEntity,
manager: EntityManager,
): Promise<string> {
const existingCompany = await manager.query(
`SELECT * FROM ${dataSourceMetadata.schema}.company WHERE "domainName" = '${domainName}'`,
);
if (existingCompany.length > 0) {
return existingCompany[0].id;
}
const companyId = v4();
const { name, city } = await this.getCompanyInfoFromDomainName(domainName);
await manager.query(
`INSERT INTO ${dataSourceMetadata.schema}.company (id, name, "domainName", address)
VALUES ($1, $2, $3, $4)`,
[companyId, name, domainName, city],
);
return companyId;
}
async getCompanyInfoFromDomainName(domainName: string): Promise<{
name: string;
city: string;
}> {
try {
const response = await this.httpService.get(`/${domainName}`);
const data = response.data;
return {
name: data.name,
city: data.city,
};
} catch (e) {
return {
name: capitalize(domainName.split('.')[0]),
city: '',
};
}
}
}

View File

@ -14,6 +14,7 @@ import { MessageService } from 'src/workspace/messaging/message/message.service'
import { MessageThreadService } from 'src/workspace/messaging/message-thread/message-thread.service';
import { ObjectRecord } from 'src/workspace/workspace-sync-metadata/types/object-record';
import { ConnectedAccountObjectMetadata } from 'src/workspace/workspace-sync-metadata/standard-objects/connected-account.object-metadata';
import { CreateCompanyService } from 'src/workspace/messaging/services/create-company.service';
@Injectable()
export class MessagingUtilsService {
@ -21,6 +22,7 @@ export class MessagingUtilsService {
private readonly messageChannelMessageAssociationService: MessageChannelMessageAssociationService,
private readonly messageService: MessageService,
private readonly messageThreadService: MessageThreadService,
private readonly createCompaniesService: CreateCompanyService,
) {}
public createQueriesFromMessageIds(
@ -201,6 +203,19 @@ export class MessagingUtilsService {
participantWorkspaceMemberId,
],
);
const companyDomainName = participant.handle
.split('@')?.[1]
.split('.')
.slice(-2)
.join('.')
.toLowerCase();
await this.createCompaniesService.createCompanyFromDomainName(
companyDomainName,
dataSourceMetadata,
manager,
);
}
}