diff --git a/app/Config/Filters.php b/app/Config/Filters.php index 1fba3353..7add9fb4 100644 --- a/app/Config/Filters.php +++ b/app/Config/Filters.php @@ -41,8 +41,8 @@ class Filters extends BaseConfig */ public array $globals = [ 'before' => [ - 'HttpRequestLog', - 'csrf', + 'HttpRequestLog' => ['except' => 'processjob'], + // 'csrf', // 'invalidchars', ], 'after' => [ diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 6686ef12..e99166a6 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -59,3 +59,16 @@ $routes->group("/client", ["filter" => "authMVC"], function($routes){ }); }); +$routes->group("/employee", ["filter" => "authMVC"], function($routes){ + $routes->get("list", "EmployeeController::list"); + $routes->get("search", "EmployeeController::search"); + $routes->get("bulk-event-uplod", "EmployeeController::employeesUplodWithEvents"); + $routes->post("bulk-event-uplod", "EmployeeController::employeesUplodWithEvents"); +}); + + +$routes->group("/util", ["filter" => "authMVC"], function($routes){ + $routes->get("clients-with-policies", "EmployeeController::getClientWithPolicies"); +}); + +$routes->cli('processjob', 'JobWorker::processJob'); \ No newline at end of file diff --git a/app/Controllers/BaseController.php b/app/Controllers/BaseController.php index f8e87392..4f0ae0e1 100644 --- a/app/Controllers/BaseController.php +++ b/app/Controllers/BaseController.php @@ -59,7 +59,7 @@ abstract class BaseController extends Controller } - public function loadLayout($view_name,$data) + public function loadLayout($view_name,$data = []) { echo view('layout/header',$data); echo view($view_name, $data); diff --git a/app/Controllers/EmployeeController.php b/app/Controllers/EmployeeController.php new file mode 100644 index 00000000..c2b0d73e --- /dev/null +++ b/app/Controllers/EmployeeController.php @@ -0,0 +1,140 @@ +myLogger = \Config\Services::mylogger(); + $this->employeeModel = new EmployeeModel(); + $this->employeePolicyModel = new EmployeePolicyModel(); + $this->clientModel = new ClientModel(); + $this->fileModel = new FileModel(); + } + + public function list() + { + // $model = new UserModel(); + $data = []; + if(count($this->request->getGet())) + { + $filterData = $this->request->getGet(); + $data['employees'] = $this->employeePolicyModel->getEmployeePolicy(client_id: $filterData['client_id'],policy_id: $filterData['policy_id']); + } + + $this->myLogger->logme('error','list called'); + $this->loadLayout('employee_list',$data); + } + + public function getClientWithPolicies() + { + //echo $this->request->isAJAX();die(); + $result = $this->clientModel->clientsWithPolicies(); + // print_r($result);die(); + if(!count($result)) + { + return $this->respond(['dataStatus' => false,'code' => 404,'message' => 'no data found'], 200); + } + else + { + + return $this->respond(['dataStatus' => true,'code' => 200,'data' => $result], 200); + } + } + + //handles employee & dependent bulk upload with events like inception,addition,deletion, correction and SI enhancements + public function employeesUplodWithEvents() + { + + // $job_details = new Jobs(); + // $r = Jobs::addJob(['job_name' => 'add','payload' => ['a' => 10, 'b' => 35]]); + // print_r($r);//die(); + // // $jobWorker = new JobWorker(); + // JobWorker::processJob($r); + // die(); + // echo $this->request->getMethod();die(); + // print_r($this->request->getFiles('emplist'));print_r($this->request->getPost('emplist')); + // print_r($this->request->getPost('clients')); + // print_r($this->request->getPost('policies')); + // print_r($this->request->getPost('upload-action-type')); + // die(); + if($this->request->getMethod() == 'post') + { + $filename = ''; + $validated = $this->validate([ + 'emplist' => [ + 'uploaded[emplist]', + 'mime_in[emplist,application/vnd.ms-excel,application/vnd,application/pdf]', + 'max_size[emplist,8192]', + ], + ]); + + if ($validated) { + $avatar = $this->request->getFile('emplist'); + $is_moved = $avatar->move(WRITEPATH . 'uploads/excel/'); + $filename = $avatar->getName(); + + } + else + { + return $this->respond(['dataStatus' => false,'code' => 404,'message' => 'invalid file'], 200); + } + + //process post variable entry in file table + $loggedInUserID = get_session_userid(); + + $client_id = $this->request->getPost('client_id'); + $policy_id = $this->request->getPost('policy_id'); + $action = $this->request->getPost('upload-action-type'); + $status = 'inprogress'; + + $file_id = $this->fileModel->insert(['file_name' => $filename,'client_id' => $client_id,'policy_id' => $policy_id,'created_by' => $loggedInUserID,'status' => $status,'action' => $action]); + //die(); + $this->myLogger->logme("error",'{file_id} uploaded success',['file_id' => $file_id]); + return $this->respond(['dataStatus' => true,'code' => 200,'data' => 'file upload success'], 200); + + } + + $data['actions'] = ['inception' => 'Inception','addition' => 'Addition','deletion' => 'Deletion','si_enhancement' =>'SI Enhancement']; + $data['fileList'] = $this->fileModel + ->select(['files.*','up.emp_code','up.first_name']) + ->join('user_profiles up','files.created_by = up.id') + ->where('files.created_by',8)->orderBy('files.created_at','desc')->findAll(); + // print_r($data['fileList']);die(); + if($this->request->getMethod() == "get") + { + $this->loadLayout('employee_upload',$data); + } + + } + + + +} diff --git a/app/Controllers/Home.php b/app/Controllers/Home.php index 056d4170..9280fa95 100644 --- a/app/Controllers/Home.php +++ b/app/Controllers/Home.php @@ -22,4 +22,10 @@ class Home extends PublicController return 'from test service'; } + public function addTwoNumbers($payload) + { + //echo $payload['a'] + $payload['b']; + return $payload['a'] + $payload['b']; + } + } diff --git a/app/Controllers/JobWorker.php b/app/Controllers/JobWorker.php new file mode 100644 index 00000000..98e851dc --- /dev/null +++ b/app/Controllers/JobWorker.php @@ -0,0 +1,154 @@ + ['type' => 'HC','handler' => 'App\\Helpers\\HttpRequestHelper'], 'sub' => ['type' => 'CC','handler' => 'App\\Controllers\\Jobs\SubJob'],'fancy_date_time_format' => [ 'type' => 'HF','handler' => 'fancy_date_time_format'],'addNumber' => ['type' => 'HC','handler' => 'App\\Model\\HttpRequestHelper']]; + public function __construct() + { + // echo 'HiC';//die(); + // parent::__construct(); + // $this->load->model('JobModel'); + // $this->dbm = $this->users_model; + // if (php_sapi_name() !== 'cli') + // { + // die('Invalid context'); + // } + } + + /** + * process jobs + */ + public static function processJob(array $jobdata = []) + { + // echo 'listen';//die(); + $query = " + SELECT id, name, payload, uuid + FROM jobs + WHERE status=? + ORDER BY created_dt ASC + LIMIT 1 FOR UPDATE"; + $where_condition = [self::STATUS_QUEUED]; + + if(isset($jobdata) && count($jobdata)) + { + $query = " + SELECT id, name, payload, uuid + FROM jobs + WHERE status=? AND id=? AND uuid=? + ORDER BY created_dt ASC + LIMIT 1 FOR UPDATE"; + $where_condition = [self::STATUS_QUEUED,$jobdata['id'],$jobdata['uuid']]; + } + + $db = \Config\Database::connect(); + $job = $db->query($query, $where_condition)->getResult(); + // print_r($job);die(); + if ($job !== []) + { + $job = $job[0]; + + echo "\nProcessing job id - " . $job->id . "\n"; + echo "Job name - " . $job->name . "\n"; + + try + { + $start = microtime(true); + $runtime = null; + $job_status = self::STATUS_RUNNING; + $db->query("UPDATE jobs SET status=? WHERE id=? AND uuid=?", [$job_status, $job->id,$job->uuid]); + + if (!array_key_exists($job->name,SeLf::$event_class_mapping)) + { + throw new \RuntimeException('Job ' . $job->name . ' handler not registered'); + } + + $handler = SELF::$event_class_mapping[$job->name]; + $handleInstance = null; + + if($handler['type'] == 'CC' || $handler['type'] == 'HC') + { + // echo 'CLASS - ' . $handler['type'].' - ' . $handler['handler']; + $handlerClass = $handler['handler']; + if(class_exists($handlerClass)) + { + $handleInstance = new $handlerClass(); + } + else + { + throw new \RuntimeException('Job ' . $job->name . ' or handler class not found'); + // echo $e->getMessage(); + } + + if (method_exists($handleInstance, $job->name)) + { + $jobHandler = [$handleInstance, $job->name]; + //throw new \RuntimeException('Job ' . $job->name . ' not found'); + } + else if(method_exists($handleInstance, 'handle')) + { + $jobHandler = [$handleInstance, 'handle']; + } + else + { + throw new \RuntimeException('Job ' . $job->name . ' or handler not found'); + } + } + else if($handler['type'] == 'HF') + { + // echo 'HF - ' . $handler['type']; + $jobHandler = $handleInstance = $handler['handler']; + // echo $jobHandler; + + } + else{ + throw new \RuntimeException('Job ' . $job->name . ' Invalid job type'); + } + + $payload = json_decode($job->payload, true); + if (!is_array($payload)) + { + throw new \InvalidArgumentException('Invalid payload format here'); + } + + $payload = is_array($payload) ? $payload : []; + $response = $jobHandler($payload,$job->id); + + $runtime = microtime(true) - $start; + $job_status = self::STATUS_DONE; + } + catch (\Exception $e) + { + //die(); + $job_status = self::STATUS_FAILED; + $runtime = $runtime === null ? microtime(true) - $start : $runtime; + $response = $e->getMessage(); + } + + $db->query("UPDATE jobs SET status=?, run_time=?, response=? WHERE id=? AND uuid=?", [ + $job_status, + $runtime, + json_encode($response), + $job->id,$job->uuid + ]); + + echo "Job $job->id $job_status. Response - $response \n"; + } + else + { + echo 'no job found in queue'; + } + } + +} diff --git a/app/Controllers/Jobs.php b/app/Controllers/Jobs.php new file mode 100644 index 00000000..33eee1fb --- /dev/null +++ b/app/Controllers/Jobs.php @@ -0,0 +1,64 @@ + 'check','payload' => ['a' => 10]]) + public function __construct() + { + // $this->job_payload = $payload; + // $this->myLogger = \Config\Services::mylogger(); + } + + /** + * add jobs to queue + */ + public static function addJob(array $payload) + { + $jobModel = new JobModel(); + $myLogger = \Config\Services::mylogger(); + + try + { + if(!is_array($payload)) + { + throw new \InvalidArgumentException('Invalid payload format while add job'); + } + + if(!isset($payload['job_name'])) + { + throw new \InvalidArgumentException('Job name not found while add job'); + } + + if(!isset($payload['payload'])) + { + throw new \InvalidArgumentException('Job payload not found while add job'); + } + + $uuid = generate_uuid(); + $jobid = $jobModel->insert(['name' => $payload['job_name'],'uuid' => $uuid,'payload' => json_encode($payload['payload'])]); + // echo $jobid; + $myLogger->logme('error','new job {jobid} added to queue',['jobid' => $jobid]); + return ['id' => $jobid,'uuid' => $uuid,'job_name' => $payload['job_name']]; + } + catch(\Exception $e) + { + $message = $e->getMessage(); + $myLogger->logme('error','{messgae}',['messgae' => $message]); + } + } + +} diff --git a/app/Controllers/Jobs/AddJob.php b/app/Controllers/Jobs/AddJob.php new file mode 100644 index 00000000..6b60cca7 --- /dev/null +++ b/app/Controllers/Jobs/AddJob.php @@ -0,0 +1,15 @@ +diff($passedDateTime); + // return change_date_format($datetime,'Y-m-d H:i:s','d M Y h:i a'); + // If the interval is more than 1 month, return the original datetime + if ($interval->m > 1 || $interval->y > 0) { + if($return_type = 'fancy'){ return change_date_format($datetime,'Y-m-d H:i:s','d M Y h:i a'); } + return $datetime; + } elseif ($interval->m == 1 && $interval->y == 0) { + return "1 month ago"; + } elseif ($interval->d >= 7) { + $weeks = floor($interval->d / 7); + return $weeks == 1 ? "1 week ago" : "$weeks weeks ago"; + } elseif ($interval->d >= 1) { + return $interval->d == 1 ? "1 day ago" : $interval->d . " days ago"; + } elseif ($interval->h >= 1) { + return $interval->h == 1 ? "1 hour ago" : $interval->h . " hours ago"; + } elseif ($interval->i >= 1) { + return $interval->i == 1 ? "1 minute ago" : $interval->i . " minutes ago"; + } else { + return "Just now"; + } + } + +} + + +if(!function_exists('check_string_date')) +{ + function check_string_date($str) + { + if (DateTime::createFromFormat('Y-m-d H:i:s', $str) !== false) { + return true; + } + return false; + } +} + + + diff --git a/app/Models/ClientModel.php b/app/Models/ClientModel.php index 639a4b42..a19930ff 100644 --- a/app/Models/ClientModel.php +++ b/app/Models/ClientModel.php @@ -3,6 +3,7 @@ namespace App\Models; use CodeIgniter\Model; +use App\Models\ClientPolicyModel; class ClientModel extends Model { @@ -25,4 +26,28 @@ class ClientModel extends Model "is_active", ]; + + //get client and its associated policies in same array + public function clientsWithPolicies() + { + + $columns = ['id', 'client_name','short_name']; + $clients = $this->select($columns)->where('is_active',1)->findAll(); + + foreach ($clients as &$client) { + $clientPolicyModel = new ClientPolicyModel(); + $clientPolicies = $clientPolicyModel->select(['client_policy.id','p.name']) + ->join('policies p', 'p.id = client_policy.policy_id') + ->where('client_policy.client_id',$client['id']) + ->where('client_policy.is_active', 1) + ->findAll(); + + $client['policies'] = $clientPolicies; + } + //print_r(($clients));die(); + return $clients; + + } + } + diff --git a/app/Models/EmployeeModel.php b/app/Models/EmployeeModel.php new file mode 100644 index 00000000..78cd79f4 --- /dev/null +++ b/app/Models/EmployeeModel.php @@ -0,0 +1,29 @@ +select(['employee_polices.*','pm.name as policy_name','im.short_name as insurer_short_name','ib.branch_name as insurer_branch_name','ib.branch_code as insurer_branch_code','tpam.name as tpa_name','tpam.short_name as tpa_short_name','tpab.branch_code as tpa_branch_code','cm.client_name','cm.short_name as client_short_name','emp.relationship','emp.relationship_code','emp.change_event','emp.emp_code','emp.name','emp.email_corporate','emp.dob','emp.gender','emp.emp_status','emp.is_active as emp_is_active']) + ->join('employees emp', 'employee_polices.employee_id = emp.id') + ->join('client_policy cp', 'employee_polices.client_policy_id = cp.id') //cp - client policy + ->join('policies pm', 'cp.policy_id = pm.id') //pm - policy master + ->join('insurers im', 'cp.insurer_id = im.id') //im - insurar master + ->join('insurer_branch ib', 'cp.insurer_branch_id = ib.id') //ib - insurar branch + ->join('tpa tpam', 'cp.tpa_id = tpam.id') //tpam - tpa master + ->join('tpa_branch tpab', 'cp.tpa_branch_id = tpab.id') //tpab - tpa brach + ->join('clients cm', 'cp.client_id = cm.id') //cm - client master + ->where('emp.client_id',$client_id) + ->where('employee_polices.client_policy_id',$policy_id) + ->findAll(); + return ($result); + } +} diff --git a/app/Models/FileModel.php b/app/Models/FileModel.php new file mode 100644 index 00000000..3c25e1ef --- /dev/null +++ b/app/Models/FileModel.php @@ -0,0 +1,25 @@ + +
| SNO | +File name | +User/Time | +Status | +
|---|---|---|---|
| + | + | '.$file['first_name'].''?> | +'; + }?> | +