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

Filter not activated LPAs 2.0 #1064

Merged
6 changes: 2 additions & 4 deletions service-api/app/src/App/src/Service/Lpa/LpaAlreadyAdded.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@ public function __construct(
public function __invoke(string $userId, string $lpaUid): ?array
{
if (($this->featureEnabled)('save_older_lpa_requests')) {

$savedLpaRecords = $this->userLpaActorMapRepository->getUsersLpas($userId);

foreach($savedLpaRecords as $record) {
foreach ($savedLpaRecords as $record) {
if ($record['SiriusUid'] === $lpaUid && !array_key_exists('ActivateBy', $record)) {
$lpa = $this->lpaService->getByUserLpaActorToken($record['Id'], $userId);

Expand Down Expand Up @@ -80,7 +79,6 @@ public function __invoke(string $userId, string $lpaUid): ?array
}

return null;

} else {
return $this->preSaveOfRequestFeature($userId, $lpaUid);
}
Expand All @@ -95,7 +93,7 @@ public function __invoke(string $userId, string $lpaUid): ?array
*/
private function preSaveOfRequestFeature(string $userId, string $lpaUid): ?array
{
$lpasAdded = $this->lpaService->getAllForUser($userId);
$lpasAdded = $this->lpaService->getAllForUser($userId, false);

foreach ($lpasAdded as $userLpaActorToken => $lpaData) {
if ($lpaData['lpa']['uId'] === $lpaUid) {
Expand Down
13 changes: 11 additions & 2 deletions service-api/app/src/App/src/Service/Lpa/LpaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,22 @@ public function getByUserLpaActorToken(string $token, string $userId): ?array
/**
* Return all LPAs for the given user_id
*
* @param string $userId User account ID to fetch LPAs for
* @param string $userId User account ID to fetch LPAs for
* @param bool $filterNotActivated Optional variable to filter LPAs if they are not activated. Defaults to true
*
* @return array An array of LPA data structures containing processed LPA data and metadata
*/
public function getAllForUser(string $userId): array
public function getAllForUser(string $userId, bool $filterNotActivated = true): array
{
// Returns an array of all the LPAs Ids (plus other metadata) in the user's account.
$lpaActorMaps = $this->userLpaActorMapRepository->getUsersLpas($userId);

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

$lpaUids = array_column($lpaActorMaps, 'SiriusUid');

if (empty($lpaUids)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function returns_null_if_lpa_not_already_added_pre_feature()
$this->featureEnabledProphecy->__invoke('save_older_lpa_requests')->willReturn(false);

$this->lpaServiceProphecy
->getAllForUser('12345')
->getAllForUser('12345', false)
->willReturn(
[
$this->userLpaActorToken => [
Expand Down Expand Up @@ -155,7 +155,7 @@ public function returns_lpa_data_if_lpa_is_already_added_pre_feature()
$this->featureEnabledProphecy->__invoke('save_older_lpa_requests')->willReturn(false);

$this->lpaServiceProphecy
->getAllForUser($this->userId)
->getAllForUser($this->userId, false)
->willReturn(
[
'xyz321-987ltc' => [
Expand Down
62 changes: 57 additions & 5 deletions service-api/app/test/AppTest/Service/Lpa/LpaServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public function cannot_get_by_user_token_with_invalid_sirius_uid()
//-------------------------------------------------------------------------
// Test getAllForUser()

private function init_valid_get_all_users()
private function init_valid_get_all_users(bool $includeActivated)
{
$t = new stdClass();

Expand All @@ -373,6 +373,13 @@ private function init_valid_get_all_users()
'SiriusUid' => 'uid-2',
'ActorId' => 2,
'Added' => new DateTime('now')
],
[
'Id' => 'token-3',
'SiriusUid' => 'uid-3',
'ActorId' => 3,
'ActivateBy' => (new DateTime('now'))->add(new \DateInterval('P1Y'))->getTimeStamp(),
'Added' => new DateTime('now')
]
];

Expand All @@ -384,12 +391,12 @@ private function init_valid_get_all_users()
'uid-2' => new Lpa([
'uId' => 'uid-2',
'status' => 'Registered',
], new DateTime()),
], new DateTime())
];

$this->userLpaActorMapInterfaceProphecy->getUsersLpas($t->UserId)->willReturn($t->mapResults);

$this->lpasInterfaceProphecy->lookup(array_column($t->mapResults, 'SiriusUid'))->willReturn($t->lpaResults);
$this->lpasInterfaceProphecy->lookup(['uid-1', 'uid-2'])->willReturn($t->lpaResults);

// check valid lpa
$this->isValidLpaProphecy->__invoke(
Expand All @@ -401,13 +408,26 @@ private function init_valid_get_all_users()
$t->lpaResults['uid-2']->getData()
)->willReturn(true);

if ($includeActivated) {
$t->lpaResults['uid-3'] = new Lpa([
'uId' => 'uid-3',
'status' => 'Registered'
], new DateTime());
$this->lpasInterfaceProphecy->lookup(['uid-1', 'uid-2', 'uid-3'])->willReturn($t->lpaResults);

// check valid lpa
$this->isValidLpaProphecy->__invoke(
$t->lpaResults['uid-3']->getData()
)->willReturn(true);
}

return $t;
}

/** @test */
public function can_get_all_lpas_for_user()
{
$t = $this->init_valid_get_all_users();
$t = $this->init_valid_get_all_users(false);

$service = $this->getLpaService();

Expand All @@ -426,6 +446,38 @@ public function can_get_all_lpas_for_user()
$this->assertArrayHasKey('actor', $result);
$this->assertArrayHasKey('lpa', $result);

$lpa = array_pop($t->lpaResults);
array_pop($t->mapResults); //discard the first lpa with TTL
$map = array_pop($t->mapResults);

$this->assertEquals($map['Id'], $result['user-lpa-actor-token']);
$this->assertEquals($lpa->getLookupTime()->getTimestamp(), strtotime($result['date']));
$this->assertEquals(null, $result['actor']); // We're expecting no actor to have been found
$this->assertEquals($lpa->getData(), $result['lpa']);
}

/** @test */
public function can_get_all_lpas_for_user_including_not_activated()
{
$t = $this->init_valid_get_all_users(true);

$service = $this->getLpaService();

$result = $service->getAllForUser($t->UserId, false);

$this->assertIsArray($result);
$this->assertCount(3, $result);

$result = array_pop($result);

//---

$this->assertIsArray($result);
$this->assertArrayHasKey('user-lpa-actor-token', $result);
$this->assertArrayHasKey('date', $result);
$this->assertArrayHasKey('actor', $result);
$this->assertArrayHasKey('lpa', $result);

$lpa = array_pop($t->lpaResults);
$map = array_pop($t->mapResults);

Expand All @@ -438,7 +490,7 @@ public function can_get_all_lpas_for_user()
/** @test */
public function cannot_get_all_lpas_for_user_when_no_maps_found()
{
$t = $this->init_valid_get_all_users();
$t = $this->init_valid_get_all_users(false);

$service = $this->getLpaService();

Expand Down