From 7bd7692a171aeb874fd8696ab6e77b6de420b94d Mon Sep 17 00:00:00 2001 From: Barry Brands Date: Mon, 7 Oct 2024 10:48:13 +0200 Subject: [PATCH] Added code for finding the right task --- .../xxllnctoktb.syncTaskToTaak.action.json | 3 +- .../NotificationToTaakHandler.php | 78 ++++++++++ src/Service/NotificationToTaakService.php | 135 ++++++++++++++++++ 3 files changed, 215 insertions(+), 1 deletion(-) create mode 100644 src/ActionHandler/NotificationToTaakHandler.php create mode 100644 src/Service/NotificationToTaakService.php diff --git a/Installation/Action/xxllnctoktb.syncTaskToTaak.action.json b/Installation/Action/xxllnctoktb.syncTaskToTaak.action.json index f12f4d7..cf5a7e7 100644 --- a/Installation/Action/xxllnctoktb.syncTaskToTaak.action.json +++ b/Installation/Action/xxllnctoktb.syncTaskToTaak.action.json @@ -8,11 +8,12 @@ "conditions": { "!!": { "var": "task_uuid" } }, - "class": "CommonGateway\\CoreBundle\\ActionHandler\\NotificationHandler", + "class": "CommonGateway\\XxllncToKTBBundle\\ActionHandler\\NotificationToTaakHandler", "configuration": { "mapping": "https://commongateway.nl/mapping/xxllnctoktb.TaskToTaak.mapping.json", "schema": "https://commongateway.nl/klant.taak.schema.json", "source": "https://development.zaaksysteem.nl/source/xxllnc.zaaksysteemv2.source.json", + "endpoint": "/api/v2/cm/task/get_task_list", "sourceIdField": "task_uuid", "pluginName": "common-gateway/xxllnc-to-ktb-bundle" } diff --git a/src/ActionHandler/NotificationToTaakHandler.php b/src/ActionHandler/NotificationToTaakHandler.php new file mode 100644 index 0000000..b381914 --- /dev/null +++ b/src/ActionHandler/NotificationToTaakHandler.php @@ -0,0 +1,78 @@ +syncNotificationToTaak. + * + * @author Conduction BV , Barry Brands + * @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 + * + * @category ActionHandler + */ + +namespace CommonGateway\XxllncToKTBBundle\ActionHandler; + +use App\Exception\GatewayException; +use CommonGateway\CoreBundle\ActionHandler\ActionHandlerInterface; +use CommonGateway\XxllncToKTBBundle\Service\NotificationToTaakService as Service; +use Psr\Cache\CacheException; +use Psr\Cache\InvalidArgumentException; +use Respect\Validation\Exceptions\ComponentException; + +class NotificationToTaakHandler implements ActionHandlerInterface +{ + + /** + * Class constructor. + * + * @param Service $service + */ + public function __construct( + private readonly Service $service, + ) + { + }//end __construct() + + + /** + * This function returns the required configuration as + * a [json-schema](https://json-schema.org/) array. + * + * @return array a [json-schema](https://json-schema.org/) that this action should comply to + */ + public function getConfiguration(): array + { + return [ + '$id' => 'https://development.zaaksysteem.nl/schemas/NotificationToTaak.ActionHandler.schema.json', + '$schema' => 'https://docs.commongateway.nl/schemas/ActionHandler.schema.json', + 'title' => 'NotificationToTaak', + 'description' => 'This handler gets throught the notification the task and syncs it to taak', + 'required' => [], + 'properties' => [], + ]; + + }//end getConfiguration() + + + /** + * This function executes the service + * + * @param array $data The data from the call + * @param array $configuration The configuration of the action + * + * @throws GatewayException + * @throws CacheException + * @throws InvalidArgumentException + * @throws ComponentException + * + * @return array + */ + public function run(array $data, array $configuration): array + { + return $this->service->execute($data, $configuration); + + }//end run() + + +}//end class diff --git a/src/Service/NotificationToTaakService.php b/src/Service/NotificationToTaakService.php new file mode 100644 index 0000000..0712cba --- /dev/null +++ b/src/Service/NotificationToTaakService.php @@ -0,0 +1,135 @@ +, Barry Brands + * @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 + * + * @category Service + */ + +namespace CommonGateway\XxllncToKTBBundle\Service; + +use App\Service\SynchronizationService as OldSynchronizationService; +use CommonGateway\CoreBundle\Service\SynchronizationService; +use CommonGateway\CoreBundle\Service\CallService; +use CommonGateway\CoreBundle\Service\MappingService; +use App\Service\GatewayResourceService as ResourceService; +use Doctrine\ORM\EntityManagerInterface; +use App\Entity\Gateway as Source; +use Psr\Log\LoggerInterface; +use Ramsay\Uuid; + +class NotificationToTaakService +{ + + /** + * @var array + */ + private array $configuration; + + /** + * @var array + */ + private array $data; + + /** + * __construct. + */ + public function __construct( + private readonly EntityManagerInterface $entityManager, + private readonly ResourceService $resourceService, + private readonly CallService $callService, + private readonly OldSynchronizationService $oldSynchronizationService, + private readonly SynchronizationService $synchronizationService, + private readonly MappingService $mappingService, + private readonly LoggerInterface $pluginLogger, + ) { + }//end __construct() + + + /** + * Synchronizes a CustomerInteractionBundle taak to the zaaksysteem v2 task equilevant. + * + * Can handle create, update and delete. Prerequisite is that the taak has a zaak that is synchronized as case in the zaaksysteem. + * + * @return array $this->data + */ + private function synchronizeTask(): array + { + $this->pluginLogger->debug('NotificationToTaakService -> synchronizeTask'); + $pluginName = 'common-gateway/xxllnc-to-ktb-bundle'; + + // Get Source zaaksysteem v2. + $source = $this->resourceService->getSource(reference: $this->configuration['source'], pluginName: $pluginName); + if ($source === null) { + return $this->data; + } + + // Get taak schema. + $schema = $this->resourceService->getSchema(reference: $this->configuration['schema'], pluginName: $pluginName); + if ($schema === null) { + return $this->data; + } + + // Get task to taak mapping. + $mapping = $this->resourceService->getMapping(reference: $this->configuration['mapping'], pluginName: $pluginName); + if ($mapping === null) { + return $this->data; + } + + $endpoint = $this->configuration['endpoint'] . "?filter[relationships.case.id]=" . $this->data['case_uuid']; + + // Fetch all tasks of the case + try { + $this->pluginLogger->info("Fetching tasks for case id: {$this->data['case_uuid']}.."); + $response = $this->callService->call($source, $endpoint, 'GET', [], false, false); + $tasks = $this->callService->decodeResponse(source: $source, response: $response); + } catch (Exception $e) { + // isset($this->style) === true && $this->style->error("Failed to fetch case: $caseID, message: {$e->getMessage()}"); + $this->pluginLogger->error("Failed to fetch tasks for case: {$this->data['case_uuid']}, message: {$e->getMessage()}"); + + return null; + }//end try + + // Check if the entity_id is equal to task id + foreach ($tasks as $task) { + if ($this->data['entity_id'] === $task['id']) { + $taskWeNeed = $task; + } + } + + if (isset($taskWeNeed) === false) { + $this->pluginLogger->error("Could not find the correct task ({$this->data['entity_id']}) in the tasks of the case ({$this->data['case_uuid']})"); + + return $this->data; + } + + // Synchronize correct task. + $synchronization = $this->oldSynchronizationService->findSyncBySource(source: $source, entity: $schema, sourceId: $taskWeNeed['id'], endpoint: $endpoint); + $synchronization = $this->oldSynchronizationService->synchronize(synchronization: $synchronization, sourceObject: $taskWeNeed, unsafe: false, mapping: $mapping); + + return $this->data; + }//end synchronizeTask() + + + /** + * Executes synchronizeTaak + * + * @param array $configuration + * @param array $data + * + * @return array $this->synchronizeTaak() + */ + public function execute(array $configuration, array $data): array + { + $this->data = $data; + $this->configuration = $configuration; + + return $this->synchronizeTask(); + }//end execute() + + +}//end class