Skip to content

Commit

Permalink
Merge pull request #52 from ConductionNL/development
Browse files Browse the repository at this point in the history
Development to main
  • Loading branch information
WilcoLouwerse authored Nov 12, 2024
2 parents 955e38c + ec9854b commit e6e0033
Show file tree
Hide file tree
Showing 28 changed files with 1,549 additions and 542 deletions.
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
['name' => 'endpoints#logs', 'url' => '/api/endpoints-logs/{id}', 'verb' => 'GET'],
['name' => 'synchronizations#contracts', 'url' => '/api/synchronizations-contracts/{id}', 'verb' => 'GET'],
['name' => 'synchronizations#logs', 'url' => '/api/synchronizations-logs/{id}', 'verb' => 'GET'],
['name' => 'synchronizations#test', 'url' => '/api/synchronizations-test/{id}', 'verb' => 'POST'],
// Mapping endpoints
['name' => 'mappings#test', 'url' => '/api/mappings/test', 'verb' => 'POST'],
// Running endpoints
Expand Down
34 changes: 9 additions & 25 deletions lib/Action/SynchronizationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,42 +44,25 @@ public function __construct(
*/
public function run(array $argument = []): array
{

$response = [];

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

// We are going to allow for a single synchronization contract to be processed at a time
if (isset($argument['synchronizationContractId']) && is_int((int) $argument['synchronizationContractId'])) {
$response['level'] = 'INFO';
$response['message'] = 'Synchronization single contract: '.$argument['synchronizationContractId'];
$synchronizationContract = $this->synchronizationContractMapper->find((int) $argument['synchronizationContractId']);
if($synchronizationContract === null){
$response['level'] = 'ERROR';
$response['message'] = 'Contract not found: '.$argument['synchronizationContractId'];
return $response;
}
try {
$this->callService->synchronizeContract($synchronization);
} catch (Exception $e) {
$response['level'] = 'ERROR';
$response['message'] = 'Failed to synchronize contract: ' . $e->getMessage();
return $response;
}
return $response;
}

// Let's find a synchronysation
$response['stackTrace'][] = 'Getting synchronization: '.$argument['synchronizationId'];
$synchronization = $this->synchronizationMapper->find((int) $argument['synchronizationId']);
if ($synchronization === null){
$response['level'] = 'WARNING';
$response['message'] = 'Synchronization not found: '.$argument['synchronizationId'];
$response['stackTrace'][] = $response['message'] = 'Synchronization not found: '.$argument['synchronizationId'];
return $response;
}

Expand All @@ -89,11 +72,12 @@ public function run(array $argument = []): array
$objects = $this->synchronizationService->synchronize($synchronization);
} catch (Exception $e) {
$response['level'] = 'ERROR';
$response['message'] = 'Failed to synchronize: ' . $e->getMessage();
$response['stackTrace'][] = $response['message'] = 'Failed to synchronize: ' . $e->getMessage();
return $response;
}

$response['stackTrace'][] = 'Synchronized '.count($objects).' successfully';
$response['level'] = 'INFO';
$response['stackTrace'][] = $response['message'] = 'Synchronized '.count($objects).' successfully';

// Let's report back about what we have just done
return $response;
Expand Down
31 changes: 25 additions & 6 deletions lib/Controller/JobsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace OCA\OpenConnector\Controller;

use Exception;
use OCA\OpenConnector\Service\ObjectService;
use OCA\OpenConnector\Service\SearchService;
use OCA\OpenConnector\Db\Job;
Expand All @@ -14,6 +15,10 @@
use OCP\BackgroundJob\IJobList;
use OCA\OpenConnector\Db\JobLogMapper;
use OCA\OpenConnector\Service\JobService;
use OCP\AppFramework\Db\DoesNotExistException;

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

class JobsController extends Controller
{
Expand All @@ -32,10 +37,12 @@ public function __construct(
private JobLogMapper $jobLogMapper,
private JobService $jobService,
private IJobList $jobList,
private SynchronizationService $synchronizationService,
private SynchronizationMapper $synchronizationMapper
)
{
parent::__construct($appName, $request);
$this->IJobList = $jobList;
$this->jobList = $jobList;
}

/**
Expand Down Expand Up @@ -219,13 +226,25 @@ public function run(int $id): JSONResponse
{
try {
$job = $this->jobMapper->find(id: $id);
if (!$job->getJobListId()) {
return new JSONResponse(data: ['error' => 'Job not scheduled'], statusCode: 404);
}
$this->IJobList->getById($job->getJobListId())->start($this->IJobList);
return new JSONResponse($this->jobLogMapper->getLastCallLog());
} catch (DoesNotExistException $exception) {
return new JSONResponse(data: ['error' => 'Not Found'], statusCode: 404);
}


if ($job->getJobListId() === false) {
return new JSONResponse(data: ['error' => 'Job not scheduled'], statusCode: 404);
}

try {
$this->jobList->getById($job->getJobListId())->start($this->jobList);
$lastLog = $this->jobLogMapper->getLastCallLog();
if ($lastLog !== null) {
return new JSONResponse(data: $lastLog, statusCode: 200);
}

return new JSONResponse(data: ['error' => 'No job log could be found, job did not went succesfully or failed to log anything'], statusCode: 500);
} catch (Exception $exception) {
return new JSONResponse(data: ['error' => $exception->getMessage()], statusCode: 400);
}
}
}
84 changes: 69 additions & 15 deletions lib/Controller/SynchronizationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use OCA\OpenConnector\Service\ObjectService;
use OCA\OpenConnector\Service\SearchService;
use OCA\OpenConnector\Db\Synchronization;
use OCA\OpenConnector\Service\SynchronizationService;
use OCA\OpenConnector\Db\SynchronizationMapper;
use OCA\OpenConnector\Db\SynchronizationContractMapper;
use OCA\OpenConnector\Db\SynchronizationContractLogMapper;
Expand All @@ -13,6 +13,8 @@
use OCP\AppFramework\Http\JSONResponse;
use OCP\IAppConfig;
use OCP\IRequest;
use Exception;
use OCP\AppFramework\Db\DoesNotExistException;

class SynchronizationsController extends Controller
{
Expand All @@ -29,16 +31,17 @@ public function __construct(
private readonly IAppConfig $config,
private readonly SynchronizationMapper $synchronizationMapper,
private readonly SynchronizationContractMapper $synchronizationContractMapper,
private readonly SynchronizationContractLogMapper $synchronizationContractLogMapper
private readonly SynchronizationContractLogMapper $synchronizationContractLogMapper,
private readonly SynchronizationService $synchronizationService
)
{
parent::__construct($appName, $request);
parent::__construct($appName, $request);

}

/**
* Returns the template of the main app's page
*
*
* This method renders the main page of the application, adding any necessary data to the template.
*
* @NoAdminRequired
Expand All @@ -47,17 +50,17 @@ public function __construct(
* @return TemplateResponse The rendered template response
*/
public function page(): TemplateResponse
{
{
return new TemplateResponse(
'openconnector',
'index',
[]
);
}

/**
* Retrieves a list of all synchronizations
*
*
* This method returns a JSON response containing an array of all synchronizations in the system.
*
* @NoAdminRequired
Expand All @@ -79,7 +82,7 @@ public function index(ObjectService $objectService, SearchService $searchService

/**
* Retrieves a single synchronization by its ID
*
*
* This method returns a JSON response containing the details of a specific synchronization.
*
* @NoAdminRequired
Expand All @@ -99,7 +102,7 @@ public function show(string $id): JSONResponse

/**
* Creates a new synchronization
*
*
* This method creates a new synchronization based on POST data.
*
* @NoAdminRequired
Expand All @@ -116,17 +119,17 @@ public function create(): JSONResponse
unset($data[$key]);
}
}

if (isset($data['id'])) {
unset($data['id']);
}

return new JSONResponse($this->synchronizationMapper->createFromArray(object: $data));
}

/**
* Updates an existing synchronization
*
*
* This method updates an existing synchronization based on its ID.
*
* @NoAdminRequired
Expand All @@ -152,7 +155,7 @@ public function update(int $id): JSONResponse

/**
* Deletes a synchronization
*
*
* This method deletes a synchronization based on its ID.
*
* @NoAdminRequired
Expand Down Expand Up @@ -182,7 +185,7 @@ public function destroy(int $id): JSONResponse
public function contracts(int $id): JSONResponse
{
try {
$contracts = $this->synchronizationContractMapper->findAll($null, null, ['synchronization_id' => $id]);
$contracts = $this->synchronizationContractMapper->findAll(null, null, ['synchronization_id' => $id]);
return new JSONResponse($contracts);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Contracts not found'], 404);
Expand All @@ -199,7 +202,7 @@ public function contracts(int $id): JSONResponse
*
* @param int $id The ID of the source to retrieve logs for
* @return JSONResponse A JSON response containing the call logs
*/
*/
public function logs(int $id): JSONResponse
{
try {
Expand All @@ -209,4 +212,55 @@ public function logs(int $id): JSONResponse
return new JSONResponse(['error' => 'Logs not found'], 404);
}
}

/**
* Tests a synchronization
*
* This method tests a synchronization without persisting anything to the database.
*
* @NoAdminRequired
* @NoCSRFRequired
*
* @param int $id The ID of the synchronization
*
* @return JSONResponse A JSON response containing the test results
*
* @example
* Request:
* empty POST
*
* Response:
* {
* "resultObject": {
* "fullName": "John Doe",
* "userAge": 30,
* "contactEmail": "[email protected]"
* },
* "isValid": true,
* "validationErrors": []
* }
*/
public function test(int $id): JSONResponse
{
try {
$synchronization = $this->synchronizationMapper->find(id: $id);
} catch (DoesNotExistException $exception) {
return new JSONResponse(data: ['error' => 'Not Found'], statusCode: 404);
}

// Try to synchronize
try {
$logAndContractArray = $this->synchronizationService->synchronize(synchronization: $synchronization, isTest: true);
// Return the result as a JSON response
return new JSONResponse(data: $logAndContractArray, statusCode: 200);
} catch (Exception $e) {
// If synchronizaiton fails, return an error response
return new JSONResponse([
'error' => 'Synchronization error',
'message' => $e->getMessage()
], 400);
}

return new JSONResponse($resultFromTest, 200);
}
}
Loading

0 comments on commit e6e0033

Please sign in to comment.