## Sync Hooks with Date Filters - Quick Start

### Backend is Ready ✅
The sync hooks system is fully functional and will execute when date filters are provided.

### Frontend Integration

#### Option 1: Using syncTableWithFilters (Recommended)

```javascript
// In your module or wherever you handle date range changes
import { getManager } from 'services/syncservice';

// When date filter changes:
async function handleDateRangeChange(startDate, endDate) {
    const mgr = await getManager();
    
    // This will trigger backend sync with filters, causing hooks to execute
    await mgr.syncTableWithFilters('time_clock_aggregates', {
        start_date: startDate,
        end_date: endDate
    });
    
    // Table will auto-refresh via 'tableSynced' event
}
```

#### Option 2: Using immediateGet

```javascript
// If you want to fetch fresh data with filters without updating lastSyncTimes
const mgr = await getManager();
const aggregates = await mgr.immediateGet('time_clock_aggregates', {
    start_date: '2025-05-01',
    end_date: '2025-08-31'
});
```

#### Option 3: Manual fetch

```javascript
// Direct API call
const response = await fetch(
    '../api/sync/pull?tables[time_clock_aggregates]=&start_date=2025-05-01&end_date=2025-08-31',
    { credentials: 'include' }
);
const data = await response.json();
console.log(data._hooks); // See hook execution results
```

### Testing

```bash
# Test in browser console:
const mgr = await (await import('services/syncservice')).getManager();
await mgr.syncTableWithFilters('time_clock_aggregates', {
    start_date: '2025-05-01',
    end_date: '2025-08-31'
});
```

### Next Steps for Full Integration

1. **Update Payroll Module** - Wire up the filter-date-range to call `syncTableWithFilters()`
2. **Add Loading Indicator** - Show spinner during sync with filters
3. **Handle Errors** - Display hook execution errors from `_hooks` response
4. **Add Refresh Button** - Allow manual trigger of sync with current filters

### Module Config Example

```sql
-- Enable sync filters for Payroll module
UPDATE modules
SET config = jsonb_set(
    config,
    '{body,0,options}',
    jsonb_build_object(
        'tableName', 'time_clock_aggregates',
        'syncFilters', true,
        'columnsToShow', ARRAY['user_id', 'start_date', 'end_date', 'total_days_worked', 'total_net_hours']
    )
)
WHERE module_id = (SELECT module_id FROM modules WHERE name = 'Payroll');
```

### Current Status

✅ Backend hooks working  
✅ Frontend API updated (`bulkPull`, `syncTableWithFilters`)  
⏳ Module-level integration (needs manual wiring)  
⏳ UI feedback for hook execution  

**You can now manually call `syncTableWithFilters()` from browser console or custom code to trigger backend aggregation!**
