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/Repository/PreRegistrationRepository.php b/api/app/src/Repository/PreRegistrationRepository.php index fdd14c3d40..6a8b524015 100644 --- a/api/app/src/Repository/PreRegistrationRepository.php +++ b/api/app/src/Repository/PreRegistrationRepository.php @@ -65,6 +65,18 @@ public function getNewClientsForExistingDeputiesArray(): array { $conn = $this->getEntityManager()->getConnection(); + /** Query to retrieve the new clients to be made from the PreReg table for existing deputies. + * + * Inner query is comparing the combination of deputy uid and case number (which is akin to a court order, just without the report type) + * Compares the combinations that exist in PreReg (essentially Sirius) with the combinations from User & Client table (Digideps) + * This gives us which combinations do not exist in Digideps + * + * Outer query bring backs fields and links PreReg table to User table via deputy_uid + * This is so we are only focused on deputies that have signed up and have a deputy_uid. + * + * We then compare the combinations of deputy uid and case number from the outer query with + * the combination of deputy uid and case number from the inner query. + */ $newMultiClentsQuery = <<executeQuery($newMultiClentsQuery); diff --git a/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php b/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php index f84a57ef5d..ae9edb2d65 100644 --- a/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php +++ b/api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php @@ -141,21 +141,33 @@ private function handleNewUser(LayDeputyshipDto $dto): ?User private function handleNewClient(LayDeputyshipDto $dto, User $newUser): ?Client { - $existingClient = $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())); + $existingClients = $this->em->getRepository(Client::class)->findByCaseNumberIncludingDischarged($dto->getCaseNumber()); + + if (count($existingClients) > 0) { + /* @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 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; }