Add settings-driven Artisan scheduler registry with Etsy sync and token refresh jobs

Introduces App\Support\DashboardSchedule (modelled on LHITS_Dashboard's
pattern) as a single settings-driven registry for recurring Artisan jobs,
replacing the two hard-coded Schedule::command() lines in
routes/console.php.

Migrated unchanged:
- integrations_refresh (integrations:refresh), every_30_seconds, withoutOverlapping(5)
- alerts_scan (alerts:scan), every_30_seconds, withoutOverlapping(5)

New:
- etsy_sync (etsy:sync), every_15_minutes - forces a fresh pull of Etsy
  receipts/listings into the existing IntegrationCache entries, decoupled
  from the bundled integrations:refresh job so it can be tuned/disabled
  independently of Parcel2Go/BamBuddy/dryer refreshes.
- etsy_token_refresh (etsy:refresh-token), every_45_minutes - proactively
  refreshes the Etsy OAuth token (tokens last ~1hr) via a new public
  Etsy::refreshAccessToken() wrapper around the existing on-demand
  accessToken() refresh logic, so the connection survives quiet periods.

Frequency is persisted to storage/app/dashboard-schedules.json (no
app_settings table exists in this app, unlike LHITS's AppSetting-backed
store) and surfaced in a new 'Scheduler' section on the existing Settings
page (ScheduleSettingsController + routes), with per-job frequency
dropdowns and manual 'Run now' buttons, matching the page's existing
Tailwind conventions.

Tests: SchedulerTest (registry/frequency behavior), ScheduleSettingsTest
(settings UI + authorization), EtsySyncScheduledJobsTest (new commands,
Http::fake only, no live Etsy calls).
This commit is contained in:
2026-11-07 17:38:11 +00:00
parent 9b13640571
commit 960189d14b
12 changed files with 681 additions and 5 deletions
+82
View File
@@ -0,0 +1,82 @@
<?php
namespace Tests\Feature;
use App\Models\User;
use App\Support\DashboardSchedule;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Support\Facades\File;
use Tests\TestCase;
class ScheduleSettingsTest extends TestCase
{
use LazilyRefreshDatabase;
protected function setUp(): void
{
parent::setUp();
File::delete(DashboardSchedule::runtimeSettingsPath());
}
protected function tearDown(): void
{
File::delete(DashboardSchedule::runtimeSettingsPath());
parent::tearDown();
}
public function test_non_admin_cannot_view_or_change_scheduler_settings(): void
{
$this->get(route('settings.index'))->assertRedirect(route('login'));
$user = User::factory()->create();
$this->actingAs($user)->get(route('settings.index'))->assertForbidden();
$this->actingAs($user)->put(route('settings.schedules.update'), [
'schedules' => ['etsy_sync' => 'hourly'],
])->assertForbidden();
}
public function test_admin_can_view_scheduler_section(): void
{
$response = $this
->actingAs(User::factory()->create(['role' => 'admin']))
->get(route('settings.index'));
$response->assertOk();
$response->assertSee('Scheduler');
$response->assertSee('etsy:sync');
$response->assertSee('etsy:refresh-token');
$response->assertSee('integrations:refresh');
$response->assertSee('Run now');
}
public function test_admin_can_change_job_frequencies(): void
{
$payload = collect(DashboardSchedule::jobs())
->mapWithKeys(fn (array $job, string $key): array => [$key => $job['default_frequency']])
->all();
$payload['etsy_sync'] = 'hourly';
$payload['etsy_token_refresh'] = 'disabled';
$response = $this
->actingAs(User::factory()->create(['role' => 'admin']))
->put(route('settings.schedules.update'), ['schedules' => $payload]);
$response->assertRedirect(route('settings.index'));
$this->assertSame('hourly', DashboardSchedule::frequencyFor('etsy_sync'));
$this->assertSame('disabled', DashboardSchedule::frequencyFor('etsy_token_refresh'));
$this->assertFileExists(DashboardSchedule::runtimeSettingsPath());
}
public function test_admin_can_manually_run_a_job(): void
{
$response = $this
->actingAs(User::factory()->create(['role' => 'admin']))
->post(route('settings.schedules.run', 'alerts_scan'));
$response->assertRedirect(route('settings.index'));
$response->assertSessionHas('status');
}
}