Skip to content

Commit 68aeb10

Browse files
authored
fix: use correct merge API endpoint in LFID enrichment (CM-1142) (#4094)
1 parent 4aaf665 commit 68aeb10

3 files changed

Lines changed: 85 additions & 81 deletions

File tree

services/apps/members_enrichment_worker/src/activities.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,6 @@ import {
1919
updateMemberUsingSquashedPayload,
2020
} from './activities/enrichment'
2121
import { refreshToken } from './activities/lf-auth0/authenticateLFAuth0'
22-
import {
23-
getIdentitiesExistInOtherMembers,
24-
mergeMembers,
25-
updateMemberWithEnrichmentData,
26-
} from './activities/lf-auth0/enrichLFAuth0'
2722
import { getEnrichmentLFAuth0 } from './activities/lf-auth0/getEnrichmentLFAuth0'
2823
import { getLFIDEnrichableMembers } from './activities/lf-auth0/getLFIDEnrichableMembers'
2924
import {
@@ -39,9 +34,12 @@ import {
3934
} from './activities/llm'
4035
import {
4136
getEnrichableMembers,
37+
getIdentitiesExistInOtherMembers,
4238
getMemberById,
39+
mergeMembers,
4340
syncMembersToOpensearch,
4441
syncOrganizationsToOpensearch,
42+
updateMemberWithEnrichmentData,
4543
} from './activities/member'
4644

4745
export {

services/apps/members_enrichment_worker/src/activities/lf-auth0/enrichLFAuth0.ts

Lines changed: 0 additions & 74 deletions
This file was deleted.

services/apps/members_enrichment_worker/src/activities/member.ts

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1-
import { MemberField, findMemberById, pgpQx } from '@crowd/data-access-layer'
2-
import { fetchMembersForEnrichment } from '@crowd/data-access-layer/src/old/apps/members_enrichment_worker'
1+
import {
2+
MemberField,
3+
PgPromiseQueryExecutor,
4+
createMemberIdentity,
5+
findMemberById,
6+
pgpQx,
7+
} from '@crowd/data-access-layer'
8+
import {
9+
fetchMembersForEnrichment,
10+
getIdentitiesExistInOtherMembers as getIdentitiesExistInOthers,
11+
updateMemberAttributes,
12+
} from '@crowd/data-access-layer/src/old/apps/members_enrichment_worker'
313
import { MemberSyncService, OrganizationSyncService } from '@crowd/opensearch'
414
import {
15+
IAttributes,
516
IEnrichableMember,
617
IEnrichmentSourceQueryInput,
18+
IMemberIdentity,
719
MemberEnrichmentSource,
820
} from '@crowd/types'
921

@@ -60,3 +72,71 @@ export async function syncMembersToOpensearch(input: string): Promise<void> {
6072
export async function syncOrganizationsToOpensearch(input: string[]): Promise<void> {
6173
await organizationSyncService.syncOrganizations(input)
6274
}
75+
76+
export async function getIdentitiesExistInOtherMembers(
77+
excludeMemberId: string,
78+
identities: IMemberIdentity[],
79+
): Promise<IMemberIdentity[]> {
80+
let rows: IMemberIdentity[] = []
81+
82+
try {
83+
const db = svc.postgres.reader
84+
rows = await getIdentitiesExistInOthers(db, excludeMemberId, identities)
85+
} catch (err) {
86+
throw err
87+
}
88+
89+
return rows
90+
}
91+
92+
export async function updateMemberWithEnrichmentData(
93+
memberId: string,
94+
identities: IMemberIdentity[],
95+
attributes?: IAttributes,
96+
): Promise<void> {
97+
try {
98+
await svc.postgres.writer.connection().tx(async (tx) => {
99+
for (const identity of identities) {
100+
await createMemberIdentity(new PgPromiseQueryExecutor(tx), {
101+
memberId,
102+
platform: identity.platform,
103+
value: identity.value,
104+
type: identity.type,
105+
verified: identity.verified || false,
106+
source: 'enrichment',
107+
})
108+
}
109+
if (attributes) {
110+
await updateMemberAttributes(tx, memberId, attributes)
111+
}
112+
})
113+
} catch (err) {
114+
throw err
115+
}
116+
}
117+
118+
export async function mergeMembers(
119+
primaryMemberId: string,
120+
secondaryMemberId: string,
121+
): Promise<void> {
122+
const res = await fetch(
123+
`${process.env['CROWD_API_SERVICE_URL']}/member/${primaryMemberId}/merge`,
124+
{
125+
method: 'PUT',
126+
headers: {
127+
Authorization: `Bearer ${process.env['CROWD_API_SERVICE_USER_TOKEN']}`,
128+
'Content-Type': 'application/json',
129+
},
130+
body: JSON.stringify({
131+
memberToMerge: secondaryMemberId,
132+
}),
133+
},
134+
)
135+
136+
if (res.status !== 200) {
137+
const body = await res.text()
138+
throw new Error(
139+
`Failed to merge member ${primaryMemberId} with ${secondaryMemberId}! Status: ${res.status}, Response: ${body}`,
140+
)
141+
}
142+
}

0 commit comments

Comments
 (0)