Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions services/libs/common_services/src/services/member-organization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,34 @@ export function inferMemberOrganizationStintChanges(
continue
}

// 5. Determine the gap window between neighbor and targetDate, then check if another org
// holds a significant (>= 30 day) dated stint that overlaps that window (multi-stint guard)
// 5. Only split when another org clearly sits between this stint and the new date.
// Overlapping orgs are treated as concurrent evidence, not a break in the stint.
const isForward = targetDate > neighbor.dateEnd
const gapStart = isForward ? neighbor.dateEnd : targetDate
const gapEnd = isForward ? targetDate : neighbor.dateStart
const hasConflict = stints.some(
(s) =>
s.organizationId !== organizationId &&
s.dateStart &&
s.dateEnd &&
s.dateStart < gapEnd &&
s.dateEnd > gapStart &&
diff(s.dateStart, s.dateEnd) >= 30,
)
const hasSeparator = stints.some((s) => {
if (
s.organizationId === organizationId ||
!s.dateStart ||
!s.dateEnd ||
diff(s.dateStart, s.dateEnd) < 30
) {
return false
}

if (isForward) {
return (
(s.dateStart > neighbor.dateEnd && s.dateStart <= targetDate) ||
(s.dateEnd > neighbor.dateEnd && s.dateEnd <= targetDate)
)
}

return (
(s.dateStart >= targetDate && s.dateStart < neighbor.dateStart) ||
(s.dateEnd >= targetDate && s.dateEnd < neighbor.dateStart)
)
})

if (hasConflict) {
// 6a. Another org owns the gap — start a fresh stint rather than bridging
if (hasSeparator) {
// 6a. Another org clearly sits in between — start a fresh stint rather than bridging
stints.push({
id: null,
organizationId,
Expand Down
Loading