@@ -26,6 +26,7 @@ import {
2626} from './types/memberTypes'
2727import { ActivityDisplayVariant } from '../../types/activityTypes'
2828import SegmentRepository from './segmentRepository'
29+ import { SegmentData } from '../../types/segmentTypes'
2930
3031const { Op } = Sequelize
3132
@@ -99,6 +100,8 @@ class MemberRepository {
99100 }
100101 }
101102
103+ await MemberRepository . includeMemberToSegments ( record . id , options )
104+
102105 await record . setActivities ( data . activities || [ ] , {
103106 transaction,
104107 } )
@@ -129,6 +132,36 @@ class MemberRepository {
129132 return this . findById ( record . id , options , true , doPopulateRelations )
130133 }
131134
135+ static async includeMemberToSegments ( memberId : string , options : IRepositoryOptions ) {
136+ const seq = SequelizeRepository . getSequelize ( options )
137+
138+ const transaction = SequelizeRepository . getTransaction ( options )
139+
140+ let bulkInsertMemberSegments = `INSERT INTO "memberSegments" ("memberId","segmentId", "tenantId", "createdAt") VALUES `
141+ const replacements = {
142+ memberId,
143+ tenantId : options . currentTenant . id ,
144+ }
145+
146+ for ( let idx = 0 ; idx < options . currentSegments . length ; idx ++ ) {
147+ bulkInsertMemberSegments += ` (:memberId, :segmentId${ idx } , :tenantId, now()) `
148+
149+ replacements [ `segmentId${ idx } ` ] = options . currentSegments [ idx ] . id
150+
151+ if ( idx !== options . currentSegments . length - 1 ) {
152+ bulkInsertMemberSegments += `,`
153+ }
154+ }
155+
156+ bulkInsertMemberSegments += ` ON CONFLICT DO NOTHING`
157+
158+ await seq . query ( bulkInsertMemberSegments , {
159+ replacements,
160+ type : QueryTypes . INSERT ,
161+ transaction,
162+ } )
163+ }
164+
132165 static async findSampleDataMemberIds ( options : IRepositoryOptions ) {
133166 const transaction = SequelizeRepository . getTransaction ( options )
134167 const currentTenant = SequelizeRepository . getCurrentTenant ( options )
@@ -482,6 +515,7 @@ class MemberRepository {
482515 where : {
483516 id,
484517 tenantId : currentTenant . id ,
518+ segmentId : options . currentSegments . map ( ( s ) => s . id ) ,
485519 } ,
486520 transaction,
487521 } )
@@ -634,6 +668,7 @@ class MemberRepository {
634668 where : {
635669 id,
636670 tenantId : currentTenant . id ,
671+ segmentId : options . currentSegments . map ( ( s ) => s . id ) ,
637672 } ,
638673 transaction,
639674 } )
@@ -659,12 +694,39 @@ class MemberRepository {
659694 where : {
660695 id : ids ,
661696 tenantId : currentTenant . id ,
697+ segmentId : options . currentSegments . map ( ( s ) => s . id ) ,
662698 } ,
663699 force,
664700 transaction,
665701 } )
666702 }
667703
704+ static async getSegments (
705+ memberIds : string [ ] ,
706+ options : IRepositoryOptions ,
707+ ) : Promise < Map < string , SegmentData [ ] > > {
708+ const results = new Map < string , SegmentData [ ] > ( )
709+
710+ const transaction = SequelizeRepository . getTransaction ( options )
711+ const seq = SequelizeRepository . getSequelize ( options )
712+
713+ const query = `
714+ select "memberId", "segmentId", "tenantId", "createdAt" from "memberSegments" where "memberId" in (:memberIds) order by "createdAt" asc;
715+ `
716+
717+ await seq . query ( query , {
718+ replacements : {
719+ memberIds,
720+ } ,
721+ type : QueryTypes . SELECT ,
722+ transaction,
723+ } )
724+
725+ // TODO: complete
726+
727+ return results
728+ }
729+
668730 static async getIdentities (
669731 memberIds : string [ ] ,
670732 options : IRepositoryOptions ,
@@ -725,6 +787,7 @@ class MemberRepository {
725787 if ( ! ignoreTenant ) {
726788 const currentTenant = SequelizeRepository . getCurrentTenant ( options )
727789 where . tenantId = currentTenant . id
790+ where . segmentId = options . currentSegments . map ( ( s ) => s . id )
728791 }
729792
730793 const record = await options . database . member . findOne ( {
0 commit comments