From 0dc972b9064b641c97952108d7d474f167d75c94 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 28 Jun 2022 11:12:10 +0000 Subject: [PATCH 1/5] Optimize sql call to get job order information. Fix creation issue in warranty handler. #689 --- src/Controller/APIController.php | 291 ++++++++++++++++++++++++++++++- src/Service/WarrantyHandler.php | 47 ++--- 2 files changed, 301 insertions(+), 37 deletions(-) diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 4fd4e30e..37e6e4c4 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -1884,8 +1884,10 @@ class APIController extends Controller implements LoggedController } // get job order data - $jo = $em->getRepository(JobOrder::class)->find($id); - if ($jo == null) + // $jo = $em->getRepository(JobOrder::class)->find($id); + // find just the job order and selected fields + $jo_data = $this->findJobOrder($id, $em); + if (empty($jo_data)) { $res->setError(true) ->setErrorMessage('No job order information found'); @@ -1893,16 +1895,21 @@ class APIController extends Controller implements LoggedController } // check if job order belongs to customer / user - if ($cust->getID() != $jo->getCustomer()->getID()) + if ($cust->getID() != $jo_data['customer_id']) { $res->setError(true) ->setErrorMessage('No job order information found'); return $res->getReturnResponse(); } - // put into job order data array - $jo_data = $this->generateJobOrderData($req, $jo, $rt); + // we already have customer from the mobile session + // get the rest of the needed information from invoice and rider rating and customer vehicle + $other_data = $this->findOtherData($jo_data, $id, $em); + // put into job order data array + // TODO: might need a new function to generate the JO data + // $jo_data = $this->generateJobOrderData($req, $jo, $rt); + $jo_data = $this->createJobOrderData($req, $jo_data, $other_data, $cust, $rt); $data = [ 'job_order' => $jo_data @@ -4696,6 +4703,280 @@ class APIController extends Controller implements LoggedController return $jo_data; } + protected function createJobOrderData($req, $jo_data, $other_data, $cust, $rt) + { + $status = $jo_data['status']; + + // need to convert the date_create to DateTime then string with the correct format + $date_create = DateTime::createFromFormat('Y-m-d H:i:s', $jo_data['date_create']); + $str_date_create = $date_create->format('M d, Y'); + + $data = [ + 'id' => $jo_data['id'], + 'date_create' => $str_date_create, + 'service_type' => $jo_data['service_type'], + 'destination' => [ + 'long' => $jo_data['longitude'], + 'lat' => $jo_data['latitude'], + ], + 'delivery_address' => $jo_data['delivery_address'], + 'delivery_instructions' => $jo_data['delivery_instructions'], + 'jo_status' => $status, + 'status' => $this->generateAPIRiderStatus($status), + ]; + + // get customer vehicle and warranty + $cv_data = $other_data['cv_data']; + $cv_id = $cv_data['id']; + $plate_number = $cv_data['plate_number']; + + $warranty = $this->findWarranty($plate_number); + + $data['customer_vehicle'] = [ + 'id' => $cv_id, + 'plate_number' => $plate_number, + 'warranty' => $warranty, + ]; + + // customer information + $data['customer'] = [ + 'first_name' => $cust->getFirstName(), + 'last_name' => $cust->getLastName(), + 'mobile_number' => $cust->getPhoneMobile(), + ]; + + // rider + $rider_data = $other_data['rider_data']; + if (empty($rider_data)) + $data['rider'] = null; + else + { + // default image url + $url_prefix = $req->getSchemeAndHttpHost(); + $image_url = $url_prefix . '/assets/images/user.gif'; + if ($rider_data['image_file'] != null) + $image_url = $url_prefix . '/uploads/' . $rider_data['image_file']; + + $coord = $rt->getRiderLocation($rider_data['id']); + + $data['rider'] = [ + 'id' => $rider_data['id'], + 'name' => $rider_data['name'], + 'plate_num' => $rider_data['plate_num'], + 'contact_num' => $rider_data['contact_num'], + 'image_url' => $image_url, + 'location' => [ + 'long' => $coord->getLongitude(), + 'lat' => $coord->getLatitude() + ] + ]; + + } + + // invoice items + $items = []; + $invoice_items = $other_data['invoice_items']; + foreach ($invoice_items as $item) + { + $items[] = [ + 'id' => $item['id'], + 'title' => $item['title'], + 'qty' => $item['qty'], + 'price' => $item['price'], + ]; + } + $data['items'] = $items; + + // dates depending on status + // need to convert the date_fulfill and date_cancel to DateTime then string with the correct format + $str_date_fulfill = ''; + $str_date_cancel = ''; + if ($jo_data['date_fulfill'] != null) + { + $date_fulfill = DateTime::createFromFormat('Y-m-d H:i:s', $jo_data['date_fulfill']); + $str_date_fulfill = $date_fulfill->format('M d, Y'); + } + if ($jo_data['date_cancel'] != null) + { + $date_cancel = DateTime::createFromFormat('Y-m-d H:i:s', $jo_data['date_cancel']); + $str_date_cancel = $date_cancel->format('M d, Y'); + } + + switch ($status) + { + case JOStatus::FULFILLED: + $data['date_fulfilled'] = $str_date_fulfill; + break; + case JOStatus::CANCELLED: + if (empty($str_date_cancel)) + { + $date_cancel = new DateTime(); + $data['date_cancelled'] = $date_cancel->format('M d, Y'); + } + else + $data['date_cancelled'] = $str_date_cancel; + + break; + } + + + return $data; + } + + protected function findJobOrder($id, EntityManagerInterface $em) + { + $found_jo = []; + + $conn = $em->getConnection(); + + $jo_sql = 'SELECT jo.date_create, jo.service_type, ST_x(jo.coordinates), ST_y(jo.coordinates), + jo.delivery_address, jo.delivery_instructions, jo.status, jo.rider_id, jo.customer_id, jo.cvehicle_id, + jo.date_fulfill, jo.date_cancel + FROM job_order jo WHERE jo.id = :id'; + $stmt = $conn->prepare($jo_sql); + $stmt->execute(['id' => $id]); + + $jo_results = $stmt->fetch(); + + if (!empty($jo_results)) + { + $found_jo = [ + 'id' => $id, + 'date_create' => $jo_results['date_create'], + 'date_fulfill' => $jo_results['date_fulfill'], + 'date_cancel' => $jo_results['date_cancel'], + 'service_type' => $jo_results['service_type'], + 'longitude' => $jo_results['ST_x(jo.coordinates)'], + 'latitude' => $jo_results['ST_y(jo.coordinates)'], + 'delivery_address' => $jo_results['delivery_address'], + 'delivery_instructions' => $jo_results['delivery_instructions'], + 'status' => $jo_results['status'], + 'rider_id' => $jo_results['rider_id'], + 'customer_id' => $jo_results['customer_id'], + 'cv_id' => $jo_results['cvehicle_id'], + ]; + } + + return $found_jo; + } + + protected function findOtherData($jo_info, $id, EntityManagerInterface $em) + { + $other_data = []; + + // need to find invoice items via invoice. so find invoice using id aka jo_id + $ii_data = $this->findInvoiceItems($id, $em); + + // need to find rider if any + $rider_id = $jo_info['rider_id']; + $r_data = []; + if ($rider_id != null) + { + $r_data = $this->findRider($rider_id, $em); + } + + // need to find customer vehicle using cvehicle_id from jo_info + $cv_id = $jo_info['cv_id']; + $cv_data = $this->findCustomerVehicleById($cv_id, $em); + + $other_data = [ + 'invoice_items' => $ii_data, + 'rider_data' => $r_data, + 'cv_data' => $cv_data, + ]; + + return $other_data; + } + + protected function findInvoiceItems($id, EntityManagerInterface $em) + { + $invoice_items = []; + + $conn = $em->getConnection(); + + // need to find invoice items via invoice. so find invoice using id aka jo_id + $inv_sql = 'SELECT i.id FROM invoice i WHERE i.job_order_id = :jo_id'; + $stmt = $conn->prepare($inv_sql); + $stmt->execute(array('jo_id' => $id)); + + $inv_results = $stmt->fetch(); + + if (empty($inv_results)) + return $invoice_items; + + $invoice_id = $inv_results['id']; + + // find invoice items using invoice id + $ii_sql = 'SELECT ii.id, ii.qty, ii.title, ii.price from invoice_item ii + WHERE ii.invoice_id = :invoice_id'; + $stmt = $conn->prepare($ii_sql); + $stmt->execute(array('invoice_id' => $invoice_id)); + + $ii_results = $stmt->fetchAll(); + + foreach ($ii_results as $ii_result) + { + $invoice_items[] = [ + 'id' => $ii_result['id'], + 'qty' => $ii_result['qty'], + 'title' => $ii_result['title'], + 'price' => $ii_result['price'], + ]; + } + + return $invoice_items; + } + + protected function findRider($id, EntityManagerInterface $em) + { + $rider_data = []; + + $conn = $em->getConnection(); + + $rider_sql = 'SELECT r.first_name, r.last_name, r.contact_num, r.image_file, r.plate_number + FROM rider r WHERE r.id = :id'; + $stmt = $conn->prepare($rider_sql); + $stmt->execute(['id' => $id]); + + $rider_result = $stmt->fetch(); + + if (!empty($rider_result)) + { + $rider_data = [ + 'id' => $id, + 'name' => $rider_result['first_name'] . ' ' . $rider_result['last_name'], + 'plate_num' => $rider_result['plate_number'], + 'contact_num' => $rider_result['contact_num'], + 'image_file' => $rider_result['image_file'], + ]; + } + + return $rider_data; + } + + protected function findCustomerVehicleById($id, EntityManagerInterface $em) + { + $cv_data = []; + + $conn = $em->getConnection(); + + $cv_sql = 'SELECT cv.plate_number FROM customer_vehicle cv WHERE cv.id = :id'; + $stmt = $conn->prepare($cv_sql); + $stmt->execute(['id' => $id]); + + $cv_result = $stmt->fetch(); + + if (!empty($cv_result)) + { + $cv_data = [ + 'id' => $id, + 'plate_number' => $cv_result['plate_number'], + ]; + } + + return $cv_data; + } + protected function normalizeString($string) { return trim(strtolower($string)); diff --git a/src/Service/WarrantyHandler.php b/src/Service/WarrantyHandler.php index b59083a2..e22b1c1e 100644 --- a/src/Service/WarrantyHandler.php +++ b/src/Service/WarrantyHandler.php @@ -14,6 +14,7 @@ use App\Entity\CustomerVehicle; use App\Service\WarrantyAPILogger; use App\Ramcar\WarrantyClass; +use App\Ramcar\WarrantyStatus; use DateTime; use DateInterval; @@ -38,7 +39,7 @@ class WarrantyHandler $bmodel_name = ''; $bsize_name =''; $sap_batt_id = ''; - $w_serial = null; + $w_serial = 'NULL'; foreach ($batt_list as $battery) { @@ -80,49 +81,31 @@ class WarrantyHandler // compute expiry date $date_expire = null; + $str_date_expire = 'NULL'; if ((!empty($warranty_class)) && (count($batt_list) != 0)) { $period = $this->getWarrantyPeriod($batt_list, $warranty_class); $date_expire = $this->computeDateExpire($date_purchase, $period); + $str_date_expire = $date_expire->format('Y-m-d H:i:s'); } // set and save values if (trim($serial) != '') $w_serial = $serial; + $str_date_purchase = $date_purchase->format('Y-m-d H:i:s'); + $date_create = new DateTime(); + $str_date_create = $date_create->format('Y-m-d H:i:s'); + // insert warranty - $q = $this->em->createQuery('INSERT App\Entity\Warranty w - SET w.serial = :serial, - w.plate_number = :plate_number, - w.first_name = :first_name, - w.last_name = :last_name, - w.mobile_number = :mobile_number, - w.date_purchase = :date_purchase, - w.warranty_class = :warranty_class, - w.create_source = :create_source, - w.customer_id = :customer_id, - w.vehicle_id = :vehicle_id, - w.bty_model_id = :bmodel_id, - w.bty_size_id = :bsize_id, - w.sap_bty_id = :sap_batt_id, - w.date_expire = :date_expire') - ->setParameters([ - 'serial' => $serial, - 'plate_number' => $plate_number, - 'first_name' => $first_name, - 'last_name' => $last_name, - 'mobile_number' => $mobile_number, - 'date_purchase' => $date_purchase, - 'warranty_class' => $warranty_class, - 'create_source' => $source, - 'customer_id' => $customer->getID(), - 'vehicle_id' => $cust_vehicle->getID(), - 'bmodel_id' => $bmodel_id, - 'bsize_id' => $bsize_id, - 'sap_batt_id' => $sap_batt_id, - 'date_expire' => $date_expire]); - $q->execute(); + $sql_values = '(' . $bmodel_id . ',' . $bsize_id . ',\'' . $sap_batt_id . '\',\'' . $w_serial . '\',\'' . $warranty_class . '\',\'' + . $plate_number . '\',\'' . WarrantyStatus::ACTIVE . '\',\'' . $str_date_create . '\',\'' . $str_date_purchase + . '\',\'' . $str_date_expire . '\',\'' . $first_name . '\',\'' . $last_name . '\',\'' . $mobile_number . '\',' . 1 . ',' . $cust_vehicle->getID() . ',' . $customer->getID() . ',\'' . $source . '\')'; + $w_sql = 'INSERT INTO `warranty` (bty_model_id,bty_size_id,sap_bty_id,serial,warranty_class,plate_number,status,date_create,date_purchase,date_expire,first_name,last_name,mobile_number,flag_activated,vehicle_id,customer_id, create_source) VALUES ' . $sql_values . ';' . "\n"; + $conn = $this->em->getConnection(); + $w_stmt = $conn->prepare($w_sql); + $w_stmt->execute(); // log warranty creation $action = 'create'; -- 2.43.5 From ad99565d454ca25a4ed519e9b2a86b86bf4258c9 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 29 Jun 2022 07:55:12 +0000 Subject: [PATCH 2/5] Add caching for getJobOrderInfo. #689 --- config/services.yaml | 5 ++ src/Controller/APIController.php | 82 ++++++++++++++++++++++++++------ src/Service/JsonCache.php | 41 ++++++++++++++++ 3 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 src/Service/JsonCache.php diff --git a/config/services.yaml b/config/services.yaml index 9d1ee14e..87442910 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -233,6 +233,11 @@ services: $loc_key: "%env(LOCATION_RIDER_ACTIVE_KEY)%" $status_key: "%env(STATUS_RIDER_KEY)%" + App\Service\JsonCache: + arguments: + $redis_prov: "@App\\Service\\RedisClientProvider" + $jo_json_info_key: "%env(JO_JSON_INFO_KEY)%" + # inventory manager App\Service\InventoryManager: arguments: diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index 37e6e4c4..e3d81204 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -48,6 +48,7 @@ use App\Service\HubDistributor; use App\Service\HubFilterLogger; use App\Service\HubFilteringGeoChecker; use App\Service\HashGenerator; +use App\Service\JsonCache; use App\Entity\MobileSession; use App\Entity\Customer; @@ -1867,7 +1868,7 @@ class APIController extends Controller implements LoggedController } // we can't use param converter for now because we want to output the proper 404 - public function getJobOrderInfo($id, Request $req, EntityManagerInterface $em, RiderTracker $rt) + public function getJobOrderInfo($id, Request $req, EntityManagerInterface $em, RiderTracker $rt, JsonCache $json_cache) { // check required parameters and api key $res = $this->checkParamsAndKey($req, $em, []); @@ -1884,18 +1885,34 @@ class APIController extends Controller implements LoggedController } // get job order data - // $jo = $em->getRepository(JobOrder::class)->find($id); // find just the job order and selected fields - $jo_data = $this->findJobOrder($id, $em); - if (empty($jo_data)) + // find the JO in cache first + $jo_info = $this->findJobOrderFromCache($id, $json_cache); + + if (!empty($jo_info)) { - $res->setError(true) - ->setErrorMessage('No job order information found'); + // check if job order belongs to customer / user + if ($cust->getID() != $jo_info['customer_id']) + { + $res->setError(true) + ->setErrorMessage('No job order information found'); + return $res->getReturnResponse(); + } + + $jo_data = $jo_info['jo_data']; + $data = [ + 'job_order' => $jo_data, + ]; + + $res->setData($data); return $res->getReturnResponse(); } - // check if job order belongs to customer / user - if ($cust->getID() != $jo_data['customer_id']) + // not in cache, get the JO from the DB + $jo_info = $this->findJobOrderFromDB($id, $em); + + // check if not in db + if (empty($jo_info)) { $res->setError(true) ->setErrorMessage('No job order information found'); @@ -1904,12 +1921,19 @@ class APIController extends Controller implements LoggedController // we already have customer from the mobile session // get the rest of the needed information from invoice and rider rating and customer vehicle - $other_data = $this->findOtherData($jo_data, $id, $em); + $other_data = $this->findOtherData($jo_info, $id, $em); + + // check if job order belongs to customer / user + if ($cust->getID() != $jo_info['customer_id']) + { + $res->setError(true) + ->setErrorMessage('No job order information found'); + return $res->getReturnResponse(); + } // put into job order data array - // TODO: might need a new function to generate the JO data // $jo_data = $this->generateJobOrderData($req, $jo, $rt); - $jo_data = $this->createJobOrderData($req, $jo_data, $other_data, $cust, $rt); + $jo_data = $this->createJobOrderData($req, $jo_info, $other_data, $cust, $rt, $json_cache); $data = [ 'job_order' => $jo_data @@ -4703,16 +4727,17 @@ class APIController extends Controller implements LoggedController return $jo_data; } - protected function createJobOrderData($req, $jo_data, $other_data, $cust, $rt) + protected function createJobOrderData($req, $jo_data, $other_data, $cust, $rt, $json_cache) { $status = $jo_data['status']; + $jo_id = $jo_data['id']; // need to convert the date_create to DateTime then string with the correct format $date_create = DateTime::createFromFormat('Y-m-d H:i:s', $jo_data['date_create']); $str_date_create = $date_create->format('M d, Y'); $data = [ - 'id' => $jo_data['id'], + 'id' => $jo_id, 'date_create' => $str_date_create, 'service_type' => $jo_data['service_type'], 'destination' => [ @@ -4819,14 +4844,41 @@ class APIController extends Controller implements LoggedController break; } + // need to put the jo data in json format to redis + $json_jo_data = json_encode($data); + $value = $cust->getID() . '|' . $json_jo_data; + + $json_cache->addJOJsonInfo($jo_id, $value); return $data; } - protected function findJobOrder($id, EntityManagerInterface $em) + protected function findJobOrderFromCache($id, $json_cache) { - $found_jo = []; + $cache_data = []; + $jo_in_cache = $json_cache->findJOJsonInfo($id); + if (!empty($jo_in_cache)) + { + // need to get customer id from the string + $j_array = explode('|', $jo_in_cache, 2); + + $cust_id = $j_array[0]; + $data = json_decode($j_array[1]); + + $cache_data = [ + 'customer_id' => $cust_id, + 'jo_data' => $data, + ]; + } + + return $cache_data; + } + + protected function findJobOrderFromDB($id, EntityManagerInterface $em) + { + // not in cache, get from database + $found_jo = []; $conn = $em->getConnection(); $jo_sql = 'SELECT jo.date_create, jo.service_type, ST_x(jo.coordinates), ST_y(jo.coordinates), diff --git a/src/Service/JsonCache.php b/src/Service/JsonCache.php new file mode 100644 index 00000000..1d8b3bcf --- /dev/null +++ b/src/Service/JsonCache.php @@ -0,0 +1,41 @@ +redis = $redis_prov->getRedisClient(); + $this->jo_json_info_key = $jo_json_info_key; + } + + public function addJOJsonInfo($jo_id, $jo_data) + { + $key = $jo_id; + + $this->redis->hset($this->jo_json_info_key, $key, $jo_data); + } + + public function findJOJsonInfo($jo_id) + { + $jo_data = ''; + + $key = $jo_id; + + // check if JO id is in redis hash + $is_exist = $this->redis->hexists($this->jo_json_info_key, $key); + if ($is_exist) + { + // get the data + $jo_data = $this->redis->hget($this->jo_json_info_key, $key); + } + + return $jo_data; + } +} -- 2.43.5 From bc0ed9ea307e6f547593ac551a3c124217bf2e9b Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 29 Jun 2022 08:01:57 +0000 Subject: [PATCH 3/5] Rename the json cache service. #689 --- config/services.yaml | 2 +- src/Controller/APIController.php | 4 ++-- src/Service/{JsonCache.php => JobOrderJsonCache.php} | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) rename src/Service/{JsonCache.php => JobOrderJsonCache.php} (97%) diff --git a/config/services.yaml b/config/services.yaml index 87442910..50791ba1 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -233,7 +233,7 @@ services: $loc_key: "%env(LOCATION_RIDER_ACTIVE_KEY)%" $status_key: "%env(STATUS_RIDER_KEY)%" - App\Service\JsonCache: + App\Service\JobOrderJsonCache: arguments: $redis_prov: "@App\\Service\\RedisClientProvider" $jo_json_info_key: "%env(JO_JSON_INFO_KEY)%" diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index e3d81204..7267ccf9 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -48,7 +48,7 @@ use App\Service\HubDistributor; use App\Service\HubFilterLogger; use App\Service\HubFilteringGeoChecker; use App\Service\HashGenerator; -use App\Service\JsonCache; +use App\Service\JobOrderJsonCache; use App\Entity\MobileSession; use App\Entity\Customer; @@ -1868,7 +1868,7 @@ class APIController extends Controller implements LoggedController } // we can't use param converter for now because we want to output the proper 404 - public function getJobOrderInfo($id, Request $req, EntityManagerInterface $em, RiderTracker $rt, JsonCache $json_cache) + public function getJobOrderInfo($id, Request $req, EntityManagerInterface $em, RiderTracker $rt, JobOrderJsonCache $json_cache) { // check required parameters and api key $res = $this->checkParamsAndKey($req, $em, []); diff --git a/src/Service/JsonCache.php b/src/Service/JobOrderJsonCache.php similarity index 97% rename from src/Service/JsonCache.php rename to src/Service/JobOrderJsonCache.php index 1d8b3bcf..414bdb52 100644 --- a/src/Service/JsonCache.php +++ b/src/Service/JobOrderJsonCache.php @@ -4,7 +4,7 @@ namespace App\Service; use App\Service\RedisClientProvider; -class JsonCache +class JobOrderJsonCache { protected $redis; protected $jo_json_info_key; -- 2.43.5 From 7b1b56c8995d752bbb3bb87216bb16bd8bcd3a78 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 29 Jun 2022 08:31:56 +0000 Subject: [PATCH 4/5] Add event listener to job order to clear the jo json cache when updating or deleting a JO. #689 --- config/services.yaml | 11 ++++++ .../JobOrderJsonCacheListener.php | 37 +++++++++++++++++++ src/Service/JobOrderJsonCache.php | 12 ++++++ 3 files changed, 60 insertions(+) create mode 100644 src/EventListener/JobOrderJsonCacheListener.php diff --git a/config/services.yaml b/config/services.yaml index 50791ba1..180c5602 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -221,6 +221,17 @@ services: event: 'postPersist' entity: 'App\Entity\JobOrder' + App\EventListener\JobOrderJsonCacheListener: + arguments: + $json_cache: "@App\\Service\\JobOrderJsonCache" + tags: + - name: 'doctrine.orm.entity_listener' + event: 'postUpdate' + entity: 'App\Entity\JobOrder' + - name: 'doctrine.orm.entity_listener' + event: 'postRemove' + entity: 'App\Entity\JobOrder' + App\Service\JobOrderCache: arguments: $redis_prov: "@App\\Service\\RedisClientProvider" diff --git a/src/EventListener/JobOrderJsonCacheListener.php b/src/EventListener/JobOrderJsonCacheListener.php new file mode 100644 index 00000000..e2410468 --- /dev/null +++ b/src/EventListener/JobOrderJsonCacheListener.php @@ -0,0 +1,37 @@ +json_cache = $json_cache; + } + + // when a job order is updated + public function postUpdate(JobOrder $jo, LifecycleEventArgs $args) + { + // get JO id + $id = $jo->getID(); + + $this->json_cache->removeJOJsonInfo($id); + } + + // when a job order is deleted + public function postRemove(JobOrder $jo, LifecycleEventArgs $args) + { + // get JO id + $id = $jo->getID(); + + $this->json_cache->removeJOJsonInfo($id); + } +} diff --git a/src/Service/JobOrderJsonCache.php b/src/Service/JobOrderJsonCache.php index 414bdb52..31e155d5 100644 --- a/src/Service/JobOrderJsonCache.php +++ b/src/Service/JobOrderJsonCache.php @@ -38,4 +38,16 @@ class JobOrderJsonCache return $jo_data; } + + public function removeJOJsonInfo($jo_id) + { + $key = $jo_id; + + // check if JO id is in redis hash + $is_exist = $this->redis->hexists($this->jo_json_info_key, $key); + if ($is_exist) + { + $this->redis->hdel($this->jo_json_info_key, $key); + } + } } -- 2.43.5 From 33ae3b624d91e17b31c9070a628cb6c34c2f43ee Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 30 Jun 2022 04:18:35 +0000 Subject: [PATCH 5/5] Rename the key for the cache. #689 --- config/services.yaml | 2 +- src/Service/JobOrderJsonCache.php | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/config/services.yaml b/config/services.yaml index 180c5602..f8b30b8a 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -247,7 +247,7 @@ services: App\Service\JobOrderJsonCache: arguments: $redis_prov: "@App\\Service\\RedisClientProvider" - $jo_json_info_key: "%env(JO_JSON_INFO_KEY)%" + $jo_json_cache_key: "%env(JO_JSON_CACHE_KEY)%" # inventory manager App\Service\InventoryManager: diff --git a/src/Service/JobOrderJsonCache.php b/src/Service/JobOrderJsonCache.php index 31e155d5..2df2fa5b 100644 --- a/src/Service/JobOrderJsonCache.php +++ b/src/Service/JobOrderJsonCache.php @@ -7,19 +7,19 @@ use App\Service\RedisClientProvider; class JobOrderJsonCache { protected $redis; - protected $jo_json_info_key; + protected $jo_json_cache_key; - public function __construct(RedisClientProvider $redis_prov, $jo_json_info_key) + public function __construct(RedisClientProvider $redis_prov, $jo_json_cache_key) { $this->redis = $redis_prov->getRedisClient(); - $this->jo_json_info_key = $jo_json_info_key; + $this->jo_json_cache_key = $jo_json_cache_key; } public function addJOJsonInfo($jo_id, $jo_data) { $key = $jo_id; - $this->redis->hset($this->jo_json_info_key, $key, $jo_data); + $this->redis->hset($this->jo_json_cache_key, $key, $jo_data); } public function findJOJsonInfo($jo_id) @@ -29,11 +29,11 @@ class JobOrderJsonCache $key = $jo_id; // check if JO id is in redis hash - $is_exist = $this->redis->hexists($this->jo_json_info_key, $key); + $is_exist = $this->redis->hexists($this->jo_json_cache_key, $key); if ($is_exist) { // get the data - $jo_data = $this->redis->hget($this->jo_json_info_key, $key); + $jo_data = $this->redis->hget($this->jo_json_cache_key, $key); } return $jo_data; @@ -44,10 +44,10 @@ class JobOrderJsonCache $key = $jo_id; // check if JO id is in redis hash - $is_exist = $this->redis->hexists($this->jo_json_info_key, $key); + $is_exist = $this->redis->hexists($this->jo_json_cache_key, $key); if ($is_exist) { - $this->redis->hdel($this->jo_json_info_key, $key); + $this->redis->hdel($this->jo_json_cache_key, $key); } } } -- 2.43.5