diff --git a/Modules/Linksehat/Http/Controllers/Api/DashboardController.php b/Modules/Linksehat/Http/Controllers/Api/DashboardController.php index d1fb7018..ac73399c 100644 --- a/Modules/Linksehat/Http/Controllers/Api/DashboardController.php +++ b/Modules/Linksehat/Http/Controllers/Api/DashboardController.php @@ -10,39 +10,55 @@ use Illuminate\Support\Facades\Http; use Modules\Linksehat\Transformers\Dashboard\DoctorResource as DoctorResourceDashboard; use Modules\Linksehat\Transformers\Dashboard\HospitalResource as HospitalResourceDashboard; use Modules\Linksehat\Transformers\Dashboard\ArticleResource as ArticleResourceDashboard; +use Symfony\Component\HttpFoundation\Response; class DashboardController extends Controller { - public function index(Request $request, $default, $limit = 20) + public function index(Request $request, $query, $limit = 20) { - if ($default === 'doctors') { + if ($query === 'doctors') { $queryDoctors = PractitionerRole::query() ->with([ - 'practitioner.person', 'speciality' + 'person' => function ($query) { + $query->select(['name']); + }, + 'speciality' => function ($query) { + $query->select(['id', 'name']); + }, ]) - ->whereHas('speciality')->get()->random($limit); + ->where('active', 1) + ->whereNot('speciality_id') + ->get(['id', 'speciality_id']) + ->random($limit); + $data = DoctorResourceDashboard::collection($queryDoctors); - } elseif ($default === 'hospitals') { + } elseif ($query === 'hospitals') { $queryHospitals = Organization::query() - ->without('meta') + ->leftJoin('addresses', function ($query) { + $query->on('organizations.main_address_id', '=', 'addresses.id'); + $query->where('addresses.addressable_type', '=', Organization::class); + }) ->where('organizations.type', 'hospital') - ->leftJoin('addresses', function ($q) { - $q->on('organizations.main_address_id', '=', 'addresses.id'); - $q->where('addresses.addressable_type', '=', Organization::class); - }); - if ($request->has('lat') && !empty($request->lat) && $request->has('lng') && !empty($request->lng)) { - $queryHospitals = $queryHospitals->selectRaw("organizations.*, addresses.text AS currentAddress, 6371 * acos (cos ( radians($request->lat) ) * cos( radians( addresses.lat ) ) * cos( radians( addresses.lng ) - radians($request->lng) ) + sin ( radians($request->lat) ) * sin( radians( addresses.lat ) )) as distance")->orderBy('distance', 'ASC'); - } - $queryHospitals = $queryHospitals->orderBy('organizations.name', 'asc')->limit($limit)->get(); + ->where('organizations.status', 'active') + ->when($request->lat && $request->lng, function ($query) use ($request) { + $query->selectRaw("organizations.id, organizations.name, addresses.text AS currentAddress, 6371 * acos (cos ( radians($request->lat) ) * cos( radians( addresses.lat ) ) * cos( radians( addresses.lng ) - radians($request->lng) ) + sin ( radians($request->lat) ) * sin( radians( addresses.lat ) )) as distance"); + $query->orderBy('distance', 'ASC'); + }, function ($query) { + $query->select(['organizations.id', 'organizations.name', 'addresses.text AS currentAddress']); + $query->orderBy('organizations.name', 'asc'); + }) + ->limit($limit) + ->get(); + $data = HospitalResourceDashboard::collection($queryHospitals); - } elseif ($default === 'articles') { + } elseif ($query === 'articles') { $data = ArticleResourceDashboard::collection(json_decode(Http::get('https://linksehat.com/api/medical-assistance-articles'))); } return response()->json([ - 'success' => true, - 'statusCode' => 200, - 'message' => 'Success', + 'status' => 'success', + 'statusCode' => Response::HTTP_OK, + 'message' => 'Data berhasil di ambil', 'data' => $data, ]); } diff --git a/Modules/Linksehat/Transformers/Dashboard/HospitalResource.php b/Modules/Linksehat/Transformers/Dashboard/HospitalResource.php index 973627e3..7de0dbd7 100644 --- a/Modules/Linksehat/Transformers/Dashboard/HospitalResource.php +++ b/Modules/Linksehat/Transformers/Dashboard/HospitalResource.php @@ -15,16 +15,10 @@ class HospitalResource extends JsonResource */ public function toArray($request) { - if (empty($this->distance)) { - $address = $this->currentAddress->text; - } else { - $address = $this->currentAddress; - } - return [ 'id' => $this->id, - 'name' => $this->name, - 'address' => $address ?? null, + 'name' => $this->name ?? '', + 'address' => $this->currentAddress ?? '', 'distance' => $this->distance ? ($this->distance < 1 ? round($this->distance * 1000, 2) . " m" : round($this->distance, 2) . " km") : null, 'photos' => [ 'title' => Str::slug($this->name),