# Weekly coaching check-in batch (Phase 3) for Windows Task Scheduler. # Reads the admin API key from .env and POSTs /discovery/coaching/run, which # generates a check-in for every eligible user whose cadence is due. # Override the target with $env:IMPACTFLOW_BASE_URL (default: local container). $ErrorActionPreference = 'Stop' $dir = Split-Path -Parent $PSScriptRoot # repo root (scripts\..) $envFile = Join-Path $dir '.env' $log = Join-Path $dir 'data\coaching-cron.log' $base = if ($env:IMPACTFLOW_BASE_URL) { $env:IMPACTFLOW_BASE_URL } else { 'http://localhost:8011' } $ts = (Get-Date).ToUniversalTime().ToString('yyyy-MM-ddTHH:mm:ssZ') try { $line = (Select-String -Path $envFile -Pattern '^IMPACTFLOW_API_KEY=' | Select-Object -First 1).Line $key = ($line -replace '^IMPACTFLOW_API_KEY=', '').Trim() if (-not $key) { throw "IMPACTFLOW_API_KEY not found in $envFile" } $resp = Invoke-RestMethod -Method Post -Uri "$base/discovery/coaching/run" ` -Headers @{ 'X-API-Key' = $key } -TimeoutSec 120 "$ts $($resp | ConvertTo-Json -Compress)" | Add-Content -Path $log } catch { "$ts ERROR: $($_.Exception.Message)" | Add-Content -Path $log }