Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/CONNECTOR-37/synchronizations #11

Merged
merged 10 commits into from
Oct 3, 2024
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
32 changes: 21 additions & 11 deletions lib/Action/SynchronizationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace OCA\OpenConnector\Action;

use OCA\OpenConnector\Service\CallService;
use OCA\OpenConnector\Service\SynchronizationService;
use OCA\OpenConnector\Db\SynchronizationMapper;
use OCA\OpenConnector\Db\SynchronizationContractMapper;

Expand All @@ -13,15 +13,15 @@
*/
class SynchronizationAction
{
private CallService $callService;
private SynchronizationService $synchronizationService;
private SynchronizationMapper $synchronizationMapper;
private SynchronizationContractMapper $synchronizationContractMapper;
public function __construct(
CallService $callService,
SynchronizationService $synchronizationService,
SynchronizationMapper $synchronizationMapper,
SynchronizationContractMapper $synchronizationContractMapper,
) {
$this->callService = $callService;
$this->synchronizationService = $synchronizationService;
$this->synchronizationMapper = $synchronizationMapper;
$this->synchronizationContractMapper = $synchronizationContractMapper;
}
Expand All @@ -33,28 +33,38 @@ public function run($argument)
$response = [];

// if we do not have a synchronization Id then everything is wrong
if (!isset($arguments['synchronizationId'])) {
$response['stackTrace'][] = 'Check for a valid synchronization ID';
if (!isset($argument['synchronizationId'])) {
// @todo: implement error handling
$response['level'] = 'WARNING';
$response['message'] = 'No synchronization ID provided';
//$response['stackTrace'][] = 'Check for a valid synchronization ID';
return $response;
}

// We are going to allow for a single synchronization contract to be processed at a time
if (isset($arguments['synchronizationContractId']) && is_int($argument['synchronizationContractId'])) {
if (isset($argument['synchronizationContractId']) && is_int($argument['synchronizationContractId'])) {
$synchronizationContract = $this->synchronizationContractMapper->find($argument['synchronizationContractId']);

//return;
$this->callService->synchronizeContract($synchronization);
return $response;
}

// oke lets synchronyse a source, why not
// Lets find a synchronysation
$response['stackTrace'][] = 'Getting synchronization';
$synchronization = $this->synchronizationMapper->find($argument['synchronizationId']);
if(!$synchronization){
$response['level'] = 'WARNING';
$response['message'] = 'No synchronization found';
return $response;
}

// Doing the synchronization
$response['stackTrace'][] = 'Doing the synchronization';
$this->synchronizationService->synchronize($synchronization);

// @todo: implement this

// Lets report back about what we have just done
return;
return $response;
}

}
2 changes: 1 addition & 1 deletion lib/Db/JobLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class JobLog extends Entity implements JsonSerializable
protected ?array $arguments = null;
protected ?int $executionTime = 3600; // the execution time in seconds
protected ?string $userId = null; // the user which the job is running for security reasons
protected ?array $stackTrace = null; // stack trace
protected ?array $stackTrace = []; // stack trace
protected ?DateTime $expires = null; // when the log will be cleared
protected ?DateTime $lastRun = null; // the last time the job was run
protected ?DateTime $nextRun = null; // the next time the job will be run
Expand Down
15 changes: 8 additions & 7 deletions lib/Service/MappingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use OCA\OpenConnector\Db\Mapping;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\SyntaxError;
//use Twig\Environment;
//use Twig\Error\LoaderError;
//use Twig\Error\SyntaxError;
use Adbar\Dot;

class MappingService
Expand All @@ -17,7 +17,7 @@ class MappingService
*
* @var Environment
*/
private Environment $twig;
//private Environment $twig;

/**
* Setting up the base class with required services.
Expand All @@ -26,9 +26,9 @@ class MappingService
* @param SessionInterface $session The current session
*/
public function __construct(
Environment $twig,
//Environment $twig,
) {
$this->twig = $twig;
//$this->twig = $twig;

}//end __construct()

Expand Down Expand Up @@ -128,7 +128,8 @@ public function mapping(Mapping $mappingObject, array $input, bool $list = false
}

// Render the value from twig.
$dotArray->set($key, $this->twig->createTemplate($value)->render($input));
// $dotArray->set($key, $this->twig->createTemplate($value)->render($input));
$dotArray->set($key, $input);
}

