major refactor

- add request status to search instance API
- use JSend-type response in elixForms clients
- readjust namespaces
- add more unit tests (manual HTTP requests too)
This commit is contained in:
2026-07-24 14:56:25 +02:00
parent ef53ee8364
commit 33ff4d9577
11 changed files with 315 additions and 129 deletions
+42 -9
View File
@@ -4,9 +4,8 @@ namespace Api\Controllers\V1;
use Api\Core\Config;
use Api\Core\Request;
use Api\Core\Response;
use ElixForms\Auth\ElixFormsApiClient;
use ElixForms\Auth\ElixFormsAuthenticationClient;
use ElixForms\Exceptions\ElixFormsException;
use ElixForms\Clients\ElixFormsApiClient;
use ElixForms\Clients\ElixFormsAuthenticationClient;
class ElixFormsController
{
@@ -30,11 +29,15 @@ class ElixFormsController
$moduleTag = $this->requiredString($query, 'moduleTag');
$fieldName = $this->requiredString($query, 'fieldName');
$fieldValue = $this->requiredString($query, 'fieldValue');
$requestStatuses = $this->requiredString($query, 'requestStatuses');
$exportGroup = $this->optionalNonEmptyString($query, 'exportGroup', 'API');
if ($moduleTag === null || $fieldName === null || $fieldValue === null || $exportGroup === null) {
return $res->json([
'error' => 'moduleTag, fieldName e fieldValue sono obbligatori; exportGroup, se specificato, deve essere una stringa non vuota.'
'status' => 'fail',
'data' => [
'parameters' => 'moduleTag, fieldName e fieldValue sono obbligatori; exportGroup, se specificato, deve essere una stringa non vuota.',
],
], 400);
}
@@ -42,11 +45,31 @@ class ElixFormsController
$password = $this->config->secret('elixforms_api_password');
if (!\is_string($username) || trim($username) === '' || !\is_string($password) || $password === '') {
throw new ElixFormsException('Credenziali elixForms non configurate.');
return $res->json([
'status' => 'error',
'message' => 'Credenziali elixForms non configurate.',
'code' => 500,
], 500);
}
$token = $this->authenticationClient->login($username, $password);
$instances = $this->apiClient->lookupByStatus($moduleTag, $token, $username);
$login = $this->authenticationClient->login($username, $password);
if ($login['status'] !== 'success') {
return $this->respondWithJSendFailure($res, $login);
}
$token = $login['data']['authToken'];
$instancesResponse = $this->apiClient->lookupByStatus(
$moduleTag,
$token,
$username,
$requestStatuses
);
if ($instancesResponse['status'] !== 'success') {
return $this->respondWithJSendFailure($res, $instancesResponse);
}
$instances = $instancesResponse['data'];
$matchingInstances = [];
foreach ($instances as $instance) {
@@ -59,13 +82,18 @@ class ElixFormsController
continue;
}
$exportTags = $this->apiClient->getExportTags(
$exportTagsResponse = $this->apiClient->getExportTags(
$requestId,
$moduleTag,
$token,
$username,
$exportGroup
);
if ($exportTagsResponse['status'] !== 'success') {
return $this->respondWithJSendFailure($res, $exportTagsResponse);
}
$exportTags = $exportTagsResponse['data'];
$exportTagsByName = array_column($exportTags, null, 'name');
if (!\array_key_exists($fieldName, $exportTagsByName)) {
@@ -80,7 +108,12 @@ class ElixFormsController
}
}
return $res->json($matchingInstances);
return $res->json(['status' => 'success', 'data' => $matchingInstances]);
}
private function respondWithJSendFailure(Response $response, array $payload)
{
return $response->json($payload, $payload['status'] === 'fail' ? 400 : 502);
}
private function requiredString(array $values, string $key): ?string