Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DDLS-432: Automate creating clients and reports bugs/improvements #1777

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions api/app/src/Repository/ClientRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 10 additions & 4 deletions api/app/src/Repository/PreRegistrationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 23 additions & 11 deletions api/app/src/v2/Registration/Uploader/LayDeputyshipUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ($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 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;
}

Expand Down
Loading