From c3440a452fac84ad0d6dfcd963f3da69ed7ab709 Mon Sep 17 00:00:00 2001 From: Gugandeep Chani Date: Fri, 20 Dec 2024 11:11:11 +0000 Subject: [PATCH 1/3] Fix to query to accept multiple clients being returned. Fix to logic to loop through multiple clients --- api/app/src/Repository/ClientRepository.php | 8 ++++---- .../Registration/Uploader/LayDeputyshipUploader.php | 12 +++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/api/app/src/Repository/ClientRepository.php b/api/app/src/Repository/ClientRepository.php index e9ef95487c..149f2072c9 100644 --- a/api/app/src/Repository/ClientRepository.php +++ b/api/app/src/Repository/ClientRepository.php @@ -135,20 +135,20 @@ public function findByCaseNumber(string $caseNumber): ?Client ->getOneOrNullResult(); } - public function findByCaseNumberIncludingDischarged(string $caseNumber): ?Client + public function findByCaseNumberIncludingDischarged(string $caseNumber): mixed { $filter = $this->_em->getFilters()->getFilter('softdeleteable'); $filter->disableForEntity(Client::class); - $client = $this + $clients = $this ->getEntityManager() ->createQuery('SELECT c FROM App\Entity\Client c WHERE LOWER(c.caseNumber) = LOWER(:caseNumber)') ->setParameter('caseNumber', $caseNumber) - ->getOneOrNullResult(); + ->getResult(); $this->_em->getFilters()->enable('softdeleteable'); - return $client; + return $clients; } public function findByIdIncludingDischarged(int $id): ?Client diff --git a/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php b/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php index f84a57ef5d..12fccd99f0 100644 --- a/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php +++ b/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php @@ -141,12 +141,14 @@ private function handleNewUser(LayDeputyshipDto $dto): ?User private function handleNewClient(LayDeputyshipDto $dto, User $newUser): ?Client { - $existingClient = $this->em->getRepository(Client::class)->findByCaseNumberIncludingDischarged($dto->getCaseNumber()); + $existingClients = $this->em->getRepository(Client::class)->findByCaseNumberIncludingDischarged($dto->getCaseNumber()); - if ($existingClient instanceof Client) { - foreach ($existingClient->getUsers() as $user) { - if ($user->getDeputyUid() == $newUser->getDeputyUid()) { - throw new \RuntimeException(sprintf('a client with case number %s already exists that is associated with a user with deputy UID %s', $existingClient->getCaseNumber(), $newUser->getDeputyUid())); + if ($existingClients) { + foreach ($existingClients as $existingClient) { + foreach ($existingClient->getUsers() as $user) { + if ($user->getDeputyUid() == $newUser->getDeputyUid()) { + throw new \RuntimeException(sprintf('a client with case number %s already exists that is associated with a user with deputy UID %s', $existingClient->getCaseNumber(), $newUser->getDeputyUid())); + } } } } else { From b82771d787a3de2e6ff6655c7d4f34ef1ba309e3 Mon Sep 17 00:00:00 2001 From: Gugandeep Chani Date: Tue, 7 Jan 2025 14:53:20 +0000 Subject: [PATCH 2/3] Refactored handleNewClient(), added comments to give context for reasoning behind logic --- .../Uploader/LayDeputyshipUploader.php | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php b/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php index 12fccd99f0..38db499f48 100644 --- a/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php +++ b/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php @@ -144,20 +144,30 @@ private function handleNewClient(LayDeputyshipDto $dto, User $newUser): ?Client $existingClients = $this->em->getRepository(Client::class)->findByCaseNumberIncludingDischarged($dto->getCaseNumber()); if ($existingClients) { + /* @var Client $existingClient */ + // If there is an existing active client, we shouldn't create a new instance of the client + foreach ($existingClients as $existingClient) { + if (!$existingClient->isDeleted()) { + throw new \RuntimeException(sprintf('an active client with case number %s already exists', $existingClient->getCaseNumber())); + } + } + // Loop through the discharged clients to ensure we are not creating an account for a deputy associated with a discharged client foreach ($existingClients as $existingClient) { foreach ($existingClient->getUsers() as $user) { if ($user->getDeputyUid() == $newUser->getDeputyUid()) { - throw new \RuntimeException(sprintf('a client with case number %s already exists that is associated with a user with deputy UID %s', $existingClient->getCaseNumber(), $newUser->getDeputyUid())); + throw new \RuntimeException(sprintf('a discharged client with case number %s already exists that is associated with a user with deputy UID %s', $existingClient->getCaseNumber(), $newUser->getDeputyUid())); } } } - } else { - $newClient = $this->clientAssembler->assembleFromLayDeputyshipDto($dto); - $newClient->addUser($newUser); - $this->em->persist($newClient); - $this->added['clients'][] = $dto->getCaseNumber(); } + // Only create a new instance of the client if one doesn't already exist, + // Or if all the clients are discharged, and we are creating an account for a deputy that is not associated with this case number already + $newClient = $this->clientAssembler->assembleFromLayDeputyshipDto($dto); + $newClient->addUser($newUser); + $this->em->persist($newClient); + $this->added['clients'][] = $dto->getCaseNumber(); + return $newClient; } From d40055d33c1ca5d5f6b7511264f949e438eebe62 Mon Sep 17 00:00:00 2001 From: Gugandeep Chani Date: Tue, 7 Jan 2025 16:26:13 +0000 Subject: [PATCH 3/3] Refactored sql query to compare deputy uid and case number pairs to identify which combinations have not signed up to digideps --- .../src/Repository/PreRegistrationRepository.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/api/app/src/Repository/PreRegistrationRepository.php b/api/app/src/Repository/PreRegistrationRepository.php index fdd14c3d40..673c35ec81 100644 --- a/api/app/src/Repository/PreRegistrationRepository.php +++ b/api/app/src/Repository/PreRegistrationRepository.php @@ -92,10 +92,16 @@ public function getNewClientsForExistingDeputiesArray(): array pr.client_address_5 AS "ClientAddress5", pr.client_postcode AS "ClientPostcode" FROM pre_registration pr - LEFT JOIN dd_user u ON pr.deputy_uid = u.deputy_uid::varchar(30) - LEFT JOIN deputy_case dc ON u.id = dc.user_id - LEFT JOIN client c ON dc.client_id = c.id - WHERE c.case_number != pr.client_case_number + INNER JOIN dd_user u ON pr.deputy_uid = u.deputy_uid::varchar(30) + WHERE (pr.deputy_uid, pr.client_case_number) IN (SELECT deputy_uid, lower(client_case_number) + FROM pre_registration + GROUP BY deputy_uid, lower(client_case_number) + EXCEPT + SELECT u.deputy_uid::varchar(30), lower(c.case_number) + FROM dd_user u + INNER JOIN deputy_case dc ON u.id = dc.user_id + INNER JOIN client c on dc.client_id = c.id + GROUP BY u.deputy_uid, lower(c.case_number)); SQL; $stmt = $conn->executeQuery($newMultiClentsQuery);