diff --git a/app/Config/Autoload.php b/app/Config/Autoload.php index a1a4356c..525acb3c 100755 --- a/app/Config/Autoload.php +++ b/app/Config/Autoload.php @@ -100,5 +100,7 @@ class Autoload extends AutoloadConfig * @var string[] * @phpstan-var list */ - public $helpers = ['uuid','session','utility', 'form', 'url', 'oauth', 'fileupload', 'excel_import_export', 'file', 'drive','ExcelSanitizeHelper', 'api_helper']; + public $helpers = ['uuid','session','utility', 'form', 'url', 'oauth', 'fileupload', + 'excel_import_export', 'file', 'drive','ExcelSanitizeHelper', 'api_helper','ExceptionHelper' +]; } diff --git a/app/Controllers/ThzController.php b/app/Controllers/ThzController.php index 457ec66a..0f8125b3 100644 --- a/app/Controllers/ThzController.php +++ b/app/Controllers/ThzController.php @@ -69,30 +69,7 @@ class ThzController extends BaseController $old = $this->thzMasterModel->where('thz_id', $data['thz_id'])->get()->getRowArray(); $result = $this->updateTicket($data); - if ($old && isset($data['status'])) { - - if ($old['status'] != $data['status']) { - $history = [ - 'thz_id' => $data['thz_id'], - 'field_name' => 'status', - 'display_name' => 'Status', - 'old_value' => $old['status'], - 'new_value' => $data['status'], - 'is_active' => 1 - ]; - - if (!empty(get_session_userid())) { - $history['created_by'] = get_session_userid(); - } - - $hist_id = $this->thzHistoryModel->insert($history); - $references = $old['status'] . " - " . $data['status'] . " - " . $hist_id; - } else { - $references = $old['status'] . " - " . $data['status']; - } - } else { - $references = "No Details"; - } + $references = ($old && isset($data['status'])) ? $this->handleTicketHistory($old, $data) : "No Details" ; $updateID = $data['thz_id']; } else { @@ -115,22 +92,10 @@ class ThzController extends BaseController 'notification' => $emailNotificationResult ? 'Email Notification Success..!!' : 'Email Notification Failed..!!', 'ref' => $references ])->setStatusCode($result ? 200 : 400); + + } catch (\Throwable $e) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; - - $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ - 'status' => 'error', - 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); + return handle_exception($e, $this->myLogger, $this->response); } } @@ -161,7 +126,7 @@ class ThzController extends BaseController } } else { $data['ticket_data'] = $tickets; - $data['assignee'] = $this->userModel->where('is_active', 1)->whereIn('role', ['3', '4'])->findAll(); + $data['assignee'] = $this->userModel->where('is_active', 1)->whereIn('role', ['3', '4'])->findAll(); // enga 5-"head" and 1-"admin" assign pannvaga so dropdown la varakudhathu , 2-"manager l2 " - ivangalum assign pannalam // 3,4 remain person varannum. $data['client_list'] = $this->clientModel->getCreatedByUserName(); @@ -176,21 +141,7 @@ class ThzController extends BaseController 'data' => $tickets, ])->setStatusCode(200); } catch (\Throwable $e) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; - - $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ - 'status' => 'error', - 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); + return handle_exception($e, $this->myLogger, $this->response); } } @@ -209,35 +160,19 @@ class ThzController extends BaseController throw new \RuntimeException('No tickets found', 404); } - if ($data['notes_type'] == "External") { - $emailNotificationResult = $this->ticketConversationSaveNotification($data); - } else { - $emailNotificationResult = false; - } - - + $emailNotificationResult = ($data['notes_type'] === "External") + ? $this->ticketConversationSaveNotification($data) + : false; return $this->response->setJSON([ 'status' => $result ? 'success' : 'error', 'message' => $result ? "Ticket Notes created successfully" : "Unable to create ticket notes. Please try again.", 'notification' => $emailNotificationResult ? 'Email Notification Success..!!' : 'Email Notification Failed Or No Need..!!' ])->setStatusCode($result ? 200 : 400); + + } catch (\Throwable $e) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; - - $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ - 'status' => 'error', - 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); + return handle_exception($e, $this->myLogger, $this->response); } } @@ -270,21 +205,7 @@ class ThzController extends BaseController return $this->response->setJSON(['status' => 'success', 'data' => $result])->setStatusCode(200); } catch (\Throwable $e) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; - - $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ - 'status' => 'error', - 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); + return handle_exception($e, $this->myLogger, $this->response); } } @@ -300,7 +221,7 @@ class ThzController extends BaseController if ($thz_id) { $result['master'] = $this->thzMasterModel - ->select('thz_master.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_name) as assignee_name') + ->select('thz_master.*, user_profiles.first_name as assignee_name') ->join('user_profiles', 'user_profiles.id = thz_master.assign_to', 'left') ->where('thz_master.thz_id', $thz_id) ->findAll(); @@ -309,7 +230,7 @@ class ThzController extends BaseController $notes = $this->thzMasterNotesModel->ticketConversationList($thz_id, $returnType); $result['notes'] = !empty($notes) ? $notes : []; } else { - $this->myLogger->logme('error', 'No tickets found for ticket id ' . json_encode($thz_id)); + $this->myLogger->logme('error', 'No tickets found for ticket id ' . json_encode($thz_id)); throw new \RuntimeException('No tickets found', 400); } } else { @@ -321,21 +242,7 @@ class ThzController extends BaseController try { return $this->response->setJSON((['status' => 'success', 'data' => $result]))->setStatusCode(200); } catch (\Throwable $e) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; - - $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ - 'status' => 'error', - 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); + return handle_exception($e, $this->myLogger, $this->response); } } else { @@ -347,7 +254,7 @@ class ThzController extends BaseController : ''; $result['related_tickets'] = $this->thzMasterModel - ->select('thz_master.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_name) as assignee_name') + ->select('thz_master.*, user_profiles.first_name as assignee_name') ->join('user_profiles', 'user_profiles.id = thz_master.assign_to', 'left') ->where('thz_master.assign_to', $assign_to) ->where('thz_master.mobile', $mobile) @@ -373,7 +280,7 @@ class ThzController extends BaseController if ($method === 'get') { $types = $this->thzTypeModel->select('id,name,is_active')->where('is_active', 1)->findAll(); if (empty($types)) { - $this->myLogger->logme('error', 'No ticket types found '); + $this->myLogger->logme('error', 'No ticket types found '); return $this->response->setJSON(['status' => 'error', 'message' => 'No ticket types found'])->setStatusCode(404); } return $this->response->setJSON(['status' => 'success', 'data' => $types])->setStatusCode(200); @@ -402,21 +309,7 @@ class ThzController extends BaseController } return $this->response->setJSON(['status' => 'error', 'message' => 'Invalid request method'])->setStatusCode(405); } catch (\Throwable $e) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; - - $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ - 'status' => 'error', - 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); + return handle_exception($e, $this->myLogger, $this->response); } } @@ -426,7 +319,7 @@ class ThzController extends BaseController $data = $this->request->getGet(); $thz_id = $data['thz_id']; // $details = $this->thzHistoryModal->whereIn('thz_id', $thz_id)->where('is_active', 1)->get()->getResultArray(); - $details = $this->thzHistoryModel->select('thz_history.*, CONCAT(user_profiles.first_name, " ", user_profiles.last_name) as created_name') + $details = $this->thzHistoryModel->select('thz_history.*, user_profiles.first_name as created_name') ->join('user_profiles', 'user_profiles.id = thz_history.created_by', 'left') ->where('thz_history.thz_id', $thz_id)->where('thz_history.is_active', 1) ->orderBy('thz_history.created_at', 'desc') @@ -435,28 +328,14 @@ class ThzController extends BaseController if (empty($details)) { - $this->myLogger->logme('error', 'No history details found for ticket id ' . json_encode($thz_id) ); + $this->myLogger->logme('error', 'No history details found for ticket id ' . json_encode($thz_id)); throw new \RuntimeException('No History found', 404); } return $this->response->setJSON(['status' => 'success', 'data' => $details])->setStatusCode(200); } catch (\Throwable $e) { - $code = ($e->getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; - - $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException - || $e instanceof \mysqli_sql_exception - || $e instanceof \PDOException; - - $context = ['title' => get_class($e), 'type' => get_class($e), 'code' => $code, 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]; - - $this->myLogger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); - - return $this->response->setJSON([ - 'status' => 'error', - 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), - 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() - ])->setStatusCode($code); + return handle_exception($e, $this->myLogger, $this->response); } } @@ -634,23 +513,23 @@ class ThzController extends BaseController $cc = getenv('G_ticketing_system_cc'); - $this->myLogger->logme('error', "env for cc G_ticketing_system_cc ".$cc); + $this->myLogger->logme('error', "env for cc G_ticketing_system_cc " . $cc); - $template = empty($assignee_email) - ? 'ticket_save_create_user_unassigned' + $template = empty($assignee_email) + ? 'ticket_save_create_user_unassigned' : 'ticket_save_create_user_assigned'; if (!empty($assignee_email)) { $data['assignee_email'] = $assignee_email; } - $this->sendEmail($template, $data, $user_email, $ticketId); + $this->sendEmail($template, $data, $user_email, $ticketId); // send to account manager and if it is more than one email also no problem we send to all account managers if (!empty($account_manager_email)) { - $this->myLogger->logme('error', "mail sent for account manager ". json_encode($account_manager_email)); + $this->myLogger->logme('error', "mail sent for account manager " . json_encode($account_manager_email)); $this->sendEmail($template, $data, $account_manager_email, $ticketId, $cc); - }else{ + } else { $this->myLogger->logme('error', "mail not sent for account manager "); } @@ -684,19 +563,19 @@ class ThzController extends BaseController } - private function sendEmail($templateName, $data, $to_email, $ticketId , $cc = "", $bcc = "") + private function sendEmail($templateName, $data, $to_email, $ticketId, $cc = "", $bcc = "") { $mailTemplate = $this->ticketMailTemplateModel->where('template_name', $templateName)->findAll()[0] ?? []; if (empty($mailTemplate)) { - $this->myLogger->logme('error','No Mail Template Found To send email in ticketing system'); + $this->myLogger->logme('error', 'No Mail Template Found To send email in ticketing system'); return; } - if( empty($cc)){ + if (empty($cc)) { $this->myLogger->logme('error', 'empty cc attached for template name : ' . json_encode($templateName)); - }else{ + } else { $this->myLogger->logme('error', "cc attached for template name : " . json_encode($templateName)); } @@ -724,7 +603,6 @@ class ThzController extends BaseController $res = MailHelper::send_email(['from_mail' => $from_mail, 'mail' => $to_mail, 'cc' => $cc_string, 'subject' => $subject, 'message' => $message, 'attachments' => $attachments, 'reply_to' => $reply_to, 'bcc' => $bcc_string]); $this->myLogger->logme('error', 'Mail Response : ' . json_encode($res)); - } @@ -744,4 +622,34 @@ class ThzController extends BaseController $data['policy_terms'] = $policy_terms; return view('view_policy_terms', $data); } + + + private function handleTicketHistory(array $old, array $data): string + { + if ($old['status'] != $data['status']) { + $history = [ + 'thz_id' => $data['thz_id'], + 'field_name' => 'status', + 'display_name' => 'Status', + 'old_value' => $old['status'], + 'new_value' => $data['status'], + 'is_active' => 1 + ]; + + if (!empty(get_session_userid())) { + $history['created_by'] = get_session_userid(); + } + + $hist_id = $this->thzHistoryModel->insert($history); + + return $old['status'] . " - " . $data['status'] . " - " . $hist_id; + } + + return $old['status'] . " - " . $data['status']; + } + + + + + } diff --git a/app/Helpers/ExceptionHelper.php b/app/Helpers/ExceptionHelper.php new file mode 100644 index 00000000..c3211950 --- /dev/null +++ b/app/Helpers/ExceptionHelper.php @@ -0,0 +1,35 @@ +getCode() && $e->getCode() >= 100 && $e->getCode() < 600) ? $e->getCode() : 500; + + $isDbError = $e instanceof \CodeIgniter\Database\Exceptions\DatabaseException + || $e instanceof \mysqli_sql_exception + || $e instanceof \PDOException; + + $context = [ + 'title' => get_class($e), + 'type' => get_class($e), + 'code' => $code, + 'message' => $e->getMessage(), + 'file' => $e->getFile(), + 'line' => $e->getLine() + ]; + + if ($logger) { + $logger->logme('error', "Exception: {message} in {file} on line {line}", $context, 0); + } else { + log_message('error', "Exception: {$context['message']} in {$context['file']} on line {$context['line']}"); + } + + return $response->setJSON([ + 'status' => 'error', + 'message' => $isDbError ? 'Data Access Error' : $e->getMessage(), + 'ref' => $isDbError ? 'database - ' : 'application - ' . $code . ' - ' . $e->getLine() + ])->setStatusCode($code); + } +} +