refactor: move segment subproject expansion out of middleware#4240
refactor: move segment subproject expansion out of middleware#4240skwowet wants to merge 6 commits into
Conversation
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
|
Your PR title doesn't contain a Jira issue key. Consider adding it for better traceability. Example:
Projects:
Please add a Jira issue key to your PR title. |
There was a problem hiding this comment.
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
segmentMiddlewareto resolve only the segments provided by the client (viafindInIds) and keepreq.currentSegmentsaligned with the request. - Added DAL helpers
getSegmentSubprojects/getSegmentSubprojectIdsand 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 ofreq.query.segments, and removed a spuriousawaitin 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.
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
| 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' |
Signed-off-by: Yeganathan S <63534555+skwowet@users.noreply.github.com>
|
Overall looks good to me.
|
Summary
CM-189 expanded every request’s segments to subprojects inside
segmentMiddleware, soreq.currentSegmentsoften 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
findInIds—currentSegmentsmatches what the client sent (usually a project group ID). Subproject expansion happens at specific call sites viagetSegmentSubprojects/getSegmentSubprojectIds. Rollup-based paths (member/org query, bot suggestions) keep using the request segment ID unchanged.Changes
Middleware
resolveToLeafSegmentsand all leaf-expansion logicfindInIdsonly; keeptoStringArrayfor query/body parsingDAL & segment layer
getSegmentSubprojectsandgetSegmentSubprojectIdstodata-access-layergetSegmentSubprojectsfromsegmentRepositorysegmentService.getSegmentSubprojectsat the DALActivity
activityServicefor query, types, and channelsgetTenantSubprojectsand the “load all tenant subprojects” fallbackcurrentSegmentsfrom middleware instead ofreq.query.segmentsMember & organization repositories
Integrations
updateandfindAndCountAllsointegration/queryworks when the frontend sends only a project group ID (CM-189ActivityTypeFilterpath)Cleanup
activityTypescloning in middlewareawaiton syncgetTenantActivityTypesinsettingsRepositoryauth-axioscomment to match the new middleware behavior