Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion backend/src/services/member/memberAttributesService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import * as lodash from 'lodash'

import { captureApiChange, memberEditProfileAction } from '@crowd/audit-logs'
import { Error404 } from '@crowd/common'
import { Error404, getAttributeValue, getCountry, hasAttributeValue } from '@crowd/common'
import {
deleteMemberBotSuggestion,
deleteMemberNoBot,
Expand Down Expand Up @@ -78,6 +78,18 @@ export default class MemberAttributesService extends LoggerBase {
}
}

if (!hasAttributeValue(data.country)) {
const location = getAttributeValue(data.location)
const country = getCountry(location)
if (country) {
data.country = {
...data.country,
system: country,
default: country,
}
}
}

await updateMemberAttributes(qx, memberId, data)

// Handle isBot status and maintain consistency with bot tracking tables
Expand Down
46 changes: 45 additions & 1 deletion backend/src/services/memberService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ import moment from 'moment-timezone'
import validator from 'validator'

import { captureApiChange, memberUnmergeAction } from '@crowd/audit-logs'
import { Error400, calculateReach, getProperDisplayName, isDomainExcluded } from '@crowd/common'
import {
Error400,
calculateReach,
getAttributeValue,
getCountry,
getProperDisplayName,
hasAttributeValue,
isDomainExcluded,
} from '@crowd/common'
import {
CommonMemberService,
getGithubInstallationToken,
Expand Down Expand Up @@ -446,6 +454,17 @@ export default class MemberService extends LoggerBase {
const toUpdate = CommonMemberService.membersMerge(existing, data)

if (toUpdate.attributes) {
if (!hasAttributeValue(toUpdate.attributes.country)) {
const location = getAttributeValue(toUpdate.attributes.location)
const country = getCountry(location)
if (country) {
toUpdate.attributes.country = {
...toUpdate.attributes.country,
system: country,
}
}
}

toUpdate.attributes = await this.setAttributesDefaultValues(toUpdate.attributes)
}

Expand All @@ -459,6 +478,17 @@ export default class MemberService extends LoggerBase {
// It is important to call it with doPopulateRelations=false
// because otherwise the performance is greatly decreased in integrations
if (data.attributes) {
if (!hasAttributeValue(data.attributes.country)) {
const location = getAttributeValue(data.attributes.location)
const country = getCountry(location)
if (country) {
data.attributes.country = {
...data.attributes.country,
system: country,
}
}
}

data.attributes = await this.setAttributesDefaultValues(data.attributes)
}

Expand Down Expand Up @@ -789,6 +819,20 @@ export default class MemberService extends LoggerBase {
data.displayName = getProperDisplayName(data.displayName)
}

if (data.attributes) {
if (!hasAttributeValue(data.attributes.country)) {
const location = getAttributeValue(data.attributes.location)
const country = getCountry(location)
if (country) {
data.attributes.country = {
...data.attributes.country,
system: country,
default: data.attributes.country?.default ?? country,
}
}
}
}

const record = await MemberRepository.update(id, data, repoOptions, {
manualChange,
})
Expand Down
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions services/apps/data_sink_worker/src/service/member.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import uniqby from 'lodash.uniqby'
import {
ApplicationError,
DEFAULT_TENANT_ID,
getAttributeValue,
getCountry,
getEarliestValidDate,
getProperDisplayName,
hasAttributeValue,
isDomainExcluded,
isObjectEmpty,
isSameMemberIdentity,
Expand Down Expand Up @@ -331,6 +334,14 @@ export default class MemberService extends LoggerBase {
'memberService -> create -> validateAttributes',
)

if (!hasAttributeValue(attributes.country)) {
const location = getAttributeValue(attributes.location)
const country = getCountry(location)
if (country) {
attributes.country = Object.assign({}, attributes.country, { system: country })
}
}

attributes = await logExecutionTimeV2(
() => memberAttributeService.setAttributesDefaultValues(attributes),
this.log,
Expand Down Expand Up @@ -592,6 +603,17 @@ export default class MemberService extends LoggerBase {

if (toUpdate.attributes) {
this.log.trace({ memberId: id }, 'Setting attribute default values!')

if (!hasAttributeValue(toUpdate.attributes.country)) {
const location = getAttributeValue(toUpdate.attributes.location)
const country = getCountry(location)
if (country) {
toUpdate.attributes.country = Object.assign({}, toUpdate.attributes.country, {
system: country,
})
}
}

toUpdate.attributes = await logExecutionTimeV2(
() => memberAttributeService.setAttributesDefaultValues(toUpdate.attributes),
this.log,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import _ from 'lodash'

import {
generateUUIDv1,
getAttributeValue,
getCountry,
hasAttributeValue,
hasIntersection,
replaceDoubleQuotes,
sanitizeMemberOrganizationDateRange,
Expand Down Expand Up @@ -45,6 +48,7 @@ import { refreshMaterializedView } from '@crowd/data-access-layer/src/utils'
import { SearchSyncApiClient } from '@crowd/opensearch'
import { RedisCache } from '@crowd/redis'
import {
IAttributes,
IEnrichableMember,
IEnrichableMemberIdentityActivityAggregate,
IMemberEnrichmentCache,
Expand Down Expand Up @@ -347,16 +351,28 @@ export async function updateMemberUsingSquashedPayload(
}

// process attributes
let attributes = existingMemberData.attributes as Record<string, unknown>
let attributes = existingMemberData.attributes as IAttributes

if (squashedPayload.attributes) {
svc.log.debug({ memberId }, 'Updating member attributes!')

attributes = _.merge({}, attributes, squashedPayload.attributes)

if (Object.keys(attributes).length > 0) {
// Infer country from location when no country source is set.
if (!hasAttributeValue(attributes.country)) {
const location = getAttributeValue(attributes.location)
const country = getCountry(location)
if (country) {
attributes.country = {
...attributes.country,
system: country,
}
}
}

const priorities = await getPriorityArray()
attributes = await setAttributesDefaultValues(attributes, priorities)
attributes = (await setAttributesDefaultValues(attributes, priorities)) as IAttributes
}
didUpdate = true
await updateMemberAttributes(qx, memberId, attributes)
Expand Down
1 change: 1 addition & 0 deletions services/libs/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"dependencies": {
"@crowd/types": "workspace:*",
"config": "^3.3.9",
"i18n-iso-countries": "^7.14.0",
"lodash.clonedeep": "^4.5.0",
"lodash.get": "~4.4.2",
"lodash.isarray": "^4.0.0",
Expand Down
1 change: 1 addition & 0 deletions services/libs/common/src/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './email-providers'
export * from './bots'
export * from './disposable-email-domains'
export * from './location'
108 changes: 108 additions & 0 deletions services/libs/common/src/constants/location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* US state / district names (not postal codes).
* Excludes georgia and washington — too easy to confuse with other places.
*/
export const US_STATE_NAMES = new Set([
'alabama',
'alaska',
'arizona',
'arkansas',
'california',
'colorado',
'connecticut',
'delaware',
'florida',
'hawaii',
'idaho',
'illinois',
'indiana',
'iowa',
'kansas',
'kentucky',
'louisiana',
'maine',
'maryland',
'massachusetts',
'michigan',
'minnesota',
'mississippi',
'missouri',
'montana',
'nebraska',
'nevada',
'new hampshire',
'new jersey',
'new mexico',
'new york',
'north carolina',
'north dakota',
'ohio',
'oklahoma',
'oregon',
'pennsylvania',
'rhode island',
'south carolina',
'south dakota',
'tennessee',
'texas',
'utah',
'vermont',
'virginia',
'west virginia',
'wisconsin',
'wyoming',
'district of columbia',
])

/** Common alternate spellings → a name the country package recognizes. */
export const COUNTRY_NAME_INPUT_ALIASES = new Map<string, string>([
['brasil', 'Brazil'],
['turkiye', 'Türkiye'],
['viet nam', 'Vietnam'],
['korea', 'South Korea'],
['england', 'United Kingdom'],
['scotland', 'United Kingdom'],
['wales', 'United Kingdom'],
])

/** Tokens that should never resolve to a country. */
export const JUNK_LOCATION_TOKENS = new Set([
'earth',
'mars',
'moon',
'remote',
'worldwide',
'world',
'global',
'internet',
'somewhere',
'nowhere',
'unknown',
'n/a',
'na',
'null',
'undefined',
'home',
'here',
'africa',
'europe',
'asia',
'antarctica',
])

/** Names that could mean more than one place — skip instead of guessing. */
export const AMBIGUOUS_LOCATION_TOKENS = new Set(['georgia', 'washington'])

/** Preferred display names for a few ISO codes with awkward official forms. */
export const COUNTRY_DISPLAY_NAME_BY_ALPHA2 = new Map<string, string>([
['US', 'United States'],
['GB', 'United Kingdom'],
['CN', 'China'],
['TW', 'Taiwan'],
['RU', 'Russia'],
['TR', 'Turkey'],
['KR', 'South Korea'],
['NL', 'Netherlands'],
['CZ', 'Czech Republic'],
['AE', 'United Arab Emirates'],
])
Loading
Loading