diff --git a/Installation/Action/waar.WaardepapierenZaakAction.action.json b/Installation/Action/waar.WaardepapierenZaakAction.action.json index 0fcc504..bfef037 100644 --- a/Installation/Action/waar.WaardepapierenZaakAction.action.json +++ b/Installation/Action/waar.WaardepapierenZaakAction.action.json @@ -2,7 +2,7 @@ "title": "WaardepapierenZaakAction", "$id": "https://waardepapieren.commonground.nl/action/waar.WaardepapierenZaakAction.action.json", "$schema": "https://docs.commongateway.nl/schemas/Action.schema.json", - "version": "0.0.2", + "version": "0.0.3", "listens": ["kiss.notification"], "description": "This is a action to update a zaak with certificate.", "throws": ["send.bericht"], @@ -11,7 +11,7 @@ { "var": "body.kenmerken.zaaktype" }, - "https://open-zaak.test.buren.opengem.nl/catalogi/api/v1/zaaktypen/f0e9be33-10ae-43f1-bc1d-9f1bb4579fd4" + "https://open-zaak.test.buren.opengem.nl/catalogi/api/v1/zaaktypen/d156d2a1-34ea-4a47-ab3f-93ae65732e89" ] }, "class": "CommonGateway\\WaardepapierenBundle\\ActionHandler\\WPZaakHandler", diff --git a/Installation/Mapping/zrc.gebruiksrechtUpstream.mapping.json b/Installation/Mapping/zrc.gebruiksrechtUpstream.mapping.json new file mode 100644 index 0000000..7b19cee --- /dev/null +++ b/Installation/Mapping/zrc.gebruiksrechtUpstream.mapping.json @@ -0,0 +1,12 @@ +{ + "title": "Gebruiksrecht", + "$id": "https://waardepapieren.commongateway.nl/mapping/drc.gebruiksrechtUpstream.mapping.json", + "$schema": "https://docs.commongateway.nl/schemas/Mapping.schema.json", + "version": "0.0.1", + "passTrough": false, + "mapping": { + "informatieobject": "informatieobject._self.synchronizations.0.sourceId", + "startdatum": "startdatum", + "omschrijvingVoorwaarden": "omschrijvingVoorwaarden" + } +} diff --git a/composer.json b/composer.json index 182ad51..ce34cb6 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,9 @@ "php": ">=7.4", "endroid/qr-code-bundle": "3.4", "common-gateway/template-bundle": ">=0.0", - "commongateway/corebundle": "^1.1.98", - "common-gateway/zgw-bundle": "^1.0.49" + "commongateway/corebundle": "^1.1.98", + "common-gateway/zgw-bundle": "^1.0.49", + "common-gateway/kiss-bundle": "^0.1.35" }, "require-dev" : { "symfony/dependency-injection" : "~3.4|~4.1|~5.0" diff --git a/src/Service/WPZaakService.php b/src/Service/WPZaakService.php index 46ebc2c..bfe65db 100644 --- a/src/Service/WPZaakService.php +++ b/src/Service/WPZaakService.php @@ -105,14 +105,18 @@ public function __construct( * * @return bool Whether the synchronization has passed. * - * @throws LoaderError - * @throws SyntaxError + * @throws Exception|LoaderError|SyntaxError */ public function synchronizeUpstream(Synchronization $synchronization): bool { $data = $this->mappingService->mapping($synchronization->getMapping(), $synchronization->getObject()->toArray()); - $response = $this->callService->call($synchronization->getSource(), $synchronization->getEndpoint(), 'POST', ['json' => $data]); + try { + $response = $this->callService->call($synchronization->getSource(), $synchronization->getEndpoint(), 'POST', ['json' => $data]); + } catch (Exception $exception) { + throw new Exception($exception->getMessage()); + } + $updateObject = $this->callService->decodeResponse($synchronization->getSource(), $response); $synchronization->getObject()->hydrate($updateObject); @@ -148,8 +152,9 @@ public function synchronizeUpstream(Synchronization $synchronization): bool * @param ObjectEntity $zaak The case the objects belong to * * @return bool Whether the synchronization has passed. + * @throws Exception */ - public function storeWaardepapierInSourceDRC(ObjectEntity $informatieobject, ObjectEntity $zaakinformatieobject, ObjectEntity $zaak): bool + public function storeWaardepapierInSourceDRC(ObjectEntity $informatieobject, ObjectEntity $zaakinformatieobject, ObjectEntity $gebruiksrecht, ObjectEntity $zaak): bool { if (count($zaak->getSynchronizations()) === 0) { return true; @@ -176,6 +181,21 @@ public function storeWaardepapierInSourceDRC(ObjectEntity $informatieobject, Obj return false; } + $gebruiksrechtSync = new Synchronization(); + $gebruiksrechtSync->setSource($drcSource); + $gebruiksrechtSync->setMapping($this->resourceService->getMapping('https://waardepapieren.commongateway.nl/mapping/drc.gebruiksrechtUpstream.mapping.json', 'common-gateway/waardepapieren-bundle')); + $gebruiksrechtSync->setEndpoint('/gebruiksrechten'); + $gebruiksrechtSync->setObject($gebruiksrecht); + + $this->entityManager->persist($gebruiksrechtSync); + $this->entityManager->flush(); + + $result = $this->synchronizeUpstream($gebruiksrechtSync); + + if ($result === false) { + return false; + } + $zioSync = new Synchronization(); $zioSync->setSource($source); $zioSync->setMapping($this->resourceService->getMapping($this->configuration['zaakInfoMapping'], 'common-gateway/waardepapieren-bundle')); @@ -200,6 +220,7 @@ public function storeWaardepapierInSourceDRC(ObjectEntity $informatieobject, Obj * @param string $informatieobjecttypeUrl The url of the information object type that is related to the case type. * * @return void Whether the synchronization has passed. + * @throws Exception */ public function saveWaardepapierInDRC(string $data, ObjectEntity $zaakObject, string $informatieobjecttypeUrl): void { @@ -229,6 +250,23 @@ public function saveWaardepapierInDRC(string $data, ObjectEntity $zaakObject, st $this->entityManager->persist($informationObject); $this->entityManager->flush(); + $gebruiksrechtSchema = $this->resourceService->getSchema('https://vng.opencatalogi.nl/schemas/drc.gebruiksrecht.schema.json', 'common-gateway/waardepapieren-bundle'); + if ($gebruiksrechtSchema === null) { + return; + } + + $gebruiksrechtArray = [ + 'informatieobject' => $informationObject, + 'startdatum' => $now->format('c'), + 'omschrijvingVoorwaarden' => 'Voorwaarden', + ]; + + $gebruiksrecht = new ObjectEntity($gebruiksrechtSchema); + + $gebruiksrecht->hydrate($gebruiksrechtArray); + $this->entityManager->persist($gebruiksrecht); + $this->entityManager->flush(); + $caseInformationObjectSchema = $this->resourceService->getSchema('https://vng.opencatalogi.nl/schemas/zrc.zaakInformatieObject.schema.json', 'common-gateway/waardepapieren-bundle'); if ($caseInformationObjectSchema === null) { return; @@ -244,7 +282,7 @@ public function saveWaardepapierInDRC(string $data, ObjectEntity $zaakObject, st $this->entityManager->persist($caseInformationObject); $this->entityManager->flush(); - $this->storeWaardepapierInSourceDRC($informationObject, $caseInformationObject, $zaakObject); + $this->storeWaardepapierInSourceDRC($informationObject, $caseInformationObject, $gebruiksrecht, $zaakObject); }//end saveWaardepapierInDRC() @@ -252,15 +290,14 @@ public function saveWaardepapierInDRC(string $data, ObjectEntity $zaakObject, st /** * Gets the zaaktype from the given zaak * - * @param ObjectEntity $zaak The zaak object. - * @param string $objectUrl The url of the zaaktype. - * @param string $schemaRef The reference of the schema - * @param string $endpoint The endpoint + * @param string $objectUrl The url of the zaaktype. + * @param string $schemaRef The reference of the schema + * @param string $endpoint The endpoint * * @return ObjectEntity|null The zaaktype of the given source * @throws Exception */ - public function getZaaktypeSubObjects(ObjectEntity $zaak, string $objectUrl, string $schemaRef, string $endpoint): ?ObjectEntity + public function getZaaktypeSubObjects(string $objectUrl, string $schemaRef, string $endpoint): ?ObjectEntity { // Get zaaktype schema. $schema = $this->resourceService->getSchema($schemaRef, 'common-gateway/waardepapieren-bundle'); @@ -274,6 +311,10 @@ public function getZaaktypeSubObjects(ObjectEntity $zaak, string $objectUrl, str } } + if (isset($objectId) === false) { + return null; + } + // Check if we have a zaaktype with findSyncByObject. $objectSync = $this->syncService->findSyncBySource($source, $schema, $objectId); @@ -304,35 +345,35 @@ public function getZaaktypeSubObjects(ObjectEntity $zaak, string $objectUrl, str /** * Gets the zaaktype from the given zaak * - * @param array $response The response of the call - * @param ObjectEntity $zaak The zaak object + * @param array $response The response of the call * * @return array The zaaktype of the given source + * @throws Exception */ - public function getSubobjects(array $response, ObjectEntity $zaak): array + public function getSubobjects(array $response): array { foreach ($response['informatieobjecttypen'] as $infoObjectType) { - $info = $this->getZaaktypeSubObjects($zaak, $infoObjectType, 'https://vng.opencatalogi.nl/schemas/ztc.informatieObjectType.schema.json', '/informatieobjecttypen'); + $info = $this->getZaaktypeSubObjects($infoObjectType, 'https://vng.opencatalogi.nl/schemas/ztc.informatieObjectType.schema.json', '/informatieobjecttypen'); $response['informatieobjecttypen'][] = $info; } foreach ($response['eigenschappen'] as $eigenschap) { - $eigenschapObject = $this->getZaaktypeSubObjects($zaak, $eigenschap, 'https://vng.opencatalogi.nl/schemas/ztc.eigenschap.schema.json', '/eigenschappen'); + $eigenschapObject = $this->getZaaktypeSubObjects($eigenschap, 'https://vng.opencatalogi.nl/schemas/ztc.eigenschap.schema.json', '/eigenschappen'); $response['eigenschappen'][] = $eigenschapObject; } foreach ($response['roltypen'] as $roltype) { - $roltypeObject = $this->getZaaktypeSubObjects($zaak, $roltype, 'https://vng.opencatalogi.nl/schemas/ztc.rolType.schema.json', '/roltypen'); + $roltypeObject = $this->getZaaktypeSubObjects($roltype, 'https://vng.opencatalogi.nl/schemas/ztc.rolType.schema.json', '/roltypen'); $response['roltypen'][] = $roltypeObject; } foreach ($response['statustypen'] as $statustype) { - $statustypeObject = $this->getZaaktypeSubObjects($zaak, $statustype, 'https://vng.opencatalogi.nl/schemas/ztc.statusType.schema.json', '/statustypen'); + $statustypeObject = $this->getZaaktypeSubObjects($statustype, 'https://vng.opencatalogi.nl/schemas/ztc.statusType.schema.json', '/statustypen'); $response['statustypen'][] = $statustypeObject; } foreach ($response['resultaattypen'] as $resultaattype) { - $resultaattypeObject = $this->getZaaktypeSubObjects($zaak, $resultaattype, 'https://vng.opencatalogi.nl/schemas/ztc.resultaatType.schema.json', '/resultaattype'); + $resultaattypeObject = $this->getZaaktypeSubObjects($resultaattype, 'https://vng.opencatalogi.nl/schemas/ztc.resultaatType.schema.json', '/resultaattypen'); $response['resultaattypen'][] = $resultaattypeObject; } @@ -344,13 +385,12 @@ public function getSubobjects(array $response, ObjectEntity $zaak): array /** * Gets the zaaktype from the given zaak * - * @param ObjectEntity $zaak The zaak object. - * @param string $zaaktypeUrl The url of the zaaktype. + * @param string $zaaktypeUrl The url of the zaaktype. * * @return ObjectEntity|null The zaaktype of the given source * @throws Exception */ - public function getZaaktypeFromSource(ObjectEntity $zaak, string $zaaktypeUrl): ?ObjectEntity + public function getZaaktypeFromSource(string $zaaktypeUrl): ?ObjectEntity { // Get zaaktype schema. $schema = $this->resourceService->getSchema('https://vng.opencatalogi.nl/schemas/ztc.zaakType.schema.json', 'common-gateway/waardepapieren-bundle'); @@ -358,12 +398,17 @@ public function getZaaktypeFromSource(ObjectEntity $zaak, string $zaaktypeUrl): // Get the uuid from the zaaktype url. $explodedUrl = explode('/', $zaaktypeUrl); + $zaaktypeId = null; foreach ($explodedUrl as $item) { if (Uuid::isValid($item)) { $zaaktypeId = $item; } } + if ($zaaktypeId === null) { + return null; + } + // Check if we have a zaaktype with findSyncByObject. $zaaktypeSync = $this->syncService->findSyncBySource($source, $schema, $zaaktypeId); @@ -375,7 +420,7 @@ public function getZaaktypeFromSource(ObjectEntity $zaak, string $zaaktypeUrl): } $response = $this->callService->decodeResponse($source, $response); - $response = $this->getSubobjects($response, $zaak); + $response = $this->getSubobjects($response); $zaaktypeSync = $this->syncService->synchronize($zaaktypeSync, $response); $this->entityManager->flush(); @@ -426,9 +471,7 @@ public function storeInSourceZRC(ObjectEntity $resultaat, ObjectEntity $status, $this->entityManager->persist($statusSync); $this->entityManager->flush(); - $result = $this->synchronizeUpstream($statusSync); - - return $result; + return $this->synchronizeUpstream($statusSync); }//end storeInSourceZRC() @@ -518,10 +561,11 @@ public function saveInZRC(ObjectEntity $zaakObject, ObjectEntity $zaaktype): voi */ public function getZaakFromSource(ObjectEntity $zaak): ?ObjectEntity { - $source = $this->resourceService->getSource($this->configuration['zrcSource'], 'common-gateway/waardepapieren-bundle'); + $source = $this->resourceService->getSource($this->configuration['zrcSource'], 'common-gateway/waardepapieren-bundle'); + $zaakSync = $zaak->getSynchronizations()->first(); try { - $response = $this->callService->call($source, '/zaken'); + $response = $this->callService->call($source, '/zaken/'.$zaakSync->getSourceId()); } catch (Exception $exception) { throw new Exception($exception->getMessage()); } @@ -549,6 +593,7 @@ public function getZaakFromSource(ObjectEntity $zaak): ?ObjectEntity * @param array $configuration Configuration for the Action. * * @return array $this->data Zaak which we updated with new data + * @throws Exception */ public function wpZaakHandler(array $data, array $configuration): array { @@ -565,12 +610,17 @@ public function wpZaakHandler(array $data, array $configuration): array $zaakUrl = $this->data['body']['resourceUrl']; // Get the uuid from the zaaktype url. $explodedUrl = explode('/', $zaakUrl); + $zaakId = null; foreach ($explodedUrl as $item) { if (Uuid::isValid($item)) { $zaakId = $item; } } + if ($zaakId === null) { + return $this->data; + } + // Find the zaak object through the source and sourceId. $zaakSync = $this->syncService->findSyncBySource($source, $schema, $zaakId); if (($zaakObject = $zaakSync->getObject()) === null) { @@ -579,19 +629,30 @@ public function wpZaakHandler(array $data, array $configuration): array $zaak = $zaakObject->toArray(['embedded' => true]); - // Get the zaaktype from the source with the url from the zaak. - $zaaktype = $zaakObject->getValue('zaaktype'); - $zaaktypeSync = $zaaktype->getSynchronizations()->first(); - $zaaktypeUrl = $zaaktypeSync->getSourceId(); + if (is_string($zaak['zaaktype']) === true) { + $zaaktypeUrl = $zaak['zaaktype']; + } + + if (is_string($zaak['zaaktype']) === false) { + // Get the zaaktype from the source with the url from the zaak. + $zaaktype = $zaakObject->getValue('zaaktype'); + $zaaktypeSync = $zaaktype->getSynchronizations()->first(); + $zaaktypeUrl = $zaaktypeSync->getSourceId(); + } + + if (isset($zaaktypeUrl) === false) { + return $this->data; + } - $zaaktype = $this->getZaaktypeFromSource($zaakObject, $zaaktypeUrl); + $zaaktype = $this->getZaaktypeFromSource($zaaktypeUrl); // Get the informatieobjecttypen of the zaaktype to set to the enkelvoudiginformatieobject. $informatieobjecttypen = $zaaktype->getValue('informatieobjecttypen'); $informatieobjecttypeUrl = $informatieobjecttypen[0]->getValue('url'); // TODO: how do we know which we need to get? // Fill certificate with persons information and/or zaak. - $certificate = $this->downloadService->downloadPdf($zaak); + $message['message'] = 'Waardepapier aangemaakt voor zaak met id: '.$zaakObject->getId()->toString(); + $certificate = $this->downloadService->downloadPdf($message); // Store waardepapier in DRC source. $this->saveWaardepapierInDRC($certificate, $zaakObject, $informatieobjecttypeUrl); @@ -603,10 +664,8 @@ public function wpZaakHandler(array $data, array $configuration): array $zaak = $this->getZaakFromSource($zaakObject); // Set the zaak as response in the dataArray response. - $dataArray['response'] = new Response(json_encode($zaak), 200); + $this->data['response'] = new Response(json_encode($zaak->toArray()), 200); - // Create a certificate for the case. - // $this->waardepapierService->waardepapierHandler($dataArray, $this->configuration); return $this->data; }//end wpZaakHandler()