GuestController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. ];
  108. $url = '/survey/submitData';
  109. $response = $this->calljava($request, $url, $formData);
  110. if(!@$response['success']){
  111. return redirect()->back()->with('error', $response['message']);
  112. }
  113. return $this->toggleSurveyMarkAsCompleted($request, $survey->uid);
  114. }
  115. public function viewSurveyFormAutoSubmit(Request $request, $accessKey){
  116. $survey = Survey::where('access_key', $accessKey)->where('is_accessible_to_target', true)->where('is_active', true)->first();
  117. if(!$survey) return $this->fail('Invalid survey access key!');
  118. if($survey->is_completed){
  119. return $this->fail('This survey has been marked as completed and you cannot modify the information saved!!');
  120. }
  121. $formData = [
  122. 'uid' => $survey->uid,
  123. 'surveyDataJson' => $request->get('dataJson'),
  124. ];
  125. $url = '/survey/submitData';
  126. $response = $this->calljava($request, $url, $formData);
  127. if(!@$response['success']){
  128. return $this->fail($response['message']);
  129. }
  130. return $this->pass();
  131. }
  132. private function toggleSurveyMarkAsCompleted(Request $request, $surveyUid){
  133. $markAsCompleted = $request->get('mark_as_completed');
  134. if($markAsCompleted){
  135. $url = '/survey/markAsCompleted';
  136. $response = $this->calljava($request, $url, ['uid' => $surveyUid]);
  137. if(!@$response['success']){
  138. return redirect()->back()->with('error', $response['message']);
  139. }
  140. }
  141. $undoMarkAsCompleted = $request->get('undo_mark_as_completed');
  142. if($undoMarkAsCompleted){
  143. $url = '/survey/undoMarkAsCompleted';
  144. $response = $this->calljava($request, $url, ['uid' => $surveyUid]);
  145. if(!@$response['success']){
  146. return redirect()->back()->with('error', $response['message']);
  147. }
  148. return redirect()->back()->with('error', 'You have marked this survey as incomplete!');
  149. }
  150. return redirect()->back()->with('success', 'Information saved!');
  151. }
  152. public function viewSurveyFormGetData(Request $request, $accessKey){
  153. $survey = Survey::where('access_key', $accessKey)->where('is_accessible_to_target', true)->where('is_active', true)->first();
  154. return $this->pass($survey);
  155. }
  156. public function surveyTemplatePreview(Request $request, $internalName){
  157. $surveyService = new SurveyService($internalName);
  158. $defaultHtml = $surveyService->defaultHTML;
  159. if(!$defaultHtml) return 'No default HTML template found for ' . $internalName;
  160. $entityType = $request->get('entityType');
  161. $entityUid = $request->get('entityUid');
  162. $initializedData = $surveyService->getInitializedData($entityType, $entityUid);
  163. $survey = new Survey;
  164. $survey->surveyhtml = $defaultHtml;
  165. $survey->survey_data = json_encode($initializedData);
  166. return $survey;
  167. }
  168. }