Skip to content

Commit

Permalink
AUT-3504: add programBatchCallbackDone endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
halaz-lazlo committed Oct 28, 2024
1 parent be8a968 commit 8a90370
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 13 deletions.
5 changes: 5 additions & 0 deletions src/Suite/Api/AC/EndPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public function programCallbackDoneUrl(int $customerId, string $triggerId): stri
return "{$this->apiBaseUrl}/{$customerId}/ac/programs/callbacks/{$triggerId}";
}

public function programBatchCallbackDoneUrl(int $customerId): string
{
return "{$this->apiBaseUrl}/{$customerId}/ac/programs/callbacks";
}

public function programCallbackCancelUrl(int $customerId, string $triggerId): string
{
return "{$this->apiBaseUrl}/{$customerId}/ac/programs/callbacks/{$triggerId}/cancel";
Expand Down
60 changes: 47 additions & 13 deletions src/Suite/Api/AC/Program.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,75 @@ public function __construct(Client $apiClient, EndPoints $endPoints)

public function programCallbackWithUserId(int $customerId, string $triggerId, int $userId)
{
$this->programCallbackDone($customerId, $triggerId, $userId, null);
$this->sendRequest(
$this->endPoints->programCallbackDoneUrl($customerId, $triggerId),
$this->createPostData($userId, null)
);
}

public function programCallbackWithListId(int $customerId, string $triggerId, int $listId)
{
$this->programCallbackDone($customerId, $triggerId, null, $listId);
$this->sendRequest(
$this->endPoints->programCallbackDoneUrl($customerId, $triggerId),
$this->createPostData(null, $listId)
);
}

public function programCallbackCancel(int $customerId, string $triggerId)
{
$this->sendRequest(
$this->endPoints->programCallbackCancelUrl($customerId, $triggerId),
null,
null
$this->createPostData(null, null)
);
}

private function programCallbackDone(int $customerId, string $triggerId, $userId, $listId)
/**
* Example for single-user use-case:
* [
* ['trigger_id' => 'a', 'user_id' => 1],
* ['trigger_id' => 'b', 'user_id' => 2]
* ]
*
* Example for user-list use-case:
* [
* ['trigger_id' => 'a', 'list_id' => 1],
* ['trigger_id' => 'b', 'list_id' => 2]
* ]
*
* Example for mixed use-case 1 (the not used participant can be omitted):
* [
* ['trigger_id' => 'a', 'list_id' => 1],
* ['trigger_id' => 'b', 'user_id' => 1]
* ]
*
* Example for mixed use-case 2:
* [
* ['trigger_id' => 'a', 'user_id' => 0, 'list_id' => 1],
* ['trigger_id' => 'b', 'user_id' => 1, 'list_id' => 0]
* ]
*/
public function programBatchCallbackDone(int $customerId, array $triggers): void
{
$this->sendRequest(
$this->endPoints->programCallbackDoneUrl($customerId, $triggerId),
$userId,
$listId
$this->endPoints->programBatchCallbackDoneUrl($customerId),
$triggers
);
}

private function sendRequest(string $url, $userId, $listId)
private function sendRequest(string $url, $postData)
{
try {
$this->apiClient->post($url, [
'user_id' => $userId,
'list_id' => $listId,
]);
$this->apiClient->post($url, $postData);
} catch (Error $ex) {
throw new RequestFailed('Program callback failed: ' . $ex->getMessage(), $ex->getCode(), $ex);
}
}

private function createPostData($userId, $listId): array
{
return [
'user_id' => $userId,
'list_id' => $listId,
];
}
}
23 changes: 23 additions & 0 deletions test/unit/Suite/Api/AC/ProgramTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,29 @@ public function programCallbackWithListId_postThrowsError_ThrowsRequestFailExcep
$this->program->programCallbackWithListId($this->customerId, self::TRIGGER_ID, self::LIST_ID);
}

/**
* @test
*/
public function programBatchCallbackDone_CalledWithCorrectParameters()
{
$postParams = [
'this' => 'is',
'the' => 'callback',
'post' => 'params',
];

$this->apiClient
->expects($this->once())
->method('post')
->with(
$this->endPoints->programBatchCallbackDoneUrl($this->customerId),
$postParams
)
->willReturn($this->apiSuccess());

$this->program->programBatchCallbackDone($this->customerId, $postParams);
}

/**
* @test
*/
Expand Down

0 comments on commit 8a90370

Please sign in to comment.