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

GetTemplatsAction: Allow to use multiple application process IDs #407

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@

use Civi\Api4\RemoteFundingApplicationProcess;
use Civi\Funding\Api4\Action\Remote\AbstractRemoteFundingAction;
use Civi\Funding\Api4\Action\Traits\ApplicationProcessIdParameterTrait;
use Civi\Funding\Api4\Action\Traits\ApplicationProcessIdParameterOptionalTrait;
use Civi\Funding\Api4\Action\Traits\ApplicationProcessIdsParameterOptionalTrait;

class GetTemplatesAction extends AbstractRemoteFundingAction {

use ApplicationProcessIdParameterTrait;
use ApplicationProcessIdParameterOptionalTrait;

use ApplicationProcessIdsParameterOptionalTrait;

public function __construct() {
parent::__construct(RemoteFundingApplicationProcess::getEntityName(), 'getTemplates');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
/*
* Copyright (C) 2025 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types = 1);

namespace Civi\Funding\Api4\Action\Traits;

/**
* @method int|null getApplicationProcessId()
* @method $this setApplicationProcessId(int|null $applicationProcessId)
*/
trait ApplicationProcessIdParameterOptionalTrait {

/**
* @var int|null
*/
protected ?int $applicationProcessId = NULL;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
* Copyright (C) 2025 SYSTOPIA GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation in version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

declare(strict_types = 1);

namespace Civi\Funding\Api4\Action\Traits;

use Webmozart\Assert\Assert;

/**
* @phpstan-method list<int>|null getApplicationProcessIds()
*/
trait ApplicationProcessIdsParameterOptionalTrait {

/**
* @var array|null
* @phpstan-var list<int>|null
*/
protected ?array $applicationProcessIds = NULL;

/**
* @phpstan-param list<int>|null $ids
*/
public function setApplicationProcessIds(?array $ids): self {
if (NULL === $ids) {
$this->applicationProcessIds = $ids;
}
else {
Assert::allInteger($ids);
$this->applicationProcessIds = array_values($ids);
}

return $this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@
use Civi\Funding\Api4\Action\Remote\ApplicationProcess\GetTemplatesAction;
use Civi\RemoteTools\ActionHandler\ActionHandlerInterface;
use Civi\RemoteTools\Api4\Api4Interface;
use Webmozart\Assert\Assert;

/**
* @phpstan-type applicationTemplateT array{id: int, label: string}
*/
final class GetTemplatesActionHandler implements ActionHandlerInterface {

public const ENTITY_NAME = 'RemoteFundingApplicationProcess';
Expand All @@ -36,14 +40,35 @@ public function __construct(Api4Interface $api4) {
}

/**
* @phpstan-return list<array{id: integer, label: string}>
* @phpstan-return list<applicationTemplateT>|array<int, list<applicationTemplateT>>
*
* @throws \CRM_Core_Exception
*/
public function getTemplates(GetTemplatesAction $action): array {
if (NULL !== $action->getApplicationProcessId()) {
Assert::null(
$action->getApplicationProcessIds(),
'Only "applicationProcessId" or "applicationProcessIds" is allowed'
);

return $this->getSingle($action->getApplicationProcessId());
}
elseif (NULL !== $action->getApplicationProcessIds()) {
return $this->getMultiple($action->getApplicationProcessIds());
}

throw new \InvalidArgumentException('Either "applicationProcessId" or "applicationProcessIds" must be set');
}

/**
* @phpstan-return list<array{id: integer, label: string}>
*
* @throws \CRM_Core_Exception
*/
private function getSingle(int $applicationProcessId) {
$fundingCaseTypeId = $this->api4->execute(FundingApplicationProcess::getEntityName(), 'get', [
'select' => ['funding_case_id.funding_case_type_id'],
'where' => [['id', '=', $action->getApplicationProcessId()]],
'where' => [['id', '=', $applicationProcessId]],
])->first()['funding_case_id.funding_case_type_id'] ?? NULL;

if (NULL === $fundingCaseTypeId) {
Expand All @@ -60,4 +85,43 @@ public function getTemplates(GetTemplatesAction $action): array {
return $templates;
}

/**
* @phpstan-param list<int> $applicationProcessIds
*
* @phpstan-return array<int, list<applicationTemplateT>>
*
* @throws \CRM_Core_Exception
*/
private function getMultiple(array $applicationProcessIds): array {
if ([] === $applicationProcessIds) {
return [];
}

$result = array_fill_keys($applicationProcessIds, []);
$joinRecords = $this->api4->execute(FundingApplicationProcess::getEntityName(), 'get', [
'select' => [
'id',
'tpl.id',
'tpl.label',
],
'where' => [
['id', 'IN', $applicationProcessIds],
],
'join' => [
[
'FundingApplicationCiviOfficeTemplate AS tpl',
'INNER',
['funding_case_id.funding_case_type_id', '=', 'tpl.case_type_id'],
],
],
]);

/** @phpstan-var array{id: int, 'tpl.id': int, 'tpl.label': string} $joinRecord */
foreach ($joinRecords as $joinRecord) {
$result[$joinRecord['id']][] = ['id' => $joinRecord['tpl.id'], 'label' => $joinRecord['tpl.label']];
}

return $result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected function setUp(): void {
$this->actionHandler = new GetTemplatesActionHandler($this->api4Mock);
}

public function testGetTemplates(): void {
public function testGetTemplatesSingle(): void {
$series = [
[
[
Expand Down Expand Up @@ -91,7 +91,7 @@ public function testGetTemplates(): void {
static::assertSame([['id' => 3, 'label' => 'test']], $this->actionHandler->getTemplates($action));
}

public function testGetTemplatesNoApplicationProcess(): void {
public function testGetTemplatesSingleNoApplicationProcess(): void {
$this->api4Mock->method('execute')
->with(FundingApplicationProcess::getEntityName(), 'get', [
'select' => ['funding_case_id.funding_case_type_id'],
Expand All @@ -104,4 +104,65 @@ public function testGetTemplatesNoApplicationProcess(): void {
static::assertSame([], $this->actionHandler->getTemplates($action));
}

public function testGetTemplatesMulti(): void {
$series = [
[
[
FundingApplicationProcess::getEntityName(),
'get',
[
'select' => ['id', 'tpl.id', 'tpl.label'],
'where' => [['id', 'IN', [2, 3]]],
'join' => [
[
'FundingApplicationCiviOfficeTemplate AS tpl',
'INNER',
['funding_case_id.funding_case_type_id', '=', 'tpl.case_type_id'],
],
],
],
],
new Result([
['id' => 2, 'tpl.id' => 4, 'tpl.label' => 'test1'],
['id' => 2, 'tpl.id' => 5, 'tpl.label' => 'test2'],
]),
],
];

$this->api4Mock->method('execute')->willReturnCallback(function (...$args) use (&$series) {
// @phpstan-ignore-next-line
[$expectedArgs, $return] = array_shift($series);
static::assertEquals($expectedArgs, $args);

return $return;
});

$action = $this->createApi4ActionMock(GetTemplatesAction::class)
->setApplicationProcessIds([2, 3]);

static::assertSame([
2 => [
['id' => 4, 'label' => 'test1'],
['id' => 5, 'label' => 'test2'],
],
3 => [],
], $this->actionHandler->getTemplates($action));
}

public function testGetTemplatesNoId(): void {
$action = $this->createApi4ActionMock(GetTemplatesAction::class);

$this->expectException(\InvalidArgumentException::class);
$this->actionHandler->getTemplates($action);
}

public function testGetTemplatesSingleAndMulti(): void {
$action = $this->createApi4ActionMock(GetTemplatesAction::class)
->setApplicationProcessId(1)
->setApplicationProcessIds([2, 3]);

$this->expectException(\InvalidArgumentException::class);
$this->actionHandler->getTemplates($action);
}

}
Loading