88 replaceDoubleQuotes ,
99 setAttributesDefaultValues ,
1010} from '@crowd/common'
11+ import { CommonMemberService } from '@crowd/common_services'
1112import {
1213 changeMemberOrganizationAffiliationOverrides ,
1314 checkOrganizationAffiliationPolicy ,
@@ -287,15 +288,18 @@ export async function updateMemberUsingSquashedPayload(
287288 hasContributions : boolean ,
288289 isHighConfidenceSourceSelectedForWorkExperiences : boolean ,
289290) : Promise < boolean > {
290- return await svc . postgres . writer . transactionally ( async ( tx ) => {
291- let updated = false
291+ const affectedOrgIds : string [ ] = [ ]
292+
293+ const wasUpdated = await svc . postgres . writer . transactionally ( async ( tx ) => {
294+ let didUpdate = false
295+
292296 const qx = dbStoreQx ( tx )
293297
294298 // process identities
295299 if ( squashedPayload . identities . length > 0 ) {
296300 svc . log . debug ( { memberId } , 'Adding to member identities!' )
297301 for ( const i of squashedPayload . identities ) {
298- updated = true
302+ didUpdate = true
299303 await createMemberIdentity (
300304 qx ,
301305 {
@@ -327,7 +331,7 @@ export async function updateMemberUsingSquashedPayload(
327331 const typed = normalized as IMemberEnrichmentDataNormalized
328332
329333 if ( typed . contributions ) {
330- updated = true
334+ didUpdate = true
331335 await updateMemberContributions ( qx , memberId , typed . contributions )
332336 }
333337 }
@@ -346,7 +350,7 @@ export async function updateMemberUsingSquashedPayload(
346350 const priorities = await getPriorityArray ( )
347351 attributes = await setAttributesDefaultValues ( attributes , priorities )
348352 }
349- updated = true
353+ didUpdate = true
350354 await updateMemberAttributes ( qx , memberId , attributes )
351355 }
352356
@@ -366,7 +370,7 @@ export async function updateMemberUsingSquashedPayload(
366370 total,
367371 }
368372
369- updated = true
373+ didUpdate = true
370374 await updateMemberReach ( qx , memberId , reach )
371375 }
372376 }
@@ -459,7 +463,8 @@ export async function updateMemberUsingSquashedPayload(
459463
460464 if ( results . toDelete . length > 0 ) {
461465 for ( const org of results . toDelete ) {
462- updated = true
466+ didUpdate = true
467+ affectedOrgIds . push ( org . orgId )
463468 await deleteMemberOrgById ( tx . transaction ( ) , org . id )
464469 }
465470 }
@@ -469,7 +474,8 @@ export async function updateMemberUsingSquashedPayload(
469474 if ( ! org . organizationId ) {
470475 throw new Error ( 'Organization ID is missing!' )
471476 }
472- updated = true
477+ didUpdate = true
478+ affectedOrgIds . push ( org . organizationId )
473479
474480 const newMemberOrgId = await insertWorkExperience (
475481 tx . transaction ( ) ,
@@ -492,7 +498,8 @@ export async function updateMemberUsingSquashedPayload(
492498
493499 if ( results . toUpdate . size > 0 ) {
494500 for ( const [ memberOrg , toUpdate ] of results . toUpdate ) {
495- updated = true
501+ didUpdate = true
502+ affectedOrgIds . push ( memberOrg . orgId )
496503 const updatedMemberOrgId = await updateMemberOrg (
497504 tx . transaction ( ) ,
498505 memberId ,
@@ -527,7 +534,7 @@ export async function updateMemberUsingSquashedPayload(
527534 }
528535 }
529536
530- if ( updated ) {
537+ if ( didUpdate ) {
531538 await setMemberEnrichmentUpdatedAt ( tx . transaction ( ) , memberId )
532539 await syncMember ( memberId )
533540 } else {
@@ -536,8 +543,23 @@ export async function updateMemberUsingSquashedPayload(
536543
537544 svc . log . debug ( { memberId } , 'Member sources processed successfully!' )
538545
539- return updated
546+ return didUpdate
540547 } )
548+
549+ if ( affectedOrgIds . length > 0 ) {
550+ const commonMemberService = new CommonMemberService (
551+ pgpQx ( svc . postgres . writer . connection ( ) ) ,
552+ svc . temporal ,
553+ svc . log ,
554+ )
555+ await commonMemberService . startAffiliationRecalculation (
556+ memberId ,
557+ [ ...new Set ( affectedOrgIds ) ] ,
558+ true ,
559+ )
560+ }
561+
562+ return wasUpdated
541563}
542564
543565export function doesIncomingOrgExistInExistingOrgs (
0 commit comments