-
Notifications
You must be signed in to change notification settings - Fork 732
Expand file tree
/
Copy pathmember.ts
More file actions
90 lines (79 loc) · 2.85 KB
/
member.ts
File metadata and controls
90 lines (79 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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(
attributes: Record<string, unknown>,
priorities: string[],
): Promise<Record<string, unknown>> {
if (!priorities) {
throw new Error(`No priorities set!`)
}
for (const attributeName of Object.keys(attributes)) {
if (typeof attributes[attributeName] === 'string') {
// we try to fix it
attributes[attributeName] = JSON.parse(attributes[attributeName] as string)
}
const nonEmptyPlatform = Object.keys(attributes[attributeName]).filter((p) => {
if (p === 'default') return false
const value = attributes[attributeName][p]
return value !== undefined && value !== null && String(value).trim().length > 0
})
const highestPriorityPlatform = getHighestPriorityPlatformForAttributes(
nonEmptyPlatform,
priorities,
)
if (highestPriorityPlatform !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
;(attributes[attributeName] as any).default =
attributes[attributeName][highestPriorityPlatform]
} else {
// Only delete if there is no existing non-empty default value.
// An attribute with only a `default` key and no platform-specific keys
// has no source platform to derive from, but its value should be preserved.
const existingDefault = (attributes[attributeName] as any).default
if (
existingDefault === undefined ||
existingDefault === null ||
String(existingDefault).trim().length === 0
) {
delete attributes[attributeName]
}
}
}
return attributes
}
export function getHighestPriorityPlatformForAttributes(
platforms: string[],
priorityArray: string[],
): string | undefined {
if (platforms.length <= 0) {
return undefined
}
const filteredPlatforms = priorityArray.filter((i) => platforms.includes(i))
return filteredPlatforms.length > 0 ? filteredPlatforms[0] : platforms[0]
}
/**
*
* @param oldReach The old reach object
* @param newReach the new reach object
* @returns The new reach object
*/
export const calculateReach = (oldReach: any, newReach: any): { total: number } => {
// Totals are recomputed, so we delete them first
delete oldReach.total
delete newReach.total
const out = merge(oldReach, newReach)
if (Object.keys(out).length === 0) {
return { total: -1 }
}
// Total is the sum of all attributes
out.total = ldSum(Object.values(out))
return out
}
export function getMemberOrganizationSourceRank(source: string | null | undefined): number {
if (source === OrganizationSource.UI) return 0
if (source === OrganizationSource.EMAIL_DOMAIN) return 1
if (source?.startsWith('enrichment-')) return 2
return 3
}