@@ -51,7 +51,7 @@ import {
5151} from '@crowd/data-access-layer/src/members/segments'
5252import { IDbMemberData } from '@crowd/data-access-layer/src/members/types'
5353import { optionsQx } from '@crowd/data-access-layer/src/queryExecutor'
54- import { fetchManySegments } from '@crowd/data-access-layer/src/segments'
54+ import { fetchManySegments , getSegmentSubprojectIds } from '@crowd/data-access-layer/src/segments'
5555import { ActivityDisplayService } from '@crowd/integrations'
5656import {
5757 ALL_PLATFORM_TYPES ,
@@ -148,6 +148,9 @@ class MemberRepository {
148148 )
149149
150150 const qx = SequelizeRepository . getQueryExecutor ( options )
151+ const currentSegments = SequelizeRepository . getSegmentIds ( options )
152+
153+ const subprojectIds = await getSegmentSubprojectIds ( qx , currentSegments )
151154
152155 if ( data . identities ) {
153156 for ( const i of data . identities as IMemberIdentity [ ] ) {
@@ -182,19 +185,15 @@ class MemberRepository {
182185 }
183186 }
184187
185- await includeMemberToSegments (
186- qx ,
187- record . id ,
188- options . currentSegments . map ( ( s ) => s . id ) ,
189- )
188+ await includeMemberToSegments ( qx , record . id , subprojectIds )
190189
191190 const memberService = new CommonMemberService ( optionsQx ( options ) , options . temporal , options . log )
192191
193192 await memberService . updateMemberOrganizations (
194193 record . id ,
195194 data . organizations ,
196195 true ,
197- options . currentSegments . map ( ( s ) => s . id ) ,
196+ subprojectIds ,
198197 options ,
199198 )
200199
@@ -234,10 +233,15 @@ class MemberRepository {
234233
235234 const bulkDeleteMemberSegments = `DELETE FROM "memberSegments" WHERE "memberId" in (:memberIds) and "segmentId" in (:segmentIds);`
236235
236+ const qx = SequelizeRepository . getQueryExecutor ( options )
237+ const currentSegments = SequelizeRepository . getSegmentIds ( options )
238+
239+ const subprojectIds = await getSegmentSubprojectIds ( qx , currentSegments )
240+
237241 await seq . query ( bulkDeleteMemberSegments , {
238242 replacements : {
239243 memberIds,
240- segmentIds : SequelizeRepository . getSegmentIds ( options ) ,
244+ segmentIds : subprojectIds ,
241245 } ,
242246 type : QueryTypes . DELETE ,
243247 transaction,
@@ -294,13 +298,12 @@ class MemberRepository {
294298 const HIGH_CONFIDENCE_LOWER_BOUND = 0.9
295299 const MEDIUM_CONFIDENCE_LOWER_BOUND = 0.7
296300
301+ const qx = SequelizeRepository . getQueryExecutor ( options )
297302 const currentSegments = SequelizeRepository . getSegmentIds ( options )
298303
299- const segmentIds = (
300- await new SegmentRepository ( options ) . getSegmentSubprojects ( currentSegments )
301- ) . map ( ( s ) => s . id )
304+ const subprojectIds = await getSegmentSubprojectIds ( qx , currentSegments )
302305
303- if ( segmentIds . length === 0 ) {
306+ if ( subprojectIds . length === 0 ) {
304307 return args . countOnly
305308 ? { count : '0' }
306309 : {
@@ -361,7 +364,7 @@ class MemberRepository {
361364 similarityFilter ,
362365 displayNameFilter ,
363366 {
364- segmentIds,
367+ segmentIds : subprojectIds ,
365368 displayName : args ?. filter ?. displayName ? `${ args . filter . displayName } %` : undefined ,
366369 memberId : args ?. filter ?. memberId ,
367370 } ,
@@ -403,7 +406,7 @@ class MemberRepository {
403406 ` ,
404407 {
405408 replacements : {
406- segmentIds,
409+ segmentIds : subprojectIds ,
407410 limit : args . limit ,
408411 offset : args . offset ,
409412 displayName : args ?. filter ?. displayName ? `${ args . filter . displayName } %` : undefined ,
@@ -509,7 +512,7 @@ class MemberRepository {
509512 similarityFilter ,
510513 displayNameFilter ,
511514 {
512- segmentIds,
515+ segmentIds : subprojectIds ,
513516 memberId : args ?. filter ?. memberId ,
514517 displayName : args ?. filter ?. displayName ? `${ args . filter . displayName } %` : undefined ,
515518 } ,
@@ -888,13 +891,19 @@ class MemberRepository {
888891 ! manualChange , // no need to track for audit if it's not a manual change
889892 )
890893
894+ const qx = SequelizeRepository . getQueryExecutor ( options )
895+ const subprojectIds = await getSegmentSubprojectIds (
896+ qx ,
897+ SequelizeRepository . getSegmentIds ( options ) ,
898+ )
899+
891900 const memberService = new CommonMemberService ( optionsQx ( options ) , options . temporal , options . log )
892901
893902 await memberService . updateMemberOrganizations (
894903 record . id ,
895904 data . organizations ,
896905 data . organizationsReplace ,
897- options . currentSegments . map ( ( s ) => s . id ) ,
906+ subprojectIds ,
898907 options ,
899908 )
900909
@@ -915,11 +924,7 @@ class MemberRepository {
915924 }
916925
917926 if ( options . currentSegments && options . currentSegments . length > 0 ) {
918- await includeMemberToSegments (
919- optionsQx ( options ) ,
920- record . id ,
921- options . currentSegments . map ( ( s ) => s . id ) ,
922- )
927+ await includeMemberToSegments ( qx , record . id , subprojectIds )
923928 }
924929
925930 // Before upserting identities, check if they already exist
@@ -998,8 +1003,6 @@ class MemberRepository {
9981003 }
9991004 }
10001005
1001- const qx = SequelizeRepository . getQueryExecutor ( options )
1002-
10031006 if ( data . identitiesToCreate && data . identitiesToCreate . length > 0 ) {
10041007 for ( const i of data . identitiesToCreate ) {
10051008 await createMemberIdentity ( qx , {
@@ -1801,6 +1804,11 @@ class MemberRepository {
18011804
18021805 const where = { [ Op . and ] : whereAnd }
18031806
1807+ const qx = SequelizeRepository . getQueryExecutor ( options )
1808+ const currentSegments = SequelizeRepository . getSegmentIds ( options )
1809+
1810+ const subprojectIds = await getSegmentSubprojectIds ( qx , currentSegments )
1811+
18041812 const records = await options . database . member . findAll ( {
18051813 attributes : [ 'id' , 'displayName' , 'attributes' ] ,
18061814 where,
@@ -1816,7 +1824,7 @@ class MemberRepository {
18161824 model : options . database . segment ,
18171825 as : 'segments' ,
18181826 where : {
1819- id : SequelizeRepository . getSegmentIds ( options ) ,
1827+ id : subprojectIds ,
18201828 } ,
18211829 } ,
18221830 ] ,
0 commit comments