Skip to content

Commit

Permalink
Add work in progress combined lpa manager and point v2 handler at it.
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperaj committed Sep 17, 2024
1 parent da7a876 commit 2c3025c
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 3 deletions.
10 changes: 7 additions & 3 deletions service-api/app/src/App/src/Handler/LpasCollectionV2Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace App\Handler;

use App\Service\Lpa\LpaService;
use App\Service\Lpa\CombinedLpaManager;
use Exception;
use Laminas\Diactoros\Response\JsonResponse;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -17,7 +17,7 @@
class LpasCollectionV2Handler implements RequestHandlerInterface
{
public function __construct(
private LpaService $lpaService,
private readonly CombinedLpaManager $lpaManager,
) {
}

Expand All @@ -28,6 +28,10 @@ public function __construct(
*/
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new JsonResponse([]);
$user = $request->getAttribute('actor-id');

$result = $this->lpaManager->getAllForUser($user);

return new JsonResponse($result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace App\Service\Lpa\Combined;

use App\DataAccess\Repository\UserLpaActorMapInterface;

/**
* @psalm-import-type UserLpaActorMap from UserLpaActorMapInterface
*/
class ResolveLpaTypes
{
/**
* Given a list of UserLpaActorMap records it will return a two part array containing
* separate lists of both Sirius and DataStore LPAs
*
* @psalm-pure
* @param $lpaActorMaps array Map of LPAs from Dynamo
* @psalm-param $lpaActorMaps UserLpaActorMap[]
* @return array{
* string[],
* string[]
* }
*/
public function __invoke(array $lpaActorMaps): array
{
$lpaUids = array_merge(
array_column($lpaActorMaps, 'SiriusUid'),
array_column($lpaActorMaps, 'LpaUid'),
);

$dataStoreUids = [];
$siriusUids = array_filter($lpaUids, function ($item) use (&$dataStoreUids) {
if (str_starts_with($item, '7')) {
return true;
}

$dataStoreUids[] = $item;
return false;
});

return [$siriusUids, $dataStoreUids];
}
}
124 changes: 124 additions & 0 deletions service-api/app/src/App/src/Service/Lpa/CombinedLpaManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

namespace App\Service\Lpa;

use App\DataAccess\ApiGateway\DataStoreLpas;
use App\DataAccess\ApiGateway\SiriusLpas;
use App\DataAccess\Repository\Response\LpaInterface;
use App\DataAccess\Repository\UserLpaActorMapInterface;
use App\Service\Lpa\Combined\ResolveLpaTypes;

/**
* @psalm-import-type UserLpaActorMap from UserLpaActorMapInterface
*/
class CombinedLpaManager implements LpaManagerInterface
{
public function __construct(
private readonly UserLpaActorMapInterface $userLpaActorMapRepository,
private readonly ResolveLpaTypes $resolveLpaTypes,
private readonly SiriusLpas $siriusLpas,
private readonly DataStoreLpas $dataStoreLpas,
private readonly SiriusLpaManager $siriusLpaManager,
) {
}

public function getByUid(string $uid): ?LpaInterface
{
return $this->siriusLpaManager->getByUid($uid);
}

public function getByUserLpaActorToken(string $token, string $userId): ?array
{
return $this->siriusLpaManager->getByUserLpaActorToken($token, $userId);
}

public function getAllForUser(string $userId): array
{
// Returns an array of all the LPAs Ids (plus other metadata) in the user's account.
$lpaActorMaps = $this->userLpaActorMapRepository->getByUserId($userId);

$lpaActorMaps = array_filter($lpaActorMaps, function ($item) {
return !array_key_exists('ActivateBy', $item);
});

return $this->lookupAndFormatLpas($lpaActorMaps);
}

public function getAllLpasAndRequestsForUser(string $userId): array
{
// Returns an array of all the LPAs Ids (plus other metadata) in the user's account.
$lpaActorMaps = $this->userLpaActorMapRepository->getByUserId($userId);

return $this->lookupAndFormatLpas($lpaActorMaps);
}

public function getByViewerCode(string $viewerCode, string $donorSurname, ?string $organisation = null): ?array
{
return $this->siriusLpaManager->getByViewerCode($viewerCode, $donorSurname, $organisation);
}

/**
* @param $lpaActorMaps array Map of LPAs from Dynamo
* @psalm-param $lpaActorMaps UserLpaActorMap[]
* @return array an array with formatted LPA results
*/
private function lookupAndFormatLpas(array $lpaActorMaps): array
{
[$siriusUids, $dataStoreUids] = ($this->resolveLpaTypes)($lpaActorMaps);

/** @var LpaInterface[] $siriusLpas */
$siriusLpas = count($siriusUids) > 0
? $this->siriusLpas->lookup($siriusUids)
: [];

/** @var LpaInterface[] $siriusLpas */
$dataStoreLpas = count($dataStoreUids) > 0
? $this->dataStoreLpas->lookup($dataStoreUids)
: [];

$keyedDataStoreLpas = [];
array_walk($dataStoreLpas, function (LpaInterface $item) use (&$keyedDataStoreLpas) {
$keyedDataStoreLpas[$item->getData()['uid']] = $item;
});

// unusual combination operation in order to preserve potential numeric keys
$lpas = $siriusLpas + $dataStoreLpas;

$result = [];

foreach ($lpaActorMaps as $item) {
$lpaId = $item['LpaUid'] ?? $item['SiriusUid'];
$lpa = $lpas[$lpaId] ?? null;

if ($lpa === null) {
$result[$item['Id']] = [
'user-lpa-actor-token' => $item['Id'],
'error' => 'NO_LPA_FOUND',
];

continue;
}

$lpaData = $lpa->getData();

// TODO load lpaData into object hydrator
// TODO combined resolveActor that uses object
$actor = ($this->resolveActor)($lpaData, (int) $item['ActorId']);

//Extract and return only LPA's where status is Registered or Cancelled
if (($this->isValidLpa)($lpaData)) {
$result[$item['Id']] = [
'user-lpa-actor-token' => $item['Id'],
'date' => $lpa->getLookupTime()->format('c'),
'actor' => $actor,
'lpa' => $lpaData,
'added' => $item['Added']->format('Y-m-d H:i:s'),
];
}
}

return $result;
}
}

0 comments on commit 2c3025c

Please sign in to comment.