%PDF- %PDF-
Direktori : /var/www/html/camillo/camillo-api-master/app/Services/ |
Current File : /var/www/html/camillo/camillo-api-master/app/Services/HookService.php |
<?php namespace App\Services; use App\Enums\NoticeStatusEnum; use App\Models\Absence; use App\Models\Classroom; use App\Models\Event; use App\Models\EventType; use App\Models\Minor; use App\Models\Notice; use App\Scopes\ClassroomScope; use App\Scopes\MinorScope; use App\Traits\EventEmitterTrait; use App\Traits\NoticeHandlerTrait; use App\User; use Illuminate\Http\Response; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; class HookService { use EventEmitterTrait; use NoticeHandlerTrait; private $request = []; /** * HookService constructor. * @param $request */ public function __construct($request) { $this->request = $request; } public function handle() { $alert_id = $this->request["alert_id"]; $ids = explode("_", $alert_id); $notice = Notice::find($ids[0]); if (!$notice) { Log::channel('database')->info(intval($ids[1]).": notices già eliminate per questo minore"); return response()->json(["data" => "OK"],Response::HTTP_OK); } $result = ($this->request["result"] == "true" || $this->request["result"] == 1) ; $name = $this->request["name"]; $surname = $this->request["surname"]; $phone = $this->request["phone"]; $contact = "$name $surname $phone"; $event_type_name = ($result) ? "emergency_call_answered" : "emergency_call_no_answer"; $event_type = EventType::where('event_type', $event_type_name)->first(); $time = date("H:i:s"); Log::channel('database')->info(intval($ids[1]).": Hook called at $time with result $result; comes from $contact"); if(!$event_type){ return response("event type not found",Response::HTTP_BAD_REQUEST); } $event = new Event([ "date" => date("Y-m-d H:i:s"), "date_as_int" => (int)date("Ymd"), "description" => __($event_type_name) . " (" . $contact . ")", "minor_id" => intval($ids[1]), "institute_id" => intval($ids[2]), "classroom_id" => intval($ids[3]), "schoolyear_id" => intval($ids[4]), "event_type_id" => intval($event_type->id), "user_id" => intval($ids[5]), ]); if(!$event->save()){ Log::channel('database')->error(intval($ids[1]).": ". $event->getErrors()); } $is_emergency_call = ($ids[0] == 9999999); if(!$is_emergency_call){ try { //aggiornare notice a handled $notice = Notice::findOrFail($ids[0]); $notice->status = NoticeStatusEnum::HANDLED; if (!$notice->save()) { Log::channel('database')->error(intval($ids[1]).": ". $notice->getErrors()); } if ($result == true) { Log::channel('database')->info(intval($ids[1]).": $time: entrato in true from $contact"); Log::channel('database')->info(intval($ids[1]).": Tentativo cancellazione coda from $contact"); $this->deleteCallsQueue($notice); $today = date("Y-m-d"); $absence = new Absence( [ "date" => $today, "reason" => __("emergency_call_answered") ] ); $minor = Minor::withoutGlobalScopes([MinorScope::class])->find($ids[1]); $user = User::find($ids[5]); $classroom = Classroom::withoutGlobalScopes([ClassroomScope::class])->find($ids[3]); Log::channel('database')->info(intval($ids[1]).": Tentativo creazione assenza from $contact"); $absence->user()->associate($user); $absence->minor()->associate($minor); $absence->classroom()->associate($classroom); if (!$absence->save()) { Log::channel('database')->error(intval($ids[1]).": ".$absence->getErrors()); } else { Log::channel('database')->info(intval($ids[1]).": Assenza creata correttamente from $contact"); } Log::channel('database')->info(intval($ids[1]).": Tentativo registrazione evento assenza from $contact"); $this->registerEventNoAuth("expected_absence_signaled", $minor, $classroom, $user); Log::channel('database')->info(intval($ids[1]).": evento assenza registrato from $contact"); /* DB::transaction(function () use($notice, $ids) { });*/ } else { Log::channel('database')->info(intval($ids[1]).": $time: Tentativo di recupero prossima chiamata in coda from $contact"); $new_notice = Notice::where('minor_id', $notice->minor_id)->where( 'institute_id', $notice->institute_id )->where('classroom_id', $notice->classroom_id)->where('schoolyear_id', $notice->schoolyear_id) ->where('status', "pending")->where('start_date', $notice->start_date)->where( 'end_date', $notice->end_date )->orderBy('id', 'asc')->first(); if ($new_notice == null) { Log::channel('database')->info(intval($ids[1]).": $time: Entro in caso 'Nessuno ha risposto'"); //Se non ci sono successive notices perchè nessuno ha risposto DB::beginTransaction(); try { //cancella tutte le chiamate Log::channel('database')->info( intval($ids[1]).": $time: tentativo cancellazione coda perchè nessuno ha risposto" ); $this->deleteCallsQueue($notice); Log::channel('database')->info(intval($ids[1]).": $time: Cancellazione avvenuta con successo"); Log::channel('database')->info( intval($ids[1]).": $time: Tentativo di registrare evento 'nessuna risposta'" ); //registra un evento di "non risposta" $minor = Minor::withoutGlobalScopes([MinorScope::class])->find($ids[1]); $user = User::find($ids[5]); $classroom = Classroom::withoutGlobalScopes([ClassroomScope::class])->find($ids[3]); $this->registerEventNoAuth("no_tutor_answered", $minor, $classroom, $user); Log::channel('database')->info(intval($ids[1]).": $time: evento 'nessuna risposta' registrato"); DB::commit(); } catch (\Exception $exception) { DB::rollBack(); Log::channel('database')->error(intval($ids[1]).": $time: cancellazione coda fallito {$exception->getMessage()}"); } return response()->json([], Response::HTTP_NO_CONTENT); } Log::channel('database')->info(intval($ids[1]).": $time: Trovata chiamata in coda successiva"); $service = new XcallyService; $service->forwardCall($new_notice); } } catch (\Exception $exception) { Log::channel('database')->error(intval($ids[1]).": $time: {$exception->getMessage()}"); } return response()->json(["data" => "OK"],Response::HTTP_OK); } } }