Skip to content

Commit d521f2f

Browse files
authored
Normalize website while creating organization (#1412)
1 parent e414c05 commit d521f2f

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

backend/src/services/organizationService.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { LoggerBase } from '@crowd/logging'
2+
import { websiteNormalizer } from '@crowd/common'
23
import { CLEARBIT_CONFIG, IS_TEST_ENV } from '../conf'
34
import MemberRepository from '../database/repositories/memberRepository'
45
import 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,

services/apps/data_sink_worker/src/service/organization.service.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { OrganizationRepository } from '@/repo/organization.repo'
66
import { DbStore } from '@crowd/database'
77
import { Logger, LoggerBase, getChildLogger } from '@crowd/logging'
88
import { IOrganization, IOrganizationSocial, PlatformType } from '@crowd/types'
9+
import { websiteNormalizer } from '@crowd/common'
910

1011
export 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

services/libs/common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ export * from './requestThrottler'
1111
export * from './rawQueryParser'
1212
export * from './byteLength'
1313
export * from './http'
14+
export * from './websiteNormalizer'
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

0 commit comments

Comments
 (0)