Skip to content

Commit

Permalink
Add get permission status service
Browse files Browse the repository at this point in the history
  • Loading branch information
Enver Cigal committed May 26, 2022
1 parent 48f6f1e commit 15fb739
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ You can get changed permission by IYS
IysManager::make()->createPermissionDriver()->getChangedPermissions();
```

You can get permission status by permission model

```php
IysManager::make()->createPermissionDriver()->getPermissionStatus(
Permission::make()
->setConsentDate('2022-02-10 09:50:02')
->setSource(ConsentSourceTypes::MOBILE)
->setRecipient('[email protected]')
->setRecipientType(RecipientTypes::INDIVIDUAL)
->setStatus(StatusTypes::APPROVE)
->setType(PermissionTypes::EMAIL)
);
```

## Testing

``` bash
Expand Down
17 changes: 17 additions & 0 deletions src/Drivers/Permission/PermissionDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ public function getChangedPermissions(?string $after = null, SourceTypes $source
->throw();
}

/**
* @throws RequestException
*/
public function getPermissionStatus(Permission $permission)
{
$permissionData = $permission->toArray();

return Http::timeout($this->timeout)
->withToken($this->bearer)->asJson()->acceptJson()
->post($this->buildUrl("consents/status"), [
"recipient" => $permissionData['recipient'],
"recipientType" => $permissionData['recipientType'],
"type" => $permissionData['type']
])
->throw();
}

private function buildUrl(string $api): string
{
return sprintf('%s/sps/%s/brands/%s/%s', $this->baseUrl, $this->iysCode, $this->branchCode, $api);
Expand Down
68 changes: 68 additions & 0 deletions tests/PermissionDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,72 @@ public function test_throw_exception_get_changed_permissions()

IysManager::make()->createPermissionDriver()->getChangedPermissions();
}

/**
* @throws RequestException
*/
public function test_get_permission_status()
{
$recipient = '[email protected]';

$consentDate = '2020-07-08 07:07:07';

$endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents/status";

$this->createHttpFakeToken();

Http::fake([
$endpoint => Http::response([
'consentDate' => $consentDate,
'source' => ConsentSourceTypes::MOBILE,
'recipientType' => RecipientTypes::INDIVIDUAL,
'status' => StatusTypes::APPROVE,
'type' => PermissionTypes::EMAIL,
'recipient' => $recipient,
'retailerCode' => 55550127,
'creationDate' => '2020-08-06 15:50:23',
'retailerTitle' => 'Test Company',
'retailerAccessCount' => 3,
'transactionId' => 'abc623z3cq4bhac9b88dadd49b767a2322be140a9n9cuc25abf1ac5392c4ca12',
])
]);

IysManager::make()->createPermissionDriver()->getPermissionStatus(Permission::make()
->setConsentDate($consentDate)
->setSource(ConsentSourceTypes::MOBILE)
->setRecipient($recipient)
->setRecipientType(RecipientTypes::INDIVIDUAL)
->setStatus(StatusTypes::APPROVE)
->setType(PermissionTypes::EMAIL));

Http::assertSent(function (Request $request) use ($endpoint, $recipient) {
return $request->url() == $this->url . $endpoint &&
$request['recipient'] == $recipient &&
$request['recipientType'] == RecipientTypes::INDIVIDUAL->value &&
$request['type'] == PermissionTypes::EMAIL->value;
});
}

public function test_throw_exception_get_permission_status()
{
$endpoint = "/sps/$this->iysCode/brands/$this->brandCode/consents/status";

$this->createHttpFakeToken();

Http::fake([
$endpoint => Http::response([
[
"errors" => [
[
"code" => "H097",
"message" => "İzin sorgulama isteği geçerli olmalıdır."
]
]
]], 400),
]);

$this->expectException(RequestException::class);

IysManager::make()->createPermissionDriver()->getChangedPermissions();
}
}

0 comments on commit 15fb739

Please sign in to comment.