Skip to content

Commit

Permalink
Add sync logic for notification
Browse files Browse the repository at this point in the history
  • Loading branch information
rjzondervan committed Oct 8, 2024
1 parent ad00c1d commit ba1ba6f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Installation/Action/syncZaakFromNotification.action.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
6 changes: 3 additions & 3 deletions Installation/Schema/cloudevents.notification.schema.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
9 changes: 4 additions & 5 deletions Installation/installation.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"installationService": "CommonGateway\\VrijBRPToZGWBundle\\Service\\InstallationService",
"applications": [],
"users": [],
"cards": [],
Expand All @@ -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"
]
}
]
}
}
}
57 changes: 50 additions & 7 deletions src/Service/NewSynchronizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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()
Expand All @@ -79,12 +82,13 @@ public function synchronizeFromSource(Synchronization $synchronization, array $s

// create new object if no object exists

Check failure on line 83 in src/Service/NewSynchronizationService.php

View workflow job for this annotation

GitHub Actions / build

Inline comments must start with a capital letter

Check failure on line 83 in src/Service/NewSynchronizationService.php

View workflow job for this annotation

GitHub Actions / build

Inline comments must end in full-stops, exclamation marks, or question marks
if (!$synchronization->getObject()) {

Check failure on line 84 in src/Service/NewSynchronizationService.php

View workflow job for this annotation

GitHub Actions / build

Operator ! prohibited; use === FALSE instead
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 {

Check warning on line 93 in src/Service/NewSynchronizationService.php

View workflow job for this annotation

GitHub Actions / build

The method synchronizeFromSource uses an else expression. Else clauses are basically not necessary and you can simplify the code by not using them.
$oldDateModified = $synchronization->getObject()->getDateModified()->getTimestamp();
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit ba1ba6f

Please sign in to comment.