@@ -182,39 +182,71 @@ class SegmentRepository extends RepositoryBase<
182
182
'sourceId' ,
183
183
'sourceParentId' ,
184
184
'customActivityTypes' ,
185
- 'activityChannels' ,
186
185
] . includes ( key ) ,
187
186
)
188
187
189
- let segmentUpdateQuery = `UPDATE segments SET `
190
- const replacements = { } as any
188
+ if ( updateFields . length > 0 ) {
189
+ let segmentUpdateQuery = `UPDATE segments SET `
190
+ const replacements = { } as any
191
191
192
- for ( const field of updateFields ) {
193
- segmentUpdateQuery += ` "${ field } " = :${ field } `
194
- replacements [ field ] = data [ field ]
192
+ for ( const field of updateFields ) {
193
+ segmentUpdateQuery += ` "${ field } " = :${ field } `
194
+ replacements [ field ] = data [ field ]
195
195
196
- if ( updateFields [ updateFields . length - 1 ] !== field ) {
197
- segmentUpdateQuery += ', '
196
+ if ( updateFields [ updateFields . length - 1 ] !== field ) {
197
+ segmentUpdateQuery += ', '
198
+ }
198
199
}
199
- }
200
200
201
- segmentUpdateQuery += ` WHERE id = :id and "tenantId" = :tenantId `
202
- replacements . tenantId = this . options . currentTenant . id
203
- replacements . id = id
201
+ segmentUpdateQuery += ` WHERE id = :id and "tenantId" = :tenantId `
202
+ replacements . tenantId = this . options . currentTenant . id
203
+ replacements . id = id
204
204
205
- if ( replacements . customActivityTypes ) {
206
- replacements . customActivityTypes = JSON . stringify ( replacements . customActivityTypes )
207
- }
205
+ if ( replacements . customActivityTypes ) {
206
+ replacements . customActivityTypes = JSON . stringify ( replacements . customActivityTypes )
207
+ }
208
208
209
- if ( replacements . activityChannels ) {
210
- replacements . activityChannels = JSON . stringify ( replacements . activityChannels )
209
+ await this . options . database . sequelize . query ( segmentUpdateQuery , {
210
+ replacements,
211
+ type : QueryTypes . UPDATE ,
212
+ transaction,
213
+ } )
211
214
}
212
215
213
- await this . options . database . sequelize . query ( segmentUpdateQuery , {
214
- replacements,
215
- type : QueryTypes . UPDATE ,
216
- transaction,
217
- } )
216
+ if ( data . activityChannels && typeof data . activityChannels === 'object' ) {
217
+ if ( Object . keys ( data . activityChannels ) . length > 0 ) {
218
+ const replacements = { }
219
+ let valuePlaceholders = ''
220
+ Object . keys ( data . activityChannels ) . forEach ( ( platform ) => {
221
+ data . activityChannels [ platform ] . forEach ( ( channel , i ) => {
222
+ valuePlaceholders += data . activityChannels [ platform ]
223
+ . map (
224
+ ( ) =>
225
+ `(:tenantId_${ platform } _${ i } , :segmentId_${ platform } _${ i } , :platform_${ platform } _${ i } , :channel_${ platform } _${ i } )` ,
226
+ )
227
+ . join ( ', ' )
228
+
229
+ replacements [ `tenantId_${ platform } _${ i } ` ] = this . options . currentTenant . id
230
+ replacements [ `segmentId_${ platform } _${ i } ` ] = id
231
+ replacements [ `platform_${ platform } _${ i } ` ] = platform
232
+ replacements [ `channel_${ platform } _${ i } ` ] = channel
233
+ } )
234
+ } )
235
+
236
+ await this . options . database . sequelize . query (
237
+ `
238
+ INSERT INTO "segmentActivityChannels" ("tenantId", "segmentId", "platform", "channel")
239
+ VALUES ${ valuePlaceholders }
240
+ ON CONFLICT DO NOTHING;
241
+ ` ,
242
+ {
243
+ replacements,
244
+ type : QueryTypes . INSERT ,
245
+ transaction,
246
+ } ,
247
+ )
248
+ }
249
+ }
218
250
219
251
return this . findById ( id )
220
252
}
@@ -298,11 +330,22 @@ class SegmentRepository extends RepositoryBase<
298
330
const transaction = this . transaction
299
331
300
332
const records = await this . options . database . sequelize . query (
301
- `SELECT *
302
- FROM segments
303
- WHERE id in (:ids)
304
- and "tenantId" = :tenantId;
305
- ` ,
333
+ `SELECT
334
+ s.*,
335
+ json_agg(sac."activityChannels") AS "activityChannels"
336
+ FROM segments s
337
+ LEFT JOIN (
338
+ SELECT
339
+ "tenantId",
340
+ json_build_object(concat(platform), json_agg(sac.channel)) AS "activityChannels"
341
+ FROM "segmentActivityChannels" sac
342
+ WHERE "tenantId" = :tenantId
343
+ GROUP BY "tenantId", "platform"
344
+ ) sac
345
+ ON sac."tenantId" = s."tenantId"
346
+ WHERE id in (:ids)
347
+ AND s."tenantId" = :tenantId
348
+ GROUP BY s.id;` ,
306
349
{
307
350
replacements : {
308
351
ids,
@@ -313,6 +356,10 @@ class SegmentRepository extends RepositoryBase<
313
356
} ,
314
357
)
315
358
359
+ records . forEach ( ( row ) => {
360
+ row . activityChannels = Object . assign ( { } , ...row . activityChannels )
361
+ } )
362
+
316
363
return records . map ( ( sr ) => SegmentRepository . populateRelations ( sr ) )
317
364
}
318
365
@@ -333,11 +380,22 @@ class SegmentRepository extends RepositoryBase<
333
380
const transaction = this . transaction
334
381
335
382
const records = await this . options . database . sequelize . query (
336
- `SELECT *
337
- FROM segments
338
- WHERE id = :id
339
- and "tenantId" = :tenantId;
340
- ` ,
383
+ `SELECT
384
+ s.*,
385
+ json_agg(sac."activityChannels") AS "activityChannels"
386
+ FROM segments s
387
+ LEFT JOIN (
388
+ SELECT
389
+ "tenantId",
390
+ json_build_object(concat(platform), json_agg(sac.channel)) AS "activityChannels"
391
+ FROM "segmentActivityChannels" sac
392
+ WHERE "tenantId" = :tenantId
393
+ GROUP BY "tenantId", "platform"
394
+ ) sac
395
+ ON sac."tenantId" = s."tenantId"
396
+ WHERE s.id = :id
397
+ AND s."tenantId" = :tenantId
398
+ GROUP BY s.id;` ,
341
399
{
342
400
replacements : {
343
401
id,
@@ -353,6 +411,7 @@ class SegmentRepository extends RepositoryBase<
353
411
}
354
412
355
413
const record = records [ 0 ]
414
+ record . activityChannels = Object . assign ( { } , ...record . activityChannels )
356
415
357
416
if ( SegmentRepository . isProjectGroup ( record ) ) {
358
417
// find projects
@@ -569,17 +628,27 @@ class SegmentRepository extends RepositoryBase<
569
628
570
629
const subprojects = await this . options . database . sequelize . query (
571
630
`
572
- select s.*,
573
- count(*) over () as "totalCount"
574
- from segments s
575
- where s."grandparentSlug" is not null
576
- and s."parentSlug" is not null
577
- and s."tenantId" = :tenantId
578
- ${ searchQuery }
579
- GROUP BY s."id"
580
- ORDER BY s."name"
581
- ${ this . getPaginationString ( criteria ) } ;
582
- ` ,
631
+ SELECT
632
+ COUNT(DISTINCT s.id) AS count,
633
+ s.*,
634
+ json_agg(sac."activityChannels") AS "activityChannels"
635
+ FROM segments s
636
+ LEFT JOIN (
637
+ SELECT
638
+ "tenantId",
639
+ json_build_object(concat(platform), json_agg(sac.channel)) AS "activityChannels"
640
+ FROM "segmentActivityChannels" sac
641
+ WHERE "tenantId" = :tenantId
642
+ GROUP BY "tenantId", "platform"
643
+ ) sac
644
+ ON sac."tenantId" = s."tenantId"
645
+ WHERE s."grandparentSlug" IS NOT NULL
646
+ AND s."parentSlug" IS NOT NULL
647
+ AND s."tenantId" = :tenantId
648
+ ${ searchQuery }
649
+ GROUP BY s.id
650
+ ${ this . getPaginationString ( criteria ) } ;
651
+ ` ,
583
652
{
584
653
replacements : {
585
654
tenantId : this . currentTenant . id ,
@@ -592,15 +661,14 @@ class SegmentRepository extends RepositoryBase<
592
661
} ,
593
662
)
594
663
595
- const count = subprojects . length > 0 ? Number . parseInt ( subprojects [ 0 ] . totalCount , 10 ) : 0
664
+ const count = subprojects . length > 0 ? Number . parseInt ( subprojects [ 0 ] . count , 10 ) : 0
596
665
597
- const integrationsBySegments = await this . queryIntegrationsForSubprojects ( subprojects )
598
-
599
- const rows = subprojects . map ( ( i ) => {
600
- let subproject = removeFieldsFromObject ( i , 'totalCount' )
601
- subproject = SegmentRepository . populateRelations ( subproject )
602
- subproject . integrations = integrationsBySegments [ subproject . id ] || [ ]
603
- return subproject
666
+ const rows = subprojects
667
+ rows . forEach ( ( row , i ) => {
668
+ rows [ i ] . activityChannels = rows [ i ] . activityChannels [ 0 ]
669
+ if ( rows [ i ] . activityChannels === null ) {
670
+ rows [ i ] . activityChannels = { }
671
+ }
604
672
} )
605
673
606
674
// TODO: Add member count to segments after implementing member relations
@@ -657,14 +725,17 @@ class SegmentRepository extends RepositoryBase<
657
725
658
726
static getActivityChannels ( options : IRepositoryOptions ) {
659
727
const channels = { }
728
+
660
729
for ( const segment of options . currentSegments ) {
661
- for ( const platform of Object . keys ( segment . activityChannels ) ) {
662
- if ( ! channels [ platform ] ) {
663
- channels [ platform ] = new Set < string > ( segment . activityChannels [ platform ] )
664
- } else {
665
- segment . activityChannels [ platform ] . forEach ( ( ch ) =>
666
- ( channels [ platform ] as Set < string > ) . add ( ch ) ,
667
- )
730
+ if ( segment . activityChannels ) {
731
+ for ( const platform of Object . keys ( segment . activityChannels ) ) {
732
+ if ( ! channels [ platform ] ) {
733
+ channels [ platform ] = new Set < string > ( segment . activityChannels [ platform ] )
734
+ } else {
735
+ segment . activityChannels [ platform ] . forEach ( ( ch ) =>
736
+ ( channels [ platform ] as Set < string > ) . add ( ch ) ,
737
+ )
738
+ }
668
739
}
669
740
}
670
741
}
0 commit comments