Skip to content

refactor: move segment subproject expansion out of middleware#4240

Open
skwowet wants to merge 6 commits into
mainfrom
fix/revert-segment-middleware-leaf-expansion
Open

refactor: move segment subproject expansion out of middleware#4240
skwowet wants to merge 6 commits into
mainfrom
fix/revert-segment-middleware-leaf-expansion

Conversation

@skwowet

@skwowet skwowet commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

Summary

CM-189 expanded every request’s segments to subprojects inside segmentMiddleware, so req.currentSegments often held hundreds of IDs before handlers ran. This PR reverts that middleware behavior and moves subproject resolution into the DAL, applied only where the data model requires it.

Middleware goes back to findInIdscurrentSegments matches what the client sent (usually a project group ID). Subproject expansion happens at specific call sites via getSegmentSubprojects / getSegmentSubprojectIds. Rollup-based paths (member/org query, bot suggestions) keep using the request segment ID unchanged.

Changes

Middleware

  • Remove resolveToLeafSegments and all leaf-expansion logic
  • Resolve segments with findInIds only; keep toStringArray for query/body parsing

DAL & segment layer

  • Add getSegmentSubprojects and getSegmentSubprojectIds to data-access-layer
  • Remove duplicate getSegmentSubprojects from segmentRepository
  • Point segmentService.getSegmentSubprojects at the DAL

Activity

  • Expand segments in activityService for query, types, and channels
  • Remove getTenantSubprojects and the “load all tenant subprojects” fallback
  • Activity API handlers use currentSegments from middleware instead of req.query.segments

Member & organization repositories

  • Expand to subproject IDs for junction writes, excludes, autocomplete, activity counts, and merge suggestions

Integrations

  • Expand segments in update and findAndCountAll so integration/query works when the frontend sends only a project group ID (CM-189 ActivityTypeFilter path)

Cleanup

  • Drop unused middleware imports, logging, and activityTypes cloning in middleware
  • Fix spurious await on sync getTenantActivityTypes in settingsRepository
  • Update auth-axios comment to match the new middleware behavior

skwowet added 2 commits June 19, 2026 16:55
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
@skwowet skwowet requested a review from ulemons June 19, 2026 11:33
@skwowet skwowet self-assigned this Jun 19, 2026
Copilot AI review requested due to automatic review settings June 19, 2026 11:33
@github-actions

Copy link
Copy Markdown
Contributor

⚠️ Jira Issue Key Missing

Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability.

Example:

  • feat: add user authentication (CM-123)
  • feat: add user authentication (IN-123)

Projects:

  • CM: Community Data Platform
  • IN: Insights

Please add a Jira issue key to your PR title.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors segment resolution so the request middleware no longer expands project-group/project segment IDs into leaf subproject IDs for every request. Instead, subproject expansion is moved into the data-access-layer and applied only at call sites where subproject IDs are required (e.g., activity queries and segment junction writes), reducing unnecessary per-request work and avoiding large req.currentSegments payloads.

Changes:

  • Reverted segmentMiddleware to resolve only the segments provided by the client (via findInIds) and keep req.currentSegments aligned with the request.
  • Added DAL helpers getSegmentSubprojects / getSegmentSubprojectIds and rewired backend call sites (activity, member/org/integration repositories) to expand segments only when needed.
  • Simplified activity types/channels resolution to use req.currentSegments (middleware-driven) instead of req.query.segments, and removed a spurious await in settings.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
services/libs/data-access-layer/src/segments/index.ts Adds DAL subproject expansion helpers used by backend call sites.
frontend/src/shared/axios/auth-axios.js Updates comment to reflect new middleware behavior around default segments.
backend/src/services/segmentService.ts Routes getSegmentSubprojects through DAL and makes tenant activity type aggregation synchronous.
backend/src/services/activityService.ts Expands current segments to subprojects for activity query/types/channels.
backend/src/middlewares/segmentMiddleware.ts Removes leaf-expansion logic; resolves request segments as-is and defaults when missing.
backend/src/database/repositories/settingsRepository.ts Removes await from now-synchronous getTenantActivityTypes.
backend/src/database/repositories/segmentRepository.ts Removes duplicate getSegmentSubprojects implementation.
backend/src/database/repositories/organizationRepository.ts Expands current segments to subproject IDs where org↔segment writes/queries require leaf IDs.
backend/src/database/repositories/memberRepository.ts Expands current segments to subproject IDs for member segment writes and related queries.
backend/src/database/repositories/integrationRepository.ts Expands current segments to subproject IDs for integration filtering when frontend sends project-group IDs.
backend/src/api/activity/activityTypes.ts Uses middleware-resolved segments (via service) rather than reading query segments directly.
backend/src/api/activity/activityChannels.ts Uses middleware-resolved segments (via service) rather than reading query segments directly.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread services/libs/data-access-layer/src/segments/index.ts Outdated
Comment thread backend/src/middlewares/segmentMiddleware.ts Outdated
Comment thread backend/src/services/activityService.ts
Comment thread backend/src/services/segmentService.ts Outdated
skwowet added 2 commits June 19, 2026 17:36
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 19, 2026 12:18

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated 2 comments.

Comment on lines +198 to +201
join segment_level sl on (sl.level = 'child' and s.id = sl.id)
or (sl.level = 'parent' and s."parentSlug" = sl.slug and s."grandparentSlug" is not null)
or (sl.level = 'grandparent' and s."grandparentSlug" = sl.slug)
where status = 'active'
Comment thread backend/src/middlewares/segmentMiddleware.ts Outdated
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
@ulemons

ulemons commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

Overall looks good to me.
Before merging I would check 2 things:

  1. is there any other service referring to subprojectId which now will get projectGroupId ? (e.g. memberService, integrationSerice.. )
  2. Before the expand was made one time in the middleware, are we sure we are not calling the getSegmentSubprojectIds multiple times now ? (e.g. create -> calls to update or something )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants