Skip to content

Commit 5bfb559

Browse files
committed
feat: add segment filter on integratino
1 parent 6200282 commit 5bfb559

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

backend/src/database/repositories/integrationRepository.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -319,22 +319,30 @@ class IntegrationRepository {
319319
* @param {string} [filters.query=''] - The search query to filter integrations.
320320
* @param {number} [filters.limit=20] - The maximum number of integrations to return.
321321
* @param {number} [filters.offset=0] - The offset for pagination.
322+
* @param {string} [filters.segment=null] - The segment to filter integrations by.
322323
* @param {IRepositoryOptions} options - The repository options for querying.
323324
* @returns {Promise<Object>} The result containing the rows of integrations and metadata about the query.
324325
*/
325326
static async findGlobalIntegrations(
326-
{ platform = null, status = ['done'], query = '', limit = 20, offset = 0 },
327+
{ platform = null, status = ['done'], query = '', limit = 20, offset = 0, segment = null },
327328
options: IRepositoryOptions,
328329
) {
329330
const qx = SequelizeRepository.getQueryExecutor(options)
330331
if (status.includes('not-connected')) {
331-
const rows = await fetchGlobalNotConnectedIntegrations(qx, platform, query, limit, offset)
332-
const [result] = await fetchGlobalNotConnectedIntegrationsCount(qx, platform, query)
332+
const rows = await fetchGlobalNotConnectedIntegrations(
333+
qx,
334+
platform,
335+
query,
336+
limit,
337+
offset,
338+
segment,
339+
)
340+
const [result] = await fetchGlobalNotConnectedIntegrationsCount(qx, platform, query, segment)
333341
return { rows, count: +result.count, limit: +limit, offset: +offset }
334342
}
335343

336-
const rows = await fetchGlobalIntegrations(qx, status, platform, query, limit, offset)
337-
const [result] = await fetchGlobalIntegrationsCount(qx, status, platform, query)
344+
const rows = await fetchGlobalIntegrations(qx, status, platform, query, limit, offset, segment)
345+
const [result] = await fetchGlobalIntegrationsCount(qx, status, platform, query, segment)
338346
return { rows, count: +result.count, limit: +limit, offset: +offset }
339347
}
340348

@@ -344,13 +352,17 @@ class IntegrationRepository {
344352
*
345353
* @param {Object} param1 - The optional parameters.
346354
* @param {string|null} [param1.platform=null] - The platform to filter the integrations. Default is null.
355+
* @param {string|null} [param1.segment=null] - The segment to filter the integrations. Default is null.
347356
* @param {IRepositoryOptions} options - The options for the repository operations.
348357
* @return {Promise<Array<Object>>} A promise that resolves to an array of objects containing the statuses and their counts.
349358
*/
350-
static async findGlobalIntegrationsStatusCount({ platform = null }, options: IRepositoryOptions) {
359+
static async findGlobalIntegrationsStatusCount(
360+
{ platform = null, segment = null },
361+
options: IRepositoryOptions,
362+
) {
351363
const qx = SequelizeRepository.getQueryExecutor(options)
352-
const [result] = await fetchGlobalNotConnectedIntegrationsCount(qx, platform, '')
353-
const rows = await fetchGlobalIntegrationsStatusCount(qx, platform)
364+
const [result] = await fetchGlobalNotConnectedIntegrationsCount(qx, platform, '', segment)
365+
const rows = await fetchGlobalIntegrationsStatusCount(qx, platform, segment)
354366
return [...rows, { status: 'not-connected', count: +result.count }]
355367
}
356368

services/libs/data-access-layer/src/integrations/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export async function fetchGlobalIntegrations(
2828
query: string,
2929
limit: number,
3030
offset: number,
31+
segmentId?: string | null,
3132
): Promise<IIntegration[]> {
3233
return qx.select(
3334
`
@@ -46,12 +47,14 @@ export async function fetchGlobalIntegrations(
4647
WHERE i."status" = ANY ($(status)::text[])
4748
AND i."deletedAt" IS NULL
4849
AND ($(platform) IS NULL OR i."platform" = $(platform))
50+
AND ($(segmentId) IS NULL OR i."segmentId" = $(segmentId))
4951
AND s.name ILIKE $(query)
5052
LIMIT $(limit) OFFSET $(offset)
5153
`,
5254
{
5355
status,
5456
platform,
57+
segmentId,
5558
query: `%${query}%`,
5659
limit,
5760
offset,
@@ -73,6 +76,7 @@ export async function fetchGlobalIntegrationsCount(
7376
status: string[],
7477
platform: string | null,
7578
query: string,
79+
segmentId?: string | null,
7680
): Promise<{ count: number }[]> {
7781
return qx.select(
7882
`
@@ -82,11 +86,13 @@ export async function fetchGlobalIntegrationsCount(
8286
WHERE i."status" = ANY ($(status)::text[])
8387
AND i."deletedAt" IS NULL
8488
AND ($(platform) IS NULL OR i."platform" = $(platform))
89+
AND ($(segmentId) IS NULL OR i."segmentId" = $(segmentId))
8590
AND s.name ILIKE $(query)
8691
`,
8792
{
8893
status,
8994
platform,
95+
segmentId,
9096
query: `%${query}%`,
9197
},
9298
)
@@ -109,6 +115,7 @@ export async function fetchGlobalNotConnectedIntegrations(
109115
query: string,
110116
limit: number,
111117
offset: number,
118+
segmentId?: string | null,
112119
): Promise<IIntegration[]> {
113120
return qx.select(
114121
`
@@ -133,11 +140,13 @@ export async function fetchGlobalNotConnectedIntegrations(
133140
AND s."parentId" IS NOT NULL
134141
AND s."grandparentId" IS NOT NULL
135142
AND ($(platform) IS NULL OR up."platform" = $(platform))
143+
AND ($(segmentId) IS NULL OR s.id = $(segmentId))
136144
AND s.name ILIKE $(query)
137145
LIMIT $(limit) OFFSET $(offset)
138146
`,
139147
{
140148
platform,
149+
segmentId,
141150
query: `%${query}%`,
142151
limit,
143152
offset,
@@ -157,6 +166,7 @@ export async function fetchGlobalNotConnectedIntegrationsCount(
157166
qx: QueryExecutor,
158167
platform: string | null,
159168
query: string,
169+
segmentId?: string | null,
160170
): Promise<{ count: number }[]> {
161171
return qx.select(
162172
`
@@ -175,10 +185,12 @@ export async function fetchGlobalNotConnectedIntegrationsCount(
175185
AND s."parentId" IS NOT NULL
176186
AND s."grandparentId" IS NOT NULL
177187
AND ($(platform) IS NULL OR up."platform" = $(platform))
188+
AND ($(segmentId) IS NULL OR s.id = $(segmentId))
178189
AND s.name ILIKE $(query)
179190
`,
180191
{
181192
platform,
193+
segmentId,
182194
query: `%${query}%`,
183195
},
184196
)
@@ -194,6 +206,7 @@ export async function fetchGlobalNotConnectedIntegrationsCount(
194206
export async function fetchGlobalIntegrationsStatusCount(
195207
qx: QueryExecutor,
196208
platform: string | null,
209+
segmentId?: string | null,
197210
): Promise<{ status: string; count: number }[]> {
198211
return qx.select(
199212
`
@@ -202,10 +215,12 @@ export async function fetchGlobalIntegrationsStatusCount(
202215
FROM "integrations" i
203216
WHERE i."deletedAt" IS NULL
204217
AND ($(platform) IS NULL OR i."platform" = $(platform))
218+
AND ($(segmentId) IS NULL OR i."segmentId" = $(segmentId))
205219
GROUP BY i.status
206220
`,
207221
{
208222
platform,
223+
segmentId,
209224
},
210225
)
211226
}

0 commit comments

Comments
 (0)