Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace OCA\SoftwareCatalog\AppInfo;

use OCA\SoftwareCatalog\Service\SoftwareCatalogue\ContactPersonHandler;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
Expand All @@ -28,6 +29,7 @@
use OCA\OpenRegister\Event\ObjectLockedEvent;
use OCA\OpenRegister\Event\ObjectUnlockedEvent;
use OCA\OpenRegister\Event\ObjectRevertedEvent;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IUserManager;
use OCP\IGroupManager;
Expand Down Expand Up @@ -95,7 +97,8 @@ public function register(IRegistrationContext $context): void
$c,
$c->get(IAppManager::class),
$c->get(\Psr\Log\LoggerInterface::class),
$c->get(SymfonyEmailService::class)
$c->get(SymfonyEmailService::class),
$c->get(IConfig::class)
);
});

Expand Down Expand Up @@ -182,7 +185,8 @@ public function register(IRegistrationContext $context): void
$container->get(IAppConfig::class),
$container->get('Psr\Log\LoggerInterface'),
$container->get(SettingsService::class),
$container->get(IDBConnection::class)
$container->get(IDBConnection::class),
$container->get(ContactPersonHandler::class),
);
});

Expand Down
102 changes: 101 additions & 1 deletion lib/Service/OrganizationSyncService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
use OCA\OpenRegister\Service\ObjectService;
use OCA\SoftwareCatalog\Service\OrganisatieService;
use OCA\SoftwareCatalog\Service\ContactpersoonService;
use OCA\SoftwareCatalog\Service\SoftwareCatalogue\ContactPersonHandler;
use OCA\SoftwareCatalog\Service\SymfonyEmailService;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IAppConfig;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -100,6 +102,7 @@ public function __construct(
LoggerInterface $logger,
SettingsService $settingsService,
private IDBConnection $db,
private readonly ContactPersonHandler $contactpersonHandler,
) {
$this->organisatieService = $organisatieService;
$this->contactpersoonService = $contactpersoonService;
Expand Down Expand Up @@ -133,7 +136,7 @@ public function performOrganizationsSync(): array
$qb = $this->db->getQueryBuilder();

$qb->select('o.uuid', $qb->createFunction('json_unquote(json_extract(o.object, \'$.status\')) as status'), 'o2.uuid as oreg_uuid', 'o2.active as active')
->from('openregister_objects', 'o')
->from(from: 'openregister_objects', alias: 'o')
->leftJoin(fromAlias:'o', join: 'openregister_organisations', alias: 'o2', condition: 'o.uuid = o2.uuid')
->where($qb->expr()->eq('o.schema', $qb->createNamedParameter($organizationSchema)))
->andWhere($qb->expr()->eq('o.register', $qb->createNamedParameter($register)))
Expand Down Expand Up @@ -161,6 +164,96 @@ public function performOrganizationsSync(): array
return $stats;
}

public function performContactSync() :array
{
// Check configuration
$voorzieningenConfig = $this->settingsService->getVoorzieningenConfig();
$register = $voorzieningenConfig['register'] ?? '';
// $organizationSchema = $voorzieningenConfig['organisatie_schema'] ?? '';
$contactSchema = $voorzieningenConfig['contactpersoon_schema'] ?? '';

$stats = [
'organizationsProcessed' => 0,
'entitiesCreated' => 0,
'entitiesUpdated' => 0,
'contactPersonsProcessed' => 0,
'usersCreated' => 0,
'usersUpdated' => 0,
'errors' => [],
'startTime' => date('Y-m-d H:i:s'),
'endTime' => null,
'duration' => null
];

$qb = $this->db->getQueryBuilder();

$qb->select(
'o.uuid',
'a.uid',
$qb->createFunction('json_unquote(json_extract(o.object, \'$.e-mailadres\')) as email'),
$qb->createFunction('json_unquote(json_extract(o.object, \'$.username\')) as username')
)
->from('openregister_objects', 'o')
->leftJoin(
fromAlias: 'o',
join: 'accounts_data',
alias: 'a',
condition: 'json_unquote(json_extract(o.object, \'$.e-mailadres\')) = a.value')
->where($qb->expr()->eq('o.register', $qb->createNamedParameter($register)))
->andWhere($qb->expr()->eq('o.schema', $qb->createNamedParameter($contactSchema)))
->andWhere($qb->expr()->isNull($qb->createFunction('json_unquote(json_extract(o.object, \'$.username\'))')));

// var_dump($qb->getSQL());
$contacts = $qb->execute()->fetchAll();

foreach ($contacts as $contact) {
$objectService = \OC::$server->get('OCA\OpenRegister\Service\ObjectService');
$contactEntity = $objectService->find($contact['uuid']);
$contactEntityObject = $contactEntity->getObject();

$contactEntityObject['username'] = $contact['uid'];

if ($contact['uid'] === null) {
$user = $this->contactpersonHandler->createUserAccount($contactEntity);
$contactEntityObject['username'] = $user->getUID();
}

$contactEntity->setObject($contactEntityObject);
$objectService->saveObject(object: $contactEntity, register: $register, schema: $contactSchema);

$stats['contactPersonsProcessed']++;
}

return $stats;
}


public function performUserSync(): array
{
$voorzieningenConfig = $this->settingsService->getVoorzieningenConfig();
$register = $voorzieningenConfig['register'] ?? '';
// $organizationSchema = $voorzieningenConfig['organisatie_schema'] ?? '';
$contactSchema = $voorzieningenConfig['contactpersoon_schema'] ?? '';


$qb = $this->db->getQueryBuilder();
$qb->select('o.uuid', $qb->createFunction('json_unquote(json_extract(`o`.`object`, \'$.username\')) as username'), $qb->createFunction('json_unquote(json_extract(o.object, \'$.organisatie\')) as organisation'), 'oo.users')
->from(from:'openregister_objects', alias: 'o')
->leftJoin(fromAlias: 'o', join: 'openregister_organisations', alias: 'oo', condition: 'oo.uuid = json_unquote(json_extract(o.object, \'$.organisatie\'))')
->where($qb->expr()->eq('o.register', $qb->createNamedParameter($register)))
->andWhere($qb->expr()->orX('o.schema', $qb->createNamedParameter($contactSchema)))
->andWhere($qb->expr()->eq($qb->createFunction('json_contains(oo.users, json_extract(`o`.`object`, \'$.username\'))'), $qb->createNamedParameter(0)));

$sql = $qb->getSQL();
// var_dump($sql);
$users = $qb->execute()->fetchAll();
// var_dump('hello');
foreach($users as $user) {
$this->organisatieService->addUsersToOrganization($user['organisation'], [$user['username']]);
}

return [];
}


/**
Expand Down Expand Up @@ -881,6 +974,9 @@ public function performScheduledSync(int $minutesBack = 10): array
// Perform the core synchronization with time-based filtering
// $syncResults = $this->performFullSync($minutesBack);
$syncResults = $this->performOrganizationsSync();

$syncResults = array_merge($this->performContactSync(), $syncResults);
$this->performUserSync();
// Record the sync time
$this->recordSyncTime();

Expand Down Expand Up @@ -948,6 +1044,10 @@ public function performManualSync(int $minutesBack = 0): array

try {
$syncResults = $this->performOrganizationsSync();

$syncResults = array_merge($this->performContactSync(), $syncResults);

$this->performUserSync();
// die;

// Perform the core synchronization with time-based filtering
Expand Down
Loading