From d3e193c7dfe5a2c0bd37db9c3a134308530af0ad Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 30 Oct 2025 00:58:30 +0000 Subject: [PATCH] [PM-26676][PM-27291] Implement organization-aware state merging Update the scan operator in _setupReportState to detect organization changes and handle them differently from same-org updates. Logic: 1. If organizationId changed -> Use currState.data directly - Allows null to clear old org's data when switching - Fixes PM-26676: Org data persists when switching orgs 2. If same org (or no orgId) -> Preserve prevState.data when null - Maintains critical flags during loading states - Fixes PM-27291: Critical flags lost when generating new reports This resolves the conflict between PR #17008 and PR #17053: - PR #17008 needed: "Don't clear data when null" (preserve critical flags) - PR #17053 needed: "Clear data when null" (remove old org data) Now both behaviors work correctly by detecting the organization context. Testing: - Organization switching: Old org data cleared, new org data loads - Critical apps: Flags preserved across report generation - Cross-org: Each org maintains its own critical app flags --- .../risk-insights-orchestrator.service.ts | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/domain/risk-insights-orchestrator.service.ts b/bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/domain/risk-insights-orchestrator.service.ts index 59d65d8615a..00358b9eed1 100644 --- a/bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/domain/risk-insights-orchestrator.service.ts +++ b/bitwarden_license/bit-common/src/dirt/reports/risk-insights/services/domain/risk-insights-orchestrator.service.ts @@ -731,11 +731,24 @@ export class RiskInsightsOrchestratorService { newReportGeneration$, this._markUnmarkUpdates$, ).pipe( - scan((prevState: ReportState, currState: ReportState) => ({ - ...prevState, - ...currState, - data: currState.data !== null ? currState.data : prevState.data, - })), + scan((prevState: ReportState, currState: ReportState) => { + // If organization changed, use new state completely (don't preserve old data) + // This allows null data to clear old org's data when switching orgs + if (currState.organizationId && prevState.organizationId !== currState.organizationId) { + return { + ...currState, + data: currState.data, // Allow null to clear old org's data + }; + } + + // Same org (or no org ID): preserve data when currState.data is null + // This preserves critical flags during loading states within the same org + return { + ...prevState, + ...currState, + data: currState.data !== null ? currState.data : prevState.data, + }; + }), startWith({ loading: false, error: null, data: null }), shareReplay({ bufferSize: 1, refCount: true }), takeUntil(this._destroy$),