From d6cf2de4abd55917e4f5b390e0521e8b201e9b0e Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 9 Nov 2021 08:33:11 +0000 Subject: [PATCH 1/9] Create service to generate a unique id. #638 --- src/Service/UniqueIdGenerator.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/Service/UniqueIdGenerator.php diff --git a/src/Service/UniqueIdGenerator.php b/src/Service/UniqueIdGenerator.php new file mode 100644 index 00000000..c0a91d0c --- /dev/null +++ b/src/Service/UniqueIdGenerator.php @@ -0,0 +1,22 @@ +em = $em; + } + + public function generateUniqueId($str_length) + { + // TODO: retry until we get a unique id + + return substr(md5(time()), 0, $str_length); + } +} -- 2.43.5 From f828db422f92c6ccdd9e089907279dc52584aca4 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Wed, 10 Nov 2021 06:51:27 +0000 Subject: [PATCH 2/9] Add generated id to customer. #638 --- src/Entity/Customer.php | 9 +++++++++ src/Service/UniqueIdGenerator.php | 22 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 4af84d96..23a49082 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -222,6 +222,12 @@ class Customer */ protected $car_club_customer_hub; + // random generated unique id + /** + * @ORM\Column(type="string", length=40, nullable=true) + */ + protected $generated_id; + public function __construct() { $this->numbers = new ArrayCollection(); @@ -259,6 +265,8 @@ class Customer $this->date_create = new DateTime(); $this->create_source = 'unknown'; + + $this->generated_id = ''; } public function getID() @@ -674,4 +682,5 @@ class Customer { return $this->car_club_customer_hub; } + } diff --git a/src/Service/UniqueIdGenerator.php b/src/Service/UniqueIdGenerator.php index c0a91d0c..7822e045 100644 --- a/src/Service/UniqueIdGenerator.php +++ b/src/Service/UniqueIdGenerator.php @@ -13,10 +13,28 @@ class UniqueIdGenerator $this->em = $em; } - public function generateUniqueId($str_length) + public function generateCustomerUniqueId($str_length) { - // TODO: retry until we get a unique id + // retry until we get a unique id + while (true) + { + try + { + } + catch (DBALException $e) + { + error_log($e->getMessage()); + // delay one second and try again + sleep(1); + continue; + } + break; + } + } + + protected function generateUniqueId($str_length) + { return substr(md5(time()), 0, $str_length); } } -- 2.43.5 From f5ce07d78ce71c7ecc2debc75978d8c95f801a0a Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 11 Nov 2021 02:49:16 +0000 Subject: [PATCH 3/9] Add index for generated id. Add checking for duplicate generated ids. #638 --- src/Entity/Customer.php | 14 +++++++++++++- src/Service/UniqueIdGenerator.php | 25 +++++++++++++------------ 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 23a49082..60f9f716 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -15,7 +15,8 @@ use App\Ramcar\CustomerClassification; * @ORM\Table(name="customer", indexes={ * @ORM\Index(name="phone_mobile_idx", columns={"phone_mobile"}), * @ORM\Index(columns={"first_name"}, flags={"fulltext"}), - * @ORM\Index(columns={"last_name"}, flags={"fulltext"}) + * @ORM\Index(columns={"last_name"}, flags={"fulltext"}), + @ORM\Index(name="generated_id_idx", columns={"generated_id"}) * }) */ class Customer @@ -683,4 +684,15 @@ class Customer return $this->car_club_customer_hub; } + public function setGeneratedID($generated_id) + { + $this->generated_id = $generated_id; + return $this; + } + + public function getGeneratedID() + { + return $this->generated_id; + } + } diff --git a/src/Service/UniqueIdGenerator.php b/src/Service/UniqueIdGenerator.php index 7822e045..3cd11f26 100644 --- a/src/Service/UniqueIdGenerator.php +++ b/src/Service/UniqueIdGenerator.php @@ -4,6 +4,8 @@ namespace App\Service; use Doctrine\ORM\EntityManagerInterface; +use App\Entity\Customer; + class UniqueIdGenerator { protected $em; @@ -15,22 +17,21 @@ class UniqueIdGenerator public function generateCustomerUniqueId($str_length) { - // retry until we get a unique id + $em = $this->em; + + // retry until we find no customer with the generated id while (true) { - try - { - } - catch (DBALException $e) - { - error_log($e->getMessage()); - // delay one second and try again - sleep(1); - continue; - } + // generate the id + $generated_id = $this->generateUniqueId($str_length); - break; + // check if generated id already exists + $cust = $em->getRepository(Customer::class)->findOneBy(['generated_id' => $generated_id]); + + if ($cust == null) + return $generated_id; } + } protected function generateUniqueId($str_length) -- 2.43.5 From 67429113ddbd821dbf0374b0c3b2be7080c98672 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 11 Nov 2021 10:35:18 +0000 Subject: [PATCH 4/9] Add command to set generated id for existing customers. #638 --- config/services.yaml | 5 + src/Command/SetCustomerGeneratedIdCommand.php | 110 ++++++++++++++++++ src/Service/UniqueIdGenerator.php | 2 + 3 files changed, 117 insertions(+) create mode 100644 src/Command/SetCustomerGeneratedIdCommand.php diff --git a/config/services.yaml b/config/services.yaml index 0498fcdd..65576192 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -301,3 +301,8 @@ services: App\Service\HubFilteringGeoChecker: arguments: $geofence_flag: "%env(HUB_GEOFENCE_ENABLE)%" + + # customer id generator + App\Service\UniqueIdGenerator: + arguments: + $em: "@doctrine.orm.entity_manager" diff --git a/src/Command/SetCustomerGeneratedIdCommand.php b/src/Command/SetCustomerGeneratedIdCommand.php new file mode 100644 index 00000000..f031ca08 --- /dev/null +++ b/src/Command/SetCustomerGeneratedIdCommand.php @@ -0,0 +1,110 @@ +em = $em; + $this->id_gen = $id_gen; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('customer:setgeneratedid') + ->setDescription('Set customer generated id.') + ->setHelp('Set customer generated id.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $em = $this->em; + + $batch_size = 20; + $i = 1; + + $em->getConnection()->getConfiguration()->setSQLLogger(null); + + // get all customers with no generated ids. First time to run this would mean all customers + $cust_q = $em->createQuery('SELECT c from App\Entity\Customer c where c.generated_id is null'); + + $customers = $cust_q->iterate(); + foreach ($customers as $row) + { + $cust = $row[0]; + $output->writeln('Processing customer ' . $cust->getID()); + + // retry until we get a unique generated id + while (true) + { + try + { + $generated_id = $this->generateUniqueId(self::GENERATED_ID_LENGTH); + + // set generated id + $cust->setGeneratedId($generated_id); + + // reopen in case we get an exception + if (!$em->isOpen()) + { + $em = $em->create( + $em->getConnection(), + $em->getConfiguration() + ); + } + + //++$i; + //if (($i % $batch_size) === 0) + //{ + $em->flush(); + $em->clear(); + //} + } + catch (DBALException $e) + { + error_log($e->getMessage()); + // delay one second and try again + sleep(1); + continue; + } + + break; + } + $em->detach($row[0]); + } + + //$em->flush(); + + return 0; + } + + protected function generateUniqueId($str_length) + { + return substr(md5(time()), 0, $str_length); + } + +} diff --git a/src/Service/UniqueIdGenerator.php b/src/Service/UniqueIdGenerator.php index 3cd11f26..90af3369 100644 --- a/src/Service/UniqueIdGenerator.php +++ b/src/Service/UniqueIdGenerator.php @@ -30,6 +30,8 @@ class UniqueIdGenerator if ($cust == null) return $generated_id; + + sleep(1); } } -- 2.43.5 From cd7b3c9e628e7476e039eb0ad66bce9d8fdd0781 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 12 Nov 2021 04:58:52 +0000 Subject: [PATCH 5/9] Add unique constraint for generated id. Add hash for generated ids for checking. #638 --- src/Command/SetCustomerGeneratedIdCommand.php | 82 ++++++++++--------- src/Entity/Customer.php | 14 ++-- src/Service/UniqueIdGenerator.php | 35 +++++++- 3 files changed, 83 insertions(+), 48 deletions(-) diff --git a/src/Command/SetCustomerGeneratedIdCommand.php b/src/Command/SetCustomerGeneratedIdCommand.php index f031ca08..b2518a14 100644 --- a/src/Command/SetCustomerGeneratedIdCommand.php +++ b/src/Command/SetCustomerGeneratedIdCommand.php @@ -14,8 +14,6 @@ use Doctrine\DBAL\DBALException; use App\Entity\Customer; -use App\Service\UniqueIdGenerator; - use PDO; class SetCustomerGeneratedIdCommand extends Command @@ -23,12 +21,13 @@ class SetCustomerGeneratedIdCommand extends Command const GENERATED_ID_LENGTH = 40; private $em; - private $id_gen; + protected $cust_generated_ids; - public function __construct(EntityManagerInterface $em, UniqueIdGenerator $id_gen) + public function __construct(EntityManagerInterface $em) { $this->em = $em; - $this->id_gen = $id_gen; + + $this->loadGeneratedIds(); parent::__construct(); } @@ -43,9 +42,7 @@ class SetCustomerGeneratedIdCommand extends Command protected function execute(InputInterface $input, OutputInterface $output) { $em = $this->em; - - $batch_size = 20; - $i = 1; + $db = $em->getConnection(); $em->getConnection()->getConfiguration()->setSQLLogger(null); @@ -58,47 +55,29 @@ class SetCustomerGeneratedIdCommand extends Command $cust = $row[0]; $output->writeln('Processing customer ' . $cust->getID()); - // retry until we get a unique generated id while (true) { - try + $generated_id = $this->generateUniqueId(self::GENERATED_ID_LENGTH); + + if (!isset($this->cust_generated_ids[$generated_id])) { - $generated_id = $this->generateUniqueId(self::GENERATED_ID_LENGTH); + $update_sql = 'UPDATE customer SET generated_id = :generated_id WHERE id = :cust_id'; + $update_stmt = $db->prepare($update_sql); + $update_stmt->execute([ + 'generated_id' => $generated_id, + 'cust_id' => $cust->getID(), + ]); - // set generated id - $cust->setGeneratedId($generated_id); + // add generated id to hash + $this->cust_generated_ids[$generated_id] = $cust; - // reopen in case we get an exception - if (!$em->isOpen()) - { - $em = $em->create( - $em->getConnection(), - $em->getConfiguration() - ); - } - - //++$i; - //if (($i % $batch_size) === 0) - //{ - $em->flush(); - $em->clear(); - //} + break; } - catch (DBALException $e) - { - error_log($e->getMessage()); - // delay one second and try again - sleep(1); - continue; - } - - break; } + $em->detach($row[0]); } - //$em->flush(); - return 0; } @@ -107,4 +86,29 @@ class SetCustomerGeneratedIdCommand extends Command return substr(md5(time()), 0, $str_length); } + protected function loadGeneratedIds() + { + error_log('loading generated ids '); + $cust_q = $this->em->createQuery('select c from App\Entity\Customer c where c.generated_id is not null'); + $cust_iter = $cust_q->iterate(); + + $this->cust_generated_ids = []; + foreach ($cust_iter as $row) + { + $customer = $row[0]; + $generated_id = $customer->getGeneratedID(); + error_log('generated id = ' . $generated_id); + if ($generated_id != null) + { + error_log('generated id is not null ' . $generated_id); + if (!isset($this->cust_generated_ids[$generated_id])) + { + error_log('setting to hash ' . $generated_id); + $this->cust_generated_ids[$generated_id] = $customer; + } + } + + $this->em->detach($row[0]); + } + } } diff --git a/src/Entity/Customer.php b/src/Entity/Customer.php index 60f9f716..a3dfa931 100644 --- a/src/Entity/Customer.php +++ b/src/Entity/Customer.php @@ -12,11 +12,15 @@ use App\Ramcar\CustomerClassification; /** * @ORM\Entity - * @ORM\Table(name="customer", indexes={ - * @ORM\Index(name="phone_mobile_idx", columns={"phone_mobile"}), - * @ORM\Index(columns={"first_name"}, flags={"fulltext"}), - * @ORM\Index(columns={"last_name"}, flags={"fulltext"}), - @ORM\Index(name="generated_id_idx", columns={"generated_id"}) + * @ORM\Table(name="customer", + * uniqueConstraints={ + * @ORM\UniqueConstraint(columns={"generated_id"}) + * }, + * indexes={ + * @ORM\Index(name="phone_mobile_idx", columns={"phone_mobile"}), + * @ORM\Index(columns={"first_name"}, flags={"fulltext"}), + * @ORM\Index(columns={"last_name"}, flags={"fulltext"}), + @ORM\Index(name="generated_id_idx", columns={"generated_id"}) * }) */ class Customer diff --git a/src/Service/UniqueIdGenerator.php b/src/Service/UniqueIdGenerator.php index 90af3369..972a7ef8 100644 --- a/src/Service/UniqueIdGenerator.php +++ b/src/Service/UniqueIdGenerator.php @@ -9,26 +9,27 @@ use App\Entity\Customer; class UniqueIdGenerator { protected $em; + protected $cust_generated_ids; public function __construct(EntityManagerInterface $em) { $this->em = $em; + + $this->loadGeneratedIds(); } public function generateCustomerUniqueId($str_length) { $em = $this->em; - // retry until we find no customer with the generated id + // retry until we have no duplicate generated id while (true) { // generate the id $generated_id = $this->generateUniqueId($str_length); // check if generated id already exists - $cust = $em->getRepository(Customer::class)->findOneBy(['generated_id' => $generated_id]); - - if ($cust == null) + if (!isset($this->cust_generated_ids[$generated_id])) return $generated_id; sleep(1); @@ -40,4 +41,30 @@ class UniqueIdGenerator { return substr(md5(time()), 0, $str_length); } + + protected function loadGeneratedIds() + { + error_log('loading generated ids '); + $cust_q = $this->em->createQuery('select c from App\Entity\Customer c where c.generated_id is not null'); + $cust_iter = $cust_q->iterate(); + + $this->cust_generated_ids = []; + foreach ($cust_iter as $row) + { + $customer = $row[0]; + $generated_id = $customer->getGeneratedID(); + error_log('generated id = ' . $generated_id); + if ($generated_id != null) + { + error_log('generated id is not null ' . $generated_id); + if (!isset($this->cust_generated_ids[$generated_id])) + { + error_log('setting to hash ' . $generated_id); + $this->cust_generated_ids[$generated_id] = $customer; + } + } + + $this->em->detach($row[0]); + } + } } -- 2.43.5 From 294aafc6ec36260066a998fe16085dfc3a8fa860 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 15 Nov 2021 07:15:21 +0000 Subject: [PATCH 6/9] Create command to generate the ids for existing customers. Create sql command to set generated ids for customers. #638 --- .../CreateCustomerGeneratedIdCommand.php | 106 ++++++++++++++++ src/Command/SetCustomerGeneratedIdCommand.php | 114 ------------------ .../load_customer_generated_ids.sql | 13 ++ 3 files changed, 119 insertions(+), 114 deletions(-) create mode 100644 src/Command/CreateCustomerGeneratedIdCommand.php delete mode 100644 src/Command/SetCustomerGeneratedIdCommand.php create mode 100644 utils/load_customer_generated_ids/load_customer_generated_ids.sql diff --git a/src/Command/CreateCustomerGeneratedIdCommand.php b/src/Command/CreateCustomerGeneratedIdCommand.php new file mode 100644 index 00000000..058711d2 --- /dev/null +++ b/src/Command/CreateCustomerGeneratedIdCommand.php @@ -0,0 +1,106 @@ +em = $em; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('customer:creategeneratedid') + ->setDescription('Create customer generated id and output to file.') + ->setHelp('Create customer generated id and output to file.') + ->addArgument('output_file', InputArgument::REQUIRED, 'Output filename'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $em = $this->em; + $db = $em->getConnection(); + + $output_file = $input->getArgument('output_file'); + + // get all customers with no generated ids. First time to run this would mean all customers + $cust_q = $em->createQuery('SELECT c from App\Entity\Customer c where c.generated_id is null'); + + $customers = $cust_q->iterate(); + + $output_info = []; + foreach ($customers as $row) + { + $cust = $row[0]; + $output->writeln('Processing customer ' . $cust->getID()); + + $generated_id = $this->generateUniqueId(self::GENERATED_ID_LENGTH); + + // set format of csv file customer id, generated id + $output_info[] = [ + $cust->getID(), + $generated_id, + ]; + + $em->detach($row[0]); + } + + // write to output file + $this->outputCustomerGeneratedIds($output_file, $output_info); + + return 0; + } + + protected function generateUniqueId($str_length) + { + $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $rand_string = ''; + $desired_length = 10; + + $rand_string = substr(str_shuffle($charset), 0, $desired_length); + + $salt = time() . $rand_string; + + return substr(md5($salt), 0, $str_length); + } + + protected function outputCustomerGeneratedIds($output_file, $entries) + { + try + { + $fh = fopen($output_file, "w"); + } + catch (Exception $e) + { + throw new Exception('The file "' . $report_file . '" could be opened.'); + } + + foreach($entries as $row) + { + fputcsv($fh, $row); + } + + fclose($fh); + } +} diff --git a/src/Command/SetCustomerGeneratedIdCommand.php b/src/Command/SetCustomerGeneratedIdCommand.php deleted file mode 100644 index b2518a14..00000000 --- a/src/Command/SetCustomerGeneratedIdCommand.php +++ /dev/null @@ -1,114 +0,0 @@ -em = $em; - - $this->loadGeneratedIds(); - - parent::__construct(); - } - - protected function configure() - { - $this->setName('customer:setgeneratedid') - ->setDescription('Set customer generated id.') - ->setHelp('Set customer generated id.'); - } - - protected function execute(InputInterface $input, OutputInterface $output) - { - $em = $this->em; - $db = $em->getConnection(); - - $em->getConnection()->getConfiguration()->setSQLLogger(null); - - // get all customers with no generated ids. First time to run this would mean all customers - $cust_q = $em->createQuery('SELECT c from App\Entity\Customer c where c.generated_id is null'); - - $customers = $cust_q->iterate(); - foreach ($customers as $row) - { - $cust = $row[0]; - $output->writeln('Processing customer ' . $cust->getID()); - - while (true) - { - $generated_id = $this->generateUniqueId(self::GENERATED_ID_LENGTH); - - if (!isset($this->cust_generated_ids[$generated_id])) - { - $update_sql = 'UPDATE customer SET generated_id = :generated_id WHERE id = :cust_id'; - $update_stmt = $db->prepare($update_sql); - $update_stmt->execute([ - 'generated_id' => $generated_id, - 'cust_id' => $cust->getID(), - ]); - - // add generated id to hash - $this->cust_generated_ids[$generated_id] = $cust; - - break; - } - } - - $em->detach($row[0]); - } - - return 0; - } - - protected function generateUniqueId($str_length) - { - return substr(md5(time()), 0, $str_length); - } - - protected function loadGeneratedIds() - { - error_log('loading generated ids '); - $cust_q = $this->em->createQuery('select c from App\Entity\Customer c where c.generated_id is not null'); - $cust_iter = $cust_q->iterate(); - - $this->cust_generated_ids = []; - foreach ($cust_iter as $row) - { - $customer = $row[0]; - $generated_id = $customer->getGeneratedID(); - error_log('generated id = ' . $generated_id); - if ($generated_id != null) - { - error_log('generated id is not null ' . $generated_id); - if (!isset($this->cust_generated_ids[$generated_id])) - { - error_log('setting to hash ' . $generated_id); - $this->cust_generated_ids[$generated_id] = $customer; - } - } - - $this->em->detach($row[0]); - } - } -} diff --git a/utils/load_customer_generated_ids/load_customer_generated_ids.sql b/utils/load_customer_generated_ids/load_customer_generated_ids.sql new file mode 100644 index 00000000..dc66316c --- /dev/null +++ b/utils/load_customer_generated_ids/load_customer_generated_ids.sql @@ -0,0 +1,13 @@ +CREATE TABLE temp_table (id int(11), generated_id varchar(40)); + +LOAD DATA INFILE '/tmp/generated_ids.csv' +INTO TABLE temp_table +FIELDS TERMINATED BY ',' +ENCLOSED BY '"' +LINES TERMINATED BY '\n' +(id, generated_id); + +UPDATE customer INNER JOIN temp_table ON temp_table.id = customer.id +SET customer.generated_id = temp_table.generated_id; + +DROP TABLE temp_table; -- 2.43.5 From f29c69cad1a491dd64519b02962ff264852704cb Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 16 Nov 2021 04:45:19 +0000 Subject: [PATCH 7/9] Fix checking for duplicate ids. #638 --- src/Service/UniqueIdGenerator.php | 34 ++++++++----------------------- 1 file changed, 9 insertions(+), 25 deletions(-) diff --git a/src/Service/UniqueIdGenerator.php b/src/Service/UniqueIdGenerator.php index 972a7ef8..0e8f05b0 100644 --- a/src/Service/UniqueIdGenerator.php +++ b/src/Service/UniqueIdGenerator.php @@ -29,7 +29,9 @@ class UniqueIdGenerator $generated_id = $this->generateUniqueId($str_length); // check if generated id already exists - if (!isset($this->cust_generated_ids[$generated_id])) + $cust = $em->getRepository(Customer::class)->findOneBy(['generated_id' => $generated_id]); + + if ($cust == null) return $generated_id; sleep(1); @@ -39,32 +41,14 @@ class UniqueIdGenerator protected function generateUniqueId($str_length) { - return substr(md5(time()), 0, $str_length); - } + $charset = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $rand_string = ''; + $desired_length = 10; - protected function loadGeneratedIds() - { - error_log('loading generated ids '); - $cust_q = $this->em->createQuery('select c from App\Entity\Customer c where c.generated_id is not null'); - $cust_iter = $cust_q->iterate(); + $rand_string = substr(str_shuffle($charset), 0, $desired_length); - $this->cust_generated_ids = []; - foreach ($cust_iter as $row) - { - $customer = $row[0]; - $generated_id = $customer->getGeneratedID(); - error_log('generated id = ' . $generated_id); - if ($generated_id != null) - { - error_log('generated id is not null ' . $generated_id); - if (!isset($this->cust_generated_ids[$generated_id])) - { - error_log('setting to hash ' . $generated_id); - $this->cust_generated_ids[$generated_id] = $customer; - } - } + $salt = time() . $rand_string; - $this->em->detach($row[0]); - } + return substr(md5($salt), 0, $str_length); } } -- 2.43.5 From c40143dced68425c535e1e4606f3e6af1494b222 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 16 Nov 2021 09:54:58 +0000 Subject: [PATCH 8/9] Add checking for duplicate generated ids when flushing. #638 --- config/services.yaml | 6 ++ src/Controller/APIController.php | 25 +++++++- src/Controller/CAPI/CustomerController.php | 15 ++++- .../CAPI/CustomerWarrantyController.php | 29 ++++++--- src/Controller/CAPI/WarrantyController.php | 18 ++++-- src/Service/CustomerGeneratedIdService.php | 59 +++++++++++++++++++ .../CustomerHandler/ResqCustomerHandler.php | 18 +++++- src/Service/UniqueIdGenerator.php | 4 -- 8 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 src/Service/CustomerGeneratedIdService.php diff --git a/config/services.yaml b/config/services.yaml index 65576192..0ba337cf 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -306,3 +306,9 @@ services: App\Service\UniqueIdGenerator: arguments: $em: "@doctrine.orm.entity_manager" + + # service to generate id for customer and to save customer + App\Service\CustomerGeneratedIdService: + arguments: + $em: "@doctrine.orm.entity_manager" + $id_gen: "@App\\Service\\UniqueIdGenerator" diff --git a/src/Controller/APIController.php b/src/Controller/APIController.php index b072d8e4..da2703be 100644 --- a/src/Controller/APIController.php +++ b/src/Controller/APIController.php @@ -47,6 +47,7 @@ use App\Service\HubSelector; use App\Service\HubDistributor; use App\Service\HubFilterLogger; use App\Service\HubFilteringGeoChecker; +use App\Service\CustomerGeneratedIdService; use App\Entity\MobileSession; use App\Entity\Customer; @@ -442,7 +443,7 @@ class APIController extends Controller implements LoggedController return $cust; } - public function updateInfo(Request $req, EntityManagerInterface $em) + public function updateInfo(Request $req, EntityManagerInterface $em, CustomerGeneratedIdService $cust_gen_id) { // check required parameters and api key $required_params = [ @@ -468,8 +469,26 @@ class APIController extends Controller implements LoggedController { $cust->setPrivacyPolicyMobile($mobile_policy); } - - $em->flush(); + + // need to check if it's a new customer or updated existing customer + // a new customer would not have a generated id while an existing one + // already have one. + if ($cust->getGeneratedId() == null) + { + // TODO: temporary fix on how to save customer with a generated id + // since we need to keep generating an id until we are sure that there + // are no duplicates for generated id + // when saving the customer. This is an additional check. + // This will keep generating an id until a unique id is generated + // and the customer entity can then be inserted + $cust_gen_id->saveCustomerWithGeneratedId($cust); + } + else + { + // we do a simple flush with no id generation for updated existing + // customers + $em->flush(); + } return $res->getReturnResponse(); } diff --git a/src/Controller/CAPI/CustomerController.php b/src/Controller/CAPI/CustomerController.php index 9148321e..0d89a0e9 100644 --- a/src/Controller/CAPI/CustomerController.php +++ b/src/Controller/CAPI/CustomerController.php @@ -15,6 +15,8 @@ use App\Entity\Customer; use App\Entity\CustomerVehicle; use App\Entity\Vehicle; +use App\Service\CustomerGeneratedIdService; + use Catalyst\APIBundle\Access\Generator as ACLGenerator; class CustomerController extends APIController @@ -26,7 +28,7 @@ class CustomerController extends APIController $this->acl_gen = $acl_gen; } - public function register(Request $req, EntityManagerInterface $em) + public function register(Request $req, EntityManagerInterface $em, CustomerGeneratedIdService $cust_gen_id) { $this->denyAccessUnlessGranted('customer.register', null, 'No access.'); @@ -150,6 +152,8 @@ class CustomerController extends APIController ]; } } + + $em->flush(); } else { @@ -194,9 +198,16 @@ class CustomerController extends APIController 'condition' => $condition, 'fuel_type' => $fuel_type, ]; + + // TODO: temporary fix on how to save customer with a generated id + // since we need to keep generating an id until we are sure that there + // are no duplicates for generated id + // when saving the customer. This is an additional check. + // This will keep generating an id until a unique id is generated + // and the customer entity can then be inserted + $cust_gen_id->saveCustomerWithGeneratedId($new_cust); } - $em->flush(); $em->clear(); return new APIResponse(true, $message, $data); diff --git a/src/Controller/CAPI/CustomerWarrantyController.php b/src/Controller/CAPI/CustomerWarrantyController.php index edf00e2e..bc942b22 100644 --- a/src/Controller/CAPI/CustomerWarrantyController.php +++ b/src/Controller/CAPI/CustomerWarrantyController.php @@ -15,6 +15,7 @@ use Catalyst\APIBundle\Response\APIResponse; use App\Service\RisingTideGateway; use App\Service\WarrantyAPILogger; +use App\Service\CustomerGeneratedIdService; use App\Entity\WarrantySerial; use App\Entity\Warranty; @@ -301,7 +302,7 @@ class CustomerWarrantyController extends APIController public function register($serial, EntityManagerInterface $em, Request $req, KernelInterface $kernel, RisingTideGateway $rt, TranslatorInterface $trans, - WarrantyAPILogger $logger) + WarrantyAPILogger $logger, CustomerGeneratedIdService $cust_gen_id) { error_log('HERE - register'); @@ -325,7 +326,7 @@ class CustomerWarrantyController extends APIController $username = $this->getUser()->getName(); $source = 'CAPI_USER_' . $username; - error_log('SOURCE: ' . $source); + // error_log('SOURCE: ' . $source); // TODO: maybe add vmake_id? since warranty cannot be created with no vmake // TODO: maybe also add mobile and email since customer creation won't let mobile and email be null @@ -353,7 +354,7 @@ class CustomerWarrantyController extends APIController // do actual registering $res = $this->updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename, $wcard_filename, - $logger, $log_data, $user_id, $action, $source); + $logger, $log_data, $user_id, $action, $source, $cust_gen_id); // flush to db $em->flush(); @@ -395,7 +396,7 @@ class CustomerWarrantyController extends APIController } protected function updateWarranty($em, $rt, $trans, $req, $serial, $inv_filename = null, $wcard_filename = null, - $logger, $log_data, $user_id, $action, $source) + $logger, $log_data, $user_id, $action, $source, $cust_gen_id) { $plate_num = $this->cleanPlateNumber($req->request->get('plate_num')); @@ -512,7 +513,6 @@ class CustomerWarrantyController extends APIController $cust->setPrivacyPromo($priv_promo); } - error_log('update entity / database'); // create or update warranty entry $warr->setSerial($serial) @@ -556,15 +556,30 @@ class CustomerWarrantyController extends APIController $em->persist($warr); - $logger->logWarrantyInfo($log_data, '', $user_id, $action, $source); + // need to check if it's a new customer or updated existing customer + // a new customer would not have a generated id while an existing one + // already have one. + if ($cust->getGeneratedId() == null) + { + // TODO: temporary fix on how to save customer with a generated id + // since we need to keep generating an id until we are sure that there + // are no duplicates for generated id + // when saving the customer. This is an additional check. + // This will keep generating an id until a unique id is generated + // and the customer entity can then be inserted + $cust_gen_id->saveCustomerWithGeneratedId($cust); + } // TODO: check if we need to do anything else $data = []; - // send sms confirmation $this->sendSMSConfirmation($rt, $req->request->get('contact_num'), $sms_message); + $logger->logWarrantyInfo($log_data, '', $user_id, $action, $source); + + $em->flush(); + return new APIResponse(true, 'Warranty registered.', $data); } diff --git a/src/Controller/CAPI/WarrantyController.php b/src/Controller/CAPI/WarrantyController.php index 4db8051c..b2f9c5c8 100644 --- a/src/Controller/CAPI/WarrantyController.php +++ b/src/Controller/CAPI/WarrantyController.php @@ -23,6 +23,7 @@ use App\Entity\Vehicle; use App\Entity\WarrantyAPILog; use App\Service\WarrantyAPILogger; +use App\Service\CustomerGeneratedIdService; use App\Ramcar\NameValue; use App\Ramcar\WarrantyClass; @@ -146,7 +147,7 @@ class WarrantyController extends APIController return new APIResponse(true, 'Warranties found.', $data); } - public function register(Request $req, EntityManagerInterface $em, WarrantyAPILogger $logger) + public function register(Request $req, EntityManagerInterface $em, WarrantyAPILogger $logger, CustomerGeneratedIdService $cust_gen_id) { $this->denyAccessUnlessGranted('warranty.register.battery', null, 'No access.'); @@ -287,7 +288,7 @@ class WarrantyController extends APIController { $em->persist($warr); - $this->getCustomerFromMobile($em, $warr); + $this->getCustomerFromMobile($em, $warr, $cust_gen_id); $em->flush(); } @@ -638,7 +639,7 @@ class WarrantyController extends APIController return new APIResponse(true, 'Warranties found.', $data); } - protected function getCustomerFromMobile($em, $warranty) + protected function getCustomerFromMobile($em, $warranty, $cust_gen_id) { $w_mobile = $warranty->getMobileNumber(); if (empty($w_mobile)) @@ -717,6 +718,7 @@ class WarrantyController extends APIController $this->createCustomerVehicle($em, $customer, $this->getDefaultVehicle($em), $w_plate_number); } } + $em->flush(); } // customer not found else @@ -738,9 +740,17 @@ class WarrantyController extends APIController $em->persist($new_cust); $this->createCustomerVehicle($em, $new_cust, $this->getDefaultVehicle($em), $w_plate_number); + + // TODO: temporary fix on how to save customer with a generated id + // since we need to keep generating an id until we are sure that there + // are no duplicates for generated id + // when saving the customer. This is an additional check. + // This will keep generating an id until a unique id is generated + // and the customer entity can then be inserted + $cust_gen_id->saveCustomerWithGeneratedId($new_cust); } - $em->flush(); + // $em->flush(); $em->clear(); } diff --git a/src/Service/CustomerGeneratedIdService.php b/src/Service/CustomerGeneratedIdService.php new file mode 100644 index 00000000..92c85836 --- /dev/null +++ b/src/Service/CustomerGeneratedIdService.php @@ -0,0 +1,59 @@ +em = $em; + $this->id_gen = $id_gen; + } + + public function saveCustomerWithGeneratedId(Customer $cust) + { + $em = $this->em; + $id_gen = $this->id_gen; + + // need to generate id + // have to try catch the flush because of the generated id + // need to retry until we get a unique generated id + while (true) + { + try + { + $generated_id = $id_gen->generateCustomerUniqueId(40); + $cust->setGeneratedId($generated_id); + + // reopen in case we get an exception + if (!$em->isOpen()) + { + $em = $em->create( + $em->getConnection(), + $em->getConfiguration() + ); + } + + $em->flush(); + } + catch (UniqueConstraintViolationException $e) + { + error_log($e->getMessage()); + // delay one second and try again + sleep(1); + continue; + + } + break; + } + } +} diff --git a/src/Service/CustomerHandler/ResqCustomerHandler.php b/src/Service/CustomerHandler/ResqCustomerHandler.php index f8685801..7a0a4204 100644 --- a/src/Service/CustomerHandler/ResqCustomerHandler.php +++ b/src/Service/CustomerHandler/ResqCustomerHandler.php @@ -4,12 +4,15 @@ namespace App\Service\CustomerHandler; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; + use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\Security\Core\Security; use App\Service\CustomerHandlerInterface; +use App\Service\CustomerGeneratedIdService; use App\Ramcar\CustomerClassification; use App\Ramcar\FuelType; @@ -34,14 +37,16 @@ class ResqCustomerHandler implements CustomerHandlerInterface protected $country_code; protected $security; protected $template_hash; + protected $cust_gen_id; public function __construct(EntityManagerInterface $em, ValidatorInterface $validator, - string $country_code, Security $security) + string $country_code, Security $security, CustomerGeneratedIdService $cust_gen_id) { $this->em = $em; $this->validator = $validator; $this->country_code = $country_code; $this->security = $security; + $this->cust_gen_id = $cust_gen_id; $this->loadTemplates(); } @@ -292,7 +297,16 @@ class ResqCustomerHandler implements CustomerHandlerInterface { // validated! save the entity $em->persist($row); - $em->flush(); + + // TODO: temporary fix on how to save customer with a generated id + // since we need to keep generating an id until we are sure that there + // are no duplicates for generated id + // when saving the customer. This is an additional check. + // This will keep generating an id until a unique id is generated + // and the customer entity can then be inserted + $this->cust_gen_id->saveCustomerWithGeneratedId($row); + + //$em->flush(); $result = [ 'id' => $row->getID(), diff --git a/src/Service/UniqueIdGenerator.php b/src/Service/UniqueIdGenerator.php index 0e8f05b0..cfca4779 100644 --- a/src/Service/UniqueIdGenerator.php +++ b/src/Service/UniqueIdGenerator.php @@ -9,13 +9,10 @@ use App\Entity\Customer; class UniqueIdGenerator { protected $em; - protected $cust_generated_ids; public function __construct(EntityManagerInterface $em) { $this->em = $em; - - $this->loadGeneratedIds(); } public function generateCustomerUniqueId($str_length) @@ -36,7 +33,6 @@ class UniqueIdGenerator sleep(1); } - } protected function generateUniqueId($str_length) -- 2.43.5 From 46be0d6df08707fc1eaedae8699f6491ed14ee29 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 16 Nov 2021 10:48:39 +0000 Subject: [PATCH 9/9] Add generated id to customer creation. #638 --- .../CreateCustomerFromWarrantyCommand.php | 18 +++++++++++++++++- .../ImportCarClubCustomerDataCommand.php | 15 ++++++++++++++- .../ImportCarClubCustomerHubCommand.php | 17 +++++++++++++++-- 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/Command/CreateCustomerFromWarrantyCommand.php b/src/Command/CreateCustomerFromWarrantyCommand.php index 5ffe3a46..a379bee8 100644 --- a/src/Command/CreateCustomerFromWarrantyCommand.php +++ b/src/Command/CreateCustomerFromWarrantyCommand.php @@ -19,6 +19,8 @@ use App\Entity\Vehicle; use App\Ramcar\FuelType; use App\Ramcar\VehicleStatusCondition; +use App\Service\CustomerGeneratedIdService; + use DateTime; class CreateCustomerFromWarrantyCommand extends Command @@ -33,9 +35,12 @@ class CreateCustomerFromWarrantyCommand extends Command protected $cvu_mfg_id; protected $cvu_brand_id; - public function __construct(EntityManagerInterface $em, $cvu_mfg_id, $cvu_brand_id) + protected $cust_gen_id; + + public function __construct(EntityManagerInterface $em, $cvu_mfg_id, $cvu_brand_id, CustomerGeneratedIdService $cust_gen_id) { $this->em = $em; + $this->cust_gen_id = $cust_gen_id; $this->cvu_mfg_id = $cvu_mfg_id; $this->cvu_brand_id = $cvu_brand_id; @@ -154,6 +159,7 @@ class CreateCustomerFromWarrantyCommand extends Command error_log("($total_warr) processing $w_mobile_num from warranty..."); $customers = $this->findCustomerByNumber($w_mobile_num); + $new_cust = null; if (!empty($customers)) { @@ -232,6 +238,16 @@ class CreateCustomerFromWarrantyCommand extends Command $total_cv_added++; } } + if ($new_cust != null) + { + // TODO: temporary fix on how to save customer with a generated id + // since we need to keep generating an id until we are sure that there + // are no duplicates for generated id + // when saving the customer. This is an additional check. + // This will keep generating an id until a unique id is generated + // and the customer entity can then be inserted + $cust_gen_id->saveCustomerWithGeneratedId($new_cust); + } $this->em->flush(); $this->em->clear(); } diff --git a/src/Command/ImportCarClubCustomerDataCommand.php b/src/Command/ImportCarClubCustomerDataCommand.php index 5ae3b167..fa997ff4 100644 --- a/src/Command/ImportCarClubCustomerDataCommand.php +++ b/src/Command/ImportCarClubCustomerDataCommand.php @@ -15,6 +15,8 @@ use Exception; use App\Entity\Customer; use App\Entity\CustomerTag; +use App\Service\CustomerGeneratedIdService; + class ImportCarClubCustomerDataCommand extends Command { // field index in csv file @@ -26,10 +28,12 @@ class ImportCarClubCustomerDataCommand extends Command protected $em; protected $cust_tag_hash; + protected $cust_gen_id; - public function __construct(EntityManagerInterface $em) + public function __construct(EntityManagerInterface $em, CustomerGeneratedIdService $cust_gen_id) { $this->em = $em; + $this->cust_gen_id = $cust_gen_id; $this->loadCustomerTags(); parent::__construct(); @@ -173,6 +177,15 @@ class ImportCarClubCustomerDataCommand extends Command ->addCustomerTag($promo_tag); $this->em->persist($new_cust); + + // TODO: temporary fix on how to save customer with a generated id + // since we need to keep generating an id until we are sure that there + // are no duplicates for generated id + // when saving the customer. This is an additional check. + // This will keep generating an id until a unique id is generated + // and the customer entity can then be inserted + $this->cust_gen_id->saveCustomerWithGeneratedId($new_cust); + $this->em->flush(); return $new_cust; diff --git a/src/Command/ImportCarClubCustomerHubCommand.php b/src/Command/ImportCarClubCustomerHubCommand.php index 15275a1c..f474164b 100644 --- a/src/Command/ImportCarClubCustomerHubCommand.php +++ b/src/Command/ImportCarClubCustomerHubCommand.php @@ -14,6 +14,8 @@ use App\Entity\CustomerTag; use App\Entity\CarClubCustomerHub; use App\Entity\Hub; +use App\Service\CustomerGeneratedIdService; + class ImportCarClubCustomerHubCommand extends Command { // field index in csv file @@ -28,10 +30,12 @@ class ImportCarClubCustomerHubCommand extends Command protected $em; protected $cust_tag_hash; + protected $cust_gen_id; - public function __construct(EntityManagerInterface $em) + public function __construct(EntityManagerInterface $em, CustomerGeneratedIdService $cust_gen_id) { $this->em = $em; + $this->cust_gen_id = $cust_gen_id; $this->loadCustomerTags(); parent::__construct(); @@ -188,7 +192,16 @@ class ImportCarClubCustomerHubCommand extends Command ->addCustomerTag($promo_tag); $this->em->persist($new_cust); - $this->em->flush(); + + // TODO: temporary fix on how to save customer with a generated id + // since we need to keep generating an id until we are sure that there + // are no duplicates for generated id + // when saving the customer. This is an additional check. + // This will keep generating an id until a unique id is generated + // and the customer entity can then be inserted + $this->cust_gen_id->saveCustomerWithGeneratedId($new_cust); + + //$this->em->flush(); return $new_cust; } -- 2.43.5