// Unset unwanted key's.
Expand Down
27 changes: 20 additions & 7 deletions lib/Service/SynchronizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OCA\OpenConnector\Service;

use OCA\OpenConnector\Db\Source;
use OCA\OpenConnector\Db\SourceMapper;
use OCA\OpenConnector\Db\Synchronization;
use OCA\OpenConnector\Db\SynchronizationMapper;
use OCA\OpenConnector\Db\SynchronizationContract;
Expand All @@ -11,9 +12,6 @@
use OCA\OpenConnector\Service\MappingService;

use Psr\Container\ContainerInterface;
use Twig\Error\LoaderError;
use Twig\Error\SyntaxError;
use Adbar\Dot;
use DateInterval;
use DateTime;

Expand All @@ -25,14 +23,17 @@ class SynchronizationService
private ContainerInterface $containerInterface;
private Synchronization $synchronization;
private SynchronizationMapper $synchronizationMapper;
private SourceMapper $sourceMapper;
private SynchronizationContractMapper $synchronizationContractMapper;
private ObjectService $objectService;
private Source $source;


public function __construct(
CallService $callService,
MappingService $mappingService,
ContainerInterface $containerInterface,
SourceMapper $sourceMapper,
SynchronizationMapper $synchronizationMapper,
SynchronizationContractMapper $synchronizationContractMapper
) {
Expand All @@ -41,6 +42,7 @@ public function __construct(
$this->containerInterface = $containerInterface;
$this->synchronizationMapper = $synchronizationMapper;
$this->synchronizationContractMapper = $synchronizationContractMapper;
$this->sourceMapper = $sourceMapper;
}

/**
Expand All @@ -52,7 +54,7 @@ public function __construct(
public function synchronize(Synchronization $synchronization)
{
$this->synchronization = $synchronization;
$objectList = [];
$objectList = $this->getAllObjectsFromSource($synchronization);

foreach($objectList as $object) {
// Get the synchronization contract for this object
Expand Down Expand Up @@ -183,20 +185,31 @@ public function updateTarget(SynchronizationContract $synchronizationContract, a
*/
public function getAllObjectsFromSource(Synchronization $synchronization)
{
$objects = [];
switch($type){
case 'register/schema':
// Setup the object service
$this->objectService = $this->containerInterface->get('OCA\OpenRegister\Service\ObjectService');

break;
case 'api':

case 'api':
//@todo: implement
//$this->callService->put($targetObject);
$source = $this->sourceMapper->get($synchronization->getSourceUrl());
$sourceObject = $this->callService->get($source->getUrl());
$objects[] = $this->getAllObjectsFromJson($sourceObject, synchronization, $source);
break;
case 'database':
//@todo: implement
break;
}
return $objects;
}

public function getAllObjectsFromArray(array $sourceObject, Synchronization $synchronization, Source $source)
{

// lekeer hacky (only works on github for now)
//$sourceObject = $this->callService->get($source->getUrl());
return $sourceObject['items'];
}
}
30 changes: 22 additions & 8 deletions src/entities/synchronization/synchronization.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,33 @@ import { TSynchronization } from './synchronization.types'
export const mockSynchronizationData = (): TSynchronization[] => [
{
id: '5137a1e5-b54d-43ad-abd1-4b5bff5fcd3f',
entity: { id: 'entity1', name: 'Entity 1' },
object: { id: 'object1', name: 'Object 1' },
name: 'Synchronization 1',
description: 'Synchronization 1',
sourceId: 'source1',
blocked: false,
tryCounter: 0,
sourceType: 'api',
sourceHash: 'source1',
sourceTargetMapping: 'source1',
sourceConfig: {},
targetId: 'target1',
targetType: 'api',
targetHash: 'target1',
targetSourceMapping: 'target1',
targetConfig: undefined,
},
{
id: '4c3edd34-a90d-4d2a-8894-adb5836ecde8',
entity: { id: 'entity2', name: 'Entity 2' },
object: { id: 'object2', name: 'Object 2' },
name: 'Synchronization 2',
description: 'Synchronization 2',
sourceId: 'source2',
blocked: true,
tryCounter: 3,
sourceType: 'api',
sourceHash: 'source2',
sourceTargetMapping: 'source2',
sourceConfig: {},
targetId: 'target2',
targetType: 'api',
targetHash: 'target2',
targetSourceMapping: '',
targetConfig: undefined,
},
]

