fix(api/dashboard): exclude revoked commitments, stop fabricating current_savings, provider-filter KPIs (adversarial-review follow-ups)#1452
Conversation
|
@coderabbitai review |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughThe change excludes revoked commitments from active queries and KPI calculations, adds revoked totals to purchase-history summaries, and filters dashboard commitment metrics by the selected provider. ChangesCommitment KPI consistency
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
✅ Action performedReview finished.
|
|
@coderabbitai full review |
✅ Action performedFull review finished. You're currently rate limited under our Fair Usage Limits Policy. Your recent PR review activity is in the 95th percentile or higher among CodeRabbit users, so adaptive limits apply. Your next review will be available in 57 minutes. |
64b4a64 to
bb3d80d
Compare
|
@coderabbitai full review |
✅ Action performedFull review finished. You're currently rate limited under our Fair Usage Limits Policy. Your recent PR review activity is in the 95th percentile or higher among CodeRabbit users, so adaptive limits apply. Your next review will be available in 24 minutes. |
…rent_savings, provider-filter KPIs (adversarial-review follow-ups) Defect #2 (HIGH): revoked/refunded commitments were counted as active on every money path. Fix applies the predicate at three layers: - SQL: add `revoked_at IS NULL` to GetActivePurchaseHistory WHERE clause (shared across dashboard KPIs, inventory, and analytics snapshots). - In-memory defense-in-depth: isActiveCommitment now returns false when p.RevokedAt != nil, guarding callers that use GetPurchaseHistoryFiltered. - summarizePurchaseHistory: add revoked branch that increments TotalRevoked and skips dollar totals, with TotalRevoked added to HistorySummary. Defect #3 (HIGH): summarizeRecommendationsWithCoverage was setting CurrentSavings = PotentialSavings in the reducer, fabricating realized savings for services with no active commitments. Remove the spurious `svc.CurrentSavings += scaled` line; CurrentSavings is exclusively owned by the getDashboardSummary loop that overwrites from actual purchase_history data. Update tests that asserted the buggy behavior. Defect #4 (MEDIUM): calculateCommitmentMetrics ignored params["provider"], mixing all-provider KPIs (ActiveCommitments, CommittedMonthly, YTDSavings, CurrentSavings, CurrentCoverage) while PotentialMonthlySavings was already filtered. Add provider parameter to calculateCommitmentMetrics and fetchCommitmentPurchases; filter purchases in-memory after the SQL fetch, mirroring fetchCommitmentRecords' existing pattern. Defect #7 (LOW): fetchCommitmentPurchases swallowed GetActivePurchaseHistory errors silently with only a comment. Now emits logging.Errorf so dashboard tiles show "--" (zeroed KPIs) with a visible log trace rather than fabricated $0 with no signal. Regression tests added that fail pre-fix and pass post-fix: - TestIsActiveCommitment_RevokedReturnsFalse - TestHandler_calculateCommitmentMetrics_RevokedExcluded - TestHandler_calculateCommitmentMetrics_ProviderFilter - TestSummarizeRecommendationsWithCoverage_CurrentSavingsIsZero (renamed) - TestSummarizePurchaseHistory_RevokedExcludedFromKPIs
bb3d80d to
27a7151
Compare
|
@coderabbitai full review |
✅ Action performedFull review finished. |
Summary
Four money-integrity defects found by adversarial review of origin/main, all confirmed still present before this fix. All verified to fail pre-fix and pass post-fix.
Defect #2 (HIGH) - revoked/refunded commitments counted as ACTIVE on every money path:
GetActivePurchaseHistory(store_postgres.go): addedrevoked_at IS NULLto SQL WHERE clause - single shared predicate covering dashboard KPIs, inventory list, and analytics snapshots simultaneously.isActiveCommitment(handler_dashboard.go): addedp.RevokedAt != nilearly-return as defense-in-depth for callers usingGetPurchaseHistoryFiltered(e.g. the History page).summarizePurchaseHistory(handler_history.go): added revoked branch that increments newTotalRevokedcounter and skips dollar totals.HistorySummary(types.go): addedTotalRevoked intfield ("total_revoked"JSON key).Defect #3 (HIGH) - by_service.current_savings fabricated as full potential for services with no active commitments:
svc.CurrentSavings += scaledfrom thesummarizeRecommendationsWithCoveragereducer. This was settingCurrentSavings = PotentialSavingsfor ALL services with recommendations, regardless of whether any commitment existed. ThegetDashboardSummaryloop already overwrites from realpurchase_historydata for services with commitments; services without commitments now correctly default to 0.TestSummarizeRecommendationsWithCoverage_PopulatesCurrentSavingsrenamed toTestSummarizeRecommendationsWithCoverage_CurrentSavingsIsZero).Defect #4 (MEDIUM) - provider chip filters only recs, not commitment KPIs:
provider stringparameter tocalculateCommitmentMetricsandfetchCommitmentPurchases.fetchCommitmentRecords.getDashboardSummarynow passesparams["provider"]so all KPIs (ActiveCommitments, CommittedMonthly, YTDSavings, CurrentSavings, CurrentCoverage) are scoped to the selected provider.Defect #7 (LOW) - GetActivePurchaseHistory error silently swallowed:
fetchCommitmentPurchasesnow callslogging.Errorfon error so the cause is visible in logs. KPIs still zero out (non-fatal) to avoid breaking the dashboard request, but with an observable signal.Test plan
go test ./internal/... -count=1- 5018 tests pass (was run and verified)go test ./... -count=1- 5833 tests pass (was run and verified)TestIsActiveCommitment_RevokedReturnsFalseTestHandler_calculateCommitmentMetrics_RevokedExcludedTestHandler_calculateCommitmentMetrics_ProviderFilterTestSummarizeRecommendationsWithCoverage_CurrentSavingsIsZeroTestSummarizePurchaseHistory_RevokedExcludedFromKPIsstore_postgres_pgxmock_test.goto pin the newrevoked_at IS NULLclauseSummary by CodeRabbit
New Features
total_revokedfield to purchase history summaries.Bug Fixes