GuestController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. use App\Http\Services\SurveyService;
  18. class GuestController extends Controller
  19. {
  20. public function section(Request $request, $guestAccessCode )
  21. {
  22. $section = Section::where('guest_access_code', $guestAccessCode)->first();
  23. abort_if(!$section, 404, 'Invalid access code');
  24. abort_if(!$section->is_active, 404, 'Invalid access code');
  25. abort_if($section->guest_access_level == 'NONE', 401, 'Invalid access code');
  26. $patient = null;
  27. if($section->note){
  28. abort_if($section->note->is_signed_by_hcp, 401, 'Note is already signed.');
  29. $patient = $section->note->client;
  30. }else{
  31. $patient = $section->client;
  32. }
  33. return view('app.guest.section', compact('patient','section', 'guestAccessCode'));
  34. }
  35. public function handout(Request $request, $handoutClientUid )
  36. {
  37. $handoutClient = HandoutClient::where('uid', $handoutClientUid)->first();
  38. abort_if((!$handoutClient || !$handoutClient->is_active), 404, 'Invalid access code');
  39. $handout = Handout::where('id', $handoutClient->handout_id)->first();
  40. abort_if((!$handout || !$handout->is_active), 404, 'Invalid access code');
  41. return Response::download(
  42. $handout->pdf_file_path,
  43. $handout->internal_name . '.pdf',
  44. ['Content-Type: application/pdf']
  45. );
  46. }
  47. public function appointmentConfirmation(Request $request, $appointmentUid )
  48. {
  49. $appointment = Appointment::where('uid', $appointmentUid)->first();
  50. abort_if(!count($appointment->confirmationRequests), 404, 'No confirmation requests on this appointment.');
  51. abort_if(!$appointment, 404, 'Invalid url');
  52. abort_if($appointment->status == 'COMPLETED', 404, 'Appointment has been completed');
  53. return view('app.guest.appointment-confirmation', compact('appointment'));
  54. }
  55. public function processAppointmentConfirmation(Request $request){
  56. $appointmentUid = $request->get('appointment_uid');
  57. $appointment = Appointment::where('uid', $appointmentUid)->first();
  58. abort_if(!count($appointment->confirmationRequests), 404, 'No confirmation requests on this appointment.');
  59. abort_if(!$appointment, 404, 'Invalid url');
  60. abort_if($appointment->status == 'COMPLETED', 404, 'Appointment has been completed');
  61. $decision = $request->get('decision');
  62. $memo = $request->get('memo');
  63. $response = null;
  64. $data = [
  65. 'uid' => $appointment->uid,
  66. 'memo' => $memo,
  67. 'confirmationDecisionEnum' => ($decision == 'REJECT' ? 'CANCELLED' : 'CONFIRMED')
  68. ];
  69. $url = '/appointment/putConfirmationDecision';
  70. $response = $this->calljava($request, $url, $data);
  71. if($response['success']){
  72. return redirect()->back()->with('success', true);
  73. }
  74. return redirect()->back()->with('error', true);
  75. }
  76. // TODO move to utility
  77. private function callJava($request, $endPoint, $data)
  78. {
  79. $url = config('stag.backendUrl') . $endPoint;
  80. $response = Http::asForm()
  81. ->withHeaders([
  82. 'secret' => 'superman'
  83. ])
  84. ->post($url, $data)
  85. ->json();
  86. return $response;
  87. }
  88. public function viewSurveyForm(Request $request, $accessKey){
  89. $survey = Survey::where('access_key', $accessKey)->where('is_accessible_to_target', true)->where('is_active', true)->first();
  90. if(!$survey) abort(404);
  91. $surveyFormPath = resource_path(Survey::FORM_PATH . '/' . $survey->internal_name . '.blade.php');
  92. if(!file_exists($surveyFormPath)) abort(404);
  93. $entity = null;
  94. if($survey->entity_type === 'Client'){
  95. $entity = Client::where('uid', $survey->entity_uid)->first();
  96. }
  97. if(!$entity) abort(404);
  98. $layout = 'app.patient.surveys.partials.form-layout';
  99. return view('app.patient.surveys.forms.form', compact('entity', 'survey', 'layout'));
  100. }
  101. public function viewSurveyFormSubmit(Request $request, $accessKey){
  102. $survey = Survey::where('access_key', $accessKey)->where('is_accessible_to_target', true)->where('is_active', true)->first();
  103. if(!$survey) abort(404);
  104. $formData = [
  105. 'uid' => $survey->uid,
  106. 'surveyDataJson' => $request->get('data'),
  107. 'surveyHTML' => $survey->surveyhtml,
  108. ];
  109. $url = '/survey/submitData';
  110. $response = $this->calljava($request, $url, $formData);
  111. if($response['success']){
  112. return redirect()->back()->with('success', 'Information saved!');
  113. }
  114. return redirect()->back()->with('error', $response['message']);
  115. }
  116. public function surveyTemplatePreview(Request $request, $internalName){
  117. $surveyService = new SurveyService($internalName);
  118. $defaultHtml = $surveyService->defaultHTML;
  119. if(!$defaultHtml) return 'No default HTML template found for ' . $internalName;
  120. $entityType = $request->get('entityType');
  121. $entityUid = $request->get('entityUid');
  122. $initializedData = $surveyService->getInitializedData($entityType, $entityUid);
  123. $survey = new Survey;
  124. $survey->surveyhtml = $defaultHtml;
  125. $survey->survey_data = json_encode($initializedData);
  126. return $survey;
  127. }
  128. }