%PDF- %PDF-
Direktori : /var/www/html/camillo/camillo-api-master/app/Services/ |
Current File : /var/www/html/camillo/camillo-api-master/app/Services/XcallyService.php |
<?php namespace App\Services; use App\Enums\NoticeStatusEnum; use App\Mail\XCallyExceptionNotification; use App\Models\Individual; use App\Models\Minor; use App\Models\Notice; use App\Scopes\IndividualScope; use App\Scopes\MinorScope; use App\Traits\EventEmitterTrait; use Carbon\Carbon; use danielme85\LaravelLogToDB\LogToDB; use GuzzleHttp\Client; use GuzzleHttp\Psr7; use Illuminate\Http\Response; use GuzzleHttp\Exception\RequestException; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use Laravel\Nova\Actions\Action; class XcallyService { use EventEmitterTrait; const LIST_ID = 4; public function forwardCall(Notice $notice) { if($notice->id != 9999999){ $notice->status = NoticeStatusEnum::UNDERWAY; $notice->saveOrFail(); } $this->registerXcallyEvent($notice, "emergency_call_forwarded"); $ids = [ $notice->id, $notice->minor_id, $notice->institute_id, $notice->classroom_id, $notice->schoolyear_id, $notice->user_id]; $alert_id = implode("_",$ids); $minor = Minor::withoutGlobalScope(MinorScope::class)->find($notice->minor_id); $individual = Individual::withoutGlobalScope(IndividualScope::class)->find($notice->individual_id); $payload = [ "firstName" => $individual->name, "lastName" => $individual->surname, "phone" => $individual->mobile, "ListId" => XcallyService::LIST_ID, "cf_1" => $alert_id, "cf_2" => $minor->gender == "M" ? 0 : 1, "cf_3" => env('XCALLY_HOOK_TARGET') ]; $client = new Client([ 'base_uri' => env('XCALLY_BASE_URL', 'https://34.242.200.76/api/cm/'), 'timeout' => 50.0, ]); try { $response = $client->request('POST', 'contacts', [ 'verify' => false, 'auth' => [ env('XCALLY_USER', "admin"), env('XCALLY_PASSWORD', "123Camillo456!") ], 'json' => $payload ]); Log::channel('database')->info("$notice->id - $alert_id - Chiamata Xcally partita"); } catch (\Exception $e) { Log::channel('database')->info("$notice->id - $alert_id - Chiamata Xcally errore"); try { $dateNow = Carbon::now()->format('Y-m-d'); $text = "$notice->institute_id - $dateNow - Mail inviata"; if (!LogToDB::model('database')->where('message', $text)->exists()) { $receivers = env('XCALLY_EXCEPTION_RECEIVERS', 'diego.baldeschi@comm-it.it'); Mail::to(explode(';', $receivers))->send(new XCallyExceptionNotification($notice->id, $alert_id, $e->getMessage())); Log::channel('database')->alert($text); } } catch (\Exception $exception) { return Action::danger($exception->getMessage()); // return Action::danger(__('Something went wrong!')); } return response($e->getMessage() ,Response::HTTP_INTERNAL_SERVER_ERROR); } } }