Added functionality to upsert pickup exceptions for public holidays and query for auto-updated exceptions. This includes new classes for handling input and service integration with the public holidays API.
62 lines
1.8 KiB
PHP
62 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Cqs\Actions\PickupExceptions;
|
|
|
|
use App\Contracts\Action;
|
|
use App\Contracts\ActionInput;
|
|
use App\Dto\PublicHolidayDto;
|
|
use App\Models\PickupException;
|
|
use App\Models\Shop;
|
|
use Domain\Enums\RepeatFrequency;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
/**
|
|
* @implements \App\Contracts\Action<
|
|
* \App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYearInput,
|
|
* void
|
|
* >
|
|
*/
|
|
final class UpsertShopsPickupExceptionsForYear implements Action
|
|
{
|
|
/**
|
|
* @param \App\Cqs\Actions\PickupExceptions\UpsertShopsPickupExceptionsForYearInput $input
|
|
*/
|
|
#[\Override]
|
|
public function __invoke(ActionInput $input): void
|
|
{
|
|
Assert::isInstanceOf($input, UpsertShopsPickupExceptionsForYearInput::class);
|
|
|
|
if ($input->holidays === []) {
|
|
return;
|
|
}
|
|
|
|
foreach ($input->holidays as $holiday) {
|
|
$exception = PickupException::query()
|
|
->firstOrNew([
|
|
'date' => $holiday->date->format('Y-m-d'),
|
|
'label' => $holiday->label,
|
|
]);
|
|
$exception->fill([
|
|
'date' => $holiday->date->format('Y-m-d'),
|
|
'repeatable' => true,
|
|
'repeat_frequency' => RepeatFrequency::YEARLY,
|
|
]);
|
|
$exception->setAttribute('auto_updated', true);
|
|
$exception->saveOrFail();
|
|
}
|
|
|
|
$repeatables = PickupException::query()
|
|
->whereYear('date', $input->year)
|
|
->whereIn('label', array_map(
|
|
fn (PublicHolidayDto $holiday): string => $holiday->label,
|
|
$input->holidays
|
|
))
|
|
->get();
|
|
|
|
Shop::query()->get()
|
|
->each(fn (Shop $shop) => $shop->pickupExceptions()
|
|
->syncWithoutDetaching($repeatables));
|
|
}
|
|
}
|