50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Fo\Ajax;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Resources\Fo\Ajax\TimeSlotResource;
|
|
use App\Models\Shop;
|
|
use Carbon\CarbonImmutable;
|
|
use Carbon\CarbonInterface;
|
|
use Domain\Ports\Repositories\ShopRepository;
|
|
use Domain\Services\PickupSchedulingService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
use Illuminate\Support\Facades\Response;
|
|
|
|
class PickupController extends Controller
|
|
{
|
|
private const int TIMESLOTS_DAY_TARGET = 28;
|
|
|
|
public function __construct(
|
|
private readonly PickupSchedulingService $pickupService,
|
|
private readonly ShopRepository $shopRepository
|
|
) {
|
|
}
|
|
|
|
public function getAvailableTimeslots(Shop $shop): AnonymousResourceCollection | JsonResponse
|
|
{
|
|
$this->authorize('viewPickupTimeslots', $shop);
|
|
|
|
$domainShop = $this->shopRepository->find($shop->getId());
|
|
|
|
if ($domainShop === null) {
|
|
return Response::json(['message' => 'Shop not found'], 404);
|
|
}
|
|
|
|
return TimeSlotResource::collection(
|
|
$this->pickupService->getTimeSlotsFor(
|
|
$domainShop,
|
|
$this->getUntil()
|
|
)
|
|
);
|
|
}
|
|
|
|
private function getUntil(): CarbonInterface
|
|
{
|
|
return CarbonImmutable::now()->addDays(self::TIMESLOTS_DAY_TARGET);
|
|
}
|
|
}
|