diff --git a/app/Console/Commands/UpsertPublicHolydaysOnShopsPickupExceptions.php b/app/Console/Commands/UpsertPublicHolydaysOnShopsPickupExceptions.php index 0e5916c6..abb7912a 100644 --- a/app/Console/Commands/UpsertPublicHolydaysOnShopsPickupExceptions.php +++ b/app/Console/Commands/UpsertPublicHolydaysOnShopsPickupExceptions.php @@ -15,22 +15,27 @@ class UpsertPublicHolydaysOnShopsPickupExceptions extends Command { /** * Execute the console command. - * - * @return void */ - public function handle() + public function handle(): int { $year = $this->argument('year'); - if (is_numeric($year) === false) { + if ($year !== null && is_numeric($year) === false) { $this->error('The year must be a number'); - return; + return self::FAILURE; } $this->info('Fetching hollidays upsert shops exceptions using Api'); - dispatch(new UpsertShopsPickupExceptionsUsingApiForYear(intval($year))); + + $job = $year !== null + ? new UpsertShopsPickupExceptionsUsingApiForYear(intval($year)) + : new UpsertShopsPickupExceptionsUsingApiForYear(); + + dispatch($job); $this->info('Done'); + + return self::SUCCESS; } } diff --git a/app/Cqs/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYear.php b/app/Cqs/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYear.php index 4f336c26..9c65481e 100644 --- a/app/Cqs/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYear.php +++ b/app/Cqs/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYear.php @@ -35,11 +35,24 @@ final class UpsertShopsPickupExceptionsUsingApiForYear implements Action { Assert::isInstanceOf($input, UpsertShopsPickupExceptionsUsingApiForYearInput::class); + if ( + $input->onlyIfMissing + && PickupException::query() + ->whereYear('date', $input->year) + ->where('auto_updated', true) + ->exists() + ) { + return; + } + $holidayList = $this->getHolidaysForYear($input->year); foreach ($holidayList as $holiday) { $exception = PickupException::query() - ->firstOrNew(['label' => $holiday->label]); + ->firstOrNew([ + 'date' => $holiday->date->format('Y-m-d'), + 'label' => $holiday->label, + ]); $exception->fill([ 'date' => $holiday->date->format('Y-m-d'), 'repeatable' => true, @@ -50,10 +63,12 @@ final class UpsertShopsPickupExceptionsUsingApiForYear implements Action } $repeatablesFromApi = PickupException::query() + ->whereYear('date', $input->year) ->whereIn('label', array_map( fn ($v) => $v->label, $holidayList - ))->get(); + )) + ->get(); Shop::query()->get() ->each(fn ($shop) => $shop->pickupExceptions() diff --git a/app/Cqs/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYearInput.php b/app/Cqs/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYearInput.php index e02e2ca5..c818c1b8 100644 --- a/app/Cqs/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYearInput.php +++ b/app/Cqs/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYearInput.php @@ -11,6 +11,7 @@ final readonly class UpsertShopsPickupExceptionsUsingApiForYearInput implements { public function __construct( public int $year, + public bool $onlyIfMissing = false, ) { Assert::greaterThanEq($this->year, 2026); } diff --git a/app/Jobs/EnsurePublicHolidaysSyncedJob.php b/app/Jobs/EnsurePublicHolidaysSyncedJob.php new file mode 100644 index 00000000..9af38e3a --- /dev/null +++ b/app/Jobs/EnsurePublicHolidaysSyncedJob.php @@ -0,0 +1,38 @@ +year >= 2026) { + $executor->execute( + $action, + new UpsertShopsPickupExceptionsUsingApiForYearInput($now->year, onlyIfMissing: true), + ); + } + + if ($now->month >= 11) { + $executor->execute( + $action, + new UpsertShopsPickupExceptionsUsingApiForYearInput($now->year + 1, onlyIfMissing: true), + ); + } + } +} diff --git a/bootstrap/jobs.php b/bootstrap/jobs.php index f83dddf0..4ea12613 100644 --- a/bootstrap/jobs.php +++ b/bootstrap/jobs.php @@ -3,10 +3,10 @@ declare(strict_types=1); use App\Jobs\DeleteOldResetTokens; +use App\Jobs\EnsurePublicHolidaysSyncedJob; use App\Jobs\SendMissingOrderReminderJob; use App\Jobs\SendReminderMail; use App\Jobs\UpdateSlidesStatus; -use App\Jobs\UpsertShopsPickupExceptionsUsingApiForYear; use Domain\Services\PickupSchedulingService; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Support\Facades\Date; @@ -34,7 +34,7 @@ return function (Schedule $schedule): void { return $now->isSameHour($pickupReminderAt); }); - $schedule->job(UpsertShopsPickupExceptionsUsingApiForYear::class) + $schedule->job(EnsurePublicHolidaysSyncedJob::class) ->onOneServer() - ->yearlyOn(11, 1, '7:30'); + ->dailyAt('07:30'); }; diff --git a/tests/Feature/App/Console/Commands/UpsertPublicHolydaysOnShopsPickupExceptionsTest.php b/tests/Feature/App/Console/Commands/UpsertPublicHolydaysOnShopsPickupExceptionsTest.php new file mode 100644 index 00000000..dff5b73f --- /dev/null +++ b/tests/Feature/App/Console/Commands/UpsertPublicHolydaysOnShopsPickupExceptionsTest.php @@ -0,0 +1,52 @@ +artisan('uwil:upsert-shops-hollydays') + // THEN + ->assertSuccessful(); + + Bus::assertDispatched(UpsertShopsPickupExceptionsUsingApiForYear::class); +}); + +it('dispatches the job for the specified numeric year', function (): void { + // GIVEN + /** @var \Tests\TestCase $this */ + Bus::fake(); + + // WHEN + $this->artisan('uwil:upsert-shops-hollydays', ['year' => '2027']) + // THEN + ->assertSuccessful(); + + Bus::assertDispatched(UpsertShopsPickupExceptionsUsingApiForYear::class); +}); + +it('fails when year argument is not numeric', function (): void { + // GIVEN + /** @var \Tests\TestCase $this */ + Bus::fake(); + + // WHEN + $this->artisan('uwil:upsert-shops-hollydays', ['year' => 'abc']) + // THEN + ->expectsOutput('The year must be a number') + ->assertFailed(); + + Bus::assertNotDispatched(UpsertShopsPickupExceptionsUsingApiForYear::class); +}); diff --git a/tests/Feature/Jobs/EnsurePublicHolidaysSyncedJobTest.php b/tests/Feature/Jobs/EnsurePublicHolidaysSyncedJobTest.php new file mode 100644 index 00000000..1bb245b8 --- /dev/null +++ b/tests/Feature/Jobs/EnsurePublicHolidaysSyncedJobTest.php @@ -0,0 +1,78 @@ +create(); + + Http::fake([ + 'https://calendrier.api.gouv.fr/jours-feries/metropole/2026.json' => Http::response([ + '2026-01-01' => '1er janvier', + '2026-12-25' => 'Jour de Noël', + ], 200), + ]); + + // WHEN + EnsurePublicHolidaysSyncedJob::dispatchSync(); + + // THEN + Http::assertSentCount(1); + expect(PickupException::query()->whereYear('date', 2026)->count())->toBe(2) + ->and(PickupException::query()->whereYear('date', 2027)->count())->toBe(0) + ->and($shop->pickupExceptions()->count())->toBe(2); + + // WHEN (second run) + Http::fake(); + EnsurePublicHolidaysSyncedJob::dispatchSync(); + + // THEN + Http::assertNothingSent(); +}); + +it('catches up missing current and next year holidays on or after november and skips subsequent runs', function (): void { + // GIVEN + Date::setTestNow('2026-11-05 07:30:00'); + $shop = Shop::factory()->create(); + + Http::fake([ + 'https://calendrier.api.gouv.fr/jours-feries/metropole/2026.json' => Http::response([ + '2026-11-11' => 'Armistice', + '2026-12-25' => 'Jour de Noël', + ], 200), + 'https://calendrier.api.gouv.fr/jours-feries/metropole/2027.json' => Http::response([ + '2027-11-11' => 'Armistice', + '2027-12-25' => 'Jour de Noël', + ], 200), + ]); + + // WHEN + EnsurePublicHolidaysSyncedJob::dispatchSync(); + + // THEN + Http::assertSentCount(2); + expect(PickupException::query()->whereYear('date', 2026)->count())->toBe(2) + ->and(PickupException::query()->whereYear('date', 2027)->count())->toBe(2) + ->and($shop->pickupExceptions()->count())->toBe(4); + + // WHEN (subsequent run) + Http::fake(); + EnsurePublicHolidaysSyncedJob::dispatchSync(); + + // THEN + Http::assertNothingSent(); +}); diff --git a/tests/Integration/WithApp/App/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYearTest.php b/tests/Integration/WithApp/App/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYearTest.php index 9b7540f5..08a0c707 100644 --- a/tests/Integration/WithApp/App/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYearTest.php +++ b/tests/Integration/WithApp/App/Actions/PickupExceptions/UpsertShopsPickupExceptionsUsingApiForYearTest.php @@ -38,6 +38,98 @@ describe('UpsertShopsPickupExceptionsUsingApiForYear action', function (): void ->and($shops->last()?->pickupExceptions()->count())->toBe(2); }); + it('does not overwrite previous year holidays when importing a new year and preserves shop associations', function (): void { + // GIVEN + $shop = Shop::factory()->create(); + + $armistice2026 = PickupException::factory()->create([ + 'date' => '2026-11-11', + 'label' => 'Armistice', + 'repeatable' => true, + 'repeat_frequency' => RepeatFrequency::YEARLY, + 'auto_updated' => true, + ]); + $christmas2026 = PickupException::factory()->create([ + 'date' => '2026-12-25', + 'label' => 'Jour de Noël', + 'repeatable' => true, + 'repeat_frequency' => RepeatFrequency::YEARLY, + 'auto_updated' => true, + ]); + $shop->pickupExceptions()->attach([$armistice2026->id, $christmas2026->id]); + + Http::fake([ + 'https://calendrier.api.gouv.fr/jours-feries/metropole/2027.json' => Http::response([ + '2027-11-11' => 'Armistice', + '2027-12-25' => 'Jour de Noël', + ], 200), + ]); + + $action = new UpsertShopsPickupExceptionsUsingApiForYear(); + $input = new UpsertShopsPickupExceptionsUsingApiForYearInput(2027); + + // WHEN + $action($input); + + // THEN + expect(PickupException::query()->count())->toBe(4) + ->and(PickupException::query()->whereYear('date', 2026)->count())->toBe(2) + ->and(PickupException::query()->whereYear('date', 2027)->count())->toBe(2) + ->and($armistice2026->fresh()?->date?->format('Y-m-d'))->toBe('2026-11-11') + ->and($christmas2026->fresh()?->date?->format('Y-m-d'))->toBe('2026-12-25') + ->and($shop->pickupExceptions()->count())->toBe(4); + }); + + it('skips the api call when onlyIfMissing is true and auto-updated holidays exist for the year', function (): void { + // GIVEN + PickupException::factory()->create([ + 'date' => '2026-01-01', + 'label' => '1er janvier', + 'repeatable' => true, + 'repeat_frequency' => RepeatFrequency::YEARLY, + 'auto_updated' => true, + ]); + + Http::fake(); + + $action = new UpsertShopsPickupExceptionsUsingApiForYear(); + $input = new UpsertShopsPickupExceptionsUsingApiForYearInput(2026, onlyIfMissing: true); + + // WHEN + $action($input); + + // THEN + Http::assertNothingSent(); + expect(PickupException::query()->count())->toBe(1); + }); + + it('calls the api when onlyIfMissing is true and no auto-updated holidays exist for the year', function (): void { + // GIVEN + PickupException::factory()->create([ + 'date' => '2026-05-01', + 'label' => 'Manual exception', + 'repeatable' => false, + 'repeat_frequency' => RepeatFrequency::YEARLY, + 'auto_updated' => false, + ]); + + Http::fake([ + 'https://calendrier.api.gouv.fr/jours-feries/metropole/2026.json' => Http::response([ + '2026-01-01' => '1er janvier', + ], 200), + ]); + + $action = new UpsertShopsPickupExceptionsUsingApiForYear(); + $input = new UpsertShopsPickupExceptionsUsingApiForYearInput(2026, onlyIfMissing: true); + + // WHEN + $action($input); + + // THEN + Http::assertSentCount(1); + expect(PickupException::query()->whereYear('date', 2026)->where('auto_updated', true)->count())->toBe(1); + }); + it('rejects years before 2026 in input', function (): void { // GIVEN / WHEN / THEN expect(fn () => new UpsertShopsPickupExceptionsUsingApiForYearInput(2025))