@@ -26,7 +26,6 @@ import {
2626} from './types/memberTypes'
2727import { ActivityDisplayVariant } from '../../types/activityTypes'
2828import SegmentRepository from './segmentRepository'
29- import { SegmentData } from '../../types/segmentTypes'
3029
3130const { Op } = Sequelize
3231
@@ -162,6 +161,25 @@ class MemberRepository {
162161 } )
163162 }
164163
164+ static async excludeMemberFromSegments ( memberId : string , options : IRepositoryOptions ) {
165+ const seq = SequelizeRepository . getSequelize ( options )
166+
167+ const transaction = SequelizeRepository . getTransaction ( options )
168+
169+ const bulkDeleteMemberSegments = `DELETE FROM "memberSegments" WHERE "memberId" = :memberId and "segmentId" in (:segmentIds);`
170+
171+ await seq . query ( bulkDeleteMemberSegments , {
172+ replacements : {
173+ memberId,
174+ segmentIds : options . currentSegments . map ( ( s ) => s . id ) ,
175+ } ,
176+ type : QueryTypes . DELETE ,
177+ transaction,
178+ } )
179+
180+ return this . findById ( memberId , options , true , false )
181+ }
182+
165183 static async findSampleDataMemberIds ( options : IRepositoryOptions ) {
166184 const transaction = SequelizeRepository . getTransaction ( options )
167185 const currentTenant = SequelizeRepository . getCurrentTenant ( options )
@@ -432,7 +450,13 @@ class MemberRepository {
432450 const currentTenant = SequelizeRepository . getCurrentTenant ( options )
433451
434452 const query = `
435- with identities as (select mi."memberId",
453+ with segment_ids as (
454+ select "memberId", array_agg("segmentId") as "segmentIds" from
455+ "memberSegments"
456+ where "tenantId" = :tenantId
457+ group by "memberId"
458+ ),
459+ identities as (select mi."memberId",
436460 array_agg(distinct mi.platform) as identities,
437461 jsonb_object_agg(mi.platform, mi.usernames) as username
438462 from (select "memberId",
@@ -464,10 +488,12 @@ class MemberRepository {
464488 m."tenantId",
465489 m."createdById",
466490 m."updatedById",
467- i.username
491+ i.username,
492+ si."segmentIds" as segments
468493 from members m
469494 inner join "memberIdentities" mi on m.id = mi."memberId"
470495 inner join identities i on i."memberId" = m.id
496+ inner join segment_ids si on si."memberId" = m.id
471497 where mi."tenantId" = :tenantId
472498 and mi.platform = :platform
473499 and mi.username in (:usernames)
@@ -515,7 +541,6 @@ class MemberRepository {
515541 where : {
516542 id,
517543 tenantId : currentTenant . id ,
518- segmentId : options . currentSegments . map ( ( s ) => s . id ) ,
519544 } ,
520545 transaction,
521546 } )
@@ -587,6 +612,11 @@ class MemberRepository {
587612 transaction,
588613 } )
589614 }
615+
616+ if ( data . segments ) {
617+ await MemberRepository . includeMemberToSegments ( record . id , { ...options , transaction } )
618+ }
619+
590620 const seq = SequelizeRepository . getSequelize ( options )
591621
592622 if ( data . username ) {
@@ -664,25 +694,30 @@ class MemberRepository {
664694
665695 const currentTenant = SequelizeRepository . getCurrentTenant ( options )
666696
667- const record = await options . database . member . findOne ( {
668- where : {
669- id,
670- tenantId : currentTenant . id ,
671- segmentId : options . currentSegments . map ( ( s ) => s . id ) ,
672- } ,
673- transaction,
674- } )
697+ const member = await MemberRepository . excludeMemberFromSegments ( id , { ...options , transaction } )
675698
676- if ( ! record ) {
677- throw new Error404 ( )
678- }
699+ // if member doesn't belong to any other segment anymore, remove it
679700
680- await record . destroy ( {
681- force,
682- transaction,
683- } )
701+ if ( member . segments . length === 0 ) {
702+ const record = await options . database . member . findOne ( {
703+ where : {
704+ id,
705+ tenantId : currentTenant . id ,
706+ segmentId : options . currentSegments . map ( ( s ) => s . id ) ,
707+ } ,
708+ transaction,
709+ } )
684710
685- await this . _createAuditLog ( AuditLogRepository . DELETE , record , record , options )
711+ if ( ! record ) {
712+ throw new Error404 ( )
713+ }
714+
715+ await record . destroy ( {
716+ force,
717+ transaction,
718+ } )
719+ await this . _createAuditLog ( AuditLogRepository . DELETE , record , record , options )
720+ }
686721 }
687722
688723 static async destroyBulk ( ids , options : IRepositoryOptions , force = false ) {
@@ -701,6 +736,7 @@ class MemberRepository {
701736 } )
702737 }
703738
739+ /*
704740 static async getSegments(
705741 memberIds: string[],
706742 options: IRepositoryOptions,
@@ -714,18 +750,37 @@ class MemberRepository {
714750 select "memberId", "segmentId", "tenantId", "createdAt" from "memberSegments" where "memberId" in (:memberIds) order by "createdAt" asc;
715751 `
716752
717- await seq . query ( query , {
753+ const data = await seq.query(query, {
718754 replacements: {
719755 memberIds,
720756 },
721757 type: QueryTypes.SELECT,
722758 transaction,
723759 })
724760
761+ for (const id of memberIds) {
762+ results.set(id, [])
763+ }
764+
765+ for (const res of data as any[]) {
766+ const { memberId, segmentId, username, sourceId, integrationId, createdAt } = res
767+ const identities = results.get(memberId)
768+
769+ identities.push({
770+ platform,
771+ username,
772+ sourceId,
773+ integrationId,
774+ createdAt,
775+ })
776+ }
777+
778+
725779 // TODO: complete
726780
727781 return results
728782 }
783+ */
729784
730785 static async getIdentities (
731786 memberIds : string [ ] ,
@@ -778,7 +833,15 @@ class MemberRepository {
778833 ) {
779834 const transaction = SequelizeRepository . getTransaction ( options )
780835
781- const include = [ ]
836+ const include = [
837+ {
838+ model : options . database . segment ,
839+ as : 'segments' ,
840+ through : {
841+ attributes : [ ] ,
842+ } ,
843+ } ,
844+ ]
782845
783846 const where : any = {
784847 id,
@@ -787,7 +850,6 @@ class MemberRepository {
787850 if ( ! ignoreTenant ) {
788851 const currentTenant = SequelizeRepository . getCurrentTenant ( options )
789852 where . tenantId = currentTenant . id
790- where . segmentId = options . currentSegments . map ( ( s ) => s . id )
791853 }
792854
793855 const record = await options . database . member . findOne ( {
0 commit comments