@@ -29,6 +29,7 @@ import {
2929} from '@crowd/data-access-layer'
3030import { IDbActivityRelation } from '@crowd/data-access-layer/src/activityRelations/types'
3131import { DbStore , arePrimitivesDbEqual } from '@crowd/data-access-layer/src/database'
32+ import { insertMemberIdentities } from '@crowd/data-access-layer/src/members/identities'
3233import {
3334 IActivityRelationCreateOrUpdateData ,
3435 IDbActivity ,
@@ -1292,7 +1293,8 @@ export default class ActivityService extends LoggerBase {
12921293 }
12931294 } else if (
12941295 payload . platform === value . platform &&
1295- payload . activity . objectMemberUsername ?. toLowerCase ( ) === value . username ?. toLowerCase ( )
1296+ payload . activity . objectMemberUsername ?. toLowerCase ( ) ===
1297+ value . username ?. toLowerCase ( )
12961298 ) {
12971299 const key = `${ payload . platform } :${ payload . activity . objectMemberUsername } `
12981300 if ( memberMap . has ( key ) ) {
@@ -1828,6 +1830,39 @@ export default class ActivityService extends LoggerBase {
18281830 { memberId : ownerId , identity : conflictIdentity } ,
18291831 'Verified identity already belongs to an existing member — attaching to it instead of creating a new one' ,
18301832 )
1833+
1834+ // Mirror MemberService.create's conflict-redirect finalization: attaching to an
1835+ // existing owner must still sync the incoming identities onto it and add it to the
1836+ // current segment, or the owner ends up missing from this activity's project.
1837+ const ownerIdentities =
1838+ ( await findIdentitiesForMembers ( this . pgQx , [ ownerId ] ) ) . get ( ownerId ) ?? [ ]
1839+ const missingIdentities = incomingIdentities . filter (
1840+ ( incoming ) =>
1841+ ! ownerIdentities . some (
1842+ ( existing ) =>
1843+ existing . platform === incoming . platform &&
1844+ existing . type === incoming . type &&
1845+ existing . value . trim ( ) . toLowerCase ( ) === incoming . value . trim ( ) . toLowerCase ( ) ,
1846+ ) ,
1847+ )
1848+ if ( missingIdentities . length > 0 ) {
1849+ await insertMemberIdentities (
1850+ this . pgQx ,
1851+ missingIdentities . map ( ( identity ) => ( {
1852+ memberId : ownerId ,
1853+ platform : identity . platform ,
1854+ value : identity . value ,
1855+ type : identity . type ,
1856+ verified : identity . verified ,
1857+ source : identity . source ,
1858+ sourceId : identity . sourceId ,
1859+ integrationId : identity . integrationId ,
1860+ verifiedBy : identity . verifiedBy ,
1861+ } ) ) ,
1862+ )
1863+ }
1864+ await this . memberRepo . addToSegments ( ownerId , [ payload . segmentId ] )
1865+
18311866 return ownerId
18321867 }
18331868
0 commit comments