*/ 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)); } }