From 77152a10b12dda7f4370452ada3d9927a4d3b366 Mon Sep 17 00:00:00 2001 From: bosiraphael <71827178+bosiraphael@users.noreply.github.com> Date: Tue, 30 Jul 2024 18:10:36 +0200 Subject: [PATCH] Fix company creation duplicate on email sync after introducing links type (#6460) - Introduce `extractDomainFromLink` - Use is inside `create-company.service` --- .../repositories/company.repository.ts | 2 +- .../services/create-company.service.ts | 6 ++-- .../extract-domain-from-link.spec.ts | 31 +++++++++++++++++++ .../utils/extract-domain-from-link.util.ts | 5 +++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 packages/twenty-server/src/modules/contact-creation-manager/utils/__tests__/extract-domain-from-link.spec.ts create mode 100644 packages/twenty-server/src/modules/contact-creation-manager/utils/extract-domain-from-link.util.ts diff --git a/packages/twenty-server/src/modules/company/repositories/company.repository.ts b/packages/twenty-server/src/modules/company/repositories/company.repository.ts index 39aeeb5d45..0f99e8f9ca 100644 --- a/packages/twenty-server/src/modules/company/repositories/company.repository.ts +++ b/packages/twenty-server/src/modules/company/repositories/company.repository.ts @@ -57,7 +57,7 @@ export class CompanyRepository { public async createCompany( workspaceId: string, companyToCreate: CompanyToCreate, - companyDomainNameColumnName, + companyDomainNameColumnName: string, transactionManager?: EntityManager, ): Promise { const dataSourceSchema = diff --git a/packages/twenty-server/src/modules/contact-creation-manager/services/create-company.service.ts b/packages/twenty-server/src/modules/contact-creation-manager/services/create-company.service.ts index 8d35f64d68..829334b7bb 100644 --- a/packages/twenty-server/src/modules/contact-creation-manager/services/create-company.service.ts +++ b/packages/twenty-server/src/modules/contact-creation-manager/services/create-company.service.ts @@ -7,8 +7,8 @@ import { v4 } from 'uuid'; import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator'; import { CompanyRepository } from 'src/modules/company/repositories/company.repository'; import { CompanyWorkspaceEntity } from 'src/modules/company/standard-objects/company.workspace-entity'; +import { extractDomainFromLink } from 'src/modules/contact-creation-manager/utils/extract-domain-from-link.util'; import { getCompanyNameFromDomainName } from 'src/modules/contact-creation-manager/utils/get-company-name-from-domain-name.util'; -import { getCompanyDomainName } from 'src/utils/getCompanyDomainName'; @Injectable() export class CreateCompanyService { private readonly httpService: AxiosInstance; @@ -55,7 +55,7 @@ export class CreateCompanyService { }, ) => ({ ...acc, - [company.domainName]: company.id, + [extractDomainFromLink(company.domainName)]: company.id, }), {}, ); @@ -64,7 +64,7 @@ export class CreateCompanyService { (domainName) => !existingCompanies.some( (company: { domainName: string }) => - getCompanyDomainName(company) === domainName, + extractDomainFromLink(company.domainName) === domainName, ), ); diff --git a/packages/twenty-server/src/modules/contact-creation-manager/utils/__tests__/extract-domain-from-link.spec.ts b/packages/twenty-server/src/modules/contact-creation-manager/utils/__tests__/extract-domain-from-link.spec.ts new file mode 100644 index 0000000000..b07fe4bd3b --- /dev/null +++ b/packages/twenty-server/src/modules/contact-creation-manager/utils/__tests__/extract-domain-from-link.spec.ts @@ -0,0 +1,31 @@ +import { extractDomainFromLink } from 'src/modules/contact-creation-manager/utils/extract-domain-from-link.util'; + +describe('extractDomainFromLink', () => { + it('should extract domain from link', () => { + const link = 'https://www.twenty.com'; + const result = extractDomainFromLink(link); + + expect(result).toBe('twenty.com'); + }); + + it('should extract domain from link without www', () => { + const link = 'https://twenty.com'; + const result = extractDomainFromLink(link); + + expect(result).toBe('twenty.com'); + }); + + it('should extract domain from link without protocol', () => { + const link = 'twenty.com'; + const result = extractDomainFromLink(link); + + expect(result).toBe('twenty.com'); + }); + + it('should extract domain from link with path', () => { + const link = 'https://twenty.com/about'; + const result = extractDomainFromLink(link); + + expect(result).toBe('twenty.com'); + }); +}); diff --git a/packages/twenty-server/src/modules/contact-creation-manager/utils/extract-domain-from-link.util.ts b/packages/twenty-server/src/modules/contact-creation-manager/utils/extract-domain-from-link.util.ts new file mode 100644 index 0000000000..90e5ace27f --- /dev/null +++ b/packages/twenty-server/src/modules/contact-creation-manager/utils/extract-domain-from-link.util.ts @@ -0,0 +1,5 @@ +export const extractDomainFromLink = (link: string) => { + const domain = link.replace(/^(https?:\/\/)?(www\.)?/i, '').split('/')[0]; + + return domain; +};