diff --git a/src/Command/ZaakCommand.php b/src/Command/ZaakCommand.php index 1cf1599..c82350b 100644 --- a/src/Command/ZaakCommand.php +++ b/src/Command/ZaakCommand.php @@ -122,7 +122,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int if (isset($zaakId) === true && Uuid::isValid($zaakId) === true ) { - if ($this->zaakService->getZaak($action->getConfiguration(), $zaakId) === true) { + if ($this->zaakService->zaakHandler(['caseId' => $zaakId], $action->getConfiguration()) === true) { return Command::FAILURE; } diff --git a/src/Service/ZaakService.php b/src/Service/ZaakService.php index d567ea0..78c6482 100644 --- a/src/Service/ZaakService.php +++ b/src/Service/ZaakService.php @@ -1,14 +1,4 @@ , Barry Brands - * @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 - * - * @category Service - */ namespace CommonGateway\XxllncZGWBundle\Service; @@ -16,6 +6,7 @@ use App\Entity\ObjectEntity; use App\Event\ActionEvent; use App\Service\SynchronizationService; +use CommonGateway\XxllncZGWBundle\XxllncZGWBundle; use CommonGateway\CoreBundle\Service\GatewayResourceService; use CommonGateway\CoreBundle\Service\MappingService; use CommonGateway\CoreBundle\Service\CallService; @@ -27,49 +18,24 @@ use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Log\LoggerInterface; +/** + * This class handles the synchronizations of xxllnc cases to zgw zrc zaken. + * + * By fetching, mapping and creating synchronizations. + * + * @author Conduction BV , Barry Brands + * @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12 + * + * @category Service + */ class ZaakService { - /** - * @var EntityManagerInterface - */ - private EntityManagerInterface $entityManager; - - /** - * @var SynchronizationService - */ - private SynchronizationService $synchronizationService; - /** * @var SymfonyStyle */ private SymfonyStyle $style; - /** - * @var CallService - */ - private CallService $callService; - - /** - * @var DRCService - */ - private DRCService $drcService; - - /** - * @var ZaakTypeService - */ - private ZaakTypeService $zaakTypeService; - - /** - * @var MappingService - */ - private MappingService $mappingService; - - /** - * @var GatewayResourceService - */ - private GatewayResourceService $resourceService; - /** * @var array */ @@ -85,40 +51,21 @@ class ZaakService */ private ?Source $xxllncAPI; - /** - * @var LoggerInterface - */ - private LoggerInterface $logger; - - /** - * @var EventDispatcherInterface The event dispatcher. - */ - private EventDispatcherInterface $eventDispatcher; - /** * __construct. */ public function __construct( - EntityManagerInterface $entityManager, - SynchronizationService $synchronizationService, - CallService $callService, - DRCService $drcService, - ZaakTypeService $zaakTypeService, - GatewayResourceService $resourceService, - MappingService $mappingService, - LoggerInterface $pluginLogger, - EventDispatcherInterface $eventDispatcher + private readonly EntityManagerInterface $entityManager, + private readonly SynchronizationService $synchronizationService, + private readonly CallService $callService, + private readonly DRCService $drcService, + private readonly ZaakTypeService $zaakTypeService, + private readonly GatewayResourceService $resourceService, + private readonly MappingService $mappingService, + private readonly LoggerInterface $pluginLogger, + private readonly EventDispatcherInterface $eventDispatcher ) { - $this->entityManager = $entityManager; - $this->synchronizationService = $synchronizationService; - $this->callService = $callService; - $this->drcService = $drcService; - $this->zaakTypeService = $zaakTypeService; - $this->resourceService = $resourceService; - $this->mappingService = $mappingService; - $this->logger = $pluginLogger; - $this->eventDispatcher = $eventDispatcher; }//end __construct() @@ -152,20 +99,20 @@ public function getZaakTypeByExtId(string $caseTypeId) { $zaakTypeSchema = $this->resourceService->getSchema( 'https://vng.opencatalogi.nl/schemas/ztc.zaakType.schema.json', - 'common-gateway/xxllnc-zgw-bundle' + XxllncZGWBundle::PLUGIN_NAME ); // Find existing zaaktype $zaakTypeSync = $this->synchronizationService->findSyncBySource($this->xxllncAPI, $zaakTypeSchema, $caseTypeId); if ($zaakTypeSync && $zaakTypeSync->getObject()) { isset($this->style) === true && $this->style->info("ZaakType found with id: {$zaakTypeSync->getObject()->getId()->toString()}."); - $this->logger->info("ZaakType found with id: {$zaakTypeSync->getObject()->getId()->toString()}."); + $this->pluginLogger->info("ZaakType found with id: {$zaakTypeSync->getObject()->getId()->toString()}.", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); return $zaakTypeSync->getObject(); } isset($this->style) === true && $this->style->info("ZaakType not found, trying to fetch and synchronise casetype with id: $caseTypeId.."); - $this->logger->info("ZaakType not found, trying to fetch and synchronise casetype with id: $caseTypeId.."); + $this->pluginLogger->info("ZaakType not found, trying to fetch and synchronise casetype with id: $caseTypeId..", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); // Fetch and create new zaaktype $zaakTypeObject = $this->zaakTypeService->getZaakType($caseTypeId); if ($zaakTypeObject) { @@ -173,7 +120,7 @@ public function getZaakTypeByExtId(string $caseTypeId) } isset($this->style) === true && $this->style->error("Could not find or create ZaakType for id: $caseTypeId"); - $this->logger->error("Could not find or create ZaakType for id: $caseTypeId"); + $this->pluginLogger->error("Could not find or create ZaakType for id: $caseTypeId", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); return null; @@ -192,13 +139,14 @@ private function checkZaakType(array $case) // If no casetype found return null. if (isset($case['instance']['casetype']['reference']) === false) { isset($this->style) === true && $this->style->error("Case has no casetype"); - $this->logger->error("Case has no casetype"); + $this->pluginLogger->error("Case has no casetype", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); return null; } // Get ZGW ZaakType. isset($this->style) === true && $this->style->info("Searching for related ZaakType (xxllnc casetype) with sourceId: {$case['instance']['casetype']['reference']}.."); + $this->pluginLogger->info("Searching for related ZaakType (xxllnc casetype) with sourceId: {$case['instance']['casetype']['reference']}..", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); $zaakTypeObject = $this->getZaakTypeByExtId($case['instance']['casetype']['reference']); if ($zaakTypeObject === null) { return null; @@ -220,12 +168,12 @@ private function getActualDocument(string $documentNumber): array { try { isset($this->style) === true && $this->style->info("Fetching actual document: $documentNumber.."); - $this->logger->info("Fetching actual document: $documentNumber.."); + $this->pluginLogger->info("Fetching actual document: $documentNumber..", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); $response = $this->callService->call($this->xxllncAPI, "/document/get_by_number/$documentNumber", 'GET', [], false, false); return $this->callService->decodeResponse($this->xxllncAPI, $response); } catch (Exception $e) { isset($this->style) === true && $this->style->error("Failed to fetch actual document: $documentNumber, message: {$e->getMessage()}"); - $this->logger->error("Failed to fetch actual document: $documentNumber, message: {$e->getMessage()}"); + $this->pluginLogger->error("Failed to fetch actual document: $documentNumber, message: {$e->getMessage()}", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); return []; } @@ -245,12 +193,12 @@ private function getInhoudDocument(string $documentId, Source $xxllncV2): ?strin { try { isset($this->style) === true && $this->style->info("Fetching inhoud document: $documentId.."); - $this->logger->info("Fetching inhoud document: $documentId.."); + $this->pluginLogger->info("Fetching inhoud document: $documentId..", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); $response = $this->callService->call($xxllncV2, "/document/download_document?id=$documentId", 'GET', [], false, false); return $this->callService->decodeResponse($xxllncV2, $response, 'application/pdf')['base64']; } catch (Exception $e) { isset($this->style) === true && $this->style->error("Failed to fetch inhoud of document: $documentId, message: {$e->getMessage()}"); - $this->logger->error("Failed to fetch inhoud of document: $documentId, message: {$e->getMessage()}"); + $this->pluginLogger->error("Failed to fetch inhoud of document: $documentId, message: {$e->getMessage()}", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); return null; } @@ -267,7 +215,7 @@ private function getInhoudDocument(string $documentId, Source $xxllncV2): ?strin private function getCaseDocuments(string $caseId): array { isset($this->style) === true && $this->style->info("Checking for documents on this case (zaakinformatieobjecten).."); - $this->logger->info("Checking for documents on this case (zaakinformatieobjecten).."); + $this->pluginLogger->info("Checking for documents on this case (zaakinformatieobjecten)..", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); // Need V2 api to fetch document inhoud. $xxllncV2 = $this->resourceService->getSource(($this->configuration['sourceV2'] ?? 'https://development.zaaksysteem.nl/source/xxllnc.zaaksysteemv2.source.json'), 'xxllnc-zgw-bundle'); if ($xxllncV2 === null) { @@ -286,7 +234,7 @@ private function getCaseDocuments(string $caseId): array return $actualDocuments; } catch (Exception $e) { isset($this->style) === true && $this->style->error("Failed to fetch case documents: {$e->getMessage()}"); - $this->logger->error("Failed to fetch case documents: {$e->getMessage()}"); + $this->pluginLogger->error("Failed to fetch case documents: {$e->getMessage()}", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); return []; } @@ -312,7 +260,7 @@ private function createFileEndpoints(ObjectEntity $zaak): void $downloadEndpoint = $this->resourceService->getEndpoint('https://vng.opencatalogi.nl/endpoints/drc.downloadEnkelvoudigInformatieObject.endpoint.json', 'common-gateway/zgw-bundle'); if (isset($downloadEndpoint) === false) { isset($this->style) === true && $this->style->error("Could not find download endpoint with ref: https://vng.opencatalogi.nl/endpoints/drc.downloadEnkelvoudigInformatieObject.endpoint.json."); - $this->logger->error("Could not find download endpoint with ref: https://vng.opencatalogi.nl/endpoints/drc.downloadEnkelvoudigInformatieObject.endpoint.json."); + $this->pluginLogger->error("Could not find download endpoint with ref: https://vng.opencatalogi.nl/endpoints/drc.downloadEnkelvoudigInformatieObject.endpoint.json.", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); return; } @@ -341,24 +289,47 @@ private function createFileEndpoints(ObjectEntity $zaak): void public function syncCase(array $case, bool $flush = true): ?ObjectEntity { // 0. Get required config objects. - $zaakSchema = $this->resourceService->getSchema( - 'https://vng.opencatalogi.nl/schemas/zrc.zaak.schema.json', - 'common-gateway/xxllnc-zgw-bundle' + $zaakReference = 'https://vng.opencatalogi.nl/schemas/zrc.zaak.schema.json'; + $zaakSchema = $this->resourceService->getSchema( + $zaakReference, + XxllncZGWBundle::PLUGIN_NAME ); - $xxllncAPI = $this->resourceService->getSource( - ($this->configuration['source'] ?? 'https://development.zaaksysteem.nl/source/xxllnc.zaaksysteem.source.json'), - 'common-gateway/xxllnc-zgw-bundle' + + if ($zaakSchema === null) { + $this->pluginLogger->error("Zaak schema $zaakReference could not be found or synced, aborting.", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); + + return null; + } + + $xxllncAPIReference = ($this->configuration['source'] ?? 'https://development.zaaksysteem.nl/source/xxllnc.zaaksysteem.source.json'); + $xxllncAPI = $this->resourceService->getSource( + $xxllncAPIReference, + XxllncZGWBundle::PLUGIN_NAME ); - $caseMapping = $this->resourceService->getMapping( - 'https://development.zaaksysteem.nl/mapping/xxllnc.XxllncCaseToZGWZaak.mapping.json', - 'common-gateway/xxllnc-zgw-bundle' + + if ($xxllncAPI === null) { + $this->pluginLogger->error("Zaaksysteem v1 API $xxllncAPIReference could not be found or synced, aborting.", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); + + return null; + } + + $caseMappingReference = 'https://development.zaaksysteem.nl/mapping/xxllnc.XxllncCaseToZGWZaak.mapping.json'; + $caseMapping = $this->resourceService->getMapping( + $caseMappingReference, + XxllncZGWBundle::PLUGIN_NAME ); + if ($caseMapping === null) { + $this->pluginLogger->error("Case mapping $caseMappingReference could not be found or synced, aborting.", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); + + return null; + } + // 1. Check related ZaakType if its already synced, if not sync. $zaakTypeObject = $this->checkZaakType($case); if ($zaakTypeObject instanceof ObjectEntity === false) { isset($this->style) === true && $this->style->error("ZaakType for case {$case['reference']} could not be found or synced, aborting."); - $this->logger->error("ZaakType for case {$case['reference']} could not be found or synced, aborting."); + $this->pluginLogger->error("ZaakType for case {$case['reference']} could not be found or synced, aborting.", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); return null; } @@ -368,7 +339,7 @@ public function syncCase(array $case, bool $flush = true): ?ObjectEntity // 3. Map the case and all its subobjects. isset($this->style) === true && $this->style->info("Mapping case to zaak.."); - $this->logger->info("Mapping case to zaak.."); + $this->pluginLogger->info("Mapping case to zaak..", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); $hydrationService = new HydrationService($this->synchronizationService, $this->entityManager); $caseAndRelatedObjects = array_merge( @@ -384,7 +355,7 @@ public function syncCase(array $case, bool $flush = true): ?ObjectEntity // 4. Check or create synchronization for case and its subobjects. isset($this->style) === true && $this->style->info("Checking subobjects for synchronizations.."); - $this->logger->info("Checking subobjects for synchronizations.."); + $this->pluginLogger->info("Checking subobjects for synchronizations..", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); $zaak = $hydrationService->searchAndReplaceSynchronizations( $zaakArray, @@ -399,7 +370,7 @@ public function syncCase(array $case, bool $flush = true): ?ObjectEntity } isset($this->style) === true && $this->style->info("Zaak object created/updated with id: {$zaak->getId()->toString()}"); - $this->logger->info("Zaak object created/updated with id: {$zaak->getId()->toString()}"); + $this->pluginLogger->info("Zaak object created/updated with id: {$zaak->getId()->toString()}", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); return $zaak; @@ -420,7 +391,7 @@ private function getXxllncAPI() )) === null ) { isset($this->style) === true && $this->style->error("Could not find Source: Xxllnc API"); - $this->logger->error("Could not find Source: Xxllnc API"); + $this->pluginLogger->error("Could not find Source: Xxllnc API", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); return false; } @@ -445,19 +416,19 @@ public function getZaak(array $configuration, string $caseID) try { isset($this->style) === true && $this->style->info("Fetching case: $caseID.."); - $this->logger->info("Fetching case: $caseID.."); + $this->pluginLogger->info("Fetching case: $caseID.."); $response = $this->callService->call($this->xxllncAPI, "/case/$caseID", 'GET', [], false, false); $case = $this->callService->decodeResponse($this->xxllncAPI, $response); } catch (Exception $e) { isset($this->style) === true && $this->style->error("Failed to fetch case: $caseID, message: {$e->getMessage()}"); - $this->logger->error("Failed to fetch case: $caseID, message: {$e->getMessage()}"); + $this->pluginLogger->error("Failed to fetch case: $caseID, message: {$e->getMessage()}"); return null; } isset($this->style) === true && $this->zaakTypeService->setStyle($this->style); isset($this->style) === true && $this->style->info("Succesfully fetched xxllnc case."); - $this->logger->info("Succesfully fetched xxllnc case."); + $this->pluginLogger->info("Succesfully fetched xxllnc case."); return $this->syncCase($case['result']); @@ -470,20 +441,35 @@ public function getZaak(array $configuration, string $caseID) * @param ObjectEntity $zaak The zaak object. * @param string $taakId The taak id. * - * @return ObjectEntity The updated taak object. + * @return ObjectEntity|null The updated taak object. */ - private function updateTaak(ObjectEntity $zaak, string $taakId): ObjectEntity + private function updateTaak(ObjectEntity $zaak, string $taakId): ?ObjectEntity { - $this->logger->info("taakId found in body, trying to update taak with zaak url"); + $this->pluginLogger->info("taakId found in body, trying to update taak with zaak url", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); + $this->style->info("taakId found in body, trying to update taak with zaak url"); $zaak = $this->resourceService->getObject($zaak->getId()->toString(), 'common-gateway/xxllnc-zgw-bundle'); $taak = $this->resourceService->getObject($taakId, 'common-gateway/xxllnc-zgw-bundle'); + if ($taak === null) { + $this->pluginLogger->error("Taak not found with id {$taakId}, can not add zaak and zaaktype url to it", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); + $this->style->error("Taak not found with id {$taakId}, can not add zaak and zaaktype url to it"); + + return null; + } + $taak->setValue('zaak', $zaak->getValue('url')); - $this->entityManager->persist($taak); - $this->entityManager->flush(); - $this->logger->info("Updated taak with zaak url"); + if ($zaak->getValue('zaaktype') !== null && $zaak->getValue('zaaktype')->getValue('url') !== null) { + $zaakTypeUrl = $zaak->getValue('zaaktype')->getValue('url'); + $data = array_merge($taak->getValue('data'), ['zaakTypeUrl' => $zaakTypeUrl]); + $taak->setValue('data', $data); + + $this->entityManager->persist($taak); + $this->entityManager->flush(); + } + $this->pluginLogger->info("Updated taak with zaak url and zaaktype url", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); + $this->style->info("Updated taak with zaak url and zaaktype url"); return $taak; }//end updateTaak() @@ -531,7 +517,7 @@ public function zaakHandler(?array $data = [], ?array $configuration = []) // Fetch the xxllnc cases. isset($this->style) === true && $this->style->info("Fetching xxllnc cases"); - $this->logger->info("Fetching xxllnc cases"); + $this->pluginLogger->info("Fetching xxllnc cases"); $callConfig = []; if (isset($configuration['query']) === true) { @@ -542,13 +528,13 @@ public function zaakHandler(?array $data = [], ?array $configuration = []) $xxllncCases = $this->callService->getAllResults($this->xxllncAPI, '/case', $callConfig, 'result.instance.rows'); } catch (Exception $e) { isset($this->style) === true && $this->style->error("Failed to fetch: {$e->getMessage()}"); - $this->logger->error("Failed to fetch: {$e->getMessage()}"); + $this->pluginLogger->error("Failed to fetch: {$e->getMessage()}", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); return null; } $caseCount = count($xxllncCases); isset($this->style) === true && $this->style->success("Fetched $caseCount cases"); - $this->logger->info("Fetched $caseCount cases"); + $this->pluginLogger->info("Fetched $caseCount cases", ['plugin' => XxllncZGWBundle::PLUGIN_NAME]); foreach ($xxllncCases as $case) { $event = new ActionEvent( diff --git a/src/XxllncZGWBundle.php b/src/XxllncZGWBundle.php index 3ab01cd..04f3163 100644 --- a/src/XxllncZGWBundle.php +++ b/src/XxllncZGWBundle.php @@ -16,6 +16,8 @@ class XxllncZGWBundle extends Bundle { + const PLUGIN_NAME = 'common-gateway/xxllnc-zgw-bundle'; + /** * Returns the path the bundle is in.