File tree Expand file tree Collapse file tree 4 files changed +33
-0
lines changed
apps/data_sink_worker/src/service Expand file tree Collapse file tree 4 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 11import { LoggerBase } from '@crowd/logging'
2+ import { websiteNormalizer } from '@crowd/common'
23import { CLEARBIT_CONFIG , IS_TEST_ENV } from '../conf'
34import MemberRepository from '../database/repositories/memberRepository'
45import organizationCacheRepository from '../database/repositories/organizationCacheRepository'
@@ -43,6 +44,11 @@ export default class OrganizationService extends LoggerBase {
4344 transaction,
4445 } )
4546
47+ // Normalize the website URL if it exists
48+ if ( data . website ) {
49+ data . website = websiteNormalizer ( data . website )
50+ }
51+
4652 // if cache exists, merge current data with cache data
4753 // if it doesn't exist, create it from incoming data
4854 if ( cache ) {
@@ -146,6 +152,11 @@ export default class OrganizationService extends LoggerBase {
146152 } )
147153 }
148154
155+ // Normalize the website URL if it exists
156+ if ( data . website ) {
157+ data . website = websiteNormalizer ( data . website )
158+ }
159+
149160 const record = await OrganizationRepository . update ( id , data , {
150161 ...this . options ,
151162 transaction,
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { OrganizationRepository } from '@/repo/organization.repo'
66import { DbStore } from '@crowd/database'
77import { Logger , LoggerBase , getChildLogger } from '@crowd/logging'
88import { IOrganization , IOrganizationSocial , PlatformType } from '@crowd/types'
9+ import { websiteNormalizer } from '@crowd/common'
910
1011export class OrganizationService extends LoggerBase {
1112 private readonly repo : OrganizationRepository
@@ -23,6 +24,11 @@ export class OrganizationService extends LoggerBase {
2324 ) : Promise < string > {
2425 data = this . normalizeSocialFields ( data )
2526
27+ // Normalize the website URL if it exists
28+ if ( data . website ) {
29+ data . website = websiteNormalizer ( data . website )
30+ }
31+
2632 // find from cache by name
2733 let cached = await this . repo . findCacheByName ( data . name )
2834
Original file line number Diff line number Diff line change @@ -11,3 +11,4 @@ export * from './requestThrottler'
1111export * from './rawQueryParser'
1212export * from './byteLength'
1313export * from './http'
14+ export * from './websiteNormalizer'
Original file line number Diff line number Diff line change 1+ export const websiteNormalizer = ( website : string ) : string => {
2+ // Prepends https:// to make valid URL
3+ const completeUrl = website . includes ( '://' ) ? website : 'https://' + website
4+
5+ const url = new URL ( completeUrl )
6+ const hostname = url . hostname
7+
8+ const parts = hostname . split ( '.' )
9+ // Ignore subdomains, return only domain and TLD
10+ if ( parts . length > 2 ) {
11+ return parts . slice ( - 2 ) . join ( '.' )
12+ }
13+
14+ return hostname
15+ }
You can’t perform that action at this time.
0 commit comments