# RMM Cache Invalidation Fix

## Issue
Company 29683395 (TK Plastics Company, Inc.) was showing 0 RMM devices on the configuration items page despite having 2 RMM site mappings configured in the database.

## Root Cause
The `/api/rmm-devices` endpoint was using a cache key that didn't account for changes in RMM site mappings:

**Old cache key format:** `rmm-devices:${companyId}:${activeFilter}`

This meant that when RMM site mappings were added for a company, the cache would continue serving the old data (with 0 RMM devices) until the cache expired (2 minutes).

## Database Verification
```sql
-- Company has 2 RMM site mappings
SELECT * FROM rmm_site_mappings WHERE company_id = 29683395;
-- Results:
-- 612f6b1f-9228-4e2a-8e63-f0ec5d0b6aaa | TK Plastics
-- aba39a7b-d9b3-4eff-88e4-4d0182d478cb | TK Plastics - Kaercher
```

## Solution

### 1. Updated Cache Key to Include Mapping Count
Modified `/app/api/rmm-devices/route.ts` to include the number of site mappings in the cache key:

**New cache key format:** `rmm-devices:${companyId}:${activeFilter}:mappings-${mappingCount}`

This ensures that when mappings are added or removed, the cache is automatically invalidated because the key changes.

### 2. Added Force Refresh Capability
Added a `skipCache` query parameter to allow bypassing the cache entirely when needed:
- Added `forceRefresh` state to the configuration items page
- Updated the refresh button to increment `forceRefresh` counter
- When `forceRefresh > 0`, adds `&skipCache=true` to the API request
- Made the refresh button show a spinner animation while loading

### 3. Improved Refresh Button UX
- Refresh button now shows spinning animation while loading
- Button is disabled during loading to prevent multiple simultaneous requests
- Button is disabled when no company is selected

## Files Modified
1. `/app/api/rmm-devices/route.ts` - Updated cache key logic and added skipCache parameter
2. `/app/configuration-items/page.tsx` - Added force refresh capability and improved refresh button

## Testing
After deploying these changes:
1. The cache key will automatically change from `rmm-devices:29683395:active:mappings-0` to `rmm-devices:29683395:active:mappings-2`
2. This will force a fresh API call that will fetch devices from both mapped RMM sites
3. Users can also click the refresh button to force bypass the cache

## Expected Behavior
- Company 29683395 should now show RMM devices from both "TK Plastics" and "TK Plastics - Kaercher" sites
- Any future changes to RMM site mappings will automatically invalidate the cache
- Users can manually force a refresh by clicking the refresh button

## Related Documentation
- `/docs/rmm-multi-site-integration.md` - RMM multi-site support architecture
- System-retrieved memory about RMM site mappings implementation
