diff --git a/Modules/Linksehat/Http/Controllers/Api/AppointmentController.php b/Modules/Linksehat/Http/Controllers/Api/AppointmentController.php new file mode 100755 index 00000000..40baa991 --- /dev/null +++ b/Modules/Linksehat/Http/Controllers/Api/AppointmentController.php @@ -0,0 +1,98 @@ +only([ + 'appointment_type', + 'organization_id', + 'speciality_id', + ]); + + $appointmentData = array_merge($appointmentData, [ + 'start_time' => $request->date . ' ' . $request->time + ]); + + $appointment = Appointment::query()->create($appointmentData); + + if ($request->has('doctor_id')) { + $practitionerRole = PractitionerRole::query()->find($request->doctor_id); + $practitionerRole->appointmentParticipantables()->create([ + 'appointment_id' => $appointment->id, + 'type' => 'doctor', + ]); + } + + if ($request->has('patient_id')) { + $person = Person::query()->find($request->patient_id); + $person->appointmentParticipantables()->create([ + 'appointment_id' => $appointment->id, + 'type' => 'patient', + ]); + } + + return Helper::responseJson($appointment, Response::HTTP_CREATED, 'Data appointment berhasil di buat'); + } + + /** + * Show the specified resource. + * @param int $id + * @return Renderable + */ + public function show($id) + { + $appointment = Appointment::query()->with(['appointmentParticipants', 'organization'])->find($id); + + return new AppointmentDetailResource($appointment); + + return response()->json($appointment); + } + + /** + * Update the specified resource in storage. + * @param Request $request + * @param int $id + * @return Renderable + */ + public function update(Request $request, $id) + { + // + } + + /** + * Remove the specified resource from storage. + * @param int $id + * @return Renderable + */ + public function destroy($id) + { + // + } +} diff --git a/Modules/Linksehat/Routes/api.php b/Modules/Linksehat/Routes/api.php index 3e09e274..c39a2a3b 100755 --- a/Modules/Linksehat/Routes/api.php +++ b/Modules/Linksehat/Routes/api.php @@ -1,5 +1,6 @@ group(function () { Route::post('doctors/{id}/schedule', 'schedule')->name('doctors.schedule'); }); + Route::middleware('auth:sanctum')->group(function () { Route::get('profile', [ProfileController::class, 'index'])->name('profile'); Route::post('profile', [ProfileController::class, 'update'])->name('profile.update'); + Route::apiResource('appointment', AppointmentController::class); Route::apiResource('families', PersonController::class)->except(['destroy']); }); }); diff --git a/Modules/Linksehat/Transformers/Appointment/AppointmentDetailResource.php b/Modules/Linksehat/Transformers/Appointment/AppointmentDetailResource.php new file mode 100755 index 00000000..f7d3726e --- /dev/null +++ b/Modules/Linksehat/Transformers/Appointment/AppointmentDetailResource.php @@ -0,0 +1,59 @@ +appointmentParticipants as $appoinmentParticipant) { + if ($appoinmentParticipant->participantable_type == Person::class) { + $patiendId = $appoinmentParticipant->participantable_id; + } elseif ($appoinmentParticipant->participantable_type == PractitionerRole::class) { + $doctorId = $appoinmentParticipant->participantable_id; + } + } + + $queryPatient = Person::query()->find($patiendId); + + $patient = [ + 'id' => $queryPatient->id, + 'name' => $queryPatient->name, + 'email' => $queryPatient->email, + 'phone' => $queryPatient->phone, + ]; + + $queryDoctor = PractitionerRole::query()->with(['practitioner.person', 'speciality'])->where('practitioner_id', $doctorId)->first(); + + $doctor = [ + 'id' => $doctorId, + 'name' => $queryDoctor->person->name, + 'speciality' => 'Spesialis ' . $queryDoctor->speciality->name, + 'length_of_work_year' => rand(1, 10) + ]; + + return [ + 'id' => $this->id, + 'date' => Carbon::createFromFormat('Y-m-d H:i:s', $this->start_time)->format('Y-m-d'), + 'time' => Carbon::createFromFormat('Y-m-d H:i:s', $this->start_time)->format('H:i'), + 'doctor' => $doctor, + 'organization' => [ + 'id' => $this->organization->id, + 'name' => $this->organization->name, + 'address' => $this->organization->currentAddress->text, + ], + 'patient' => $patient, + ]; + } +} diff --git a/app/Models/Appointment.php b/app/Models/Appointment.php index 8726344a..7491db79 100644 --- a/app/Models/Appointment.php +++ b/app/Models/Appointment.php @@ -12,7 +12,7 @@ class Appointment extends Model protected $fillable = [ 'status', 'cancelation_reason', - 'appointmnet_type', + 'appointment_type', 'speciality_id', 'description', 'duration_minutes', @@ -22,4 +22,19 @@ class Appointment extends Model 'comment', 'patient_instruction' ]; + + public function appointmentParticipants() + { + return $this->hasMany(AppointmentParticipant::class, 'appointment_id'); + } + + public function speciality() + { + return $this->belongsTo(Speciality::class, 'speciality_id'); + } + + public function organization() + { + return $this->belongsTo(Organization::class, 'organization_id'); + } } diff --git a/app/Models/AppointmentParticipant.php b/app/Models/AppointmentParticipant.php index ea5f89bd..6943cc51 100644 --- a/app/Models/AppointmentParticipant.php +++ b/app/Models/AppointmentParticipant.php @@ -15,4 +15,9 @@ class AppointmentParticipant extends Model 'participantable_type', 'participantable_id', ]; + + public function participantable() + { + return $this->morphTo(); + } } diff --git a/app/Models/Person.php b/app/Models/Person.php index 1d612566..dce3ef27 100755 --- a/app/Models/Person.php +++ b/app/Models/Person.php @@ -102,4 +102,9 @@ class Person extends Model { return $this->hasOne(User::class, 'person_id'); } + + public function appointmentParticipantables() + { + return $this->morphMany(AppointmentParticipant::class, 'participantable'); + } } diff --git a/app/Models/PractitionerRole.php b/app/Models/PractitionerRole.php index 6b0decdf..b2241fff 100755 --- a/app/Models/PractitionerRole.php +++ b/app/Models/PractitionerRole.php @@ -89,4 +89,9 @@ class PractitionerRole extends Model { return $this->hasOneThrough(Person::class, Practitioner::class, 'person_id', 'id'); } + + public function appointmentParticipantables() + { + return $this->morphMany(AppointmentParticipant::class, 'participantable'); + } }