Expand Down
61 changes: 25 additions & 36 deletions src/entities/synchronization/synchronization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,34 @@ import { TSynchronization } from './synchronization.types'
export class Synchronization implements TSynchronization {

public id: string
public entity: object
public object: object | null
public action: object | null
public gateway: object | null
public sourceObject: object | null
public endpoint: string | null
public sourceId: string | null
public hash: string | null
public sha: string | null
public blocked: boolean
public sourceLastChanged: string | null
public lastChecked: string | null
public lastSynced: string | null
public dateCreated: string | null
public dateModified: string | null
public tryCounter: number
public dontSyncBefore: string | null
public mapping: object | null
public name: string
public description: string
public sourceId: string
public sourceType: string
public sourceHash: string
public sourceTargetMapping: string
public sourceConfig: object
public targetId: string
public targetType: string
public targetHash: string
public targetSourceMapping: string
public targetConfig: object

constructor(synchronization: TSynchronization) {
this.id = synchronization.id || ''
this.entity = synchronization.entity
this.object = synchronization.object || null
this.action = synchronization.action || null
this.gateway = synchronization.gateway || null
this.sourceObject = synchronization.sourceObject || null
this.endpoint = synchronization.endpoint || null
this.sourceId = synchronization.sourceId || null
this.hash = synchronization.hash || null
this.sha = synchronization.sha || null
this.blocked = synchronization.blocked || false
this.sourceLastChanged = synchronization.sourceLastChanged || null
this.lastChecked = synchronization.lastChecked || null
this.lastSynced = synchronization.lastSynced || null
this.dateCreated = synchronization.dateCreated || null
this.dateModified = synchronization.dateModified || null
this.tryCounter = synchronization.tryCounter || 0
this.dontSyncBefore = synchronization.dontSyncBefore || null
this.mapping = synchronization.mapping || null
this.name = synchronization.name || ''
this.description = synchronization.description || ''
this.sourceId = synchronization.sourceId || ''
this.sourceType = synchronization.sourceType || ''
this.sourceHash = synchronization.sourceHash || ''
this.sourceTargetMapping = synchronization.sourceTargetMapping || ''
this.sourceConfig = synchronization.sourceConfig || {}
this.targetId = synchronization.targetId || ''
this.targetType = synchronization.targetType || ''
this.targetHash = synchronization.targetHash || ''
this.targetSourceMapping = synchronization.targetSourceMapping || ''
this.targetConfig = synchronization.targetConfig || {}

}

public validate(): SafeParseReturnType<TSynchronization, unknown> {
Expand Down
30 changes: 12 additions & 18 deletions src/entities/synchronization/synchronization.types.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
export type TSynchronization = {
id?: string
entity: object
object?: object | null
action?: object | null
gateway?: object | null
sourceObject?: object | null
endpoint?: string | null
sourceId?: string | null
hash?: string | null
sha?: string | null
blocked?: boolean
sourceLastChanged?: string | null
lastChecked?: string | null
lastSynced?: string | null
dateCreated?: string | null
dateModified?: string | null
tryCounter?: number
dontSyncBefore?: string | null
mapping?: object | null
name: string
description: string
sourceId: string
sourceType: string
sourceHash: string
sourceTargetMapping: string
sourceConfig: object
targetId: string
targetType: string
targetHash: string
targetSourceMapping: string
targetConfig: object
}
2 changes: 1 addition & 1 deletion src/modals/Job/DeleteJob.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { jobStore, navigationStore } from '../../store/store.js'
<NcLoadingIcon v-if="loading" :size="20" />
<TrashCanOutline v-if="!loading" :size="20" />
</template>
Verwijderen
Delete
</NcButton>
</template>
</NcDialog>
Expand Down
Loading