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 Helper::responseJson(new AppointmentDetailResource($appointment)); } /** * Update the specified resource in storage. * @param Request $request * @param int $id * @return Renderable */ public function update(Request $request, Appointment $appointment) { $idPatient = AppointmentParticipant::query()->where('appointment_id', $appointment->id)->where('participantable_type', Person::class)->first(); $patient = Person::query()->select(['owner_user_id'])->find($idPatient->participantable_id); if (Gate::forUser(auth()->user())->allows('update-appointment', $patient)) { $appointmentData = [ 'start_time' => $request->date . ' ' . $request->time ]; $appointment->update($appointmentData); $appointment->appointmentParticipants()->updateOrCreate( [ 'appointment_id' => $appointment->id, 'type' => 'patient', 'participantable_type' => Person::class, ], [ 'participantable_id' => $request->patient_id, ] ); return Helper::responseJson(data: $appointment->with(['appointmentParticipants'])->get(), message: 'Data berhasil di update'); } elseif (Gate::forUser(auth()->user())->denies('update-appointment', $patient)) { abort(Response::HTTP_FORBIDDEN, 'Tidak bisa update karena bukan pemilik!'); } } /** * Remove the specified resource from storage. * @param int $id * @return Renderable */ public function destroy($id) { // } }