fix: prevent stale activityRelations in affiliations refresh (CM-1132)#4088
fix: prevent stale activityRelations in affiliations refresh (CM-1132)#4088
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. |
|
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. |
1 similar comment
|
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. |
|
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.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit b20038a. Configure here.
There was a problem hiding this comment.
Pull request overview
This PR fixes a correctness issue in affiliation refresh where memberSegmentAffiliations (manual, segment-scoped) could prevent updates to activityRelations in other segments, leaving stale organizationId values. The approach splits affiliation processing into a global “base” pass (member organizations only) plus per-segment “manual” passes, and ensures the base pass skips rows covered by manual affiliations.
Changes:
- Extend
TimelineItemwithskipManualAffiliationSegmentsto control base-pass skipping behavior. - Split timeline building into base (global) vs manual (per segment) passes, and discard manual “gap” items.
- Update the activityRelations update query to use alias-qualified columns and add a
NOT EXISTSguard to avoid overwriting rows covered by manual affiliations.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| services/libs/data-access-layer/src/member-organization-affiliation/types.ts | Adds skipManualAffiliationSegments to timeline items to support multi-pass processing. |
| services/libs/data-access-layer/src/member-organization-affiliation/index.ts | Splits timeline generation into base/manual passes and updates the batch update query with manual-coverage skipping logic. |
💡 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>
|
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. |
|
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. |
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. |
…imeline func 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. |
epipav
left a comment
There was a problem hiding this comment.
looks good 👍 added one comment
| AND msa."segmentId" = ar."segmentId" | ||
| AND msa."organizationId" IS NOT NULL | ||
| AND (msa."dateStart" IS NULL OR ar."timestamp" >= msa."dateStart"::date) | ||
| AND (msa."dateEnd" IS NULL OR ar."timestamp" < msa."dateEnd"::date + interval '1 day') |
There was a problem hiding this comment.
Why do we have dateEnd + interval '1 day' here?

Summary
When a member had
memberSegmentAffiliations(MSA) rows, the affiliation timeline builder mixed them together withmemberOrganizationsinto a single timeline.selectPrimaryWorkExperiencewould pick the MSA as the primary org for any overlapping date range (MSAs have highest precedence), producing aTimelineItemwith asegmentId.processAffiliationActivitiesthen appliedWHERE segmentId = Xfor that item, so activities in other segments were never updated. This left staleorganizationIdvalues inactivityRelationsfor the unprocessed segments.What changed
The timeline is now split into two passes:
Base timeline
Built from
memberOrganizationsonly, without asegmentId, and applies globally across all segments. When MSAs exist, each item is flagged withskipManualAffiliationSegments.Manual timeline
Built per MSA group (grouped by
segmentId), with each item scoped to its segment. Gap items (null org) are discarded since the base timeline already covers those date ranges.In
processAffiliationActivities, theskipManualAffiliationSegmentsflag adds aNOT EXISTS (SELECT 1 FROM memberSegmentAffiliations ...)condition so the base pass skips activity rows already covered by an MSA. Those rows are then handled by the manual pass.This ensures every
activityRelationsrow is updated exactly once, with no double updates and no missed rows.Note
Medium Risk
Updates the affiliation refresh logic and SQL update criteria for
activityRelations, which can affect large batches of rows and correctness of organization attribution across segments.Overview
Fixes affiliation refresh so
memberSegmentAffiliationsno longer cause some segments’activityRelations.organizationIdto remain stale.The timeline build is split into a base (global) pass from
memberOrganizationsand a manual (per-segment) pass from MSAs;buildTimelinenow accepts a singleaffiliationslist and can omit the fallback range.TimelineItemaddsskipManualAffiliationSegments, andprocessAffiliationActivitiesuses it to add aNOT EXISTSguard so the base pass won’t overwrite rows covered by MSAs; SQL conditions were also updated to consistently use thearalias.Reviewed by Cursor Bugbot for commit ef5ebae. Bugbot is set up for automated code reviews on this repo. Configure here.