fix(holidays): preserve current year holidays and add daily catch-up sync
This commit is contained in:
parent
6dfef2c7a1
commit
455bcd4988
8 changed files with 292 additions and 11 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ final readonly class UpsertShopsPickupExceptionsUsingApiForYearInput implements
|
|||
{
|
||||
public function __construct(
|
||||
public int $year,
|
||||
public bool $onlyIfMissing = false,
|
||||
) {
|
||||
Assert::greaterThanEq($this->year, 2026);
|
||||
}
|
||||
|
|
|
|||
38
app/Jobs/EnsurePublicHolidaysSyncedJob.php
Normal file
38
app/Jobs/EnsurePublicHolidaysSyncedJob.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Jobs;
|
||||
|
||||
use App\Contracts\Executor;
|
||||
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsUsingApiForYear as UpsertShopsPickupExceptionsAction;
|
||||
use App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsUsingApiForYearInput;
|
||||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||
use Illuminate\Foundation\Queue\Queueable;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
|
||||
class EnsurePublicHolidaysSyncedJob implements ShouldQueue
|
||||
{
|
||||
use Queueable;
|
||||
|
||||
public function handle(
|
||||
Executor $executor,
|
||||
UpsertShopsPickupExceptionsAction $action,
|
||||
): void {
|
||||
$now = Date::now();
|
||||
|
||||
if ($now->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),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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');
|
||||
};
|
||||
|
|
|
|||
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Jobs\UpsertShopsPickupExceptionsUsingApiForYear;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
|
||||
afterEach(function (): void {
|
||||
Date::setTestNow();
|
||||
});
|
||||
|
||||
it('dispatches the job for next year when year argument is omitted', function (): void {
|
||||
// GIVEN
|
||||
/** @var \Tests\TestCase $this */
|
||||
Date::setTestNow('2026-06-15 10:00:00');
|
||||
Bus::fake();
|
||||
|
||||
// WHEN
|
||||
$this->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);
|
||||
});
|
||||
78
tests/Feature/Jobs/EnsurePublicHolidaysSyncedJobTest.php
Normal file
78
tests/Feature/Jobs/EnsurePublicHolidaysSyncedJobTest.php
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Jobs\EnsurePublicHolidaysSyncedJob;
|
||||
use App\Models\PickupException;
|
||||
use App\Models\Shop;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Date;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
afterEach(function (): void {
|
||||
Date::setTestNow();
|
||||
});
|
||||
|
||||
it('syncs current year before november when missing and skips subsequent runs', function (): void {
|
||||
// GIVEN
|
||||
Date::setTestNow('2026-06-15 07:30:00');
|
||||
$shop = Shop::factory()->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();
|
||||
});
|
||||
|
|
@ -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))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue