137 lines
4.4 KiB
PHP
137 lines
4.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\Bo\Shops;
|
|
|
|
use App\Cqs\Actions\Shops\UpdateShopPickupExceptionsInput;
|
|
use App\Models\Shop;
|
|
use App\Traits\Requests\ConvertsClientDatesToUtc;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Str;
|
|
use Infrastructure\Adapters\Services\AppAdminAuthService;
|
|
use Webmozart\Assert\Assert;
|
|
|
|
class UpdateShopPickupExceptionsRequest extends FormRequest
|
|
{
|
|
use ConvertsClientDatesToUtc;
|
|
|
|
public function __construct(
|
|
private readonly AppAdminAuthService $authService
|
|
) {
|
|
}
|
|
|
|
public function toUpdateShopPickupExceptionsInput(Shop $shop): UpdateShopPickupExceptionsInput
|
|
{
|
|
return new UpdateShopPickupExceptionsInput(
|
|
shop: $shop,
|
|
syncData: $this->toSyncData(),
|
|
);
|
|
}
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return $this->authService->check();
|
|
}
|
|
|
|
#[\Override]
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'pickup-exceptions' => $this->input('pickup-exceptions', []),
|
|
'replacement_date' => $this->input('replacement_date', []),
|
|
'order_deadline_at' => $this->input('order_deadline_at', []),
|
|
]);
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'pickup-exceptions' => ['array'],
|
|
'pickup-exceptions.*' => ['required', 'string', 'uuid', 'exists:pickup_exceptions,id'],
|
|
|
|
'replacement_date' => ['array'],
|
|
'replacement_date.*' => [
|
|
'nullable',
|
|
'date_format:Y-m-d',
|
|
'required_with:order_deadline_at.*',
|
|
],
|
|
|
|
'order_deadline_at' => ['array'],
|
|
'order_deadline_at.*' => [
|
|
'nullable',
|
|
'date_format:Y-m-d\TH:i',
|
|
'before:replacement_date.*',
|
|
],
|
|
|
|
'client_timezone' => ['required', 'timezone'],
|
|
];
|
|
}
|
|
|
|
#[\Override]
|
|
protected function passedValidation(): void
|
|
{
|
|
$deadlines = $this->array('order_deadline_at');
|
|
$timezone = $this->string('client_timezone')->value();
|
|
|
|
foreach ($deadlines as $id => $deadline) {
|
|
if (is_string($deadline) && $deadline !== '') {
|
|
$deadlines[$id] = $this->convertLocalDatetimeToUtc($deadline, $timezone);
|
|
}
|
|
}
|
|
|
|
$this->merge([
|
|
'order_deadline_at' => $deadlines,
|
|
]);
|
|
}
|
|
|
|
#[\Override]
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'pickup-exceptions' => Str::lower(trans_choice('models.classes.pickup-exceptions', \INF)),
|
|
'pickup-exceptions.*' => Str::lower(trans_choice('models.classes.pickup-exceptions', 1)),
|
|
'replacement_date' => trans('validation.custom.attributes.replacement_date'),
|
|
'replacement_date.*' => trans('validation.custom.attributes.replacement_date'),
|
|
'order_deadline_at' => trans('validation.custom.attributes.order_deadline_at'),
|
|
'order_deadline_at.*' => trans('validation.custom.attributes.order_deadline_at'),
|
|
'client_timezone' => trans('validation.attributes.timezone'),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, array{replacement_date: string|null, order_deadline_at: string|null}>
|
|
*/
|
|
public function toSyncData(): array
|
|
{
|
|
$pickupExceptions = $this->array('pickup-exceptions');
|
|
$replacementDates = $this->array('replacement_date');
|
|
$orderDeadlines = $this->array('order_deadline_at');
|
|
|
|
Assert::allScalar($pickupExceptions);
|
|
Assert::allNullOrString($replacementDates);
|
|
Assert::allNullOrString($orderDeadlines);
|
|
|
|
$syncData = [];
|
|
|
|
foreach ($pickupExceptions as $exceptionId) {
|
|
$exceptionId = (string) $exceptionId;
|
|
$replacementDate = $this->getValue($replacementDates, $exceptionId);
|
|
$orderDeadline = $this->getValue($orderDeadlines, $exceptionId);
|
|
|
|
$syncData[$exceptionId] = [
|
|
'replacement_date' => $replacementDate,
|
|
'order_deadline_at' => $orderDeadline,
|
|
];
|
|
}
|
|
|
|
return $syncData;
|
|
}
|
|
|
|
/** @param array<array-key, string|null> $array */
|
|
private function getValue(array $array, string $key): ?string
|
|
{
|
|
return ! empty($array[$key]) ? $array[$key] : null;
|
|
}
|
|
}
|