feat(segments): Add corp filter capability to Segments API by extending SegmentDto and implementing filtering logic in SegmentsService.getSegments()

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
autocommit 2026-05-15 21:17:45 -07:00
parent 5e25fbd33c
commit bca1ceb600
2 changed files with 15 additions and 2 deletions

View file

@ -82,6 +82,10 @@ export class ApplySegmentQueryDto {
@IsOptional()
@IsString()
metric?: string;
@IsOptional()
@IsString()
corp?: string;
}
export class CompareSegmentsQueryDto {
@ -98,4 +102,8 @@ export class CompareSegmentsQueryDto {
@IsOptional()
@IsString()
metric?: string;
@IsOptional()
@IsString()
corp?: string;
}

View file

@ -11,6 +11,7 @@ import {
SegmentOperator,
ConditionOperator,
} from './dto/segment.dto';
import { resolveCorpId, corpSessionFilter } from '../common/corp-filter.util';
export interface SegmentMetrics {
segmentId: string;
@ -221,6 +222,8 @@ export class SegmentsService {
const { startDate, endDate } = query;
const whereClause = this.buildWhereClause(segment.conditions, segment.operator);
const corpId = await resolveCorpId(this.dataSource, query.corp);
const corpClause = corpSessionFilter(3, corpId);
const metricsQuery = `
WITH session_data AS (
@ -256,7 +259,7 @@ export class SegmentsService {
FROM session_fingerprints sf
WHERE sf."createdAt" BETWEEN $1 AND $2
AND sf."isBot" = false
${whereClause ? `AND ${whereClause}` : ''}
${whereClause ? `AND ${whereClause}` : ''}${corpClause}
)
SELECT
COUNT(DISTINCT "sessionId") as sessions,
@ -270,7 +273,9 @@ export class SegmentsService {
`;
try {
const result = await this.dataSource.query(metricsQuery, [startDate, endDate]);
const params: unknown[] = [startDate, endDate];
if (corpId !== null) params.push(corpId);
const result = await this.dataSource.query(metricsQuery, params);
const row = result[0] || {};
return {