HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1014-azure #14~24.04.1-Ubuntu SMP Fri Oct 3 20:52:11 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
File: /mnt/data/ghayatcom/ghayatcom-api/app/Library/PusherLibrary.php
<?php

namespace App\Library;

use App\User;
use App\Appointment;
use App\DependantAccountHolder;
use App\UserDetails;
use App\WaitingRoom;
use Illuminate\Support\Carbon;
use App\Http\Resources\AppointmentCollection;
use App\Http\Resources\AppointmentCreationResource;
use App\Events\AppointmentList;
use App\Events\Pusher\AppointmentCreationUpcoming;
use App\Events\Pusher\AppointmentCreationRequest;
use App\Events\Pusher\WaitingRoomEvent;
use App\Events\Pusher\LeaveWaitingRoom;
use App\Events\Pusher\EndConsultationWaitingRoom;
use App\Events\NotificationEvent;
use App\Enums\AppoitmentStatusEnum;


class PusherLibrary {
    public function patientAppointmentList($type, $appointment_id, $queryExecute='No', $queryArr=[]) {
        $roleType = '';
        $role_id = NULL;
        $type = ($type != "") ? $type : NULL;
        if($queryExecute == 'Yes') {
            if(!empty($queryArr)) {
                if(auth()->user()->hasRole(['patient'])) {
                    $role_id = $queryArr->doctor_id;
                    $roleType = 'doctor';
                } else {
                    $role_id = $queryArr->patient_id;
                    $roleType = 'patient';
                }

                $result['id'] = $queryArr->id;
                $result['pusher_type'] = $type;
                $result['consultation_type'] = $queryArr->consultation_type;
                $result['patient_id'] = $queryArr->patient_id;
                $result['doctor_id'] = $queryArr->doctor_id;
                $result['appointment_start_dt'] = $queryArr->appointment_start_dt;
                $result['appointment_end_dt'] = $queryArr->appointment_end_dt;
                $result['consultation_end_time'] = $queryArr->consultation_end_time;
                $result['appointment_status'] = $queryArr->appointment_status;
                // $result['appointment_status_str'] = ($queryArr->appointment_status == 1 ? "New":($queryArr->appointment_status == 2 ? "Approved":($queryArr->appointment_status == 3 ? "Completed":($queryArr->appointment_status == 4 ? "Cancelled":($queryArr->appointment_status == 5 ? "Expired":($queryArr->appointment_status == 6 ? "Rejected":""))))));
            } else {
                $result = NULL;
            }
        } else {
            $query = Appointment::find($appointment_id);
            if($query != "") {
                if(auth()->user()->hasRole(['patient'])) {
                    $role_id = $query->doctor_id;
                    $roleType = 'doctor';
                } else {
                    $role_id = $query->patient_id;
                    $roleType = 'patient';
                }

                $result['id'] = $query->id;
                $result['pusher_type'] = $type;
                $result['consultation_type'] = $query->consultation_type;
                $result['patient_id'] = $query->patient_id;
                $result['doctor_id'] = $query->doctor_id;
                $result['appointment_start_dt'] = $query->appointment_start_dt;
                $result['appointment_end_dt'] = $query->appointment_end_dt;
                $result['consultation_end_time'] = $query->consultation_end_time;
                $result['appointment_status'] = $query->appointment_status;
                /**
                 * @var object $query
                 */
                // $result['appointment_status_str'] = ($query->appointment_status == 1 ? "New":($query->appointment_status == 2 ? "Approved":($query->appointment_status == 3 ? "Completed":($query->appointment_status == 4 ? "Cancelled":($query->appointment_status == 5 ? "Expired":($query->appointment_status == 6 ? "Rejected":""))))));

            } else {
                $result = NULL;
            }
        }

        if($result != null) {
            event(new AppointmentList($result, $roleType, $type, $role_id));
        }
        // return $result;
    }

    public function appointmentCreationPusher($id, $role_id, $consultation) {
        $query = Appointment::find($id);
        if($query != "") {
            $result = new AppointmentCreationResource($query);
            if($consultation == 1) {
                event(new AppointmentCreationUpcoming($result, $role_id));
            }
            if($consultation != 1) {
                event(new AppointmentCreationRequest($result, $role_id));
            }
        }
    }

    public function waitingRoom($doctor_id) {
        $query = UserDetails::where('user_id', $doctor_id)->first();
        if($query != "") {
            if(!empty($query->waiting_room)) {
                $json['id'] = $query->user_id;
                $json['waiting_room'] = $query->waiting_room;
                event(new WaitingRoom($json));
            }
        }
    }
    public function LeavewaitingRoom($patient_id, $doctor_id, $appointment_id, $patient_appointment_id) {
        $query = User::where('id', $patient_id)->first();
        if($query != "") {
            if(!empty($query->waiting_room)) {
                $json['id'] = $query->id;
                $json['waiting_room'] = $query->waiting_room;
                event(new LeaveWaitingRoom($json, $patient_id));
            }
        }
    }
    public function EndConsultationwaitingRoomEvent($json, $patient_id){
        event(new EndConsultationWaitingRoom($json, $patient_id));
    }

    public function notificationRetrieve() {
        $result = auth()->user()->unreadNotifications->all();
        return $result;
    }

    public function notificationRetrieveEvent($user_id=null) {
        if($user_id != null) {
            $data = User::find($user_id)->unreadNotifications->first();
            $unread_count = User::find($user_id)->unreadNotifications->count();
        } else {
            $data = auth()->user()->unreadNotifications->first();
            $unread_count = auth()->user()->unreadNotifications->count();
            $user_id = auth()->user()->id;
        }

        $result_array = ['notify' => $data, 'unread_count' => $unread_count];

        event(new NotificationEvent($result_array, $user_id));
    }
}