diff --git a/Installation/Action/syncZaakFromNotification.action.json b/Installation/Action/syncZaakFromNotification.action.json index 8375041..16fa13e 100644 --- a/Installation/Action/syncZaakFromNotification.action.json +++ b/Installation/Action/syncZaakFromNotification.action.json @@ -1,6 +1,6 @@ { "title": "Sync Cases from VrijBRP", - "$id": "https://commongateway.nl/action/vrijbrp.synczaken.action.json", + "$id": "https://commongateway.nl/action/vrijbrp.createZaakFromNotification.action.json", "$schema": "https://docs.commongateway.nl/schemas/Action.schema.json", "version": "0.0.1", "listens": [ diff --git a/Installation/Schema/cloudevents.notification.schema.json b/Installation/Schema/cloudevents.notification.schema.json index 4487d66..54a69ce 100644 --- a/Installation/Schema/cloudevents.notification.schema.json +++ b/Installation/Schema/cloudevents.notification.schema.json @@ -1,8 +1,8 @@ { - "$id": "https://commongateway.nl/schema/notifications.notification.schema.json", + "$id": "https://commongateway.nl/schema/vrijbrp.cloudevent.schema.json", "$schema": "https://docs.commongateway.nl/schemas/Entity.schema.json", - "version": "0.1", - "title": "Notification", + "version": "0.2", + "title": "CloudEvent", "description": "A notification object conform ZGW Notification API: https://petstore.swagger.io/?url=https://raw.githubusercontent.com/VNG-Realisatie/notificaties-api/1.0.0/src/openapi.yaml#/notificaties/notificaties_create", "type": "object", "properties": { diff --git a/Installation/installation.json b/Installation/installation.json index ef8fb8f..b058d3d 100644 --- a/Installation/installation.json +++ b/Installation/installation.json @@ -1,5 +1,4 @@ { - "installationService": "CommonGateway\\VrijBRPToZGWBundle\\Service\\InstallationService", "applications": [], "users": [], "cards": [], @@ -8,17 +7,17 @@ "endpoints": { "schemas": [ { - "reference": "https://commongateway.nl/schema/notifications.notification.schema.json", - "description": "An endpoint for Notifications", + "reference": "https://commongateway.nl/schema/vrijbrp.cloudevent.schema.json", + "description": "An endpoint for CloudEvents", "path": "/vrijbrp/cloudevents", "version": "0.0.1", "methods": [ "POST" ], "throws": [ - "cloudevents.notification.created" + "vrijbrp.cloudevent.created" ] } ] } -} \ No newline at end of file +} diff --git a/src/Service/NewSynchronizationService.php b/src/Service/NewSynchronizationService.php index 0df1e23..1a54c7c 100644 --- a/src/Service/NewSynchronizationService.php +++ b/src/Service/NewSynchronizationService.php @@ -7,6 +7,7 @@ use App\Entity\ObjectEntity; use App\Entity\Synchronization; use App\Service\SynchronizationService; +use CommonGateway\CoreBundle\Service\CacheService; use CommonGateway\CoreBundle\Service\CallService; use CommonGateway\CoreBundle\Service\GatewayResourceService; use CommonGateway\CoreBundle\Service\HydrationService; @@ -45,6 +46,7 @@ class NewSynchronizationService * @param EntityManagerInterface $entityManager The Entity Manager * @param MappingService $mappingService The mappingService * @param HydrationService $hydrationService The hydrationService + * @param CacheService $cacheService The Cache Service */ public function __construct( private readonly GatewayResourceService $resourceService, @@ -54,6 +56,7 @@ public function __construct( private readonly EntityManagerInterface $entityManager, private readonly MappingService $mappingService, private readonly HydrationService $hydrationService, + private readonly CacheService $cacheService ) { }//end __construct() @@ -79,12 +82,13 @@ public function synchronizeFromSource(Synchronization $synchronization, array $s // create new object if no object exists if (!$synchronization->getObject()) { - isset($this->io) && $this->io->text('creating new objectEntity'); - $this->synchronizationLogger->info('creating new objectEntity'); - $object = new ObjectEntity($synchronization->getEntity()); - $object->addSynchronization($synchronization); - $this->entityManager->persist($object); - $this->entityManager->persist($synchronization); +// isset($this->io) && $this->io->text('creating new objectEntity'); +// $this->synchronizationLogger->info('creating new objectEntity'); +// $object = new ObjectEntity($synchronization->getEntity()); +// $object->addSynchronization($synchronization); +// $this->entityManager->persist($object); +// var_dump('new'); +// $this->entityManager->persist($synchronization); $oldDateModified = null; } else { $oldDateModified = $synchronization->getObject()->getDateModified()->getTimestamp(); @@ -244,7 +248,46 @@ public function synchronizeCollectionHandler(array $data, array $configuration): public function synchronizeFromNotificationHandler(array $data, array $configuration): array { - var_dump(array_keys($data)); + $source = $this->resourceService->getSource(reference: $configuration['source'], pluginName: "common-gateway/vrijbrp-to-zgw-bundle"); + $schema = $this->resourceService->getSchema(reference: $configuration['schema'], pluginName: "common-gateway/vrijbrp-to-zgw-bundle"); + + if ($source === null) { + return $data; + } + + if ($schema === null) { + return $data; + } + + if (isset($configuration['mapping']) === true) { + $mapping = $this->resourceService->getMapping(reference: $configuration['mapping'], pluginName: "common-gateway/vrijbrp-to-zgw-bundle"); + + if ($mapping === null) { + return $data; + } + } + + $url = $data['body']['data']['resourceUrl']; + $sourceId = $data['body']['data']['zaakId']; + + $length = strlen($url) - strlen($source->getLocation()) - strlen($sourceId) -1; + + $endpoint = substr(string: $url, offset: strlen(string: $source->getLocation()), length: $length); + + $synchronization = $this->syncService->findSyncBySource(source: $source, entity: $schema, sourceId: $sourceId, endpoint: $endpoint); + + if ($synchronization->getMapping() === null && isset($mapping) === true) { + $synchronization->setMapping($mapping); + } + + try { + $this->synchronizeFromSource(synchronization: $synchronization); + $this->entityManager->flush(); + + $this->cacheService->cacheObject($synchronization->getObject()); + } catch (Exception $exception) { + $this->synchronizationLogger->error(message: $exception->getMessage(), context: ['plugin' => 'common-gateway/vrijbrp-to-zgw-bundle', 'trace' => $exception->getTraceAsString()]); + } return $data;