Skip to content

Commit e11b637

Browse files
authored
refactor: move segment subproject expansion out of middleware (#4240)
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
1 parent 71cd667 commit e11b637

12 files changed

Lines changed: 214 additions & 252 deletions

File tree

backend/src/api/activity/activityChannels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import PermissionChecker from '../../services/user/permissionChecker'
1818
export default async (req, res) => {
1919
new PermissionChecker(req).validateHas(Permissions.values.activityRead)
2020

21-
const payload = await new ActivityService(req).findActivityChannels(req.query.segments)
21+
const payload = await new ActivityService(req).findActivityChannels()
2222

2323
await req.responseHandler.success(req, res, payload)
2424
}

backend/src/api/activity/activityTypes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import PermissionChecker from '../../services/user/permissionChecker'
44

55
export default async (req, res) => {
66
new PermissionChecker(req).validateHas(Permissions.values.activityRead)
7-
const payload = await new ActivityService(req).findActivityTypes(req.query.segments)
7+
8+
const payload = await new ActivityService(req).findActivityTypes()
89

910
await req.responseHandler.success(req, res, payload)
1011
}

backend/src/database/repositories/integrationRepository.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '@crowd/data-access-layer/src/integrations'
1414
import { QueryExecutor } from '@crowd/data-access-layer/src/queryExecutor'
1515
import { getReposGroupedByOrgForIntegrations } from '@crowd/data-access-layer/src/repositories'
16+
import { getSegmentSubprojectIds } from '@crowd/data-access-layer/src/segments'
1617
import { IntegrationRunState, PlatformType } from '@crowd/types'
1718

1819
import SequelizeFilterUtils from '../utils/sequelizeFilterUtils'
@@ -66,10 +67,15 @@ class IntegrationRepository {
6667

6768
const transaction = SequelizeRepository.getTransaction(options)
6869

70+
const qx = SequelizeRepository.getQueryExecutor(options)
71+
const currentSegments = SequelizeRepository.getSegmentIds(options)
72+
73+
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
74+
6975
let record = await options.database.integration.findOne({
7076
where: {
7177
id,
72-
segmentId: SequelizeRepository.getSegmentIds(options),
78+
segmentId: subprojectIds,
7379
},
7480
transaction,
7581
})
@@ -450,6 +456,10 @@ class IntegrationRepository {
450456
nestedFields: {
451457
sentiment: 'sentiment.sentiment',
452458
},
459+
// QueryParser filters on req.currentSegments directly (e.g., projectGroupId).
460+
// Since integrations are stored per subproject, segment filtering is applied manually below
461+
// after expanding to subprojectIds.
462+
withSegments: false,
453463
},
454464
options,
455465
)
@@ -461,11 +471,19 @@ class IntegrationRepository {
461471
offset,
462472
})
463473

