diff --git a/Job_Queue.txt b/Job_Queue.txt new file mode 100755 index 00000000..56a6051c --- /dev/null +++ b/Job_Queue.txt @@ -0,0 +1 @@ +1 \ No newline at end of file diff --git a/app/Controllers/MasterController.php b/app/Controllers/MasterController.php index 1599bac2..a875e437 100755 --- a/app/Controllers/MasterController.php +++ b/app/Controllers/MasterController.php @@ -30,6 +30,8 @@ use App\Models\StateModel; use App\Models\PolicyTypeModel; use App\Models\CDMasterModel; use App\Models\ClientDepositModel; +use App\Models\EmployeePolicyModel; +use App\Models\FileModel; use App\Models\InsurerExcelExportTemplateModel; use App\Models\SettingsModel; use App\Models\VehicleModel; @@ -1574,7 +1576,7 @@ class MasterController extends AdminController $to = $this->request->getPost('to'); $subject = $this->request->getPost('subject'); $mail_content = $this->request->getPost('content'); - + $common = ['mail_type'=>'test_mail_ui']; /*****CHOOSE ANY ONE AT A TIME, COMMENT ANOTHER ONE******/ /*****CALLING DIRECLT USING LIB******/ @@ -1591,7 +1593,7 @@ class MasterController extends AdminController /*****CALLING DIRECLT USING LIB******/ /*****CALLING VIA MAIL HELPER******/ - $res = MailHelper::send_email(['mail' => $to, 'subject' => $subject, 'message' => $mail_content]); + $res = MailHelper::send_email(['mail' => $to, 'subject' => $subject,'common'=>$common ,'message' => $mail_content]); return $this->respond(['status' => 'success','code' => 200,'data' => $res],200); /*****CALLING VIA MAIL HELPER******/ @@ -1623,10 +1625,10 @@ class MasterController extends AdminController ["filePath" => ROOTPATH."public/sample_excel/sample_inception.xls","fileName" => "sample_inception.xls"], ["filePath" => ROOTPATH."public/assets/images/login_bg.jpg","fileName" => "login_bg.jpg"] ]; - - + $common = ['mail_type'=>'test_mail_cli']; $email_id = CLI::getOption('to') ? CLI::getOption('to') : $email_id; - $res = MailHelper::send_email(['mail' => $email_id, 'subject' => 'Mail Via CLI', 'message' => $message,'attachments' => $attachments]); + $res = MailHelper::send_email(['mail' => $email_id, 'subject' => 'Mail Via CLI', 'message' => $message,'attachments' => $attachments,'common'=>$common]); + echo "Inside the master controller"; print_r($res); } diff --git a/app/Helpers/GmailResponseHandler.php b/app/Helpers/GmailResponseHandler.php new file mode 100644 index 00000000..27347b94 --- /dev/null +++ b/app/Helpers/GmailResponseHandler.php @@ -0,0 +1,94 @@ +store_gmail_response_data($params); + $this->gmail_response_logger($params); + + } + catch(Exception $e) + { + + $error = $e->getMessage(); + echo "An Error Occured" . $error; + log_message('error',$error); + + } + } + + + public function store_gmail_response_data($params){ + // foreach ($params as $key => $value){ + // $prepare_data = [ + // $key => $value + // ]; + // } + // $data = [ + // 'email' => isset($params['params']['mail']) ? $params['params']['mail'] : null, + // 'bcc' => isset($params['params']['bcc']) ? json_encode($params['params']['bcc']) : null, + // 'cc' => isset($params['params']['cc']) ? json_encode($params['params']['cc']) : null, + // 'message' => isset($params['params']['message']) ? $params['params']['message'] : null, + // 'attachments' => isset($params['params']['attachments']) ? json_encode($params['params']['attachments']) : null, + // 'client_id' => isset($params['params']['common']['client_id']) ? $params['params']['common']['client_id'] : null, + // 'client_branch_id' => isset($params['params']['common']['client_branch_id']) ? $params['params']['common']['client_branch_id'] : null, + // 'client_policy_id' => isset($params['params']['common']['client_policy_id']) ? $params['params']['common']['client_policy_id'] : null, + // 'employee_policy_id' => isset($params['params']['common']['employee_policy_id']) ? $params['params']['common']['employee_policy_id'] : null, + // 'employee_id' => isset($params['params']['common']['employee_id']) ? $params['params']['common']['employee_id'] : null, + // 'mail_type' => isset($params['params']['common']['mail_type']) ? $params['params']['common']['mail_type'] : null, + // 'gmail_api_status' => isset($params['gmail_api']['status']) ? $params['gmail_api']['status'] : null, + // 'gmail_api_id' => isset($params['gmail_api']['data']['id']) ? $params['gmail_api']['data']['id'] : null + // ]; + // Initialize data array + $data = []; + // print_r($params['params']['common']);die(); + if (isset($params['params']['common'])) { + foreach ($params['params']['common'] as $key => $value) { + $data[$key] = isset($value) ? $value : null; + } + unset($params['params']['common']); + } + if (isset($params['params'])){ + foreach($params['params'] as $key => $value){ + $arr = ['bcc','cc','attachments']; + if(in_array($key,$arr)){ + $data[$key] = isset($value)?json_encode($value):null; + }else{ + $data[$key] = isset($value)?$value:null; + } + } + } + if(isset($params['gmail_api']['status']) && $params['gmail_api']['status'] == 1 ){ + $data['gmail_api_status'] = isset($params['gmail_api']['status']) ? $params['gmail_api']['status'] : null; + $data['gmail_api_id'] = isset($params['gmail_api']['data']['id']) ? $params['gmail_api']['data']['id'] : null; + $data['received_message'] = isset($params['gmail_api']['data']['labelIds']) ? json_encode($params['gmail_api']['data']['labelIds']) : null; + + } + elseif(isset($params['gmail_api']['status']) && $params['gmail_api']['status'] == false ){ + $data['gmail_api_status'] = $params['gmail_api']['status']; + if(isset($params['gmail_api']['data'])){ + $data['received_message'] = json_encode($params['gmail_api']['data']); + } + } + + $gmailModel = new GmailSentHistoryModel(); + $save_status = $gmailModel->insert($data); + + + } + + public function gmail_response_logger($params){ + + } + + +} \ No newline at end of file diff --git a/app/Helpers/JWTToken.php b/app/Helpers/JWTToken.php index 196bdaca..41d76b5c 100755 --- a/app/Helpers/JWTToken.php +++ b/app/Helpers/JWTToken.php @@ -11,7 +11,7 @@ use Firebase\JWT\SignatureInvalidException; // use Psr\Http\Message\RequestInterface; use CodeIgniter\HTTP\RequestInterface; use ReflectionClass; - +use Exception; use App\Models\EmployeeModel; use App\Models\LevelContactModel; diff --git a/app/Helpers/MailHelper.php b/app/Helpers/MailHelper.php index 0bc21c46..78f5983b 100755 --- a/app/Helpers/MailHelper.php +++ b/app/Helpers/MailHelper.php @@ -11,7 +11,6 @@ use PHPMailer\PHPMailer\Exception; use App\Models\JobModel; - class MailHelper { public static function send_email_smtp($params) @@ -94,7 +93,7 @@ class MailHelper $subject = $params['subject']; $message = $params['message']; $attachments = isset($params['attachments']) && count($params['attachments']) ? $params['attachments'] : []; - + $common = isset($params['common'])?$params['common']:''; //check BCC mail if (isset($params['bcc'])) { $bcc = $params['bcc']; @@ -118,26 +117,36 @@ class MailHelper if(isset($params['reply_to']) && !empty($params['reply_to'])){ $reply_to = $params['reply_to']; } + $MailDataHelper = new GmailResponseHandler(); + $res['params'] = $params; try { - $res = $gmailapi->sendMessage($emaill, $subject, $message, 'no-reply@nhanceindia.in', $reply_to, $cc, $bcc,$attachments); - - if($res['status']) + $res['gmail_api'] = $gmailapi->sendMessage($emaill, $subject, $message, 'no-reply@nhanceindia.in', $reply_to, $cc, $bcc,$attachments); + if($res['gmail_api']['status']) { + $response = $res; + // $MailDataHelper->receive_and_distribute_param_to_functions($response); return json_encode(['status' => 'success','code' => 200, 'message'=>('Email Sent Successfully...'.$emaill),'data' => $res,],200); } else { $myLogger->logme('error', "mail sent failed - $emaill"); + $response = $res; + // $MailDataHelper->receive_and_distribute_param_to_functions($response); return json_encode(['status' => 'failed','code' => 404,'message'=>( 'Email Sent Failed...' . $emaill ),'data' => $res ],404); } } catch (Exception $e) { - $msg = $e->getMessage(); + $msg['error'] = $e->getMessage(); + $msg['params'] = $params; $myLogger->logme('error', "mail sent failed - $msg"); + $response = $msg; + // $MailDataHelper->receive_and_distribute_param_to_functions($response); return json_encode(['status' => 'failed','code' => 500,'data' => $msg],500); - } + } + + } diff --git a/app/Models/GmailSentHistoryModel.php b/app/Models/GmailSentHistoryModel.php new file mode 100644 index 00000000..9a0ae8ea --- /dev/null +++ b/app/Models/GmailSentHistoryModel.php @@ -0,0 +1,11 @@ +nul + if not errorlevel 1 ( + echo phpqueue.bat is already running. Exiting. + exit /b + ) +) + +REM Write the current process ID (PID) to the PID file +echo %PROCESS_ID% > "%PID_FILE%" + +REM Main loop +:loop +REM Check if the Job_Queue file exists +if not exist "%JOB_QUEUE_FILE%" ( + echo Job Queue file not found. + exit /b +) + +REM Read the value of Job_Queue from the file +set /p Job_Queue=<%JOB_QUEUE_FILE% + +if "%Job_Queue%"=="1" ( + REM Run PHP job processing + php "%ROOT_DIR%htdocs/nhance/public/index.php" cli/processjob +) else ( + echo No job to process. +) + +REM Sleep for 1 second before the next iteration +timeout /T 1 >nul +goto loop diff --git a/phpqueue.pid b/phpqueue.pid new file mode 100644 index 00000000..db08d2ac --- /dev/null +++ b/phpqueue.pid @@ -0,0 +1 @@ +575 diff --git a/phpqueue.sh b/phpqueue.sh new file mode 100755 index 00000000..f1e7176b --- /dev/null +++ b/phpqueue.sh @@ -0,0 +1,56 @@ +!/bin/bash +while true; do + echo "Running job at $(date)" + /usr/bin/php /opt/lampp/htdocs/nhance/public/index.php cli/processjob + !/bin/bash +!/bin/bash + +# Read the Job_Queue variable from the temporary file +#!/bin/bash + +#!/bin/bash + +#!/bin/bash + +# Define the root directory for the project and PID file path +#!/bin/bash + +# Define paths +# ROOT_DIR=$(dirname "$(realpath "$0")") +# JOB_QUEUE_FILE="$ROOT_DIR/job_queue" +# PID_FILE="$ROOT_DIR/phpqueue.pid" + + +# # Check if the script is already running +# if [[ -f "$PID_FILE" ]] && kill -0 "$(cat $PID_FILE)" 2>/dev/null; then +# echo "phpqueue.sh is already running. Exiting." +# exit 1 +# fi + +# # Write the current process ID (PID) to the PID file +# echo $$ > "$PID_FILE" + +# # Ensure the PID file is removed on script exit +# trap "rm -f $PID_FILE" EXIT + +# # Main loop +# while true; do +# # Check if the Job_Queue file exists +# if [[ ! -f "$JOB_QUEUE_FILE" ]]; then +# echo "Job Queue file not found." +# exit 1 +# fi + +# # Read the value of Job_Queue from the file +# Job_Queue=$(cat "$JOB_QUEUE_FILE") + +# if [[ "$Job_Queue" -eq 1 ]]; then +# /usr/bin/php /opt/lampp/htdocs/nhance/public/index.php cli/processjob +# else +# echo "No job to process." +# fi + +# # Sleep for 1 second before the next iteration +# sleep 1 +# done + diff --git a/phpsetvariable.sh b/phpsetvariable.sh new file mode 100755 index 00000000..69c1b0ac --- /dev/null +++ b/phpsetvariable.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +# Validate input argument +# if [[ "$1" == "1" ]]; then +# echo "1" > /opt/lampp/htdocs/nhance/Job_Queue +# echo "Job Queue variable has been set to: 1" +# elif [[ "$1" == "0" ]]; then +# echo "0" > /opt/lampp/htdocs/nhance/Job_Queue +# echo "Job Queue variable has been set to: 0" +# else +# echo "Invalid argument. Use 1 to enable the queue or 0 to disable it." +# exit 1 +# fi