Skip to content

Commit

Permalink
Merge pull request #8 from CommonGateway/feature/MF-12/certificate
Browse files Browse the repository at this point in the history
Updated  WPZaakService
  • Loading branch information
smisidjan authored Nov 2, 2023
2 parents ab226ba + a2e2ed0 commit 4355d7f
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 40 deletions.
4 changes: 2 additions & 2 deletions Installation/Action/waar.WaardepapierenZaakAction.action.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -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",
Expand Down
12 changes: 12 additions & 0 deletions Installation/Mapping/zrc.gebruiksrechtUpstream.mapping.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
131 changes: 95 additions & 36 deletions src/Service/WPZaakService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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'));
Expand All @@ -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
{
Expand Down Expand Up @@ -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;
Expand All @@ -244,23 +282,22 @@ 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()


/**
* 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');
Expand All @@ -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);

Expand Down Expand Up @@ -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;
}

Expand All @@ -344,26 +385,30 @@ 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');
$source = $this->resourceService->getSource($this->configuration['ztcSource'], 'common-gateway/waardepapieren-bundle');

// 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);

Expand All @@ -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();

Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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
{
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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()
Expand Down

0 comments on commit 4355d7f

Please sign in to comment.