474+
const qx = SequelizeRepository.getQueryExecutor(options)
475+
const currentSegments = SequelizeRepository.getSegmentIds(options)
476+
477+
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
478+
479+
const segmentWhere = { segmentId: subprojectIds }
480+
const where = parsed.where ? { [Op.and]: [parsed.where, segmentWhere] } : segmentWhere
481+
464482
let {
465483
rows,
466484
count, // eslint-disable-line prefer-const
467485
} = await options.database.integration.findAndCountAll({
468-
...(parsed.where ? { where: parsed.where } : {}),
486+
where,
469487
...(parsed.having ? { having: parsed.having } : {}),
470488
order: parsed.order,
471489
limit: limit ? parsed.limit : undefined,

backend/src/database/repositories/memberRepository.ts

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {
5151
} from '@crowd/data-access-layer/src/members/segments'
5252
import { IDbMemberData } from '@crowd/data-access-layer/src/members/types'
5353
import { optionsQx } from '@crowd/data-access-layer/src/queryExecutor'
54-
import { fetchManySegments } from '@crowd/data-access-layer/src/segments'
54+
import { fetchManySegments, getSegmentSubprojectIds } from '@crowd/data-access-layer/src/segments'
5555
import { ActivityDisplayService } from '@crowd/integrations'
5656
import {
5757
ALL_PLATFORM_TYPES,
@@ -148,6 +148,9 @@ class MemberRepository {
148148
)
149149

150150
const qx = SequelizeRepository.getQueryExecutor(options)
151+
const currentSegments = SequelizeRepository.getSegmentIds(options)
152+
153+
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
151154

152155
if (data.identities) {
153156
for (const i of data.identities as IMemberIdentity[]) {
@@ -182,19 +185,15 @@ class MemberRepository {
182185
}
183186
}
184187

185-
await includeMemberToSegments(
186-
qx,
187-
record.id,
188-
options.currentSegments.map((s) => s.id),
189-
)
188+
await includeMemberToSegments(qx, record.id, subprojectIds)
190189

191190
const memberService = new CommonMemberService(optionsQx(options), options.temporal, options.log)
192191

193192
await memberService.updateMemberOrganizations(
194193
record.id,
195194
data.organizations,
196195
true,
197-
options.currentSegments.map((s) => s.id),
196+
subprojectIds,
198197
options,
199198
)
200199

@@ -234,10 +233,15 @@ class MemberRepository {
234233

235234
const bulkDeleteMemberSegments = `DELETE FROM "memberSegments" WHERE "memberId" in (:memberIds) and "segmentId" in (:segmentIds);`
236235

236+
const qx = SequelizeRepository.getQueryExecutor(options)
237+
const currentSegments = SequelizeRepository.getSegmentIds(options)
238+
239+
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
240+
237241
await seq.query(bulkDeleteMemberSegments, {
238242
replacements: {
239243
memberIds,
240-
segmentIds: SequelizeRepository.getSegmentIds(options),
244+
segmentIds: subprojectIds,
241245
},
242246
type: QueryTypes.DELETE,
243247
transaction,
@@ -294,13 +298,12 @@ class MemberRepository {
294298
const HIGH_CONFIDENCE_LOWER_BOUND = 0.9
295299
const MEDIUM_CONFIDENCE_LOWER_BOUND = 0.7
296300

301+
const qx = SequelizeRepository.getQueryExecutor(options)
297302
const currentSegments = SequelizeRepository.getSegmentIds(options)
298303

299-
const segmentIds = (
300-
await new SegmentRepository(options).getSegmentSubprojects(currentSegments)
301-
).map((s) => s.id)
304+
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
302305

303-
if (segmentIds.length === 0) {
306+
if (subprojectIds.length === 0) {
304307
return args.countOnly
305308
? { count: '0' }
306309
: {
@@ -361,7 +364,7 @@ class MemberRepository {
361364
similarityFilter,
362365
displayNameFilter,
363366
{
364-
segmentIds,
367+
segmentIds: subprojectIds,
365368
displayName: args?.filter?.displayName ? `${args.filter.displayName}%` : undefined,
366369
memberId: args?.filter?.memberId,
367370
},
@@ -403,7 +406,7 @@ class MemberRepository {
403406
`,
404407
{
405408
replacements: {
406-
segmentIds,
409+
segmentIds: subprojectIds,
407410
limit: args.limit,
408411
offset: args.offset,
409412
displayName: args?.filter?.displayName ? `${args.filter.displayName}%` : undefined,
@@ -509,7 +512,7 @@ class MemberRepository {
509512
similarityFilter,
510513
displayNameFilter,
511514
{
512-
segmentIds,
515+
segmentIds: subprojectIds,
513516
memberId: args?.filter?.memberId,
514517
displayName: args?.filter?.displayName ? `${args.filter.displayName}%` : undefined,
515518
},
@@ -888,13 +891,19 @@ class MemberRepository {
888891
!manualChange, // no need to track for audit if it's not a manual change
889892
)
890893

894+
const qx = SequelizeRepository.getQueryExecutor(options)
895+
const subprojectIds = await getSegmentSubprojectIds(
896+
qx,
897+
SequelizeRepository.getSegmentIds(options),
898+
)
899+
891900
const memberService = new CommonMemberService(optionsQx(options), options.temporal, options.log)
892901

893902
await memberService.updateMemberOrganizations(
894903
record.id,
895904
data.organizations,
896905
data.organizationsReplace,
897-
options.currentSegments.map((s) => s.id),
906+
subprojectIds,
898907
options,
899908
)
900909

@@ -915,11 +924,7 @@ class MemberRepository {
915924
}
916925

917926
if (options.currentSegments && options.currentSegments.length > 0) {
918-
await includeMemberToSegments(
919-
optionsQx(options),
920-
record.id,
921-
options.currentSegments.map((s) => s.id),
922-
)
927+
await includeMemberToSegments(qx, record.id, subprojectIds)
923928
}
924929

925930
// Before upserting identities, check if they already exist
@@ -998,8 +1003,6 @@ class MemberRepository {
9981003
}
9991004
}
10001005

1001-
const qx = SequelizeRepository.getQueryExecutor(options)
1002-
10031006
if (data.identitiesToCreate && data.identitiesToCreate.length > 0) {
10041007
for (const i of data.identitiesToCreate) {
10051008
await createMemberIdentity(qx, {
@@ -1801,6 +1804,11 @@ class MemberRepository {
18011804

18021805
const where = { [Op.and]: whereAnd }
18031806

1807+
const qx = SequelizeRepository.getQueryExecutor(options)
1808+
const currentSegments = SequelizeRepository.getSegmentIds(options)
1809+
1810+
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
1811+
18041812
const records = await options.database.member.findAll({
18051813
attributes: ['id', 'displayName', 'attributes'],
18061814
where,
@@ -1816,7 +1824,7 @@ class MemberRepository {
18161824
model: options.database.segment,
18171825
as: 'segments',
18181826
where: {
1819-
id: SequelizeRepository.getSegmentIds(options),
1827+
id: subprojectIds,
18201828
},
18211829
},
18221830
],

backend/src/database/repositories/organizationRepository.ts

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import {
3434
} from '@crowd/data-access-layer/src/organizations'
3535
import { findAttribute } from '@crowd/data-access-layer/src/organizations/attributesConfig'
3636
import { optionsQx } from '@crowd/data-access-layer/src/queryExecutor'
37-
import { findSegmentById } from '@crowd/data-access-layer/src/segments'
37+
import { findSegmentById, getSegmentSubprojectIds } from '@crowd/data-access-layer/src/segments'
3838
import {
3939
IMemberRenderFriendlyRole,
4040
IMemberRoleWithOrganization,
@@ -163,11 +163,12 @@ class OrganizationRepository {
163163
await OrganizationRepository.setIdentities(record.id, data.identities, options)
164164
}
165165

166-
await addOrgsToSegments(
167-
optionsQx(options),
168-
options.currentSegments.map((s) => s.id),
169-
[record.id],
170-
)
166+
const currentSegments = SequelizeRepository.getSegmentIds(options)
167+
168+
const qx = SequelizeRepository.getQueryExecutor(options)
169+
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
170+
171+
await addOrgsToSegments(qx, subprojectIds, [record.id])
171172

172173
return this.findById(record.id, options)
173174
}
@@ -182,10 +183,15 @@ class OrganizationRepository {
182183

183184
const bulkDeleteOrganizationSegments = `DELETE FROM "organizationSegments" WHERE "organizationId" in (:organizationIds) and "segmentId" in (:segmentIds);`
184185

186+
const currentSegments = SequelizeRepository.getSegmentIds(options)
187+
188+
const qx = SequelizeRepository.getQueryExecutor(options)
189+
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
190+
185191
await seq.query(bulkDeleteOrganizationSegments, {
186192
replacements: {
187193
organizationIds,
188-
segmentIds: SequelizeRepository.getSegmentIds(options),
194+
segmentIds: subprojectIds,
189195
},
190196
type: QueryTypes.DELETE,
191197
transaction,
@@ -430,11 +436,12 @@ class OrganizationRepository {
430436
}
431437

432438
if (data.segments) {
433-
await addOrgsToSegments(
434-
optionsQx(options),
435-
options.currentSegments.map((s) => s.id),
436-
[record.id],
437-
)
439+
const qx = SequelizeRepository.getQueryExecutor(options)
440+
const currentSegments = SequelizeRepository.getSegmentIds(options)
441+
442+
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
443+
444+
await addOrgsToSegments(qx, subprojectIds, [record.id])
438445
}
439446

440447
await captureApiChange(
@@ -842,10 +849,10 @@ class OrganizationRepository {
842849
const HIGH_CONFIDENCE_LOWER_BOUND = 0.9
843850
const MEDIUM_CONFIDENCE_LOWER_BOUND = 0.7
844851

852+
const qx = SequelizeRepository.getQueryExecutor(options)
845853
const currentSegments = SequelizeRepository.getSegmentIds(options)
846-
const segmentIds = (
847-
await new SegmentRepository(options).getSegmentSubprojects(currentSegments)
848-
).map((s) => s.id)
854+
855+
const segmentIds = await getSegmentSubprojectIds(qx, currentSegments)
849856

850857
let similarityFilter = ''
851858
const similarityConditions = []
@@ -1619,7 +1626,10 @@ class OrganizationRepository {
16191626

16201627
static async findAllAutocomplete(query, limit, options: IRepositoryOptions) {
16211628
const tenant = SequelizeRepository.getCurrentTenant(options)
1622-
const segmentIds = SequelizeRepository.getSegmentIds(options)
1629+
const qx = SequelizeRepository.getQueryExecutor(options)
1630+
const currentSegments = SequelizeRepository.getSegmentIds(options)
1631+
1632+
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
16231633

16241634
const records = await options.database.sequelize.query(
16251635
`
@@ -1643,7 +1653,7 @@ class OrganizationRepository {
16431653
replacements: {
16441654
limit: limit ? Number(limit) : 20,
16451655
tenantId: tenant.id,
1646-
segmentIds,
1656+
segmentIds: subprojectIds,
16471657
queryLike: `%${query}%`,
16481658
queryExact: query,
16491659
uuid: validator.isUUID(query) ? query : null,
@@ -1758,9 +1768,7 @@ class OrganizationRepository {
17581768
const qx = SequelizeRepository.getQueryExecutor(options)
17591769
const activityTypes = SegmentRepository.getActivityTypes(options)
17601770

1761-
const subprojectIds = (
1762-
await new SegmentRepository(options).getSegmentSubprojects(currentSegments)
1763-
).map((s) => s.id)
1771+
const subprojectIds = await getSegmentSubprojectIds(qx, currentSegments)
17641772

17651773
const result = await queryActivities(
17661774
{

0 commit comments

Comments
 (0)