|
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' |
3 | 13 | import { MemberSyncService, OrganizationSyncService } from '@crowd/opensearch' |
4 | 14 | import { |
| 15 | + IAttributes, |
5 | 16 | IEnrichableMember, |
6 | 17 | IEnrichmentSourceQueryInput, |
| 18 | + IMemberIdentity, |
7 | 19 | MemberEnrichmentSource, |
8 | 20 | } from '@crowd/types' |
9 | 21 |
|
@@ -60,3 +72,71 @@ export async function syncMembersToOpensearch(input: string): Promise<void> { |
60 | 72 | export async function syncOrganizationsToOpensearch(input: string[]): Promise<void> { |
61 | 73 | await organizationSyncService.syncOrganizations(input) |
62 | 74 | } |
| 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