Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions services/libs/common/src/member.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import merge from 'lodash.merge'
import ldSum from 'lodash.sum'

import { OrganizationSource } from '@crowd/types'

/* eslint-disable @typescript-eslint/no-explicit-any */

export async function setAttributesDefaultValues(
Expand Down Expand Up @@ -79,3 +81,10 @@ export const calculateReach = (oldReach: any, newReach: any): { total: number }
out.total = ldSum(Object.values(out))
return out
}

export function getMemberOrganizationSourceRank(source: string | undefined): number {
if (source === OrganizationSource.UI) return 0
if (source === OrganizationSource.EMAIL_DOMAIN) return 1
if (source?.startsWith('enrichment-')) return 2
return 3
Comment thread
skwowet marked this conversation as resolved.
Outdated
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from 'lodash'
import { v4 as uuid } from 'uuid'

import { getLongestDateRange } from '@crowd/common'
import { getLongestDateRange, getMemberOrganizationSourceRank } from '@crowd/common'
import { getServiceChildLogger } from '@crowd/logging'
import {
IChangeAffiliationOverrideData,
Expand Down Expand Up @@ -81,7 +81,16 @@ async function prepareMemberOrganizationAffiliationTimeline(
return withDates[0]
}

// 2. get the two orgs with the most members, and return the one with the most members if there's no draw
// 2. among dated rows, pick the best source tier (ui > email-domain > enrichment-*)
if (withDates.length > 1) {
const sourceRank = (row: AffiliationItem) =>
getMemberOrganizationSourceRank((row as MemberOrganizationWithOverrides).source)
const bestRank = Math.min(...withDates.map(sourceRank))
orgs = withDates.filter((row) => sourceRank(row) === bestRank)
if (orgs.length === 1) return orgs[0]
Comment thread
skwowet marked this conversation as resolved.
Outdated
Comment thread
skwowet marked this conversation as resolved.
Outdated
}
Comment thread
skwowet marked this conversation as resolved.

// 3. get the two orgs with the most members, and return the one with the most members if there's no draw
// only compare member orgs (manual affiliations don't have memberCount)
const memberOrgsOnly = orgs.filter(
(row: AffiliationItem) => 'segmentId' in row && !!row.segmentId,
Expand All @@ -93,7 +102,7 @@ async function prepareMemberOrganizationAffiliationTimeline(
}
}

// 3. there's a draw, return the one with the longer date range
// 4. there's a draw, return the one with the longer date range
return getLongestDateRange(orgs)
}
}
Expand Down Expand Up @@ -243,6 +252,7 @@ async function prepareMemberOrganizationAffiliationTimeline(
mo."dateStart",
mo."dateEnd",
mo."createdAt",
mo."source",
coalesce(ovr."isPrimaryWorkExperience", false) as "isPrimaryWorkExperience",
coalesce(a.total_count, 0) as "memberCount"
FROM "memberOrganizations" mo
Expand Down
Loading