Files
Caramel-CRM/app/Console/Commands/CaldavSyncCommand.php
T

40 lines
1.0 KiB
PHP
Raw Normal View History

2025-12-02 17:32:52 +01:00
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Cache;
2025-12-02 17:32:52 +01:00
use App\Services\CaldavService;
use App\Models\Todo;
class CaldavSyncCommand extends Command
{
protected $signature = 'caldav:sync';
2025-12-02 17:32:52 +01:00
protected $description = 'Sync CalDAV VTODOs into local todos table';
public function handle(CaldavService $service)
{
// only run every 5 minutes although the task is called every minute
$cacheKey = 'caldav_sync_last_run';
if (\Illuminate\Support\Facades\Cache::has($cacheKey)) {
return 0;
}
Cache::put($cacheKey, true, 300);
Log::info('Running CalDAV sync');
$this->info('Starting CalDAV sync');
2025-12-02 17:32:52 +01:00
$todos = $service->getTodos();
$count = 0;
foreach ($todos as $todo) {
Todo::upsert($todo->attributesToArray(), 'id');
$count++;
}
$this->info("Synced " . count($todos) . " todos.");
return 0;
}
}