GuestController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Appointment;
  4. use App\Models\CareMonth;
  5. use App\Models\CareMonthEntry;
  6. use App\Models\Client;
  7. use App\Models\Handout;
  8. use App\Models\HandoutClient;
  9. use App\Models\Pro;
  10. use App\Models\Section;
  11. use App\Models\Survey;
  12. use App\Models\SectionTemplate;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\File;
  15. use Illuminate\Support\Facades\Http;
  16. use Illuminate\Support\Facades\Response;
  17. class GuestController extends Controller
  18. {
  19. public function section(Request $request, $guestAccessCode )
  20. {
  21. $section = Section::where('guest_access_code', $guestAccessCode)->first();
  22. abort_if(!$section, 404, 'Invalid access code');
  23. abort_if(!$section->is_active, 404, 'Invalid access code');
  24. abort_if($section->guest_access_level == 'NONE', 401, 'Invalid access code');
  25. $patient = null;
  26. if($section->note){
  27. abort_if($section->note->is_signed_by_hcp, 401, 'Note is already signed.');
  28. $patient = $section->note->client;
  29. }else{
  30. $patient = $section->client;
  31. }
  32. return view('app.guest.section', compact('patient','section', 'guestAccessCode'));
  33. }
  34. public function handout(Request $request, $handoutClientUid )
  35. {
  36. $handoutClient = HandoutClient::where('uid', $handoutClientUid)->first();
  37. abort_if((!$handoutClient || !$handoutClient->is_active), 404, 'Invalid access code');
  38. $handout = Handout::where('id', $handoutClient->handout_id)->first();
  39. abort_if((!$handout || !$handout->is_active), 404, 'Invalid access code');
  40. return Response::download(
  41. $handout->pdf_file_path,
  42. $handout->internal_name . '.pdf',
  43. ['Content-Type: application/pdf']
  44. );
  45. }
  46. public function appointmentConfirmation(Request $request, $appointmentUid )
  47. {
  48. $appointment = Appointment::where('uid', $appointmentUid)->first();
  49. abort_if(!count($appointment->confirmationRequests), 404, 'No confirmation requests on this appointment.');
  50. abort_if(!$appointment, 404, 'Invalid url');
  51. abort_if($appointment->status == 'COMPLETED', 404, 'Appointment has been completed');
  52. return view('app.guest.appointment-confirmation', compact('appointment'));
  53. }
  54. public function processAppointmentConfirmation(Request $request){
  55. $appointmentUid = $request->get('appointment_uid');
  56. $appointment = Appointment::where('uid', $appointmentUid)->first();
  57. abort_if(!count($appointment->confirmationRequests), 404, 'No confirmation requests on this appointment.');
  58. abort_if(!$appointment, 404, 'Invalid url');
  59. abort_if($appointment->status == 'COMPLETED', 404, 'Appointment has been completed');
  60. $decision = $request->get('decision');
  61. $memo = $request->get('memo');
  62. $response = null;
  63. $data = [
  64. 'uid' => $appointment->uid,
  65. 'memo' => $memo,
  66. 'confirmationDecisionEnum' => ($decision == 'REJECT' ? 'CANCELLED' : 'CONFIRMED')
  67. ];
  68. $url = '/appointment/putConfirmationDecision';
  69. $response = $this->calljava($request, $url, $data);
  70. if($response['success']){
  71. return redirect()->back()->with('success', true);
  72. }
  73. return redirect()->back()->with('error', true);
  74. }
  75. // TODO move to utility
  76. private function callJava($request, $endPoint, $data)
  77. {
  78. $url = config('stag.backendUrl') . $endPoint;
  79. $response = Http::asForm()
  80. ->withHeaders([
  81. 'secret' => 'superman'
  82. ])
  83. ->post($url, $data)
  84. ->json();
  85. return $response;
  86. }
  87. public function viewSurveyForm(Request $request, $accessKey){
  88. $survey = Survey::where('access_key', $accessKey)->where('is_accessible_to_target', true)->where('is_active', true)->first();
  89. if(!$survey) abort(404);
  90. $surveyFormPath = resource_path(Survey::FORM_PATH . '/' . $survey->internal_name . '.blade.php');
  91. if(!file_exists($surveyFormPath)) abort(404);
  92. $entity = null;
  93. if($survey->entity_type === 'Client'){
  94. $entity = Client::where('uid', $survey->entity_uid)->first();
  95. }
  96. if(!$entity) abort(404);
  97. return view('app.admin.surveys.forms.'.$survey->internal_name, compact('entity', 'survey'));
  98. }
  99. public function viewSurveyFormSubmit(Request $request, $accessKey){
  100. $survey = Survey::where('access_key', $accessKey)->where('is_accessible_to_target', true)->where('is_active', true)->first();
  101. if(!$survey) abort(404);
  102. $data = $request->all();
  103. unset($data['_token']);
  104. $url = '/survey/submitData';
  105. $response = $this->calljava($request, $url, [
  106. 'uid' => $survey->uid,
  107. 'surveyDataJson' => json_encode($data)
  108. ]);
  109. if($response['success']){
  110. return redirect()->back()->with('success', 'Information saved!');
  111. }
  112. return redirect()->back()->with('error', $response['message']);
  113. }
  114. }