validate($request, [ 'issuer_network_agent_id' => 'required|integer|exists:networks_agents,id', 'act_id' => 'required|integer|exists:nh_acts,id', 'insurance_id' => 'required|integer|exists:nh_insurances,id', 'beneficiary_id' => 'nullable|int|exists:nh_having_rights,id', 'password' => 'required|string' ]); $act_id = $request->input('act_id'); $issuer_network_agent_id = $request->input('issuer_network_agent_id'); $insurance = NhInsurance::find($request->input('insurance_id')); if (!in_array($insurance->state, [InsuranceState::PAID, InsuranceState::PARTIALLY_PAID])) { return $this->errorResponse(__('errors.insurance_not_in_order')); } $user = $insurance->user; $this->userCredentialsVerification($user, $request->input('password')); $beneficiary_id = $request->input('beneficiary_id'); if (!empty($beneficiary_id)) { $beneficiary = NhInsurancesHavingRight::where('insurance_id', $insurance->id)->where('having_right_id', $beneficiary_id)->first(); if (!isset($beneficiary)) { return $this->errorResponse(trans('errors.beneficiary_not_found')); } } else { $beneficiary_id = null; } $currentRequest = NhAuthorizationOfCareRequest::where('insurance_id', $insurance->id)->where('act_id', $act_id) ->where('beneficiary_id', $beneficiary_id)->where('state', InsuranceSubscriptionState::UNDER_VALIDATION)->first(); if (isset($currentRequest)) { return $this->errorResponse(__('errors.act_application_already_pending')); } $act = NhAct::find($act_id); try { $datetime = $this->getCurrentTimeByCountryCode($user->network->country->code_country); DB::beginTransaction(); $authRequest = NhAuthorizationOfCareRequest::create([ 'request_id' => $this->generateRequestID(), 'issuer_network_agent_id' => $issuer_network_agent_id, 'insurance_id' => $insurance->id, 'beneficiary_id' => $beneficiary_id, 'to' => isset($beneficiary) ? 'HAVING_RIGHT' : 'INSURED', 'act_id' => $act_id, 'state' => InsuranceSubscriptionState::UNDER_VALIDATION, ]); $authRequest->created_at = $authRequest->updated_at = $datetime; DB::commit(); $recipients = array_map(function ($email) { return preg_replace("/\s+/", "", $email); }, NhValidatingAgent::where('role', 'DOCTOR')->whereNotNull('password')->pluck('email')->toArray()); $title = __('messages.new_care_authorisation'); $insured = $user->lastname . ' ' . $user->firstname; if (isset($beneficiary)) { $insured = $beneficiary->beneficiary->lastname . ' ' . $beneficiary->beneficiary->firstname; } $message = __('messages.new_care_authorisation_email', ['insured' => $insured, 'act' => $act->name]); Mail::mailer('smtp')->raw($message, function ($message) use ($recipients, $title) { $message->subject($title) ->to($recipients); }); } catch (Throwable $t) { DB::rollBack(); Log::error('-------- Authorization of care error -----------'); Log::error($t->getMessage() . " :\n" . $t->getTraceAsString()); } return $this->successResponse(__('messages.new_care_authorisation_sent')); } public function treatRequest(Request $request) { $this->validate($request, [ 'request_id' => 'required|integer|exists:nh_authorization_of_care_requests,id', 'validating_agent_id' => 'required|integer|exists:nh_validating_agents,id', 'action' => 'required|in:ACCEPT,REJECT' ]); $action = $request->input('action'); $validating_agent_id = $request->input('validating_agent_id'); $request = NhAuthorizationOfCareRequest::findOrFail($request->input('request_id')); if ($request->state != InsuranceSubscriptionState::UNDER_VALIDATION) { return $this->errorResponse(trans('errors.care_request_already_been_processed')); } $datetime = $this->getCurrentTimeByCountryCode($request->insurance->network->country->code_country); if ($action == 'ACCEPT') { $request->state = InsuranceSubscriptionState::ACCEPTED; $message = trans('messages.care_request_accepted'); $notification = trans('messages.care_request_accepted_notification', ['request_id' => $request->request_id]); } else { $request->state = InsuranceSubscriptionState::REJECTED; $message = trans('messages.care_request_rejected'); $notification = trans('messages.care_request_rejected_notification', ['request_id' => $request->request_id]); } $request->validating_agent_id = $validating_agent_id; $request->updated_at = $datetime; $request->save(); try { $client = new Client([ 'base_uri' => config('services.notification_service.base_uri'), ]); $headers = [ 'Authorization' => config('services.notification_service.key'), ]; $body = new stdClass(); $body->user_code = $request->user->user_code; $body->message = $notification; $body->date = $datetime; $data = new stdClass(); $data->screen = "demandeAutorisationSoinScreen"; $data->data = new stdClass(); $data->data->id = $request->id; $body->data = $data; $client->request('POST', '/onesignal/pushToUser', ['json' => $body, 'headers' => $headers]); } catch (Throwable $t) { Log::error('-------- Treat Care Request notification not sent-----------'); Log::error($t->getMessage() . '\n' . $t->getTraceAsString()); } return $this->successResponse($message); } /** * @OA\Get( * path="/authorizations-care-requests", * summary="Lister toutes les demandes autorisation de soin", * tags={"Demandes d'autorisation de soins"}, * security={{"api_key":{}}}, * @OA\Parameter( * parameter="issuer_network_agent_id", * name="issuer_network_agent_id", * description="ID network de l'agent emetteur", * in="query", * required=false, * @OA\Schema( * type="integer", * ) * ), * @OA\Parameter( * parameter="user_id", * name="user_id", * description="ID de l'utilisateur", * in="query", * required=false, * @OA\Schema( * type="integer", * ) * ), * @OA\Parameter( * parameter="page", * name="page", * description="Page", * in="query", * required=false, * @OA\Schema( * type="integer" * ) * ), * @OA\Response( * response=200, * description="OK", * @OA\JsonContent( * ref="#/components/schemas/ApiResponse", * example = { * "status" : 200, * "response" : {{"id":2,"request_id":"E7879YZMOFOE","user_id":349,"act_id":7,"state":"EN COURS DE VALIDATION","validating_agent_id":null,"created_at":"2022-02-01T10:00:02.000000Z","updated_at":"2022-02-01T10:00:02.000000Z"," * act_name":"Les analyses de biologie m\u00e9dicale","act_code":"CODE4","network_id":250,"user_lastname":"Tom Di","user_firstname":null,"user_phone":"+237690716648","user_email":"ddoubletom@gmail.com","validating_agent_lastname":null, * "validating_agent_firstname":null,"validating_agent_phone":null,"validating_agent_email":null,"network":{"id":250,"name":"Cnamgs-pharmacies"}}}, * "error":null * } * ) * ) * ) */ public function getAll(Request $request) { $this->validate($request, [ 'user_id' => 'nullable|integer|exists:users,id', 'issuer_network_agent_id' => 'nullable|integer|exists:networks_agents,id', ]); $user_id = $request->input('user_id'); $issuer_network_agent_id = $request->input('issuer_network_agent_id'); $query = NhInfosAuthorizationOfCareRequest::with(['network:id,name']); if (!empty($user_id)) { $query = $query->where('user_id', $user_id); } if (!empty($issuer_network_agent_id)) { $query = $query->where('issuer_network_agent_id', $issuer_network_agent_id); } $requests = $query->orderBy('created_at', 'DESC')->paginate($request->input('perPage', 10)); foreach ($requests->items() as $r) { $r->state = trans('states.' . $r->state); if ($r->to == 'HAVING_RIGHT') { $r->user_lastname = $r->beneficiary_lastname; $r->user_firstname = $r->beneficiary_firstname; } $r->to = trans('states.' . $r->to); unset($r->beneficiary_lastname, $r->beneficiary_firstname); } return $this->successResponse($requests); } private function generateRequestID(): string { do { $code = generateTransactionCode(); $codeCorrect = NhAuthorizationOfCareRequest::where('request_id', $code)->count() < 0; } while ($codeCorrect); return $code; } }