Merge pull request 'chore: standardize chrono provider' (#2) from chore/standardize-chrono-provider into main
Reviewed-on: #2
This commit was merged in pull request #2.
This commit is contained in:
@@ -1,178 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace KTXM\ChronoManager\Controllers;
|
|
||||||
|
|
||||||
use KTXC\Http\Response\JsonResponse;
|
|
||||||
use KTXC\SessionIdentity;
|
|
||||||
use KTXC\SessionTenant;
|
|
||||||
use KTXF\Controller\ControllerAbstract;
|
|
||||||
use KTXF\Chrono\Collection\ICollectionBase;
|
|
||||||
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use KTXF\Resource\Selector\SourceSelector;
|
|
||||||
use KTXM\ChronoManager\Manager;
|
|
||||||
|
|
||||||
class CollectionController extends ControllerAbstract {
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
private readonly SessionTenant $tenantIdentity,
|
|
||||||
private readonly SessionIdentity $userIdentity,
|
|
||||||
private Manager $chronoManager,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve all collections matching criteria
|
|
||||||
*
|
|
||||||
* @param SourceSelector|null $sources collection sources
|
|
||||||
* @param array|null $filter collection filter options
|
|
||||||
* @param array|null $sort collection sorting options
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/collection/list', name: 'chronomanager.collection.list', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function list(?SourceSelector $sources = null, ?array $filter = null, ?array $sort = null, ?string $uid = null): JsonResponse {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// retrieve collections
|
|
||||||
$responseData = $this->chronoManager->collectionList($tenantId, $userId, $sources, $filter, $sort);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Confirm if specific collections are available for a specific user
|
|
||||||
*
|
|
||||||
* @param SourceSelector $sources collection sources
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/collection/extant', name: 'chronomanager.collection.extant', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function extant(SourceSelector $sources, ?string $uid = null): JsonResponse {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// retrieve collection status
|
|
||||||
$responseData = $this->chronoManager->collectionExtant($tenantId, $userId, $sources);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch a collection
|
|
||||||
*
|
|
||||||
* @param string $provider provider identifier
|
|
||||||
* @param string $service service identifier
|
|
||||||
* @param string|int $identifier collection identifier
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/collection/fetch', name: 'chronomanager.collection.fetch', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function fetch(string $provider, string $service, string|int $identifier, ?string $uid = null): JsonResponse {
|
|
||||||
try {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// retrieve collection
|
|
||||||
$responseData = $this->chronoManager->collectionFetch($tenantId, $userId, $provider, $service, $identifier);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
} catch (InvalidArgumentException $e) {
|
|
||||||
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a collection
|
|
||||||
*
|
|
||||||
* @param string $provider provider identifier
|
|
||||||
* @param string $service service identifier
|
|
||||||
* @param ICollectionBase|array $data collection to create
|
|
||||||
* @param array $options additional options
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/collection/create', name: 'chronomanager.collection.create', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function create(string $provider, string $service, ICollectionBase|array $data, array $options = [], ?string $uid = null): JsonResponse {
|
|
||||||
try {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// create collection
|
|
||||||
$responseData = $this->chronoManager->collectionCreate($tenantId, $userId, $provider, $service, $data, $options);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
} catch (InvalidArgumentException $e) {
|
|
||||||
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Modify an existing collection
|
|
||||||
*
|
|
||||||
* @param string $provider provider identifier
|
|
||||||
* @param string $service service identifier
|
|
||||||
* @param string|int $identifier collection identifier
|
|
||||||
* @param ICollectionBase|array $data collection data
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/collection/modify', name: 'chronomanager.collection.modify', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function modify(string $provider, string $service, string|int $identifier, ICollectionBase|array $data, ?string $uid = null): JsonResponse {
|
|
||||||
try {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// modify collection
|
|
||||||
$responseData = $this->chronoManager->collectionModify($tenantId, $userId, $provider, $service, $identifier, $data);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
} catch (InvalidArgumentException $e) {
|
|
||||||
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete a collection
|
|
||||||
*
|
|
||||||
* @param string $provider provider identifier
|
|
||||||
* @param string $service service identifier
|
|
||||||
* @param string|int $identifier collection identifier
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/collection/destroy', name: 'chronomanager.collection.destroy', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function destroy(string $provider, string $service, string|int $identifier, string|null $uid = null): JsonResponse {
|
|
||||||
try {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// destroy collection
|
|
||||||
$success = $this->chronoManager->collectionDestroy($tenantId, $userId, $provider, $service, $identifier);
|
|
||||||
|
|
||||||
return new JsonResponse(['success' => $success], $success ? JsonResponse::HTTP_OK : JsonResponse::HTTP_NOT_FOUND);
|
|
||||||
} catch (InvalidArgumentException $e) {
|
|
||||||
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
635
lib/Controllers/DefaultController.php
Normal file
635
lib/Controllers/DefaultController.php
Normal file
@@ -0,0 +1,635 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace KTXM\ChronoManager\Controllers;
|
||||||
|
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use KTXC\Http\Response\JsonResponse;
|
||||||
|
use KTXC\SessionIdentity;
|
||||||
|
use KTXC\SessionTenant;
|
||||||
|
use KTXF\Controller\ControllerAbstract;
|
||||||
|
use KTXF\Resource\Selector\SourceSelector;
|
||||||
|
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
||||||
|
use KTXM\ChronoManager\Manager;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
class DefaultController extends ControllerAbstract {
|
||||||
|
|
||||||
|
private const ERR_MISSING_PROVIDER = 'Missing parameter: provider';
|
||||||
|
private const ERR_MISSING_IDENTIFIER = 'Missing parameter: identifier';
|
||||||
|
private const ERR_MISSING_SERVICE = 'Missing parameter: service';
|
||||||
|
private const ERR_MISSING_COLLECTION = 'Missing parameter: collection';
|
||||||
|
private const ERR_MISSING_DATA = 'Missing parameter: data';
|
||||||
|
private const ERR_MISSING_SOURCES = 'Missing parameter: sources';
|
||||||
|
private const ERR_MISSING_IDENTIFIERS = 'Missing parameter: identifiers';
|
||||||
|
private const ERR_INVALID_OPERATION = 'Invalid operation: ';
|
||||||
|
private const ERR_INVALID_PROVIDER = 'Invalid parameter: provider must be a string';
|
||||||
|
private const ERR_INVALID_SERVICE = 'Invalid parameter: service must be a string';
|
||||||
|
private const ERR_INVALID_IDENTIFIER = 'Invalid parameter: identifier must be a string';
|
||||||
|
private const ERR_INVALID_COLLECTION = 'Invalid parameter: collection must be a string or integer';
|
||||||
|
private const ERR_INVALID_SOURCES = 'Invalid parameter: sources must be an array';
|
||||||
|
private const ERR_INVALID_IDENTIFIERS = 'Invalid parameter: identifiers must be an array';
|
||||||
|
private const ERR_INVALID_DATA = 'Invalid parameter: data must be an array';
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private readonly SessionTenant $tenantIdentity,
|
||||||
|
private readonly SessionIdentity $userIdentity,
|
||||||
|
private Manager $chronoManager,
|
||||||
|
private readonly LoggerInterface $logger,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Main API endpoint for mail operations
|
||||||
|
*
|
||||||
|
* Single operation:
|
||||||
|
* {
|
||||||
|
* "version": 1,
|
||||||
|
* "transaction": "tx-1",
|
||||||
|
* "operation": "entity.create",
|
||||||
|
* "data": {...}
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* @return JsonResponse
|
||||||
|
*/
|
||||||
|
#[AuthenticatedRoute('/v1', name: 'chrono.manager.v1', methods: ['POST'])]
|
||||||
|
public function index(
|
||||||
|
int $version,
|
||||||
|
string $transaction,
|
||||||
|
string|null $operation = null,
|
||||||
|
array|null $data = null,
|
||||||
|
string|null $user = null
|
||||||
|
): JsonResponse {
|
||||||
|
|
||||||
|
// authorize request
|
||||||
|
$tenantId = $this->tenantIdentity->identifier();
|
||||||
|
$userId = $this->userIdentity->identifier();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
if ($operation !== null) {
|
||||||
|
$result = $this->processOperation($tenantId, $userId, $operation, $data ?? [], []);
|
||||||
|
return new JsonResponse([
|
||||||
|
'version' => $version,
|
||||||
|
'transaction' => $transaction,
|
||||||
|
'operation' => $operation,
|
||||||
|
'status' => 'success',
|
||||||
|
'data' => $result
|
||||||
|
], JsonResponse::HTTP_OK);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new InvalidArgumentException('Operation must be provided');
|
||||||
|
|
||||||
|
} catch (Throwable $t) {
|
||||||
|
$this->logger->error('Error processing request', ['exception' => $t]);
|
||||||
|
return new JsonResponse([
|
||||||
|
'version' => $version,
|
||||||
|
'transaction' => $transaction,
|
||||||
|
'operation' => $operation,
|
||||||
|
'status' => 'error',
|
||||||
|
'data' => [
|
||||||
|
'code' => $t->getCode(),
|
||||||
|
'message' => $t->getMessage()
|
||||||
|
]
|
||||||
|
], JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process a single operation
|
||||||
|
*/
|
||||||
|
private function processOperation(string $tenantId, string $userId, string $operation, array $data): mixed {
|
||||||
|
return match ($operation) {
|
||||||
|
// Provider operations
|
||||||
|
'provider.list' => $this->providerList($tenantId, $userId, $data),
|
||||||
|
'provider.fetch' => $this->providerFetch($tenantId, $userId, $data),
|
||||||
|
'provider.extant' => $this->providerExtant($tenantId, $userId, $data),
|
||||||
|
|
||||||
|
// Service operations
|
||||||
|
'service.list' => $this->serviceList($tenantId, $userId, $data),
|
||||||
|
'service.fetch' => $this->serviceFetch($tenantId, $userId, $data),
|
||||||
|
'service.extant' => $this->serviceExtant($tenantId, $userId, $data),
|
||||||
|
'service.create' => $this->serviceCreate($tenantId, $userId, $data),
|
||||||
|
'service.update' => $this->serviceUpdate($tenantId, $userId, $data),
|
||||||
|
'service.delete' => $this->serviceDelete($tenantId, $userId, $data),
|
||||||
|
'service.test' => $this->serviceTest($tenantId, $userId, $data),
|
||||||
|
|
||||||
|
// Collection operations
|
||||||
|
'collection.list' => $this->collectionList($tenantId, $userId, $data),
|
||||||
|
'collection.fetch' => $this->collectionFetch($tenantId, $userId, $data),
|
||||||
|
'collection.extant' => $this->collectionExtant($tenantId, $userId, $data),
|
||||||
|
'collection.create' => $this->collectionCreate($tenantId, $userId, $data),
|
||||||
|
'collection.update' => $this->collectionUpdate($tenantId, $userId, $data),
|
||||||
|
'collection.delete' => $this->collectionDelete($tenantId, $userId, $data),
|
||||||
|
|
||||||
|
// Entity operations
|
||||||
|
'entity.list' => $this->entityList($tenantId, $userId, $data),
|
||||||
|
'entity.fetch' => $this->entityFetch($tenantId, $userId, $data),
|
||||||
|
'entity.extant' => $this->entityExtant($tenantId, $userId, $data),
|
||||||
|
'entity.create' => $this->entityCreate($tenantId, $userId, $data),
|
||||||
|
'entity.update' => $this->entityUpdate($tenantId, $userId, $data),
|
||||||
|
'entity.delete' => $this->entityDelete($tenantId, $userId, $data),
|
||||||
|
'entity.delta' => $this->entityDelta($tenantId, $userId, $data),
|
||||||
|
'entity.move' => throw new InvalidArgumentException('Operation not implemented: ' . $operation),
|
||||||
|
'entity.copy' => throw new InvalidArgumentException('Operation not implemented: ' . $operation),
|
||||||
|
|
||||||
|
default => throw new InvalidArgumentException(self::ERR_INVALID_OPERATION . $operation)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== Provider Operations ====================
|
||||||
|
|
||||||
|
private function providerList(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
|
||||||
|
$sources = null;
|
||||||
|
if (isset($data['sources']) && is_array($data['sources'])) {
|
||||||
|
$sources = new SourceSelector();
|
||||||
|
$sources->jsonDeserialize($data['sources']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->providerList($tenantId, $userId, $sources);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function providerFetch(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
|
||||||
|
if (!isset($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->providerFetch($tenantId, $userId, $data['identifier']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function providerExtant(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
|
||||||
|
if (!isset($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_SOURCES);
|
||||||
|
}
|
||||||
|
if (!is_array($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SOURCES);
|
||||||
|
}
|
||||||
|
$sources = new SourceSelector();
|
||||||
|
$sources->jsonDeserialize($data['sources']);
|
||||||
|
|
||||||
|
return $this->chronoManager->providerExtant($tenantId, $userId, $sources);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ==================== Service Operations =====================
|
||||||
|
|
||||||
|
private function serviceList(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
|
||||||
|
$sources = null;
|
||||||
|
if (isset($data['sources']) && is_array($data['sources'])) {
|
||||||
|
$sources = new SourceSelector();
|
||||||
|
$sources->jsonDeserialize($data['sources']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->serviceList($tenantId, $userId, $sources);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function serviceFetch(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
|
||||||
|
if (!isset($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->serviceFetch($tenantId, $userId, $data['provider'], $data['identifier']);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function serviceExtant(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
|
||||||
|
if (!isset($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_SOURCES);
|
||||||
|
}
|
||||||
|
if (!is_array($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SOURCES);
|
||||||
|
}
|
||||||
|
$sources = new SourceSelector();
|
||||||
|
$sources->jsonDeserialize($data['sources']);
|
||||||
|
|
||||||
|
return $this->chronoManager->serviceExtant($tenantId, $userId, $sources);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function serviceCreate(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['data'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_DATA);
|
||||||
|
}
|
||||||
|
if (!is_array($data['data'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->serviceCreate(
|
||||||
|
$tenantId,
|
||||||
|
$userId,
|
||||||
|
$data['provider'],
|
||||||
|
$data['data']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function serviceUpdate(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER);
|
||||||
|
}
|
||||||
|
if (!isset($data['data'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_DATA);
|
||||||
|
}
|
||||||
|
if (!is_array($data['data'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->serviceUpdate(
|
||||||
|
$tenantId,
|
||||||
|
$userId,
|
||||||
|
$data['provider'],
|
||||||
|
$data['identifier'],
|
||||||
|
$data['data']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function serviceDelete(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->serviceDelete(
|
||||||
|
$tenantId,
|
||||||
|
$userId,
|
||||||
|
$data['provider'],
|
||||||
|
$data['identifier']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function serviceTest(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
|
||||||
|
if (!isset($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($data['identifier']) && !isset($data['location']) && !isset($data['identity'])) {
|
||||||
|
throw new InvalidArgumentException('Either a service identifier or location and identity must be provided for service test');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->serviceTest(
|
||||||
|
$tenantId,
|
||||||
|
$userId,
|
||||||
|
$data['provider'],
|
||||||
|
$data['identifier'] ?? null,
|
||||||
|
$data['location'] ?? null,
|
||||||
|
$data['identity'] ?? null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== Collection Operations ====================
|
||||||
|
|
||||||
|
private function collectionList(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
$sources = null;
|
||||||
|
if (isset($data['sources']) && is_array($data['sources'])) {
|
||||||
|
$sources = new SourceSelector();
|
||||||
|
$sources->jsonDeserialize($data['sources']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$filter = $data['filter'] ?? null;
|
||||||
|
$sort = $data['sort'] ?? null;
|
||||||
|
|
||||||
|
return $this->chronoManager->collectionList($tenantId, $userId, $sources, $filter, $sort);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function collectionExtant(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_SOURCES);
|
||||||
|
}
|
||||||
|
if (!is_array($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SOURCES);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sources = new SourceSelector();
|
||||||
|
$sources->jsonDeserialize($data['sources']);
|
||||||
|
|
||||||
|
return $this->chronoManager->collectionExtant($tenantId, $userId, $sources);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function collectionFetch(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_SERVICE);
|
||||||
|
}
|
||||||
|
if (!is_string($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||||
|
}
|
||||||
|
if (!isset($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['identifier']) && !is_int($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_COLLECTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->collectionFetch(
|
||||||
|
$tenantId,
|
||||||
|
$userId,
|
||||||
|
$data['provider'],
|
||||||
|
$data['service'],
|
||||||
|
$data['identifier']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function collectionCreate(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_SERVICE);
|
||||||
|
}
|
||||||
|
if (!is_string($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||||
|
}
|
||||||
|
if (isset($data['collection']) && !is_string($data['collection']) && !is_int($data['collection'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_COLLECTION);
|
||||||
|
}
|
||||||
|
if (!isset($data['properties'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_DATA);
|
||||||
|
}
|
||||||
|
if (!is_array($data['properties'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->collectionCreate(
|
||||||
|
$tenantId,
|
||||||
|
$userId,
|
||||||
|
$data['provider'],
|
||||||
|
$data['service'],
|
||||||
|
$data['collection'] ?? null,
|
||||||
|
$data['properties']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function collectionUpdate(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_SERVICE);
|
||||||
|
}
|
||||||
|
if (!is_string($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||||
|
}
|
||||||
|
if (!isset($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['identifier']) && !is_int($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_COLLECTION);
|
||||||
|
}
|
||||||
|
if (!isset($data['properties'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_DATA);
|
||||||
|
}
|
||||||
|
if (!is_array($data['properties'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_DATA);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->collectionUpdate(
|
||||||
|
$tenantId,
|
||||||
|
$userId,
|
||||||
|
$data['provider'],
|
||||||
|
$data['service'],
|
||||||
|
$data['identifier'],
|
||||||
|
$data['properties']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function collectionDelete(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_SERVICE);
|
||||||
|
}
|
||||||
|
if (!is_string($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||||
|
}
|
||||||
|
if (!isset($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['identifier']) && !is_int($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->collectionDelete(
|
||||||
|
$tenantId,
|
||||||
|
$userId,
|
||||||
|
$data['provider'],
|
||||||
|
$data['service'],
|
||||||
|
$data['identifier'],
|
||||||
|
$data['options'] ?? []
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ==================== Entity Operations ====================
|
||||||
|
|
||||||
|
private function entityList(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_SOURCES);
|
||||||
|
}
|
||||||
|
if (!is_array($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SOURCES);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sources = new SourceSelector();
|
||||||
|
$sources->jsonDeserialize($data['sources']);
|
||||||
|
|
||||||
|
$filter = $data['filter'] ?? null;
|
||||||
|
$sort = $data['sort'] ?? null;
|
||||||
|
$range = $data['range'] ?? null;
|
||||||
|
|
||||||
|
return $this->chronoManager->entityList($tenantId, $userId, $sources, $filter, $sort, $range);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function entityFetch(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_SERVICE);
|
||||||
|
}
|
||||||
|
if (!is_string($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||||
|
}
|
||||||
|
if (!isset($data['collection'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_COLLECTION);
|
||||||
|
}
|
||||||
|
if (!is_string($data['collection']) && !is_int($data['collection'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_COLLECTION);
|
||||||
|
}
|
||||||
|
if (!isset($data['identifiers'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_IDENTIFIERS);
|
||||||
|
}
|
||||||
|
if (!is_array($data['identifiers'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIERS);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->entityFetch(
|
||||||
|
$tenantId,
|
||||||
|
$userId,
|
||||||
|
$data['provider'],
|
||||||
|
$data['service'],
|
||||||
|
$data['collection'],
|
||||||
|
$data['identifiers']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function entityExtant(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_SOURCES);
|
||||||
|
}
|
||||||
|
if (!is_array($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SOURCES);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sources = new SourceSelector();
|
||||||
|
$sources->jsonDeserialize($data['sources']);
|
||||||
|
|
||||||
|
return $this->chronoManager->entityExtant($tenantId, $userId, $sources);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function entityCreate(string $tenantId, string $userId, array $data = []): mixed {
|
||||||
|
|
||||||
|
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['service']) || !is_string($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||||
|
}
|
||||||
|
if (!isset($data['collection'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_COLLECTION);
|
||||||
|
}
|
||||||
|
$properties = $data['properties'] ?? $data['data'] ?? null;
|
||||||
|
if (!is_array($properties)) {
|
||||||
|
throw new InvalidArgumentException('Invalid parameter: properties must be an array');
|
||||||
|
}
|
||||||
|
$options = $data['options'] ?? [];
|
||||||
|
|
||||||
|
return $this->chronoManager->entityCreate($tenantId, $userId, $data['provider'], $data['service'], $data['collection'], $properties, $options);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function entityUpdate(string $tenantId, string $userId, array $data = []): mixed {
|
||||||
|
|
||||||
|
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['service']) || !is_string($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||||
|
}
|
||||||
|
if (!isset($data['collection'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_COLLECTION);
|
||||||
|
}
|
||||||
|
if (!isset($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER);
|
||||||
|
}
|
||||||
|
$properties = $data['properties'] ?? $data['data'] ?? null;
|
||||||
|
if (!is_array($properties)) {
|
||||||
|
throw new InvalidArgumentException('Invalid parameter: properties must be an array');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->entityUpdate($tenantId, $userId, $data['provider'], $data['service'], $data['collection'], $data['identifier'], $properties);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function entityDelete(string $tenantId, string $userId, array $data = []): mixed {
|
||||||
|
|
||||||
|
if (!isset($data['provider']) || !is_string($data['provider'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_PROVIDER);
|
||||||
|
}
|
||||||
|
if (!isset($data['service']) || !is_string($data['service'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SERVICE);
|
||||||
|
}
|
||||||
|
if (!isset($data['collection'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_COLLECTION);
|
||||||
|
}
|
||||||
|
if (!isset($data['identifier'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_IDENTIFIER);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->chronoManager->entityDelete($tenantId, $userId, $data['provider'], $data['service'], $data['collection'], $data['identifier']);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function entityDelta(string $tenantId, string $userId, array $data): mixed {
|
||||||
|
if (!isset($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_MISSING_SOURCES);
|
||||||
|
}
|
||||||
|
if (!is_array($data['sources'])) {
|
||||||
|
throw new InvalidArgumentException(self::ERR_INVALID_SOURCES);
|
||||||
|
}
|
||||||
|
|
||||||
|
$sources = new SourceSelector();
|
||||||
|
$sources->jsonDeserialize($data['sources']);
|
||||||
|
|
||||||
|
return $this->chronoManager->entityDelta($tenantId, $userId, $sources);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,202 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace KTXM\ChronoManager\Controllers;
|
|
||||||
|
|
||||||
use KTXC\Http\Response\JsonResponse;
|
|
||||||
use KTXC\SessionIdentity;
|
|
||||||
use KTXC\SessionTenant;
|
|
||||||
use KTXF\Controller\ControllerAbstract;
|
|
||||||
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use KTXF\Resource\Selector\SourceSelector;
|
|
||||||
use KTXM\ChronoManager\Manager;
|
|
||||||
|
|
||||||
class EntityController extends ControllerAbstract {
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
private readonly SessionTenant $tenantIdentity,
|
|
||||||
private readonly SessionIdentity $userIdentity,
|
|
||||||
private Manager $chronoManager,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* List entities for a specific user
|
|
||||||
*
|
|
||||||
* @param SourceSelector|null $sources entity sources
|
|
||||||
* @param array|null $filter entity filter
|
|
||||||
* @param array|null $sort entity sort
|
|
||||||
* @param array|null $range entity range
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/entity/list', name: 'chronomanager.entity.list', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function list(?SourceSelector $sources = null, ?array $filter = null, ?array $sort = null, ?array $range = null, ?string $uid = null): JsonResponse {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// retrieve entities
|
|
||||||
$responseData = $this->chronoManager->entityList($tenantId, $userId, $sources, $filter, $sort, $range);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delta of entity changes since last request
|
|
||||||
*
|
|
||||||
* @param SourceSelector $sources entity sources
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/entity/delta', name: 'chronomanager.entity.delta', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function delta(SourceSelector $sources, ?string $uid = null): JsonResponse {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// retrieve entity delta
|
|
||||||
$responseData = $this->chronoManager->entityDelta($tenantId, $userId, $sources);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Confirm if specific entities are available for a specific user
|
|
||||||
*
|
|
||||||
* @param SourceSelector $sources entity sources
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/entity/extant', name: 'chronomanager.entity.extant', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function extant(SourceSelector $sources, ?string $uid = null): JsonResponse {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// retrieve entity status
|
|
||||||
$responseData = $this->chronoManager->entityExtant($tenantId, $userId, $sources);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch specific entities from a specific collection
|
|
||||||
*
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
* @param string $provider provider identifier
|
|
||||||
* @param string $service service identifier
|
|
||||||
* @param string|int $collection collection identifier
|
|
||||||
* @param array<string|int> $identifiers entity identifiers
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/entity/fetch', name: 'chronomanager.entity.fetch', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function fetch(string $provider, string $service, string|int $collection, array $identifiers, ?string $uid = null): JsonResponse {
|
|
||||||
try {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// retrieve entities
|
|
||||||
$responseData = $this->chronoManager->entityFetch($tenantId, $userId, $provider, $service, $collection, $identifiers);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
} catch (InvalidArgumentException $e) {
|
|
||||||
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new entity in a collection
|
|
||||||
*
|
|
||||||
* @param string $provider provider identifier
|
|
||||||
* @param string $service service identifier
|
|
||||||
* @param string|int $collection collection identifier
|
|
||||||
* @param array $data entity to create
|
|
||||||
* @param array $options additional options
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/entity/create', name: 'chronomanager.entity.create', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function create(string $provider, string $service, string|int $collection, array $data, array $options = [], ?string $uid = null): JsonResponse {
|
|
||||||
try {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// create entity
|
|
||||||
$responseData = $this->chronoManager->entityCreate($tenantId, $userId, $provider, $service, $collection, $data, $options);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
} catch (InvalidArgumentException $e) {
|
|
||||||
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Modify an existing entity in a collection
|
|
||||||
*
|
|
||||||
* @param string $provider provider identifier
|
|
||||||
* @param string $service service identifier
|
|
||||||
* @param string|int $collection collection identifier
|
|
||||||
* @param string|int $identifier entity identifier
|
|
||||||
* @param array $entity entity with modifications
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/entity/modify', name: 'chronomanager.entity.modify', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function modify(string $provider, string $service, string|int $collection, string|int $identifier, array $data, ?string $uid = null): JsonResponse {
|
|
||||||
try {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// modify entity
|
|
||||||
$responseData = $this->chronoManager->entityModify($tenantId, $userId, $provider, $service, $collection, $identifier, $data);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
} catch (InvalidArgumentException $e) {
|
|
||||||
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete an entity from a collection
|
|
||||||
*
|
|
||||||
* @param string $provider provider identifier
|
|
||||||
* @param string $service service identifier
|
|
||||||
* @param string|int $collection collection identifier
|
|
||||||
* @param string|int $identifier entity identifier
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/entity/destroy', name: 'chronomanager.entity.destroy', methods: ['POST'])]
|
|
||||||
public function destroy(string $provider, string $service, string|int $collection, string|int $identifier, ?string $uid = null): JsonResponse {
|
|
||||||
try {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// destroy entity
|
|
||||||
$success = $this->chronoManager->entityDestroy($tenantId, $userId, $provider, $service, $collection, $identifier);
|
|
||||||
|
|
||||||
return new JsonResponse(['success' => $success], $success ? JsonResponse::HTTP_OK : JsonResponse::HTTP_NOT_FOUND);
|
|
||||||
} catch (InvalidArgumentException $e) {
|
|
||||||
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,50 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace KTXM\ChronoManager\Controllers;
|
|
||||||
|
|
||||||
use KTXC\Http\Response\JsonResponse;
|
|
||||||
use KTXF\Controller\ControllerAbstract;
|
|
||||||
use KTXF\Resource\Selector\SourceSelector;
|
|
||||||
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
|
||||||
use KTXM\ChronoManager\Manager;
|
|
||||||
|
|
||||||
class ProviderController extends ControllerAbstract {
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
private Manager $chronoManager,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve list of available providers
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/provider/list', name: 'chronomanager.provider.list', methods: ['GET'])]
|
|
||||||
|
|
||||||
public function list(): JsonResponse {
|
|
||||||
$providers = $this->chronoManager->providerList();
|
|
||||||
return new JsonResponse($providers, JsonResponse::HTTP_OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Confirm which providers are available
|
|
||||||
*
|
|
||||||
* @param SourceSelector $sources provider sources
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/provider/extant', name: 'chronomanager.provider.extant', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function extant(SourceSelector $sources): JsonResponse {
|
|
||||||
$responseData = $this->chronoManager->providerExtant($sources);
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
|
||||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace KTXM\ChronoManager\Controllers;
|
|
||||||
|
|
||||||
use KTXC\Http\Response\JsonResponse;
|
|
||||||
use KTXC\SessionIdentity;
|
|
||||||
use KTXC\SessionTenant;
|
|
||||||
use KTXF\Controller\ControllerAbstract;
|
|
||||||
use KTXF\Routing\Attributes\AuthenticatedRoute;
|
|
||||||
use InvalidArgumentException;
|
|
||||||
use KTXF\Resource\Selector\SourceSelector;
|
|
||||||
use KTXM\ChronoManager\Manager;
|
|
||||||
|
|
||||||
class ServiceController extends ControllerAbstract {
|
|
||||||
|
|
||||||
public function __construct(
|
|
||||||
private readonly SessionTenant $tenantIdentity,
|
|
||||||
private readonly SessionIdentity $userIdentity,
|
|
||||||
private Manager $chronoManager,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieve services for a specific user
|
|
||||||
*
|
|
||||||
* @param SourceSelector|null $sources service sources
|
|
||||||
* @param array|null $filter service filter options
|
|
||||||
* @param array|null $sort service sorting options
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/service/list', name: 'chronomanager.service.list', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function list(?SourceSelector $sources = null, ?array $filter = null, ?array $sort = null, ?string $uid = null): JsonResponse {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// retrieve services
|
|
||||||
$responseData = $this->chronoManager->serviceList($tenantId, $userId, $sources, $filter, $sort);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Confirm if specific services are available for a specific user
|
|
||||||
*
|
|
||||||
* @param SourceSelector $sources service sources
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/service/extant', name: 'chronomanager.service.extant', methods: ['POST'])]
|
|
||||||
|
|
||||||
public function extant(SourceSelector $sources, ?string $uid = null): JsonResponse {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// retrieve services status
|
|
||||||
$responseData = $this->chronoManager->serviceExtant($tenantId, $userId, $sources);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch specific service for a specific user
|
|
||||||
*
|
|
||||||
* @param string $provider provider identifier
|
|
||||||
* @param string $identifier service identifier
|
|
||||||
* @param string|null $uid user identifier
|
|
||||||
*
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
|
||||||
#[AuthenticatedRoute('/service/fetch', name: 'chronomanager.service.fetch', methods: ['POST'])]
|
|
||||||
public function fetch(string $provider, string $identifier, ?string $uid = null): JsonResponse {
|
|
||||||
try {
|
|
||||||
// authorize request
|
|
||||||
$tenantId = $this->tenantIdentity->identifier();
|
|
||||||
$userId = $this->userIdentity->identifier();
|
|
||||||
// retrieve service
|
|
||||||
$responseData = $this->chronoManager->serviceFetch($tenantId, $userId, $provider, $identifier);
|
|
||||||
|
|
||||||
return new JsonResponse($responseData, JsonResponse::HTTP_OK);
|
|
||||||
} catch (InvalidArgumentException $e) {
|
|
||||||
return new JsonResponse(['error' => $e->getMessage()], JsonResponse::HTTP_BAD_REQUEST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
821
lib/Manager.php
821
lib/Manager.php
File diff suppressed because it is too large
Load Diff
@@ -10,9 +10,9 @@ import { useServicesStore } from '@/stores/servicesStore'
|
|||||||
* It initializes the chronoStore which manages calendars, events, tasks, and journals state.
|
* It initializes the chronoStore which manages calendars, events, tasks, and journals state.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
console.log('[ChronoManager] Booting Chrono Manager module...')
|
console.log('[Chrono Manager] Booting Chrono Manager module...')
|
||||||
|
|
||||||
console.log('[ChronoManager] Chrono Manager module booted successfully')
|
console.log('[Chrono Manager] Chrono Manager module booted successfully')
|
||||||
|
|
||||||
// Export store for external use if needed
|
// Export store for external use if needed
|
||||||
export { useCollectionsStore, useEntitiesStore, useProvidersStore, useServicesStore }
|
export { useCollectionsStore, useEntitiesStore, useProvidersStore, useServicesStore }
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* Class model for Collection Interface
|
* Class model for Collection Interface
|
||||||
*/
|
*/
|
||||||
import type {
|
|
||||||
CollectionInterface,
|
import type { CollectionContentsInterface, CollectionInterface, CollectionPropertiesInterface } from "@/types/collection";
|
||||||
CollectionContentsInterface,
|
|
||||||
CollectionPermissionsInterface,
|
|
||||||
} from "@/types/collection";
|
|
||||||
|
|
||||||
export class CollectionObject implements CollectionInterface {
|
export class CollectionObject implements CollectionInterface {
|
||||||
|
|
||||||
@@ -13,81 +10,195 @@ export class CollectionObject implements CollectionInterface {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this._data = {
|
this._data = {
|
||||||
'@type': 'chrono:collection',
|
provider: '',
|
||||||
provider: null,
|
service: '',
|
||||||
service: null,
|
collection: null,
|
||||||
in: null,
|
identifier: '',
|
||||||
id: null,
|
|
||||||
label: null,
|
|
||||||
description: null,
|
|
||||||
priority: null,
|
|
||||||
visibility: null,
|
|
||||||
color: null,
|
|
||||||
enabled: true,
|
|
||||||
signature: null,
|
signature: null,
|
||||||
permissions: {},
|
created: null,
|
||||||
contents: {},
|
modified: null,
|
||||||
|
properties: {
|
||||||
|
'@type': 'chrono.collection',
|
||||||
|
version: 1,
|
||||||
|
total: 0,
|
||||||
|
contents: {},
|
||||||
|
label: '',
|
||||||
|
description: null,
|
||||||
|
rank: 0,
|
||||||
|
visibility: null,
|
||||||
|
color: null,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fromJson(data: CollectionInterface): CollectionObject {
|
fromJson(data: CollectionInterface): CollectionObject {
|
||||||
this._data = data;
|
this._data = data;
|
||||||
|
if (data.properties) {
|
||||||
|
this._data.properties = new CollectionPropertiesObject().fromJson(data.properties as CollectionPropertiesInterface);
|
||||||
|
} else {
|
||||||
|
this._data.properties = new CollectionPropertiesObject();
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
toJson(): CollectionInterface {
|
toJson(): CollectionInterface {
|
||||||
return this._data;
|
const json = { ...this._data };
|
||||||
|
if (this._data.properties instanceof CollectionPropertiesObject) {
|
||||||
|
json.properties = this._data.properties.toJson();
|
||||||
|
}
|
||||||
|
return json;
|
||||||
}
|
}
|
||||||
|
|
||||||
clone(): CollectionObject {
|
clone(): CollectionObject {
|
||||||
const cloned = new CollectionObject();
|
const cloned = new CollectionObject();
|
||||||
cloned._data = JSON.parse(JSON.stringify(this._data));
|
cloned._data = { ...this._data };
|
||||||
|
cloned._data.properties = this.properties.clone();
|
||||||
return cloned;
|
return cloned;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Properties */
|
/** Immutable Properties */
|
||||||
|
|
||||||
|
get provider(): string {
|
||||||
|
return this._data.provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
get service(): string | number {
|
||||||
|
return this._data.service;
|
||||||
|
}
|
||||||
|
|
||||||
|
get collection(): string | number | null {
|
||||||
|
return this._data.collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
get identifier(): string | number {
|
||||||
|
return this._data.identifier;
|
||||||
|
}
|
||||||
|
|
||||||
|
get signature(): string | null | undefined {
|
||||||
|
return this._data.signature;
|
||||||
|
}
|
||||||
|
|
||||||
|
get created(): string | null | undefined {
|
||||||
|
return this._data.created;
|
||||||
|
}
|
||||||
|
|
||||||
|
get modified(): string | null | undefined {
|
||||||
|
return this._data.modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
get properties(): CollectionPropertiesObject {
|
||||||
|
if (this._data.properties instanceof CollectionPropertiesObject) {
|
||||||
|
return this._data.properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._data.properties) {
|
||||||
|
const hydrated = new CollectionPropertiesObject().fromJson(this._data.properties as CollectionPropertiesInterface);
|
||||||
|
this._data.properties = hydrated;
|
||||||
|
return hydrated;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new CollectionPropertiesObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
set properties(value: CollectionPropertiesObject) {
|
||||||
|
if (value instanceof CollectionPropertiesObject) {
|
||||||
|
this._data.properties = value as any;
|
||||||
|
} else {
|
||||||
|
this._data.properties = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class CollectionPropertiesObject implements CollectionPropertiesInterface {
|
||||||
|
|
||||||
|
_data!: CollectionPropertiesInterface;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this._data = {
|
||||||
|
'@type': 'chrono.collection',
|
||||||
|
version: 1,
|
||||||
|
total: 0,
|
||||||
|
contents: {},
|
||||||
|
label: '',
|
||||||
|
description: null,
|
||||||
|
rank: null,
|
||||||
|
visibility: null,
|
||||||
|
color: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fromJson(data: CollectionPropertiesInterface): CollectionPropertiesObject {
|
||||||
|
this._data = data;
|
||||||
|
|
||||||
|
const raw = this._data as any;
|
||||||
|
if ((!raw.contents || Object.keys(raw.contents).length === 0) && raw.content !== undefined && raw.content !== null) {
|
||||||
|
if (typeof raw.content === 'string') {
|
||||||
|
raw.contents = {
|
||||||
|
event: raw.content === 'event',
|
||||||
|
task: raw.content === 'task',
|
||||||
|
journal: raw.content === 'journal',
|
||||||
|
};
|
||||||
|
} else if (typeof raw.content === 'object') {
|
||||||
|
raw.contents = raw.content;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
toJson(): CollectionPropertiesInterface {
|
||||||
|
return this._data;
|
||||||
|
}
|
||||||
|
|
||||||
|
clone(): CollectionPropertiesObject {
|
||||||
|
const cloned = new CollectionPropertiesObject();
|
||||||
|
cloned._data = { ...this._data };
|
||||||
|
return cloned;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Immutable Properties */
|
||||||
|
|
||||||
get '@type'(): string {
|
get '@type'(): string {
|
||||||
return this._data['@type'];
|
return this._data['@type'];
|
||||||
}
|
}
|
||||||
|
|
||||||
get provider(): string | null {
|
get version(): number {
|
||||||
return this._data.provider;
|
return this._data.version;
|
||||||
}
|
}
|
||||||
|
|
||||||
set provider(value: string | null) {
|
get total(): number | undefined {
|
||||||
this._data.provider = value;
|
return this._data.total;
|
||||||
}
|
}
|
||||||
|
|
||||||
get service(): string | null {
|
get contents(): CollectionContentsInterface {
|
||||||
return this._data.service;
|
const raw = this._data as any;
|
||||||
|
|
||||||
|
if (raw.contents && Object.keys(raw.contents).length > 0) {
|
||||||
|
return raw.contents;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof raw.content === 'string') {
|
||||||
|
return {
|
||||||
|
event: raw.content === 'event',
|
||||||
|
task: raw.content === 'task',
|
||||||
|
journal: raw.content === 'journal',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (raw.content && typeof raw.content === 'object') {
|
||||||
|
return raw.content;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
set service(value: string | null) {
|
/** Mutable Properties */
|
||||||
this._data.service = value;
|
|
||||||
|
get label(): string {
|
||||||
|
return this._data.label || '';
|
||||||
}
|
}
|
||||||
|
|
||||||
get in(): number | string | null {
|
set label(value: string) {
|
||||||
return this._data.in;
|
|
||||||
}
|
|
||||||
|
|
||||||
set in(value: number | string | null) {
|
|
||||||
this._data.in = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
get id(): number | string | null {
|
|
||||||
return this._data.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
set id(value: number | string | null) {
|
|
||||||
this._data.id = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
get label(): string | null {
|
|
||||||
return this._data.label;
|
|
||||||
}
|
|
||||||
|
|
||||||
set label(value: string | null) {
|
|
||||||
this._data.label = value;
|
this._data.label = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,19 +210,19 @@ export class CollectionObject implements CollectionInterface {
|
|||||||
this._data.description = value;
|
this._data.description = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
get priority(): number | null {
|
get rank(): number | null {
|
||||||
return this._data.priority;
|
return this._data.rank;
|
||||||
}
|
}
|
||||||
|
|
||||||
set priority(value: number | null) {
|
set rank(value: number | null) {
|
||||||
this._data.priority = value;
|
this._data.rank = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
get visibility(): string | null {
|
get visibility(): boolean | null {
|
||||||
return this._data.visibility;
|
return this._data.visibility;
|
||||||
}
|
}
|
||||||
|
|
||||||
set visibility(value: string | null) {
|
set visibility(value: boolean | null) {
|
||||||
this._data.visibility = value;
|
this._data.visibility = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,36 +234,4 @@ export class CollectionObject implements CollectionInterface {
|
|||||||
this._data.color = value;
|
this._data.color = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
get enabled(): boolean {
|
}
|
||||||
return this._data.enabled;
|
|
||||||
}
|
|
||||||
|
|
||||||
set enabled(value: boolean) {
|
|
||||||
this._data.enabled = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
get signature(): string | null {
|
|
||||||
return this._data.signature;
|
|
||||||
}
|
|
||||||
|
|
||||||
set signature(value: string | null) {
|
|
||||||
this._data.signature = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
get permissions(): CollectionPermissionsInterface {
|
|
||||||
return this._data.permissions;
|
|
||||||
}
|
|
||||||
|
|
||||||
set permissions(value: CollectionPermissionsInterface) {
|
|
||||||
this._data.permissions = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
get contents(): CollectionContentsInterface {
|
|
||||||
return this._data.contents;
|
|
||||||
}
|
|
||||||
|
|
||||||
set contents(value: CollectionContentsInterface) {
|
|
||||||
this._data.contents = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -9,154 +9,118 @@ import { EventObject } from "./event";
|
|||||||
import { TaskObject } from "./task";
|
import { TaskObject } from "./task";
|
||||||
import { JournalObject } from "./journal";
|
import { JournalObject } from "./journal";
|
||||||
|
|
||||||
|
type EntityPropertiesData = EventInterface | TaskInterface | JournalInterface;
|
||||||
|
type EntityPropertiesObject = EventObject | TaskObject | JournalObject;
|
||||||
|
type EntityInternal = Omit<EntityInterface<EntityPropertiesData>, 'properties'> & {
|
||||||
|
properties: EntityPropertiesData | EntityPropertiesObject;
|
||||||
|
};
|
||||||
|
|
||||||
export class EntityObject implements EntityInterface {
|
export class EntityObject implements EntityInterface {
|
||||||
|
|
||||||
_data!: EntityInterface;
|
_data!: EntityInternal;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this._data = {
|
this._data = {
|
||||||
'@type': 'chrono:entity',
|
provider: '',
|
||||||
version: 1,
|
service: '',
|
||||||
in: null,
|
collection: '',
|
||||||
id: null,
|
identifier: '',
|
||||||
createdOn: null,
|
|
||||||
createdBy: null,
|
|
||||||
modifiedOn: null,
|
|
||||||
modifiedBy: null,
|
|
||||||
signature: null,
|
signature: null,
|
||||||
data: null,
|
created: null,
|
||||||
|
modified: null,
|
||||||
|
properties: new EventObject().toJson(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fromJson(data: EntityInterface): EntityObject {
|
fromJson(data: EntityInterface): EntityObject {
|
||||||
this._data = data;
|
this._data = data as EntityInternal;
|
||||||
if (data.data) {
|
if (data.properties) {
|
||||||
const type = data.data.type;
|
this._data.properties = this.hydrateProperties(data.properties);
|
||||||
if (type === 'task') {
|
|
||||||
this._data.data = new TaskObject().fromJson(data.data as TaskInterface);
|
|
||||||
} else if (type === 'journal') {
|
|
||||||
this._data.data = new JournalObject().fromJson(data.data as JournalInterface);
|
|
||||||
} else {
|
|
||||||
this._data.data = new EventObject().fromJson(data.data as EventInterface);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
this._data.data = null;
|
this._data.properties = new EventObject();
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
toJson(): EntityInterface {
|
toJson(): EntityInterface {
|
||||||
const json = { ...this._data };
|
const json = { ...this._data };
|
||||||
if (this._data.data instanceof EventObject ||
|
if (this._data.properties instanceof EventObject ||
|
||||||
this._data.data instanceof TaskObject ||
|
this._data.properties instanceof TaskObject ||
|
||||||
this._data.data instanceof JournalObject) {
|
this._data.properties instanceof JournalObject) {
|
||||||
json.data = this._data.data.toJson();
|
json.properties = this._data.properties.toJson();
|
||||||
}
|
}
|
||||||
return json;
|
return json as EntityInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
clone(): EntityObject {
|
clone(): EntityObject {
|
||||||
const cloned = new EntityObject();
|
const cloned = new EntityObject();
|
||||||
cloned._data = JSON.parse(JSON.stringify(this._data));
|
cloned._data = JSON.parse(JSON.stringify(this.toJson())) as EntityInternal;
|
||||||
return cloned;
|
return cloned;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Properties */
|
private hydrateProperties(properties: EntityPropertiesData): EntityPropertiesObject {
|
||||||
|
const type = properties.type;
|
||||||
|
if (type === 'task') {
|
||||||
|
return new TaskObject().fromJson(properties as TaskInterface);
|
||||||
|
}
|
||||||
|
|
||||||
get '@type'(): string {
|
if (type === 'journal') {
|
||||||
return this._data['@type'];
|
return new JournalObject().fromJson(properties as JournalInterface);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new EventObject().fromJson(properties as EventInterface);
|
||||||
}
|
}
|
||||||
|
|
||||||
get version(): number {
|
/** Immutable Properties */
|
||||||
return this._data.version;
|
|
||||||
|
get provider(): string {
|
||||||
|
return this._data.provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
set version(value: number) {
|
get service(): string {
|
||||||
this._data.version = value;
|
return this._data.service;
|
||||||
}
|
}
|
||||||
|
|
||||||
get in(): string | number | null {
|
get collection(): string | number {
|
||||||
return this._data.in;
|
return this._data.collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
set in(value: string | number | null) {
|
get identifier(): string | number {
|
||||||
this._data.in = value;
|
return this._data.identifier;
|
||||||
}
|
|
||||||
|
|
||||||
get id(): string | number | null {
|
|
||||||
return this._data.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
set id(value: string | number | null) {
|
|
||||||
this._data.id = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
get createdOn(): Date | null {
|
|
||||||
return this._data.createdOn;
|
|
||||||
}
|
|
||||||
|
|
||||||
set createdOn(value: Date | null) {
|
|
||||||
this._data.createdOn = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
get createdBy(): string | null {
|
|
||||||
return this._data.createdBy;
|
|
||||||
}
|
|
||||||
|
|
||||||
set createdBy(value: string | null) {
|
|
||||||
this._data.createdBy = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
get modifiedOn(): Date | null {
|
|
||||||
return this._data.modifiedOn;
|
|
||||||
}
|
|
||||||
|
|
||||||
set modifiedOn(value: Date | null) {
|
|
||||||
this._data.modifiedOn = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
get modifiedBy(): string | null {
|
|
||||||
return this._data.modifiedBy;
|
|
||||||
}
|
|
||||||
|
|
||||||
set modifiedBy(value: string | null) {
|
|
||||||
this._data.modifiedBy = value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get signature(): string | null {
|
get signature(): string | null {
|
||||||
return this._data.signature;
|
return this._data.signature;
|
||||||
}
|
}
|
||||||
|
|
||||||
set signature(value: string | null) {
|
get created(): string | null {
|
||||||
this._data.signature = value;
|
return this._data.created;
|
||||||
}
|
}
|
||||||
|
|
||||||
get data(): EventObject | TaskObject | JournalObject | null {
|
get modified(): string | null {
|
||||||
if (this._data.data instanceof EventObject ||
|
return this._data.modified;
|
||||||
this._data.data instanceof TaskObject ||
|
}
|
||||||
this._data.data instanceof JournalObject) {
|
|
||||||
return this._data.data;
|
get properties(): EntityPropertiesObject {
|
||||||
|
if (this._data.properties instanceof EventObject ||
|
||||||
|
this._data.properties instanceof TaskObject ||
|
||||||
|
this._data.properties instanceof JournalObject) {
|
||||||
|
return this._data.properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this._data.data) {
|
if (this._data.properties) {
|
||||||
const type = this._data.data.type;
|
const hydrated = this.hydrateProperties(this._data.properties as EntityPropertiesData);
|
||||||
let hydrated;
|
this._data.properties = hydrated;
|
||||||
if (type === 'task') {
|
|
||||||
hydrated = new TaskObject().fromJson(this._data.data as TaskInterface);
|
|
||||||
} else if (type === 'journal') {
|
|
||||||
hydrated = new JournalObject().fromJson(this._data.data as JournalInterface);
|
|
||||||
} else {
|
|
||||||
hydrated = new EventObject().fromJson(this._data.data as EventInterface);
|
|
||||||
}
|
|
||||||
this._data.data = hydrated;
|
|
||||||
return hydrated;
|
return hydrated;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
const defaultProperties = new EventObject();
|
||||||
|
this._data.properties = defaultProperties;
|
||||||
|
return defaultProperties;
|
||||||
}
|
}
|
||||||
|
|
||||||
set data(value: EventObject | TaskObject | JournalObject | null) {
|
set properties(value: EntityPropertiesObject) {
|
||||||
this._data.data = value;
|
this._data.properties = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
196
src/models/identity.ts
Normal file
196
src/models/identity.ts
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
/**
|
||||||
|
* Identity implementation classes for Mail Manager services
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type {
|
||||||
|
ServiceIdentity,
|
||||||
|
ServiceIdentityNone,
|
||||||
|
ServiceIdentityBasic,
|
||||||
|
ServiceIdentityToken,
|
||||||
|
ServiceIdentityOAuth,
|
||||||
|
ServiceIdentityCertificate
|
||||||
|
} from '@/types/service';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base Identity class
|
||||||
|
*/
|
||||||
|
export abstract class Identity {
|
||||||
|
abstract toJson(): ServiceIdentity;
|
||||||
|
|
||||||
|
static fromJson(data: ServiceIdentity): Identity {
|
||||||
|
switch (data.type) {
|
||||||
|
case 'NA':
|
||||||
|
return IdentityNone.fromJson(data);
|
||||||
|
case 'BA':
|
||||||
|
return IdentityBasic.fromJson(data);
|
||||||
|
case 'TA':
|
||||||
|
return IdentityToken.fromJson(data);
|
||||||
|
case 'OA':
|
||||||
|
return IdentityOAuth.fromJson(data);
|
||||||
|
case 'CC':
|
||||||
|
return IdentityCertificate.fromJson(data);
|
||||||
|
default:
|
||||||
|
throw new Error(`Unknown identity type: ${(data as any).type}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* No authentication
|
||||||
|
*/
|
||||||
|
export class IdentityNone extends Identity {
|
||||||
|
readonly type = 'NA' as const;
|
||||||
|
|
||||||
|
static fromJson(_data: ServiceIdentityNone): IdentityNone {
|
||||||
|
return new IdentityNone();
|
||||||
|
}
|
||||||
|
|
||||||
|
toJson(): ServiceIdentityNone {
|
||||||
|
return {
|
||||||
|
type: this.type
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basic authentication (username/password)
|
||||||
|
*/
|
||||||
|
export class IdentityBasic extends Identity {
|
||||||
|
readonly type = 'BA' as const;
|
||||||
|
identity: string;
|
||||||
|
secret: string;
|
||||||
|
|
||||||
|
constructor(identity: string = '', secret: string = '') {
|
||||||
|
super();
|
||||||
|
this.identity = identity;
|
||||||
|
this.secret = secret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJson(data: ServiceIdentityBasic): IdentityBasic {
|
||||||
|
return new IdentityBasic(data.identity, data.secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
toJson(): ServiceIdentityBasic {
|
||||||
|
return {
|
||||||
|
type: this.type,
|
||||||
|
identity: this.identity,
|
||||||
|
secret: this.secret
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Token authentication (API key, static token)
|
||||||
|
*/
|
||||||
|
export class IdentityToken extends Identity {
|
||||||
|
readonly type = 'TA' as const;
|
||||||
|
token: string;
|
||||||
|
|
||||||
|
constructor(token: string = '') {
|
||||||
|
super();
|
||||||
|
this.token = token;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJson(data: ServiceIdentityToken): IdentityToken {
|
||||||
|
return new IdentityToken(data.token);
|
||||||
|
}
|
||||||
|
|
||||||
|
toJson(): ServiceIdentityToken {
|
||||||
|
return {
|
||||||
|
type: this.type,
|
||||||
|
token: this.token
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OAuth authentication
|
||||||
|
*/
|
||||||
|
export class IdentityOAuth extends Identity {
|
||||||
|
readonly type = 'OA' as const;
|
||||||
|
accessToken: string;
|
||||||
|
accessScope?: string[];
|
||||||
|
accessExpiry?: number;
|
||||||
|
refreshToken?: string;
|
||||||
|
refreshLocation?: string;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
accessToken: string = '',
|
||||||
|
accessScope?: string[],
|
||||||
|
accessExpiry?: number,
|
||||||
|
refreshToken?: string,
|
||||||
|
refreshLocation?: string
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
this.accessScope = accessScope;
|
||||||
|
this.accessExpiry = accessExpiry;
|
||||||
|
this.refreshToken = refreshToken;
|
||||||
|
this.refreshLocation = refreshLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJson(data: ServiceIdentityOAuth): IdentityOAuth {
|
||||||
|
return new IdentityOAuth(
|
||||||
|
data.accessToken,
|
||||||
|
data.accessScope,
|
||||||
|
data.accessExpiry,
|
||||||
|
data.refreshToken,
|
||||||
|
data.refreshLocation
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
toJson(): ServiceIdentityOAuth {
|
||||||
|
return {
|
||||||
|
type: this.type,
|
||||||
|
accessToken: this.accessToken,
|
||||||
|
...(this.accessScope && { accessScope: this.accessScope }),
|
||||||
|
...(this.accessExpiry && { accessExpiry: this.accessExpiry }),
|
||||||
|
...(this.refreshToken && { refreshToken: this.refreshToken }),
|
||||||
|
...(this.refreshLocation && { refreshLocation: this.refreshLocation })
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
isExpired(): boolean {
|
||||||
|
if (!this.accessExpiry) return false;
|
||||||
|
return Date.now() / 1000 >= this.accessExpiry;
|
||||||
|
}
|
||||||
|
|
||||||
|
expiresIn(): number {
|
||||||
|
if (!this.accessExpiry) return Infinity;
|
||||||
|
return Math.max(0, this.accessExpiry - Date.now() / 1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Client certificate authentication (mTLS)
|
||||||
|
*/
|
||||||
|
export class IdentityCertificate extends Identity {
|
||||||
|
readonly type = 'CC' as const;
|
||||||
|
certificate: string;
|
||||||
|
privateKey: string;
|
||||||
|
passphrase?: string;
|
||||||
|
|
||||||
|
constructor(certificate: string = '', privateKey: string = '', passphrase?: string) {
|
||||||
|
super();
|
||||||
|
this.certificate = certificate;
|
||||||
|
this.privateKey = privateKey;
|
||||||
|
this.passphrase = passphrase;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJson(data: ServiceIdentityCertificate): IdentityCertificate {
|
||||||
|
return new IdentityCertificate(
|
||||||
|
data.certificate,
|
||||||
|
data.privateKey,
|
||||||
|
data.passphrase
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
toJson(): ServiceIdentityCertificate {
|
||||||
|
return {
|
||||||
|
type: this.type,
|
||||||
|
certificate: this.certificate,
|
||||||
|
privateKey: this.privateKey,
|
||||||
|
...(this.passphrase && { passphrase: this.passphrase })
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,22 @@
|
|||||||
/**
|
export { CollectionObject } from './collection';
|
||||||
* Central export point for all Chrono Manager models
|
export { EntityObject } from './entity';
|
||||||
*/
|
export { EventObject } from './event';
|
||||||
|
export { TaskObject } from './task';
|
||||||
export { Collection } from './collection';
|
export { JournalObject } from './journal';
|
||||||
export { Entity } from './entity';
|
export { ProviderObject } from './provider';
|
||||||
export { Event } from './event';
|
export { ServiceObject } from './service';
|
||||||
export { Task } from './task';
|
export {
|
||||||
export { Journal } from './journal';
|
Identity,
|
||||||
export { Provider } from './provider';
|
IdentityNone,
|
||||||
export { Service } from './service';
|
IdentityBasic,
|
||||||
|
IdentityToken,
|
||||||
|
IdentityOAuth,
|
||||||
|
IdentityCertificate
|
||||||
|
} from './identity';
|
||||||
|
export {
|
||||||
|
Location,
|
||||||
|
LocationUri,
|
||||||
|
LocationSocketSole,
|
||||||
|
LocationSocketSplit,
|
||||||
|
LocationFile
|
||||||
|
} from './location';
|
||||||
|
|||||||
240
src/models/location.ts
Normal file
240
src/models/location.ts
Normal file
@@ -0,0 +1,240 @@
|
|||||||
|
/**
|
||||||
|
* Location implementation classes for Mail Manager services
|
||||||
|
*/
|
||||||
|
|
||||||
|
import type {
|
||||||
|
ServiceLocation,
|
||||||
|
ServiceLocationUri,
|
||||||
|
ServiceLocationSocketSole,
|
||||||
|
ServiceLocationSocketSplit,
|
||||||
|
ServiceLocationFile
|
||||||
|
} from '@/types/service';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base Location class
|
||||||
|
*/
|
||||||
|
export abstract class Location {
|
||||||
|
abstract toJson(): ServiceLocation;
|
||||||
|
|
||||||
|
static fromJson(data: ServiceLocation): Location {
|
||||||
|
switch (data.type) {
|
||||||
|
case 'URI':
|
||||||
|
return LocationUri.fromJson(data);
|
||||||
|
case 'SOCKET_SOLE':
|
||||||
|
return LocationSocketSole.fromJson(data);
|
||||||
|
case 'SOCKET_SPLIT':
|
||||||
|
return LocationSocketSplit.fromJson(data);
|
||||||
|
case 'FILE':
|
||||||
|
return LocationFile.fromJson(data);
|
||||||
|
default:
|
||||||
|
throw new Error(`Unknown location type: ${(data as any).type}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* URI-based service location for API and web services
|
||||||
|
* Used by: JMAP, Gmail API, etc.
|
||||||
|
*/
|
||||||
|
export class LocationUri extends Location {
|
||||||
|
readonly type = 'URI' as const;
|
||||||
|
scheme: string;
|
||||||
|
host: string;
|
||||||
|
port: number;
|
||||||
|
path?: string;
|
||||||
|
verifyPeer: boolean;
|
||||||
|
verifyHost: boolean;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
scheme: string = 'https',
|
||||||
|
host: string = '',
|
||||||
|
port: number = 443,
|
||||||
|
path?: string,
|
||||||
|
verifyPeer: boolean = true,
|
||||||
|
verifyHost: boolean = true
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
this.scheme = scheme;
|
||||||
|
this.host = host;
|
||||||
|
this.port = port;
|
||||||
|
this.path = path;
|
||||||
|
this.verifyPeer = verifyPeer;
|
||||||
|
this.verifyHost = verifyHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJson(data: ServiceLocationUri): LocationUri {
|
||||||
|
return new LocationUri(
|
||||||
|
data.scheme,
|
||||||
|
data.host,
|
||||||
|
data.port,
|
||||||
|
data.path,
|
||||||
|
data.verifyPeer ?? true,
|
||||||
|
data.verifyHost ?? true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
toJson(): ServiceLocationUri {
|
||||||
|
return {
|
||||||
|
type: this.type,
|
||||||
|
scheme: this.scheme,
|
||||||
|
host: this.host,
|
||||||
|
port: this.port,
|
||||||
|
...(this.path && { path: this.path }),
|
||||||
|
...(this.verifyPeer !== undefined && { verifyPeer: this.verifyPeer }),
|
||||||
|
...(this.verifyHost !== undefined && { verifyHost: this.verifyHost })
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
getUrl(): string {
|
||||||
|
const path = this.path || '';
|
||||||
|
return `${this.scheme}://${this.host}:${this.port}${path}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Single socket-based service location
|
||||||
|
* Used by: services using a single host/port combination
|
||||||
|
*/
|
||||||
|
export class LocationSocketSole extends Location {
|
||||||
|
readonly type = 'SOCKET_SOLE' as const;
|
||||||
|
host: string;
|
||||||
|
port: number;
|
||||||
|
encryption: 'none' | 'ssl' | 'tls' | 'starttls';
|
||||||
|
verifyPeer: boolean;
|
||||||
|
verifyHost: boolean;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
host: string = '',
|
||||||
|
port: number = 993,
|
||||||
|
encryption: 'none' | 'ssl' | 'tls' | 'starttls' = 'ssl',
|
||||||
|
verifyPeer: boolean = true,
|
||||||
|
verifyHost: boolean = true
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
this.host = host;
|
||||||
|
this.port = port;
|
||||||
|
this.encryption = encryption;
|
||||||
|
this.verifyPeer = verifyPeer;
|
||||||
|
this.verifyHost = verifyHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJson(data: ServiceLocationSocketSole): LocationSocketSole {
|
||||||
|
return new LocationSocketSole(
|
||||||
|
data.host,
|
||||||
|
data.port,
|
||||||
|
data.encryption,
|
||||||
|
data.verifyPeer ?? true,
|
||||||
|
data.verifyHost ?? true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
toJson(): ServiceLocationSocketSole {
|
||||||
|
return {
|
||||||
|
type: this.type,
|
||||||
|
host: this.host,
|
||||||
|
port: this.port,
|
||||||
|
encryption: this.encryption,
|
||||||
|
...(this.verifyPeer !== undefined && { verifyPeer: this.verifyPeer }),
|
||||||
|
...(this.verifyHost !== undefined && { verifyHost: this.verifyHost })
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split socket-based service location
|
||||||
|
* Used by: traditional IMAP/SMTP configurations
|
||||||
|
*/
|
||||||
|
export class LocationSocketSplit extends Location {
|
||||||
|
readonly type = 'SOCKET_SPLIT' as const;
|
||||||
|
inboundHost: string;
|
||||||
|
inboundPort: number;
|
||||||
|
inboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls';
|
||||||
|
outboundHost: string;
|
||||||
|
outboundPort: number;
|
||||||
|
outboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls';
|
||||||
|
inboundVerifyPeer: boolean;
|
||||||
|
inboundVerifyHost: boolean;
|
||||||
|
outboundVerifyPeer: boolean;
|
||||||
|
outboundVerifyHost: boolean;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
inboundHost: string = '',
|
||||||
|
inboundPort: number = 993,
|
||||||
|
inboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls' = 'ssl',
|
||||||
|
outboundHost: string = '',
|
||||||
|
outboundPort: number = 465,
|
||||||
|
outboundEncryption: 'none' | 'ssl' | 'tls' | 'starttls' = 'ssl',
|
||||||
|
inboundVerifyPeer: boolean = true,
|
||||||
|
inboundVerifyHost: boolean = true,
|
||||||
|
outboundVerifyPeer: boolean = true,
|
||||||
|
outboundVerifyHost: boolean = true
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
this.inboundHost = inboundHost;
|
||||||
|
this.inboundPort = inboundPort;
|
||||||
|
this.inboundEncryption = inboundEncryption;
|
||||||
|
this.outboundHost = outboundHost;
|
||||||
|
this.outboundPort = outboundPort;
|
||||||
|
this.outboundEncryption = outboundEncryption;
|
||||||
|
this.inboundVerifyPeer = inboundVerifyPeer;
|
||||||
|
this.inboundVerifyHost = inboundVerifyHost;
|
||||||
|
this.outboundVerifyPeer = outboundVerifyPeer;
|
||||||
|
this.outboundVerifyHost = outboundVerifyHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJson(data: ServiceLocationSocketSplit): LocationSocketSplit {
|
||||||
|
return new LocationSocketSplit(
|
||||||
|
data.inboundHost,
|
||||||
|
data.inboundPort,
|
||||||
|
data.inboundEncryption,
|
||||||
|
data.outboundHost,
|
||||||
|
data.outboundPort,
|
||||||
|
data.outboundEncryption,
|
||||||
|
data.inboundVerifyPeer ?? true,
|
||||||
|
data.inboundVerifyHost ?? true,
|
||||||
|
data.outboundVerifyPeer ?? true,
|
||||||
|
data.outboundVerifyHost ?? true
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
toJson(): ServiceLocationSocketSplit {
|
||||||
|
return {
|
||||||
|
type: this.type,
|
||||||
|
inboundHost: this.inboundHost,
|
||||||
|
inboundPort: this.inboundPort,
|
||||||
|
inboundEncryption: this.inboundEncryption,
|
||||||
|
outboundHost: this.outboundHost,
|
||||||
|
outboundPort: this.outboundPort,
|
||||||
|
outboundEncryption: this.outboundEncryption,
|
||||||
|
...(this.inboundVerifyPeer !== undefined && { inboundVerifyPeer: this.inboundVerifyPeer }),
|
||||||
|
...(this.inboundVerifyHost !== undefined && { inboundVerifyHost: this.inboundVerifyHost }),
|
||||||
|
...(this.outboundVerifyPeer !== undefined && { outboundVerifyPeer: this.outboundVerifyPeer }),
|
||||||
|
...(this.outboundVerifyHost !== undefined && { outboundVerifyHost: this.outboundVerifyHost })
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* File-based service location
|
||||||
|
* Used by: local file system providers
|
||||||
|
*/
|
||||||
|
export class LocationFile extends Location {
|
||||||
|
readonly type = 'FILE' as const;
|
||||||
|
path: string;
|
||||||
|
|
||||||
|
constructor(path: string = '') {
|
||||||
|
super();
|
||||||
|
this.path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
static fromJson(data: ServiceLocationFile): LocationFile {
|
||||||
|
return new LocationFile(data.path);
|
||||||
|
}
|
||||||
|
|
||||||
|
toJson(): ServiceLocationFile {
|
||||||
|
return {
|
||||||
|
type: this.type,
|
||||||
|
path: this.path
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
/**
|
/**
|
||||||
* Class model for Provider Interface
|
* Class model for Provider Interface
|
||||||
*/
|
*/
|
||||||
import type { ProviderCapabilitiesInterface, ProviderInterface } from "@/types/provider";
|
|
||||||
|
import type {
|
||||||
|
ProviderInterface,
|
||||||
|
ProviderCapabilitiesInterface
|
||||||
|
} from "@/types/provider";
|
||||||
|
|
||||||
export class ProviderObject implements ProviderInterface {
|
export class ProviderObject implements ProviderInterface {
|
||||||
|
|
||||||
@@ -9,8 +13,8 @@ export class ProviderObject implements ProviderInterface {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this._data = {
|
this._data = {
|
||||||
'@type': 'chrono:provider',
|
'@type': 'chrono.provider',
|
||||||
id: '',
|
identifier: '',
|
||||||
label: '',
|
label: '',
|
||||||
capabilities: {},
|
capabilities: {},
|
||||||
};
|
};
|
||||||
@@ -25,16 +29,11 @@ export class ProviderObject implements ProviderInterface {
|
|||||||
return this._data;
|
return this._data;
|
||||||
}
|
}
|
||||||
|
|
||||||
clone(): ProviderObject {
|
capable(capability: keyof ProviderCapabilitiesInterface): boolean {
|
||||||
const cloned = new ProviderObject();
|
const value = this._data.capabilities?.[capability];
|
||||||
cloned._data = JSON.parse(JSON.stringify(this._data));
|
return value !== undefined && value !== false;
|
||||||
return cloned;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
capable(capability: keyof ProviderCapabilitiesInterface): boolean {
|
|
||||||
return !!(this._data.capabilities && this._data.capabilities[capability]);
|
|
||||||
}
|
|
||||||
|
|
||||||
capability(capability: keyof ProviderCapabilitiesInterface): any | null {
|
capability(capability: keyof ProviderCapabilitiesInterface): any | null {
|
||||||
if (this._data.capabilities) {
|
if (this._data.capabilities) {
|
||||||
return this._data.capabilities[capability];
|
return this._data.capabilities[capability];
|
||||||
@@ -48,8 +47,8 @@ export class ProviderObject implements ProviderInterface {
|
|||||||
return this._data['@type'];
|
return this._data['@type'];
|
||||||
}
|
}
|
||||||
|
|
||||||
get id(): string {
|
get identifier(): string {
|
||||||
return this._data.id;
|
return this._data.identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
get label(): string {
|
get label(): string {
|
||||||
|
|||||||
@@ -1,7 +1,15 @@
|
|||||||
/**
|
/**
|
||||||
* Class model for Service Interface
|
* Class model for Service Interface
|
||||||
*/
|
*/
|
||||||
import type { ServiceCapabilitiesInterface, ServiceInterface } from "@/types/service";
|
|
||||||
|
import type {
|
||||||
|
ServiceInterface,
|
||||||
|
ServiceCapabilitiesInterface,
|
||||||
|
ServiceIdentity,
|
||||||
|
ServiceLocation
|
||||||
|
} from "@/types/service";
|
||||||
|
import { Identity } from './identity';
|
||||||
|
import { Location } from './location';
|
||||||
|
|
||||||
export class ServiceObject implements ServiceInterface {
|
export class ServiceObject implements ServiceInterface {
|
||||||
|
|
||||||
@@ -11,10 +19,10 @@ export class ServiceObject implements ServiceInterface {
|
|||||||
this._data = {
|
this._data = {
|
||||||
'@type': 'chrono:service',
|
'@type': 'chrono:service',
|
||||||
provider: '',
|
provider: '',
|
||||||
id: '',
|
identifier: null,
|
||||||
label: '',
|
label: null,
|
||||||
capabilities: {},
|
enabled: false,
|
||||||
enabled: true,
|
capabilities: {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,16 +35,11 @@ export class ServiceObject implements ServiceInterface {
|
|||||||
return this._data;
|
return this._data;
|
||||||
}
|
}
|
||||||
|
|
||||||
clone(): ServiceObject {
|
capable(capability: keyof ServiceCapabilitiesInterface): boolean {
|
||||||
const cloned = new ServiceObject();
|
const value = this._data.capabilities?.[capability];
|
||||||
cloned._data = JSON.parse(JSON.stringify(this._data));
|
return value !== undefined && value !== false;
|
||||||
return cloned;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
capable(capability: keyof ServiceCapabilitiesInterface): boolean {
|
|
||||||
return !!(this._data.capabilities && this._data.capabilities[capability]);
|
|
||||||
}
|
|
||||||
|
|
||||||
capability(capability: keyof ServiceCapabilitiesInterface): any | null {
|
capability(capability: keyof ServiceCapabilitiesInterface): any | null {
|
||||||
if (this._data.capabilities) {
|
if (this._data.capabilities) {
|
||||||
return this._data.capabilities[capability];
|
return this._data.capabilities[capability];
|
||||||
@@ -54,18 +57,24 @@ export class ServiceObject implements ServiceInterface {
|
|||||||
return this._data.provider;
|
return this._data.provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
get id(): string {
|
get identifier(): string | number | null {
|
||||||
return this._data.id;
|
return this._data.identifier;
|
||||||
}
|
|
||||||
|
|
||||||
get label(): string {
|
|
||||||
return this._data.label;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
get capabilities(): ServiceCapabilitiesInterface | undefined {
|
get capabilities(): ServiceCapabilitiesInterface | undefined {
|
||||||
return this._data.capabilities;
|
return this._data.capabilities;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Mutable Properties */
|
||||||
|
|
||||||
|
get label(): string | null {
|
||||||
|
return this._data.label;
|
||||||
|
}
|
||||||
|
|
||||||
|
set label(value: string | null) {
|
||||||
|
this._data.label = value;
|
||||||
|
}
|
||||||
|
|
||||||
get enabled(): boolean {
|
get enabled(): boolean {
|
||||||
return this._data.enabled;
|
return this._data.enabled;
|
||||||
}
|
}
|
||||||
@@ -74,4 +83,46 @@ export class ServiceObject implements ServiceInterface {
|
|||||||
this._data.enabled = value;
|
this._data.enabled = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get location(): ServiceLocation | null {
|
||||||
|
return this._data.location ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
set location(value: ServiceLocation | null) {
|
||||||
|
this._data.location = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
get identity(): ServiceIdentity | null {
|
||||||
|
return this._data.identity ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
set identity(value: ServiceIdentity | null) {
|
||||||
|
this._data.identity = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
get auxiliary(): Record<string, any> {
|
||||||
|
return this._data.auxiliary ?? {};
|
||||||
|
}
|
||||||
|
|
||||||
|
set auxiliary(value: Record<string, any>) {
|
||||||
|
this._data.auxiliary = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Helper Methods */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get identity as a class instance for easier manipulation
|
||||||
|
*/
|
||||||
|
getIdentity(): Identity | null {
|
||||||
|
if (!this._data.identity) return null;
|
||||||
|
return Identity.fromJson(this._data.identity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get location as a class instance for easier manipulation
|
||||||
|
*/
|
||||||
|
getLocation(): Location | null {
|
||||||
|
if (!this._data.location) return null;
|
||||||
|
return Location.fromJson(this._data.location);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,7 @@
|
|||||||
* Collection management service
|
* Collection management service
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { createFetchWrapper } from '@KTXC/utils/helpers/fetch-wrapper-core';
|
import { transceivePost } from './transceive';
|
||||||
|
|
||||||
const fetchWrapper = createFetchWrapper();
|
|
||||||
import type {
|
import type {
|
||||||
CollectionListRequest,
|
CollectionListRequest,
|
||||||
CollectionListResponse,
|
CollectionListResponse,
|
||||||
@@ -14,74 +12,119 @@ import type {
|
|||||||
CollectionFetchResponse,
|
CollectionFetchResponse,
|
||||||
CollectionCreateRequest,
|
CollectionCreateRequest,
|
||||||
CollectionCreateResponse,
|
CollectionCreateResponse,
|
||||||
CollectionModifyRequest,
|
CollectionUpdateResponse,
|
||||||
CollectionModifyResponse,
|
CollectionUpdateRequest,
|
||||||
CollectionDestroyRequest,
|
CollectionDeleteResponse,
|
||||||
CollectionDestroyResponse,
|
CollectionDeleteRequest,
|
||||||
|
CollectionInterface,
|
||||||
} from '../types/collection';
|
} from '../types/collection';
|
||||||
|
import { useIntegrationStore } from '@KTXC/stores/integrationStore';
|
||||||
|
import { CollectionObject, CollectionPropertiesObject } from '../models/collection';
|
||||||
|
|
||||||
const BASE_URL = '/m/chrono_manager/collection';
|
/**
|
||||||
|
* Helper to create the right collection model class based on provider identifier
|
||||||
|
* Uses provider-specific factory if available, otherwise returns base CollectionObject
|
||||||
|
*/
|
||||||
|
function createCollectionObject(data: CollectionInterface): CollectionObject {
|
||||||
|
const integrationStore = useIntegrationStore();
|
||||||
|
const factoryItem = integrationStore.getItemById('mail_collection_factory', data.provider) as any;
|
||||||
|
const factory = factoryItem?.factory;
|
||||||
|
|
||||||
|
// Use provider factory if available, otherwise base class
|
||||||
|
return factory ? factory(data) : new CollectionObject().fromJson(data);
|
||||||
|
}
|
||||||
|
|
||||||
export const collectionService = {
|
export const collectionService = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List all available collections
|
* Retrieve list of collections, optionally filtered by source selector
|
||||||
*
|
*
|
||||||
* @param request - Collection list request parameters
|
* @param request - list request parameters
|
||||||
* @returns Promise with collection list grouped by provider and service
|
*
|
||||||
|
* @returns Promise with collection object list grouped by provider, service, and collection identifier
|
||||||
*/
|
*/
|
||||||
async list(request: CollectionListRequest = {}): Promise<CollectionListResponse> {
|
async list(request: CollectionListRequest = {}): Promise<Record<string, Record<string, Record<string, CollectionObject>>>> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/list`, request);
|
const response = await transceivePost<CollectionListRequest, CollectionListResponse>('collection.list', request);
|
||||||
|
|
||||||
|
// Convert nested response to CollectionObject instances
|
||||||
|
const providerList: Record<string, Record<string, Record<string, CollectionObject>>> = {};
|
||||||
|
Object.entries(response).forEach(([providerId, providerServices]) => {
|
||||||
|
const serviceList: Record<string, Record<string, CollectionObject>> = {};
|
||||||
|
Object.entries(providerServices).forEach(([serviceId, serviceCollections]) => {
|
||||||
|
const collectionList: Record<string, CollectionObject> = {};
|
||||||
|
Object.entries(serviceCollections).forEach(([collectionId, collectionData]) => {
|
||||||
|
collectionList[collectionId] = createCollectionObject(collectionData);
|
||||||
|
});
|
||||||
|
serviceList[serviceId] = collectionList;
|
||||||
|
});
|
||||||
|
providerList[providerId] = serviceList;
|
||||||
|
});
|
||||||
|
|
||||||
|
return providerList;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check which collections exist/are available
|
* Retrieve a specific collection by provider and identifier
|
||||||
|
*
|
||||||
|
* @param request - fetch request parameters
|
||||||
|
*
|
||||||
|
* @returns Promise with collection object
|
||||||
|
*/
|
||||||
|
async fetch(request: CollectionFetchRequest): Promise<CollectionObject> {
|
||||||
|
const response = await transceivePost<CollectionFetchRequest, CollectionFetchResponse>('collection.fetch', request);
|
||||||
|
return createCollectionObject(response);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve collection availability status for a given source selector
|
||||||
|
*
|
||||||
|
* @param request - extant request parameters
|
||||||
*
|
*
|
||||||
* @param request - Collection extant request with source selector
|
|
||||||
* @returns Promise with collection availability status
|
* @returns Promise with collection availability status
|
||||||
*/
|
*/
|
||||||
async extant(request: CollectionExtantRequest): Promise<CollectionExtantResponse> {
|
async extant(request: CollectionExtantRequest): Promise<CollectionExtantResponse> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/extant`, request);
|
return await transceivePost<CollectionExtantRequest, CollectionExtantResponse>('collection.extant', request);
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch a specific collection
|
|
||||||
*
|
|
||||||
* @param request - Collection fetch request
|
|
||||||
* @returns Promise with collection details
|
|
||||||
*/
|
|
||||||
async fetch(request: CollectionFetchRequest): Promise<CollectionFetchResponse> {
|
|
||||||
return await fetchWrapper.post(`${BASE_URL}/fetch`, request);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new collection
|
* Create a new collection
|
||||||
*
|
*
|
||||||
* @param request - Collection create request
|
* @param request - create request parameters
|
||||||
* @returns Promise with created collection
|
*
|
||||||
|
* @returns Promise with created collection object
|
||||||
*/
|
*/
|
||||||
async create(request: CollectionCreateRequest): Promise<CollectionCreateResponse> {
|
async create(request: CollectionCreateRequest): Promise<CollectionObject> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/create`, request);
|
if (request.properties instanceof CollectionPropertiesObject) {
|
||||||
|
request.properties = request.properties.toJson();
|
||||||
|
}
|
||||||
|
const response = await transceivePost<CollectionCreateRequest, CollectionCreateResponse>('collection.create', request);
|
||||||
|
return createCollectionObject(response);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modify an existing collection
|
* Update an existing collection
|
||||||
*
|
*
|
||||||
* @param request - Collection modify request
|
* @param request - update request parameters
|
||||||
* @returns Promise with modified collection
|
*
|
||||||
|
* @returns Promise with updated collection object
|
||||||
*/
|
*/
|
||||||
async modify(request: CollectionModifyRequest): Promise<CollectionModifyResponse> {
|
async update(request: CollectionUpdateRequest): Promise<CollectionObject> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/modify`, request);
|
if (request.properties instanceof CollectionPropertiesObject) {
|
||||||
|
request.properties = request.properties.toJson();
|
||||||
|
}
|
||||||
|
const response = await transceivePost<CollectionUpdateRequest, CollectionUpdateResponse>('collection.update', request);
|
||||||
|
return createCollectionObject(response);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a collection
|
* Delete a collection
|
||||||
*
|
*
|
||||||
* @param request - Collection destroy request
|
* @param request - delete request parameters
|
||||||
|
*
|
||||||
* @returns Promise with deletion result
|
* @returns Promise with deletion result
|
||||||
*/
|
*/
|
||||||
async destroy(request: CollectionDestroyRequest): Promise<CollectionDestroyResponse> {
|
async delete(request: CollectionDeleteRequest): Promise<CollectionDeleteResponse> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/destroy`, request);
|
return await transceivePost<CollectionDeleteRequest, CollectionDeleteResponse>('collection.delete', request);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,99 +2,149 @@
|
|||||||
* Entity management service
|
* Entity management service
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { createFetchWrapper } from '@KTXC/utils/helpers/fetch-wrapper-core';
|
import { transceivePost } from './transceive';
|
||||||
|
|
||||||
const fetchWrapper = createFetchWrapper();
|
|
||||||
import type {
|
import type {
|
||||||
EntityListRequest,
|
EntityListRequest,
|
||||||
EntityListResponse,
|
EntityListResponse,
|
||||||
EntityDeltaRequest,
|
|
||||||
EntityDeltaResponse,
|
|
||||||
EntityExtantRequest,
|
|
||||||
EntityExtantResponse,
|
|
||||||
EntityFetchRequest,
|
EntityFetchRequest,
|
||||||
EntityFetchResponse,
|
EntityFetchResponse,
|
||||||
|
EntityExtantRequest,
|
||||||
|
EntityExtantResponse,
|
||||||
EntityCreateRequest,
|
EntityCreateRequest,
|
||||||
EntityCreateResponse,
|
EntityCreateResponse,
|
||||||
EntityModifyRequest,
|
EntityUpdateRequest,
|
||||||
EntityModifyResponse,
|
EntityUpdateResponse,
|
||||||
EntityDestroyRequest,
|
EntityDeleteRequest,
|
||||||
EntityDestroyResponse,
|
EntityDeleteResponse,
|
||||||
|
EntityDeltaRequest,
|
||||||
|
EntityDeltaResponse,
|
||||||
|
EntityInterface,
|
||||||
} from '../types/entity';
|
} from '../types/entity';
|
||||||
|
import { useIntegrationStore } from '@KTXC/stores/integrationStore';
|
||||||
|
import { EntityObject } from '../models';
|
||||||
|
|
||||||
const BASE_URL = '/m/chrono_manager/entity';
|
/**
|
||||||
|
* Helper to create the right entity model class based on provider identifier
|
||||||
|
* Uses provider-specific factory if available, otherwise returns base EntityObject
|
||||||
|
*/
|
||||||
|
function createEntityObject(data: EntityInterface): EntityObject {
|
||||||
|
const integrationStore = useIntegrationStore();
|
||||||
|
const factoryItem = integrationStore.getItemById('chrono_entity_factory', data.provider) as any;
|
||||||
|
const factory = factoryItem?.factory;
|
||||||
|
|
||||||
|
// Use provider factory if available, otherwise base class
|
||||||
|
return factory ? factory(data) : new EntityObject().fromJson(data);
|
||||||
|
}
|
||||||
|
|
||||||
export const entityService = {
|
export const entityService = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List all available entities (events, tasks, journals)
|
* Retrieve list of entities, optionally filtered by source selector
|
||||||
*
|
*
|
||||||
* @param request - Entity list request parameters
|
* @param request - list request parameters
|
||||||
* @returns Promise with entity list grouped by provider, service, and collection
|
*
|
||||||
|
* @returns Promise with entity object list grouped by provider, service, collection, and entity identifier
|
||||||
*/
|
*/
|
||||||
async list(request: EntityListRequest = {}): Promise<EntityListResponse> {
|
async list(request: EntityListRequest = {}): Promise<Record<string, Record<string, Record<string, Record<string, EntityObject>>>>> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/list`, request);
|
const response = await transceivePost<EntityListRequest, EntityListResponse>('entity.list', request);
|
||||||
|
|
||||||
|
// Convert nested response to EntityObject instances
|
||||||
|
const providerList: Record<string, Record<string, Record<string, Record<string, EntityObject>>>> = {};
|
||||||
|
Object.entries(response).forEach(([providerId, providerServices]) => {
|
||||||
|
const serviceList: Record<string, Record<string, Record<string, EntityObject>>> = {};
|
||||||
|
Object.entries(providerServices).forEach(([serviceId, serviceCollections]) => {
|
||||||
|
const collectionList: Record<string, Record<string, EntityObject>> = {};
|
||||||
|
Object.entries(serviceCollections).forEach(([collectionId, collectionEntities]) => {
|
||||||
|
const entityList: Record<string, EntityObject> = {};
|
||||||
|
Object.entries(collectionEntities).forEach(([entityId, entityData]) => {
|
||||||
|
entityList[entityId] = createEntityObject(entityData);
|
||||||
|
});
|
||||||
|
collectionList[collectionId] = entityList;
|
||||||
|
});
|
||||||
|
serviceList[serviceId] = collectionList;
|
||||||
|
});
|
||||||
|
providerList[providerId] = serviceList;
|
||||||
|
});
|
||||||
|
|
||||||
|
return providerList;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get delta changes for entities
|
* Retrieve a specific entity by provider and identifier
|
||||||
*
|
*
|
||||||
* @param request - Entity delta request with source selector
|
* @param request - fetch request parameters
|
||||||
* @returns Promise with delta changes (created, modified, deleted)
|
*
|
||||||
|
* @returns Promise with entity objects keyed by identifier
|
||||||
*/
|
*/
|
||||||
async delta(request: EntityDeltaRequest): Promise<EntityDeltaResponse> {
|
async fetch(request: EntityFetchRequest): Promise<Record<string, EntityObject>> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/delta`, request);
|
const response = await transceivePost<EntityFetchRequest, EntityFetchResponse>('entity.fetch', request);
|
||||||
|
|
||||||
|
// Convert response to EntityObject instances
|
||||||
|
const list: Record<string, EntityObject> = {};
|
||||||
|
Object.entries(response).forEach(([identifier, entityData]) => {
|
||||||
|
list[identifier] = createEntityObject(entityData);
|
||||||
|
});
|
||||||
|
|
||||||
|
return list;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check which entities exist/are available
|
* Retrieve entity availability status for a given source selector
|
||||||
|
*
|
||||||
|
* @param request - extant request parameters
|
||||||
*
|
*
|
||||||
* @param request - Entity extant request with source selector
|
|
||||||
* @returns Promise with entity availability status
|
* @returns Promise with entity availability status
|
||||||
*/
|
*/
|
||||||
async extant(request: EntityExtantRequest): Promise<EntityExtantResponse> {
|
async extant(request: EntityExtantRequest): Promise<EntityExtantResponse> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/extant`, request);
|
return await transceivePost<EntityExtantRequest, EntityExtantResponse>('entity.extant', request);
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetch specific entities
|
|
||||||
*
|
|
||||||
* @param request - Entity fetch request
|
|
||||||
* @returns Promise with entity details
|
|
||||||
*/
|
|
||||||
async fetch(request: EntityFetchRequest): Promise<EntityFetchResponse> {
|
|
||||||
return await fetchWrapper.post(`${BASE_URL}/fetch`, request);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new entity
|
* Create a new entity
|
||||||
*
|
*
|
||||||
* @param request - Entity create request
|
* @param request - create request parameters
|
||||||
* @returns Promise with created entity
|
*
|
||||||
|
* @returns Promise with created entity object
|
||||||
*/
|
*/
|
||||||
async create(request: EntityCreateRequest): Promise<EntityCreateResponse> {
|
async create(request: EntityCreateRequest): Promise<EntityObject> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/create`, request);
|
const response = await transceivePost<EntityCreateRequest, EntityCreateResponse>('entity.create', request);
|
||||||
|
return createEntityObject(response);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modify an existing entity
|
* Update an existing entity
|
||||||
*
|
*
|
||||||
* @param request - Entity modify request
|
* @param request - update request parameters
|
||||||
* @returns Promise with modified entity
|
*
|
||||||
|
* @returns Promise with updated entity object
|
||||||
*/
|
*/
|
||||||
async modify(request: EntityModifyRequest): Promise<EntityModifyResponse> {
|
async update(request: EntityUpdateRequest): Promise<EntityObject> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/modify`, request);
|
const response = await transceivePost<EntityUpdateRequest, EntityUpdateResponse>('entity.update', request);
|
||||||
|
return createEntityObject(response);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete an entity
|
* Delete an entity
|
||||||
*
|
*
|
||||||
* @param request - Entity destroy request
|
* @param request - delete request parameters
|
||||||
|
*
|
||||||
* @returns Promise with deletion result
|
* @returns Promise with deletion result
|
||||||
*/
|
*/
|
||||||
async destroy(request: EntityDestroyRequest): Promise<EntityDestroyResponse> {
|
async delete(request: EntityDeleteRequest): Promise<EntityDeleteResponse> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/destroy`, request);
|
return await transceivePost<EntityDeleteRequest, EntityDeleteResponse>('entity.delete', request);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve delta changes for entities
|
||||||
|
*
|
||||||
|
* @param request - delta request parameters
|
||||||
|
*
|
||||||
|
* @returns Promise with delta changes (created, modified, deleted)
|
||||||
|
*/
|
||||||
|
async delta(request: EntityDeltaRequest): Promise<EntityDeltaResponse> {
|
||||||
|
return await transceivePost<EntityDeltaRequest, EntityDeltaResponse>('entity.delta', request);
|
||||||
|
},
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default entityService;
|
export default entityService;
|
||||||
|
|||||||
@@ -1,16 +1,4 @@
|
|||||||
/**
|
|
||||||
* Central export point for all Chrono Manager services
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Services
|
|
||||||
export { providerService } from './providerService';
|
export { providerService } from './providerService';
|
||||||
export { serviceService } from './serviceService';
|
export { serviceService } from './serviceService';
|
||||||
export { collectionService } from './collectionService';
|
export { collectionService } from './collectionService';
|
||||||
export { entityService } from './entityService';
|
export { entityService } from './entityService';
|
||||||
|
|
||||||
// Type exports
|
|
||||||
export type * from '../types/common';
|
|
||||||
export type * from '../types/provider';
|
|
||||||
export type * from '../types/service';
|
|
||||||
export type * from '../types/collection';
|
|
||||||
export type * from '../types/entity';
|
|
||||||
|
|||||||
@@ -2,34 +2,74 @@
|
|||||||
* Provider management service
|
* Provider management service
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { createFetchWrapper } from '@KTXC/utils/helpers/fetch-wrapper-core';
|
import type {
|
||||||
|
ProviderListRequest,
|
||||||
|
ProviderListResponse,
|
||||||
|
ProviderExtantRequest,
|
||||||
|
ProviderExtantResponse,
|
||||||
|
ProviderFetchRequest,
|
||||||
|
ProviderFetchResponse,
|
||||||
|
ProviderInterface,
|
||||||
|
} from '../types/provider';
|
||||||
|
import { useIntegrationStore } from '@KTXC/stores/integrationStore';
|
||||||
|
import { transceivePost } from './transceive';
|
||||||
|
import { ProviderObject } from '../models/provider';
|
||||||
|
|
||||||
const fetchWrapper = createFetchWrapper();
|
/**
|
||||||
import type { ProviderListResponse, ProviderExtantResponse } from '../types/provider';
|
* Helper to create the right provider model class based on provider identifier
|
||||||
import type { SourceSelector } from '../types/common';
|
* Uses provider-specific factory if available, otherwise returns base ProviderObject
|
||||||
|
*/
|
||||||
const BASE_URL = '/m/chrono_manager/provider';
|
function createProviderObject(data: ProviderInterface): ProviderObject {
|
||||||
|
const integrationStore = useIntegrationStore();
|
||||||
|
const factoryItem = integrationStore.getItemById('chrono_provider_factory', data.identifier) as any;
|
||||||
|
const factory = factoryItem?.factory;
|
||||||
|
|
||||||
|
// Use provider factory if available, otherwise base class
|
||||||
|
return factory ? factory(data) : new ProviderObject().fromJson(data);
|
||||||
|
}
|
||||||
|
|
||||||
export const providerService = {
|
export const providerService = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List all available providers
|
* Retrieve list of providers, optionally filtered by source selector
|
||||||
*
|
*
|
||||||
* @returns Promise with provider list keyed by provider ID
|
* @param request - list request parameters
|
||||||
|
*
|
||||||
|
* @returns Promise with provider object list keyed by provider identifier
|
||||||
*/
|
*/
|
||||||
async list(): Promise<ProviderListResponse> {
|
async list(request: ProviderListRequest = {}): Promise<Record<string, ProviderObject>> {
|
||||||
return await fetchWrapper.get(`${BASE_URL}/list`);
|
const response = await transceivePost<ProviderListRequest, ProviderListResponse>('provider.list', request);
|
||||||
|
|
||||||
|
// Convert response to ProviderObject instances
|
||||||
|
const list: Record<string, ProviderObject> = {};
|
||||||
|
Object.entries(response).forEach(([providerId, providerData]) => {
|
||||||
|
list[providerId] = createProviderObject(providerData);
|
||||||
|
});
|
||||||
|
|
||||||
|
return list;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check which providers exist/are available
|
* Retrieve specific provider by identifier
|
||||||
*
|
*
|
||||||
* @param sources - Source selector with provider IDs to check
|
* @param request - fetch request parameters
|
||||||
|
*
|
||||||
|
* @returns Promise with provider object
|
||||||
|
*/
|
||||||
|
async fetch(request: ProviderFetchRequest): Promise<ProviderObject> {
|
||||||
|
const response = await transceivePost<ProviderFetchRequest, ProviderFetchResponse>('provider.fetch', request);
|
||||||
|
return createProviderObject(response);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve provider availability status for a given source selector
|
||||||
|
*
|
||||||
|
* @param request - extant request parameters
|
||||||
*
|
*
|
||||||
* @returns Promise with provider availability status
|
* @returns Promise with provider availability status
|
||||||
*/
|
*/
|
||||||
async extant(sources: SourceSelector): Promise<ProviderExtantResponse> {
|
async extant(request: ProviderExtantRequest): Promise<ProviderExtantResponse> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/extant`, { sources });
|
return await transceivePost<ProviderExtantRequest, ProviderExtantResponse>('provider.extant', request);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -2,50 +2,161 @@
|
|||||||
* Service management service
|
* Service management service
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { createFetchWrapper } from '@KTXC/utils/helpers/fetch-wrapper-core';
|
|
||||||
|
|
||||||
const fetchWrapper = createFetchWrapper();
|
|
||||||
import type {
|
import type {
|
||||||
ServiceListRequest,
|
ServiceListRequest,
|
||||||
ServiceListResponse,
|
ServiceListResponse,
|
||||||
ServiceExtantRequest,
|
|
||||||
ServiceExtantResponse,
|
|
||||||
ServiceFetchRequest,
|
ServiceFetchRequest,
|
||||||
ServiceFetchResponse,
|
ServiceFetchResponse,
|
||||||
|
ServiceExtantRequest,
|
||||||
|
ServiceExtantResponse,
|
||||||
|
ServiceCreateResponse,
|
||||||
|
ServiceCreateRequest,
|
||||||
|
ServiceUpdateResponse,
|
||||||
|
ServiceUpdateRequest,
|
||||||
|
ServiceDeleteResponse,
|
||||||
|
ServiceDeleteRequest,
|
||||||
|
ServiceDiscoverRequest,
|
||||||
|
ServiceDiscoverResponse,
|
||||||
|
ServiceTestRequest,
|
||||||
|
ServiceTestResponse,
|
||||||
|
ServiceInterface,
|
||||||
} from '../types/service';
|
} from '../types/service';
|
||||||
|
import { useIntegrationStore } from '@KTXC/stores/integrationStore';
|
||||||
|
import { transceivePost } from './transceive';
|
||||||
|
import { ServiceObject } from '../models/service';
|
||||||
|
|
||||||
const BASE_URL = '/m/chrono_manager/service';
|
/**
|
||||||
|
* Helper to create the right service model class based on provider identifier
|
||||||
|
* Uses provider-specific factory if available, otherwise returns base ServiceObject
|
||||||
|
*/
|
||||||
|
function createServiceObject(data: ServiceInterface): ServiceObject {
|
||||||
|
const integrationStore = useIntegrationStore();
|
||||||
|
const factoryItem = integrationStore.getItemById('chrono_service_factory', data.provider) as any;
|
||||||
|
const factory = factoryItem?.factory;
|
||||||
|
|
||||||
|
// Use provider factory if available, otherwise base class
|
||||||
|
return factory ? factory(data) : new ServiceObject().fromJson(data);
|
||||||
|
}
|
||||||
|
|
||||||
export const serviceService = {
|
export const serviceService = {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List all available services
|
* Retrieve list of services, optionally filtered by source selector
|
||||||
*
|
*
|
||||||
* @param request - Service list request parameters
|
* @param request - list request parameters
|
||||||
* @returns Promise with service list grouped by provider
|
*
|
||||||
|
* @returns Promise with service object list grouped by provider and keyed by service identifier
|
||||||
*/
|
*/
|
||||||
async list(request: ServiceListRequest = {}): Promise<ServiceListResponse> {
|
async list(request: ServiceListRequest = {}): Promise<Record<string, Record<string, ServiceObject>>> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/list`, request);
|
const response = await transceivePost<ServiceListRequest, ServiceListResponse>('service.list', request);
|
||||||
|
|
||||||
|
// Convert nested response to ServiceObject instances
|
||||||
|
const providerList: Record<string, Record<string, ServiceObject>> = {};
|
||||||
|
Object.entries(response).forEach(([providerId, providerServices]) => {
|
||||||
|
const serviceList: Record<string, ServiceObject> = {};
|
||||||
|
Object.entries(providerServices).forEach(([serviceId, serviceData]) => {
|
||||||
|
serviceList[serviceId] = createServiceObject(serviceData);
|
||||||
|
});
|
||||||
|
providerList[providerId] = serviceList;
|
||||||
|
});
|
||||||
|
|
||||||
|
return providerList;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check which services exist/are available
|
* Retrieve a specific service by provider and identifier
|
||||||
|
*
|
||||||
|
* @param request - fetch request parameters
|
||||||
|
*
|
||||||
|
* @returns Promise with service object
|
||||||
|
*/
|
||||||
|
async fetch(request: ServiceFetchRequest): Promise<ServiceObject> {
|
||||||
|
const response = await transceivePost<ServiceFetchRequest, ServiceFetchResponse>('service.fetch', request);
|
||||||
|
return createServiceObject(response);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve service availability status for a given source selector
|
||||||
|
*
|
||||||
|
* @param request - extant request parameters
|
||||||
*
|
*
|
||||||
* @param request - Service extant request with source selector
|
|
||||||
* @returns Promise with service availability status
|
* @returns Promise with service availability status
|
||||||
*/
|
*/
|
||||||
async extant(request: ServiceExtantRequest): Promise<ServiceExtantResponse> {
|
async extant(request: ServiceExtantRequest): Promise<ServiceExtantResponse> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/extant`, request);
|
return await transceivePost<ServiceExtantRequest, ServiceExtantResponse>('service.extant', request);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch a specific service
|
* Retrieve discoverable services for a given source selector, sorted by provider
|
||||||
*
|
*
|
||||||
* @param request - Service fetch request with provider and service IDs
|
* @param request - discover request parameters
|
||||||
* @returns Promise with service details
|
*
|
||||||
|
* @returns Promise with array of discovered services sorted by provider
|
||||||
*/
|
*/
|
||||||
async fetch(request: ServiceFetchRequest): Promise<ServiceFetchResponse> {
|
async discover(request: ServiceDiscoverRequest): Promise<ServiceObject[]> {
|
||||||
return await fetchWrapper.post(`${BASE_URL}/fetch`, request);
|
const response = await transceivePost<ServiceDiscoverRequest, ServiceDiscoverResponse>('service.discover', request);
|
||||||
|
|
||||||
|
// Convert discovery results to ServiceObjects
|
||||||
|
const services: ServiceObject[] = [];
|
||||||
|
Object.entries(response).forEach(([providerId, location]) => {
|
||||||
|
const serviceData: ServiceInterface = {
|
||||||
|
'@type': 'chrono:service',
|
||||||
|
provider: providerId,
|
||||||
|
identifier: null,
|
||||||
|
label: null,
|
||||||
|
enabled: false,
|
||||||
|
location: location,
|
||||||
|
};
|
||||||
|
services.push(createServiceObject(serviceData));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Sort by provider
|
||||||
|
return services.sort((a, b) => a.provider.localeCompare(b.provider));
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test service connectivity and configuration
|
||||||
|
*
|
||||||
|
* @param request - Service test request
|
||||||
|
* @returns Promise with test results
|
||||||
|
*/
|
||||||
|
async test(request: ServiceTestRequest): Promise<ServiceTestResponse> {
|
||||||
|
return await transceivePost<ServiceTestRequest, ServiceTestResponse>('service.test', request);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new service
|
||||||
|
*
|
||||||
|
* @param request - create request parameters
|
||||||
|
*
|
||||||
|
* @returns Promise with created service object
|
||||||
|
*/
|
||||||
|
async create(request: ServiceCreateRequest): Promise<ServiceObject> {
|
||||||
|
const response = await transceivePost<ServiceCreateRequest, ServiceCreateResponse>('service.create', request);
|
||||||
|
return createServiceObject(response);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update a existing service
|
||||||
|
*
|
||||||
|
* @param request - update request parameters
|
||||||
|
*
|
||||||
|
* @returns Promise with updated service object
|
||||||
|
*/
|
||||||
|
async update(request: ServiceUpdateRequest): Promise<ServiceObject> {
|
||||||
|
const response = await transceivePost<ServiceUpdateRequest, ServiceUpdateResponse>('service.update', request);
|
||||||
|
return createServiceObject(response);
|
||||||
|
},
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a service
|
||||||
|
*
|
||||||
|
* @param request - delete request parameters
|
||||||
|
*
|
||||||
|
* @returns Promise with deletion result
|
||||||
|
*/
|
||||||
|
async delete(request: { provider: string; identifier: string | number }): Promise<any> {
|
||||||
|
return await transceivePost<ServiceDeleteRequest, ServiceDeleteResponse>('service.delete', request);
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
50
src/services/transceive.ts
Normal file
50
src/services/transceive.ts
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/**
|
||||||
|
* API Client for Chrono Manager
|
||||||
|
* Provides a centralized way to make API calls with envelope wrapping/unwrapping
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { createFetchWrapper } from '@KTXC/utils/helpers/fetch-wrapper-core';
|
||||||
|
import type { ApiRequest, ApiResponse } from '../types/common';
|
||||||
|
|
||||||
|
const fetchWrapper = createFetchWrapper();
|
||||||
|
const API_URL = '/m/chrono_manager/v1';
|
||||||
|
const API_VERSION = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a unique transaction ID
|
||||||
|
*/
|
||||||
|
export function generateTransactionId(): string {
|
||||||
|
return `${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make an API call with automatic envelope wrapping and unwrapping
|
||||||
|
*
|
||||||
|
* @param operation - Operation name (e.g., 'provider.list', 'service.autodiscover')
|
||||||
|
* @param data - Operation-specific request data
|
||||||
|
* @param user - Optional user identifier override
|
||||||
|
* @returns Promise with unwrapped response data
|
||||||
|
* @throws Error if the API returns an error status
|
||||||
|
*/
|
||||||
|
export async function transceivePost<TRequest, TResponse>(
|
||||||
|
operation: string,
|
||||||
|
data: TRequest,
|
||||||
|
user?: string
|
||||||
|
): Promise<TResponse> {
|
||||||
|
const request: ApiRequest<TRequest> = {
|
||||||
|
version: API_VERSION,
|
||||||
|
transaction: generateTransactionId(),
|
||||||
|
operation,
|
||||||
|
data,
|
||||||
|
user
|
||||||
|
};
|
||||||
|
|
||||||
|
const response: ApiResponse<TResponse> = await fetchWrapper.post(API_URL, request);
|
||||||
|
|
||||||
|
if (response.status === 'error') {
|
||||||
|
const errorMessage = `[${operation}] ${response.data.message}${response.data.code ? ` (code: ${response.data.code})` : ''}`;
|
||||||
|
throw new Error(errorMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response.data;
|
||||||
|
}
|
||||||
@@ -1,202 +1,307 @@
|
|||||||
/**
|
/**
|
||||||
* Chrono Manager - Collections Store
|
* Collections Store
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { defineStore } from 'pinia';
|
import { ref, computed, readonly } from 'vue'
|
||||||
import { ref } from 'vue';
|
import { defineStore } from 'pinia'
|
||||||
import { collectionService } from '../services/collectionService';
|
import { collectionService } from '../services'
|
||||||
import type {
|
import { CollectionObject, CollectionPropertiesObject } from '../models/collection'
|
||||||
SourceSelector,
|
import type { SourceSelector, ListFilter, ListSort } from '../types'
|
||||||
ListFilter,
|
|
||||||
ListSort,
|
|
||||||
} from '../types/common';
|
|
||||||
import { CollectionObject } from '../models/collection';
|
|
||||||
import type { ServiceObject } from '../models/service';
|
|
||||||
import type { CollectionInterface } from '../types/collection';
|
|
||||||
|
|
||||||
export const useCollectionsStore = defineStore('chronoCollectionsStore', () => {
|
export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
|
||||||
// State
|
// State
|
||||||
const collections = ref<CollectionObject[]>([]);
|
const _collections = ref<Record<string, CollectionObject>>({})
|
||||||
|
const transceiving = ref(false)
|
||||||
// Actions
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve collections from the server
|
* Get count of collections in store
|
||||||
*/
|
*/
|
||||||
async function list(
|
const count = computed(() => Object.keys(_collections.value).length)
|
||||||
sources?: SourceSelector,
|
|
||||||
filter?: ListFilter,
|
|
||||||
sort?: ListSort,
|
|
||||||
uid?: string
|
|
||||||
): Promise<CollectionObject[]> {
|
|
||||||
try {
|
|
||||||
const response = await collectionService.list({ sources, filter, sort, uid });
|
|
||||||
|
|
||||||
// Flatten the nested response into a flat array
|
|
||||||
const flatCollections: CollectionObject[] = [];
|
|
||||||
Object.entries(response).forEach(([_providerId, providerCollections]) => {
|
|
||||||
Object.entries(providerCollections).forEach(([_serviceId, serviceCollections]) => {
|
|
||||||
Object.values(serviceCollections).forEach((collection: CollectionInterface) => {
|
|
||||||
flatCollections.push(new CollectionObject().fromJson(collection));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
console.debug('[Chrono Manager](Store) - Successfully retrieved', flatCollections.length, 'collections:', flatCollections.map(c => ({
|
|
||||||
id: c.id,
|
|
||||||
label: c.label,
|
|
||||||
service: c.service,
|
|
||||||
provider: c.provider
|
|
||||||
})));
|
|
||||||
|
|
||||||
collections.value = flatCollections;
|
|
||||||
return flatCollections;
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error('[Chrono Manager](Store) - Failed to retrieve collections:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch a specific collection
|
* Check if any collections are present in store
|
||||||
*/
|
*/
|
||||||
async function fetch(
|
const has = computed(() => count.value > 0)
|
||||||
provider: string,
|
|
||||||
service: string,
|
|
||||||
identifier: string | number,
|
|
||||||
uid?: string
|
|
||||||
): Promise<CollectionObject | null> {
|
|
||||||
try {
|
|
||||||
const response = await collectionService.fetch({ provider, service, identifier, uid });
|
|
||||||
|
|
||||||
return new CollectionObject().fromJson(response);
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error('[Chrono Manager](Store) - Failed to fetch collection:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a fresh collection object with default values
|
* Get all collections present in store
|
||||||
*/
|
*/
|
||||||
function fresh(): CollectionObject {
|
const collections = computed(() => Object.values(_collections.value))
|
||||||
return new CollectionObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new collection
|
* Get all collections present in store grouped by service
|
||||||
*/
|
*/
|
||||||
async function create(
|
const collectionsByService = computed(() => {
|
||||||
service: ServiceObject,
|
const groups: Record<string, CollectionObject[]> = {}
|
||||||
collection: CollectionObject,
|
|
||||||
options?: string[],
|
Object.values(_collections.value).forEach((collection) => {
|
||||||
uid?: string
|
const serviceKey = `${collection.provider}:${collection.service}`
|
||||||
): Promise<CollectionObject | null> {
|
if (!groups[serviceKey]) {
|
||||||
try {
|
groups[serviceKey] = []
|
||||||
if (service.provider === null || service.id === null) {
|
|
||||||
throw new Error('Invalid service object, must have a provider and identifier');
|
|
||||||
}
|
}
|
||||||
|
groups[serviceKey].push(collection)
|
||||||
|
})
|
||||||
|
|
||||||
|
return groups
|
||||||
|
})
|
||||||
|
|
||||||
const response = await collectionService.create({
|
/**
|
||||||
provider: service.provider,
|
* Get a specific collection from store, with optional retrieval
|
||||||
service: service.id,
|
*
|
||||||
data: collection.toJson(),
|
* @param provider - provider identifier
|
||||||
options,
|
* @param service - service identifier
|
||||||
uid
|
* @param identifier - collection identifier
|
||||||
});
|
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||||
|
*
|
||||||
const createdCollection = new CollectionObject().fromJson(response);
|
* @returns Collection object or null
|
||||||
collections.value.push(createdCollection);
|
*/
|
||||||
|
function collection(provider: string, service: string | number, identifier: string | number, retrieve: boolean = false): CollectionObject | null {
|
||||||
console.debug('[Chrono Manager](Store) - Successfully created collection');
|
const key = identifierKey(provider, service, identifier)
|
||||||
|
if (retrieve === true && !_collections.value[key]) {
|
||||||
return createdCollection;
|
console.debug(`[Chrono Manager][Store] - Force fetching collection "${key}"`)
|
||||||
} catch (error: any) {
|
fetch(provider, service, identifier)
|
||||||
console.error('[Chrono Manager](Store) - Failed to create collection:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return _collections.value[key] || null
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modify an existing collection
|
* Get all collections for a specific service
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier
|
||||||
|
* @param service - service identifier
|
||||||
|
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||||
|
*
|
||||||
|
* @returns Array of collection objects
|
||||||
*/
|
*/
|
||||||
async function modify(
|
function collectionsForService(provider: string, service: string | number, retrieve: boolean = false): CollectionObject[] {
|
||||||
collection: CollectionObject,
|
const serviceKeyPrefix = `${provider}:${service}:`
|
||||||
uid?: string
|
const serviceCollections = Object.entries(_collections.value)
|
||||||
): Promise<CollectionObject | null> {
|
.filter(([key]) => key.startsWith(serviceKeyPrefix))
|
||||||
try {
|
.map(([_, collection]) => collection)
|
||||||
if (!collection.provider || !collection.service || !collection.id) {
|
|
||||||
throw new Error('Collection must have provider, service, and id');
|
if (retrieve === true && serviceCollections.length === 0) {
|
||||||
}
|
console.debug(`[Chrono Manager][Store] - Force fetching collections for service "${provider}:${service}"`)
|
||||||
|
const sources: SourceSelector = {
|
||||||
const response = await collectionService.modify({
|
[provider]: {
|
||||||
provider: collection.provider,
|
[String(service)]: true
|
||||||
service: collection.service,
|
|
||||||
identifier: collection.id,
|
|
||||||
data: collection.toJson(),
|
|
||||||
uid
|
|
||||||
});
|
|
||||||
|
|
||||||
const modifiedCollection = new CollectionObject().fromJson(response);
|
|
||||||
const index = collections.value.findIndex(c => c.id === collection.id);
|
|
||||||
if (index !== -1) {
|
|
||||||
collections.value[index] = modifiedCollection;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.debug('[Chrono Manager](Store) - Successfully modified collection');
|
|
||||||
|
|
||||||
return modifiedCollection;
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error('[Chrono Manager](Store) - Failed to modify collection:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete a collection
|
|
||||||
*/
|
|
||||||
async function destroy(
|
|
||||||
collection: CollectionObject,
|
|
||||||
uid?: string
|
|
||||||
): Promise<boolean> {
|
|
||||||
try {
|
|
||||||
if (!collection.provider || !collection.service || !collection.id) {
|
|
||||||
throw new Error('Collection must have provider, service, and id');
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await collectionService.destroy({
|
|
||||||
provider: collection.provider,
|
|
||||||
service: collection.service,
|
|
||||||
identifier: collection.id,
|
|
||||||
uid
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.success) {
|
|
||||||
const index = collections.value.findIndex(c => c.id === collection.id);
|
|
||||||
if (index !== -1) {
|
|
||||||
collections.value.splice(index, 1);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
list(sources)
|
||||||
console.debug('[Chrono Manager](Store) - Successfully destroyed collection');
|
}
|
||||||
|
|
||||||
return response.success;
|
return serviceCollections
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create unique key for a collection
|
||||||
|
*/
|
||||||
|
function identifierKey(provider: string, service: string | number | null, identifier: string | number | null): string {
|
||||||
|
return `${provider}:${service ?? ''}:${identifier ?? ''}`
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve all or specific collections, optionally filtered by source selector
|
||||||
|
*
|
||||||
|
* @param sources - optional source selector
|
||||||
|
* @param filter - optional list filter
|
||||||
|
* @param sort - optional list sort
|
||||||
|
*
|
||||||
|
* @returns Promise with collection object list keyed by provider, service, and collection identifier
|
||||||
|
*/
|
||||||
|
async function list(sources?: SourceSelector, filter?: ListFilter, sort?: ListSort): Promise<Record<string, CollectionObject>> {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const response = await collectionService.list({ sources, filter, sort })
|
||||||
|
|
||||||
|
// Flatten nested structure: provider:service:collection -> "provider:service:collection": object
|
||||||
|
const collections: Record<string, CollectionObject> = {}
|
||||||
|
Object.entries(response).forEach(([_providerId, providerServices]) => {
|
||||||
|
Object.entries(providerServices).forEach(([_serviceId, serviceCollections]) => {
|
||||||
|
Object.entries(serviceCollections).forEach(([_collectionId, collectionObj]) => {
|
||||||
|
const key = identifierKey(collectionObj.provider, collectionObj.service, collectionObj.identifier)
|
||||||
|
collections[key] = collectionObj
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Merge retrieved collections into state
|
||||||
|
_collections.value = { ..._collections.value, ...collections }
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully retrieved', Object.keys(collections).length, 'collections')
|
||||||
|
return collections
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('[Chrono Manager](Store) - Failed to destroy collection:', error);
|
console.error('[Chrono Manager][Store] - Failed to retrieve collections:', error)
|
||||||
throw error;
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a specific collection by provider, service, and identifier
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier
|
||||||
|
* @param service - service identifier
|
||||||
|
* @param identifier - collection identifier
|
||||||
|
*
|
||||||
|
* @returns Promise with collection object
|
||||||
|
*/
|
||||||
|
async function fetch(provider: string, service: string | number, identifier: string | number): Promise<CollectionObject> {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const response = await collectionService.fetch({ provider, service, collection: identifier })
|
||||||
|
|
||||||
|
// Merge fetched collection into state
|
||||||
|
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||||
|
_collections.value[key] = response
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully fetched collection:', key)
|
||||||
|
return response
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to fetch collection:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
/**
|
||||||
// State
|
* Retrieve collection availability status for a given source selector
|
||||||
collections,
|
*
|
||||||
|
* @param sources - source selector to check availability for
|
||||||
|
*
|
||||||
|
* @returns Promise with collection availability status
|
||||||
|
*/
|
||||||
|
async function extant(sources: SourceSelector) {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const response = await collectionService.extant({ sources })
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'collections')
|
||||||
|
return response
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to check collections:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new collection with given provider, service, and data
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier for the new collection
|
||||||
|
* @param service - service identifier for the new collection
|
||||||
|
* @param collection - optional parent collection identifier
|
||||||
|
* @param data - collection properties for creation
|
||||||
|
*
|
||||||
|
* @returns Promise with created collection object
|
||||||
|
*/
|
||||||
|
async function create(provider: string, service: string | number, collection: string | number | null, data: CollectionPropertiesObject): Promise<CollectionObject> {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const response = await collectionService.create({
|
||||||
|
provider,
|
||||||
|
service,
|
||||||
|
collection,
|
||||||
|
properties: data
|
||||||
|
})
|
||||||
|
|
||||||
|
// Merge created collection into state
|
||||||
|
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||||
|
_collections.value[key] = response
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully created collection:', key)
|
||||||
|
return response
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to create collection:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update an existing collection with given provider, service, identifier, and data
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier for the collection to update
|
||||||
|
* @param service - service identifier for the collection to update
|
||||||
|
* @param identifier - collection identifier for the collection to update
|
||||||
|
* @param data - collection properties for update
|
||||||
|
*
|
||||||
|
* @returns Promise with updated collection object
|
||||||
|
*/
|
||||||
|
async function update(provider: string, service: string | number, identifier: string | number, data: CollectionPropertiesObject): Promise<CollectionObject> {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const response = await collectionService.update({
|
||||||
|
provider,
|
||||||
|
service,
|
||||||
|
identifier,
|
||||||
|
properties: data
|
||||||
|
})
|
||||||
|
|
||||||
|
// Merge updated collection into state
|
||||||
|
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||||
|
_collections.value[key] = response
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully updated collection:', key)
|
||||||
|
return response
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to update collection:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a collection by provider, service, and identifier
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier for the collection to delete
|
||||||
|
* @param service - service identifier for the collection to delete
|
||||||
|
* @param identifier - collection identifier for the collection to delete
|
||||||
|
*
|
||||||
|
* @returns Promise with deletion result
|
||||||
|
*/
|
||||||
|
async function remove(provider: string, service: string | number, identifier: string | number): Promise<any> {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
await collectionService.delete({ provider, service, identifier })
|
||||||
|
|
||||||
|
// Remove deleted collection from state
|
||||||
|
const key = identifierKey(provider, service, identifier)
|
||||||
|
delete _collections.value[key]
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully deleted collection:', key)
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to delete collection:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return public API
|
||||||
|
return {
|
||||||
|
// State (readonly)
|
||||||
|
transceiving: readonly(transceiving),
|
||||||
|
// Getters
|
||||||
|
count,
|
||||||
|
has,
|
||||||
|
collections,
|
||||||
|
collectionsByService,
|
||||||
|
collectionsForService,
|
||||||
// Actions
|
// Actions
|
||||||
|
collection,
|
||||||
list,
|
list,
|
||||||
fetch,
|
fetch,
|
||||||
fresh,
|
extant,
|
||||||
create,
|
create,
|
||||||
modify,
|
update,
|
||||||
destroy,
|
delete: remove,
|
||||||
};
|
}
|
||||||
});
|
})
|
||||||
|
|||||||
@@ -1,281 +1,346 @@
|
|||||||
/**
|
/**
|
||||||
* Chrono Manager - Entities Store
|
* Entities Store
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { defineStore } from 'pinia';
|
import { ref, computed, readonly } from 'vue'
|
||||||
import { ref } from 'vue';
|
import { defineStore } from 'pinia'
|
||||||
import { entityService } from '../services/entityService';
|
import { entityService } from '../services'
|
||||||
import { EntityObject } from '../models/entity';
|
import { EntityObject } from '../models'
|
||||||
import { EventObject } from '../models/event';
|
import type { SourceSelector, ListFilter, ListSort, ListRange } from '../types/common'
|
||||||
import { TaskObject } from '../models/task';
|
|
||||||
import { JournalObject } from '../models/journal';
|
|
||||||
import { CollectionObject } from '../models/collection';
|
|
||||||
import type {
|
|
||||||
SourceSelector,
|
|
||||||
ListFilter,
|
|
||||||
ListSort,
|
|
||||||
ListRange,
|
|
||||||
} from '../types/common';
|
|
||||||
import type {
|
|
||||||
EntityInterface,
|
|
||||||
} from '../types/entity';
|
|
||||||
|
|
||||||
export const useEntitiesStore = defineStore('chronoEntitiesStore', () => {
|
export const useEntitiesStore = defineStore('chronoEntitiesStore', () => {
|
||||||
// State
|
// State
|
||||||
const entities = ref<EntityObject[]>([]);
|
const _entities = ref<Record<string, EntityObject>>({})
|
||||||
|
const transceiving = ref(false)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get count of entities in store
|
||||||
|
*/
|
||||||
|
const count = computed(() => Object.keys(_entities.value).length)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if any entities are present in store
|
||||||
|
*/
|
||||||
|
const has = computed(() => count.value > 0)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all entities present in store
|
||||||
|
*/
|
||||||
|
const entities = computed(() => Object.values(_entities.value))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a specific entity from store, with optional retrieval
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier
|
||||||
|
* @param service - service identifier
|
||||||
|
* @param collection - collection identifier
|
||||||
|
* @param identifier - entity identifier
|
||||||
|
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||||
|
*
|
||||||
|
* @returns Entity object or null
|
||||||
|
*/
|
||||||
|
function entity(provider: string, service: string | number, collection: string | number, identifier: string | number, retrieve: boolean = false): EntityObject | null {
|
||||||
|
const key = identifierKey(provider, service, collection, identifier)
|
||||||
|
if (retrieve === true && !_entities.value[key]) {
|
||||||
|
console.debug(`[Chrono Manager][Store] - Force fetching entity "${key}"`)
|
||||||
|
fetch(provider, service, collection, [identifier])
|
||||||
|
}
|
||||||
|
|
||||||
|
return _entities.value[key] || null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all entities for a specific collection
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier
|
||||||
|
* @param service - service identifier
|
||||||
|
* @param collection - collection identifier
|
||||||
|
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||||
|
*
|
||||||
|
* @returns Array of entity objects
|
||||||
|
*/
|
||||||
|
function entitiesForCollection(provider: string, service: string | number, collection: string | number, retrieve: boolean = false): EntityObject[] {
|
||||||
|
const collectionKeyPrefix = `${provider}:${service}:${collection}:`
|
||||||
|
const collectionEntities = Object.entries(_entities.value)
|
||||||
|
.filter(([key]) => key.startsWith(collectionKeyPrefix))
|
||||||
|
.map(([_, entity]) => entity)
|
||||||
|
|
||||||
|
if (retrieve === true && collectionEntities.length === 0) {
|
||||||
|
console.debug(`[Chrono Manager][Store] - Force fetching entities for collection "${provider}:${service}:${collection}"`)
|
||||||
|
const sources: SourceSelector = {
|
||||||
|
[provider]: {
|
||||||
|
[String(service)]: {
|
||||||
|
[String(collection)]: true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
list(sources)
|
||||||
|
}
|
||||||
|
|
||||||
|
return collectionEntities
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create unique key for an entity
|
||||||
|
*/
|
||||||
|
function identifierKey(provider: string, service: string | number, collection: string | number, identifier: string | number): string {
|
||||||
|
return `${provider}:${service}:${collection}:${identifier}`
|
||||||
|
}
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset the store to initial state
|
* Retrieve all or specific entities, optionally filtered by source selector
|
||||||
|
*
|
||||||
|
* @param sources - optional source selector
|
||||||
|
* @param filter - optional list filter
|
||||||
|
* @param sort - optional list sort
|
||||||
|
* @param range - optional list range
|
||||||
|
*
|
||||||
|
* @returns Promise with entity object list keyed by identifier
|
||||||
*/
|
*/
|
||||||
function reset(): void {
|
async function list(sources?: SourceSelector, filter?: ListFilter, sort?: ListSort, range?: ListRange): Promise<Record<string, EntityObject>> {
|
||||||
entities.value = [];
|
transceiving.value = true
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* List entities for all or specific collection
|
|
||||||
*/
|
|
||||||
async function list(
|
|
||||||
provider: string | null,
|
|
||||||
service: string | null,
|
|
||||||
collection: string | number | null,
|
|
||||||
filter?: ListFilter,
|
|
||||||
sort?: ListSort,
|
|
||||||
range?: ListRange,
|
|
||||||
uid?: string
|
|
||||||
): Promise<EntityObject[]> {
|
|
||||||
try {
|
try {
|
||||||
// Validate hierarchical requirements
|
const response = await entityService.list({ sources, filter, sort, range })
|
||||||
if (collection !== null && (service === null || provider === null)) {
|
|
||||||
throw new Error('Collection requires both service and provider');
|
|
||||||
}
|
|
||||||
if (service !== null && provider === null) {
|
|
||||||
throw new Error('Service requires provider');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build sources object level by level
|
// Flatten nested structure: provider:service:collection:entity -> "provider:service:collection:entity": object
|
||||||
const sources: SourceSelector = {};
|
const entities: Record<string, EntityObject> = {}
|
||||||
if (provider !== null) {
|
Object.entries(response).forEach(([providerId, providerServices]) => {
|
||||||
if (service !== null) {
|
Object.entries(providerServices).forEach(([serviceId, serviceCollections]) => {
|
||||||
if (collection !== null) {
|
Object.entries(serviceCollections).forEach(([collectionId, collectionEntities]) => {
|
||||||
sources[provider] = { [service]: { [collection]: true } };
|
Object.entries(collectionEntities).forEach(([entityId, entityData]) => {
|
||||||
} else {
|
const key = identifierKey(providerId, serviceId, collectionId, entityId)
|
||||||
sources[provider] = { [service]: true };
|
entities[key] = entityData
|
||||||
}
|
})
|
||||||
} else {
|
})
|
||||||
sources[provider] = true;
|
})
|
||||||
}
|
})
|
||||||
}
|
|
||||||
|
|
||||||
// Transmit
|
// Merge retrieved entities into state
|
||||||
const response = await entityService.list({ sources, filter, sort, range, uid });
|
_entities.value = { ..._entities.value, ...entities }
|
||||||
|
|
||||||
// Flatten the nested response into a flat array
|
console.debug('[Chrono Manager][Store] - Successfully retrieved', Object.keys(entities).length, 'entities')
|
||||||
const flatEntities: EntityObject[] = [];
|
return entities
|
||||||
Object.entries(response).forEach(([, providerEntities]) => {
|
|
||||||
Object.entries(providerEntities).forEach(([, serviceEntities]) => {
|
|
||||||
Object.entries(serviceEntities).forEach(([, collectionEntities]) => {
|
|
||||||
Object.values(collectionEntities).forEach((entity: EntityInterface) => {
|
|
||||||
flatEntities.push(new EntityObject().fromJson(entity));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
console.debug('[Chrono Manager](Store) - Successfully retrieved', flatEntities.length, 'entities');
|
|
||||||
|
|
||||||
entities.value = flatEntities;
|
|
||||||
return flatEntities;
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('[Chrono Manager](Store) - Failed to retrieve entities:', error);
|
console.error('[Chrono Manager][Store] - Failed to retrieve entities:', error)
|
||||||
throw error;
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch entities for a specific collection
|
* Retrieve specific entities by provider, service, collection, and identifiers
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier
|
||||||
|
* @param service - service identifier
|
||||||
|
* @param collection - collection identifier
|
||||||
|
* @param identifiers - array of entity identifiers to fetch
|
||||||
|
*
|
||||||
|
* @returns Promise with entity objects keyed by identifier
|
||||||
*/
|
*/
|
||||||
async function fetch(
|
async function fetch(provider: string, service: string | number, collection: string | number, identifiers: (string | number)[]): Promise<Record<string, EntityObject>> {
|
||||||
collection: CollectionObject,
|
transceiving.value = true
|
||||||
identifiers: (string | number)[],
|
|
||||||
uid?: string
|
|
||||||
): Promise<EntityObject[]> {
|
|
||||||
try {
|
try {
|
||||||
if (!collection.provider || !collection.service || !collection.id) {
|
const response = await entityService.fetch({ provider, service, collection, identifiers })
|
||||||
throw new Error('Collection must have provider, service, and id');
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await entityService.fetch({
|
|
||||||
provider: collection.provider,
|
|
||||||
service: collection.service,
|
|
||||||
collection: collection.id,
|
|
||||||
identifiers,
|
|
||||||
uid
|
|
||||||
});
|
|
||||||
|
|
||||||
return Object.values(response).map(entity => new EntityObject().fromJson(entity));
|
// Merge fetched entities into state
|
||||||
|
const entities: Record<string, EntityObject> = {}
|
||||||
|
Object.entries(response).forEach(([identifier, entityData]) => {
|
||||||
|
const key = identifierKey(provider, service, collection, identifier)
|
||||||
|
entities[key] = entityData
|
||||||
|
_entities.value[key] = entityData
|
||||||
|
})
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully fetched', Object.keys(entities).length, 'entities')
|
||||||
|
return entities
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('[Chrono Manager](Store) - Failed to fetch entities:', error);
|
console.error('[Chrono Manager][Store] - Failed to fetch entities:', error)
|
||||||
throw error;
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a fresh entity object
|
* Retrieve entity availability status for a given source selector
|
||||||
|
*
|
||||||
|
* @param sources - source selector to check availability for
|
||||||
|
*
|
||||||
|
* @returns Promise with entity availability status
|
||||||
*/
|
*/
|
||||||
function fresh(type: string): EntityObject {
|
async function extant(sources: SourceSelector) {
|
||||||
const entity = new EntityObject();
|
transceiving.value = true
|
||||||
|
|
||||||
if (type === 'event') {
|
|
||||||
entity.data = new EventObject();
|
|
||||||
} else if (type === 'task') {
|
|
||||||
entity.data = new TaskObject();
|
|
||||||
} else if (type === 'journal') {
|
|
||||||
entity.data = new JournalObject();
|
|
||||||
} else {
|
|
||||||
entity.data = new EventObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (entity.data) {
|
|
||||||
entity.data.created = new Date();
|
|
||||||
}
|
|
||||||
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new entity
|
|
||||||
*/
|
|
||||||
async function create(
|
|
||||||
collection: CollectionObject,
|
|
||||||
entity: EntityObject,
|
|
||||||
options?: string[],
|
|
||||||
uid?: string
|
|
||||||
): Promise<EntityObject | null> {
|
|
||||||
try {
|
try {
|
||||||
if (!collection.provider || !collection.service || !collection.id) {
|
const response = await entityService.extant({ sources })
|
||||||
throw new Error('Collection must have provider, service, and id');
|
console.debug('[Chrono Manager][Store] - Successfully checked entity availability')
|
||||||
}
|
return response
|
||||||
|
|
||||||
const response = await entityService.create({
|
|
||||||
provider: collection.provider,
|
|
||||||
service: collection.service,
|
|
||||||
collection: collection.id,
|
|
||||||
data: entity.toJson(),
|
|
||||||
options,
|
|
||||||
uid
|
|
||||||
});
|
|
||||||
|
|
||||||
const createdEntity = new EntityObject().fromJson(response);
|
|
||||||
entities.value.push(createdEntity);
|
|
||||||
|
|
||||||
console.debug('[Chrono Manager](Store) - Successfully created entity');
|
|
||||||
|
|
||||||
return createdEntity;
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('[Chrono Manager](Store) - Failed to create entity:', error);
|
console.error('[Chrono Manager][Store] - Failed to check entity availability:', error)
|
||||||
throw error;
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Modify an existing entity
|
* Create a new entity with given provider, service, collection, and data
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier for the new entity
|
||||||
|
* @param service - service identifier for the new entity
|
||||||
|
* @param collection - collection identifier for the new entity
|
||||||
|
* @param data - entity properties for creation
|
||||||
|
*
|
||||||
|
* @returns Promise with created entity object
|
||||||
*/
|
*/
|
||||||
async function modify(
|
async function create(provider: string, service: string | number, collection: string | number, data: any): Promise<EntityObject> {
|
||||||
collection: CollectionObject,
|
transceiving.value = true
|
||||||
entity: EntityObject,
|
|
||||||
uid?: string
|
|
||||||
): Promise<EntityObject | null> {
|
|
||||||
try {
|
try {
|
||||||
if (!collection.provider || !collection.service || !collection.id) {
|
const response = await entityService.create({ provider, service, collection, properties: data })
|
||||||
throw new Error('Collection must have provider, service, and id');
|
|
||||||
}
|
// Add created entity to state
|
||||||
if (!entity.in || !entity.id) {
|
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
|
||||||
throw new Error('Invalid entity object, must have an collection and entity identifier');
|
_entities.value[key] = response
|
||||||
}
|
|
||||||
if (collection.id !== entity.in) {
|
|
||||||
throw new Error('Invalid entity object, does not belong to the specified collection');
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await entityService.modify({
|
console.debug('[Chrono Manager][Store] - Successfully created entity:', key)
|
||||||
provider: collection.provider,
|
return response
|
||||||
service: collection.service,
|
|
||||||
collection: collection.id,
|
|
||||||
identifier: entity.id,
|
|
||||||
data: entity.toJson(),
|
|
||||||
uid
|
|
||||||
});
|
|
||||||
|
|
||||||
const modifiedEntity = new EntityObject().fromJson(response);
|
|
||||||
const index = entities.value.findIndex(e => e.id === entity.id);
|
|
||||||
if (index !== -1) {
|
|
||||||
entities.value[index] = modifiedEntity;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.debug('[Chrono Manager](Store) - Successfully modified entity');
|
|
||||||
|
|
||||||
return modifiedEntity;
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('[Chrono Manager](Store) - Failed to modify entity:', error);
|
console.error('[Chrono Manager][Store] - Failed to create entity:', error)
|
||||||
throw error;
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete an entity
|
* Update an existing entity with given provider, service, collection, identifier, and data
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier for the entity to update
|
||||||
|
* @param service - service identifier for the entity to update
|
||||||
|
* @param collection - collection identifier for the entity to update
|
||||||
|
* @param identifier - entity identifier for the entity to update
|
||||||
|
* @param data - entity properties for update
|
||||||
|
*
|
||||||
|
* @returns Promise with updated entity object
|
||||||
*/
|
*/
|
||||||
async function destroy(
|
async function update(provider: string, service: string | number, collection: string | number, identifier: string | number, data: any): Promise<EntityObject> {
|
||||||
collection: CollectionObject,
|
transceiving.value = true
|
||||||
entity: EntityObject,
|
|
||||||
uid?: string
|
|
||||||
): Promise<boolean> {
|
|
||||||
try {
|
try {
|
||||||
if (!collection.provider || !collection.service || !collection.id) {
|
const response = await entityService.update({ provider, service, collection, identifier, properties: data })
|
||||||
throw new Error('Collection must have provider, service, and id');
|
|
||||||
}
|
// Update entity in state
|
||||||
if (!entity.in || !entity.id) {
|
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
|
||||||
throw new Error('Invalid entity object, must have an collection and entity identifier');
|
_entities.value[key] = response
|
||||||
}
|
|
||||||
if (collection.id !== entity.in) {
|
|
||||||
throw new Error('Invalid entity object, does not belong to the specified collection');
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await entityService.destroy({
|
console.debug('[Chrono Manager][Store] - Successfully updated entity:', key)
|
||||||
provider: collection.provider,
|
return response
|
||||||
service: collection.service,
|
|
||||||
collection: collection.id,
|
|
||||||
identifier: entity.id,
|
|
||||||
uid
|
|
||||||
});
|
|
||||||
|
|
||||||
if (response.success) {
|
|
||||||
const index = entities.value.findIndex(e => e.id === entity.id);
|
|
||||||
if (index !== -1) {
|
|
||||||
entities.value.splice(index, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.debug('[Chrono Manager](Store) - Successfully destroyed entity');
|
|
||||||
|
|
||||||
return response.success;
|
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('[Chrono Manager](Store) - Failed to destroy entity:', error);
|
console.error('[Chrono Manager][Store] - Failed to update entity:', error)
|
||||||
throw error;
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete an entity by provider, service, collection, and identifier
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier for the entity to delete
|
||||||
|
* @param service - service identifier for the entity to delete
|
||||||
|
* @param collection - collection identifier for the entity to delete
|
||||||
|
* @param identifier - entity identifier for the entity to delete
|
||||||
|
*
|
||||||
|
* @returns Promise with deletion result
|
||||||
|
*/
|
||||||
|
async function remove(provider: string, service: string | number, collection: string | number, identifier: string | number): Promise<any> {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const response = await entityService.delete({ provider, service, collection, identifier })
|
||||||
|
|
||||||
|
// Remove entity from state
|
||||||
|
const key = identifierKey(provider, service, collection, identifier)
|
||||||
|
delete _entities.value[key]
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully deleted entity:', key)
|
||||||
|
return response
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to delete entity:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve delta changes for entities
|
||||||
|
*
|
||||||
|
* @param sources - source selector for delta check
|
||||||
|
*
|
||||||
|
* @returns Promise with delta changes (additions, modifications, deletions)
|
||||||
|
*
|
||||||
|
* Note: Delta returns only identifiers, not full entities.
|
||||||
|
* Caller should fetch full entities for additions/modifications separately.
|
||||||
|
*/
|
||||||
|
async function delta(sources: SourceSelector) {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const response = await entityService.delta({ sources })
|
||||||
|
|
||||||
|
// Process delta and update store
|
||||||
|
Object.entries(response).forEach(([provider, providerData]) => {
|
||||||
|
// Skip if no changes for provider
|
||||||
|
if (providerData === false) return
|
||||||
|
|
||||||
|
Object.entries(providerData).forEach(([service, serviceData]) => {
|
||||||
|
// Skip if no changes for service
|
||||||
|
if (serviceData === false) return
|
||||||
|
|
||||||
|
Object.entries(serviceData).forEach(([collection, collectionData]) => {
|
||||||
|
// Skip if no changes for collection
|
||||||
|
if (collectionData === false) return
|
||||||
|
|
||||||
|
// Process deletions (remove from store)
|
||||||
|
if (collectionData.deletions && collectionData.deletions.length > 0) {
|
||||||
|
collectionData.deletions.forEach((identifier) => {
|
||||||
|
const key = identifierKey(provider, service, collection, identifier)
|
||||||
|
delete _entities.value[key]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: additions and modifications contain only identifiers
|
||||||
|
// The caller should fetch full entities using the fetch() method
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully processed delta changes')
|
||||||
|
return response
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to process delta:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return public API
|
||||||
return {
|
return {
|
||||||
// State
|
// State (readonly)
|
||||||
|
transceiving: readonly(transceiving),
|
||||||
|
// Getters
|
||||||
|
count,
|
||||||
|
has,
|
||||||
entities,
|
entities,
|
||||||
|
entitiesForCollection,
|
||||||
// Actions
|
// Actions
|
||||||
reset,
|
entity,
|
||||||
list,
|
list,
|
||||||
fetch,
|
fetch,
|
||||||
fresh,
|
extant,
|
||||||
create,
|
create,
|
||||||
modify,
|
update,
|
||||||
destroy,
|
delete: remove,
|
||||||
};
|
delta,
|
||||||
});
|
}
|
||||||
|
})
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
/**
|
|
||||||
* Central export point for all Chrono Manager stores
|
|
||||||
*/
|
|
||||||
|
|
||||||
export { useCollectionsStore } from './collectionsStore';
|
export { useCollectionsStore } from './collectionsStore';
|
||||||
export { useEntitiesStore } from './entitiesStore';
|
export { useEntitiesStore } from './entitiesStore';
|
||||||
export { useProvidersStore } from './providersStore';
|
export { useProvidersStore } from './providersStore';
|
||||||
|
|||||||
@@ -1,62 +1,142 @@
|
|||||||
/**
|
/**
|
||||||
* Chrono Manager - Providers Store
|
* Providers Store
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { defineStore } from 'pinia';
|
import { ref, computed, readonly } from 'vue'
|
||||||
import { ref } from 'vue';
|
import { defineStore } from 'pinia'
|
||||||
import { providerService } from '../services/providerService';
|
import { providerService } from '../services'
|
||||||
import type {
|
import { ProviderObject } from '../models/provider'
|
||||||
SourceSelector,
|
import type { SourceSelector } from '../types'
|
||||||
ProviderInterface,
|
|
||||||
} from '../types';
|
|
||||||
|
|
||||||
export const useProvidersStore = defineStore('chronoProvidersStore', () => {
|
export const useProvidersStore = defineStore('chronoProvidersStore', () => {
|
||||||
// State
|
// State
|
||||||
const providers = ref<Record<string, ProviderInterface>>({});
|
const _providers = ref<Record<string, ProviderObject>>({})
|
||||||
|
const transceiving = ref(false)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get count of providers in store
|
||||||
|
*/
|
||||||
|
const count = computed(() => Object.keys(_providers.value).length)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if any providers are present in store
|
||||||
|
*/
|
||||||
|
const has = computed(() => count.value > 0)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all providers present in store
|
||||||
|
*/
|
||||||
|
const providers = computed(() => Object.values(_providers.value))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a specific provider from store, with optional retrieval
|
||||||
|
*
|
||||||
|
* @param identifier - Provider identifier
|
||||||
|
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||||
|
*
|
||||||
|
* @returns Provider object or null
|
||||||
|
*/
|
||||||
|
function provider(identifier: string, retrieve: boolean = false): ProviderObject | null {
|
||||||
|
if (retrieve === true && !_providers.value[identifier]) {
|
||||||
|
console.debug(`[Chrono Manager][Store] - Force fetching provider "${identifier}"`)
|
||||||
|
fetch(identifier)
|
||||||
|
}
|
||||||
|
|
||||||
|
return _providers.value[identifier] || null
|
||||||
|
}
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List all available providers
|
* Retrieve all or specific providers, optionally filtered by source selector
|
||||||
*
|
*
|
||||||
* @returns Promise with provider list keyed by provider ID
|
* @param request - list request parameters
|
||||||
|
*
|
||||||
|
* @returns Promise with provider object list keyed by provider identifier
|
||||||
*/
|
*/
|
||||||
async function list(): Promise<Record<string, ProviderInterface>> {
|
async function list(sources?: SourceSelector): Promise<Record<string, ProviderObject>> {
|
||||||
|
transceiving.value = true
|
||||||
try {
|
try {
|
||||||
const response = await providerService.list();
|
const providers = await providerService.list({ sources })
|
||||||
|
|
||||||
console.debug('[Chrono Manager](Store) - Successfully retrieved', Object.keys(response).length, 'providers:', Object.keys(response));
|
// Merge retrieved providers into state
|
||||||
|
_providers.value = { ..._providers.value, ...providers }
|
||||||
providers.value = response;
|
|
||||||
return response;
|
console.debug('[Chrono Manager][Store] - Successfully retrieved', Object.keys(providers).length, 'providers')
|
||||||
|
return providers
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('[Chrono Manager](Store) - Failed to retrieve providers:', error);
|
console.error('[Chrono Manager][Store] - Failed to retrieve providers:', error)
|
||||||
throw error;
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check which providers exist/are available
|
* Retrieve a specific provider by identifier
|
||||||
*
|
*
|
||||||
* @param sources - Source selector with provider IDs to check
|
* @param identifier - provider identifier
|
||||||
* @returns Promise with provider availability status
|
*
|
||||||
|
* @returns Promise with provider object
|
||||||
*/
|
*/
|
||||||
async function extant(sources: SourceSelector): Promise<Record<string, boolean>> {
|
async function fetch(identifier: string): Promise<ProviderObject> {
|
||||||
|
transceiving.value = true
|
||||||
try {
|
try {
|
||||||
const response = await providerService.extant(sources);
|
const provider = await providerService.fetch({ identifier })
|
||||||
return response;
|
|
||||||
|
// Merge fetched provider into state
|
||||||
|
_providers.value[provider.identifier] = provider
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully fetched provider:', provider.identifier)
|
||||||
|
return provider
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('[Chrono Manager](Store) - Failed to check provider existence:', error);
|
console.error('[Chrono Manager][Store] - Failed to fetch provider:', error)
|
||||||
throw error;
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve provider availability status for a given source selector
|
||||||
|
*
|
||||||
|
* @param sources - source selector to check availability for
|
||||||
|
*
|
||||||
|
* @returns Promise with provider availability status
|
||||||
|
*/
|
||||||
|
async function extant(sources: SourceSelector) {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const response = await providerService.extant({ sources })
|
||||||
|
|
||||||
|
Object.entries(response).forEach(([providerId, providerStatus]) => {
|
||||||
|
if (providerStatus === false) {
|
||||||
|
delete _providers.value[providerId]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'providers')
|
||||||
|
return response
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to check providers:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return public API
|
||||||
return {
|
return {
|
||||||
// State
|
// State
|
||||||
|
transceiving: readonly(transceiving),
|
||||||
|
// computed
|
||||||
|
count,
|
||||||
|
has,
|
||||||
providers,
|
providers,
|
||||||
|
provider,
|
||||||
// Actions
|
// functions
|
||||||
list,
|
list,
|
||||||
|
fetch,
|
||||||
extant,
|
extant,
|
||||||
};
|
}
|
||||||
});
|
})
|
||||||
|
|||||||
@@ -1,95 +1,259 @@
|
|||||||
/**
|
/**
|
||||||
* Chrono Manager - Services Store
|
* Services Store
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { defineStore } from 'pinia';
|
import { ref, computed, readonly } from 'vue'
|
||||||
import { ref } from 'vue';
|
import { defineStore } from 'pinia'
|
||||||
import { serviceService } from '../services/serviceService';
|
import { serviceService } from '../services'
|
||||||
import { ServiceObject } from '../models/service';
|
import { ServiceObject } from '../models/service'
|
||||||
import type { ServiceInterface } from '../types/service';
|
|
||||||
import type {
|
import type {
|
||||||
SourceSelector,
|
SourceSelector,
|
||||||
ListFilter,
|
ServiceInterface,
|
||||||
ListSort,
|
} from '../types'
|
||||||
} from '../types/common';
|
|
||||||
|
|
||||||
export const useServicesStore = defineStore('chronoServicesStore', () => {
|
export const useServicesStore = defineStore('chronoServicesStore', () => {
|
||||||
// State
|
// State
|
||||||
const services = ref<ServiceObject[]>([]);
|
const _services = ref<Record<string, ServiceObject>>({})
|
||||||
// Actions
|
const transceiving = ref(false)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve services from the server
|
* Get count of services in store
|
||||||
*/
|
*/
|
||||||
async function list(
|
const count = computed(() => Object.keys(_services.value).length)
|
||||||
sources?: SourceSelector,
|
|
||||||
filter?: ListFilter,
|
|
||||||
sort?: ListSort,
|
|
||||||
uid?: string
|
|
||||||
): Promise<ServiceObject[]> {
|
|
||||||
try {
|
|
||||||
const response = await serviceService.list({ sources, filter, sort, uid });
|
|
||||||
|
|
||||||
// Flatten the nested response into a flat array
|
/**
|
||||||
const flatServices: ServiceObject[] = [];
|
* Check if any services are present in store
|
||||||
Object.entries(response).forEach(([providerId, providerServices]) => {
|
*/
|
||||||
Object.values(providerServices).forEach((service: ServiceInterface) => {
|
const has = computed(() => count.value > 0)
|
||||||
// Ensure provider is set on the service object
|
|
||||||
service.provider = service.provider || providerId;
|
|
||||||
flatServices.push(new ServiceObject().fromJson(service));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
console.debug('[Chrono Manager](Store) - Successfully retrieved', flatServices.length, 'services:', flatServices.map(s => ({
|
/**
|
||||||
id: s.id,
|
* Get all services present in store
|
||||||
label: s.label,
|
*/
|
||||||
provider: s.provider
|
const services = computed(() => Object.values(_services.value))
|
||||||
})));
|
|
||||||
|
|
||||||
services.value = flatServices;
|
/**
|
||||||
return flatServices;
|
* Get all services present in store grouped by provider
|
||||||
} catch (error: any) {
|
*/
|
||||||
console.error('[Chrono Manager](Store) - Failed to retrieve services:', error);
|
const servicesByProvider = computed(() => {
|
||||||
throw error;
|
const groups: Record<string, ServiceObject[]> = {}
|
||||||
|
|
||||||
|
Object.values(_services.value).forEach((service) => {
|
||||||
|
const providerServices = (groups[service.provider] ??= [])
|
||||||
|
providerServices.push(service)
|
||||||
|
})
|
||||||
|
|
||||||
|
return groups
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a specific service from store, with optional retrieval
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier
|
||||||
|
* @param identifier - service identifier
|
||||||
|
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||||
|
*
|
||||||
|
* @returns Service object or null
|
||||||
|
*/
|
||||||
|
function service(provider: string, identifier: string | number, retrieve: boolean = false): ServiceObject | null {
|
||||||
|
const key = identifierKey(provider, identifier)
|
||||||
|
if (retrieve === true && !_services.value[key]) {
|
||||||
|
console.debug(`[Chrono Manager][Store] - Force fetching service "${key}"`)
|
||||||
|
fetch(provider, identifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return _services.value[key] || null
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch a specific service
|
* Unique key for a service
|
||||||
*
|
*/
|
||||||
* @param provider - Provider identifier
|
function identifierKey(provider: string, identifier: string | number | null): string {
|
||||||
* @param identifier - Service identifier
|
return `${provider}:${identifier ?? ''}`
|
||||||
* @param uid - Optional user identifier
|
}
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve all or specific services, optionally filtered by source selector
|
||||||
|
*
|
||||||
|
* @param sources - optional source selector
|
||||||
|
*
|
||||||
|
* @returns Promise with service object list keyed by provider and service identifier
|
||||||
|
*/
|
||||||
|
async function list(sources?: SourceSelector): Promise<Record<string, ServiceObject>> {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const response = await serviceService.list({ sources })
|
||||||
|
|
||||||
|
// Flatten nested structure: provider-id: { service-id: object } -> "provider-id:service-id": object
|
||||||
|
const services: Record<string, ServiceObject> = {}
|
||||||
|
Object.entries(response).forEach(([_providerId, providerServices]) => {
|
||||||
|
Object.entries(providerServices).forEach(([_serviceId, serviceObj]) => {
|
||||||
|
const key = identifierKey(serviceObj.provider, serviceObj.identifier)
|
||||||
|
services[key] = serviceObj
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
// Merge retrieved services into state
|
||||||
|
_services.value = { ..._services.value, ...services }
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully retrieved', Object.keys(services).length, 'services')
|
||||||
|
return services
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to retrieve services:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve a specific service by provider and identifier
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier
|
||||||
|
* @param identifier - service identifier
|
||||||
|
*
|
||||||
* @returns Promise with service object
|
* @returns Promise with service object
|
||||||
*/
|
*/
|
||||||
async function fetch(
|
async function fetch(provider: string, identifier: string | number): Promise<ServiceObject> {
|
||||||
provider: string,
|
transceiving.value = true
|
||||||
identifier: string,
|
|
||||||
uid?: string
|
|
||||||
): Promise<ServiceObject | null> {
|
|
||||||
try {
|
try {
|
||||||
const response = await serviceService.fetch({ provider, service: identifier, uid });
|
const service = await serviceService.fetch({ provider, identifier })
|
||||||
return new ServiceObject().fromJson(response);
|
|
||||||
|
// Merge fetched service into state
|
||||||
|
const key = identifierKey(service.provider, service.identifier)
|
||||||
|
_services.value[key] = service
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully fetched service:', key)
|
||||||
|
return service
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
console.error('[Chrono Manager](Store) - Failed to fetch service:', error);
|
console.error('[Chrono Manager][Store] - Failed to fetch service:', error)
|
||||||
throw error;
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a fresh service object with default values
|
* Retrieve service availability status for a given source selector
|
||||||
|
*
|
||||||
|
* @param sources - source selector to check availability for
|
||||||
|
*
|
||||||
|
* @returns Promise with service availability status
|
||||||
*/
|
*/
|
||||||
function fresh(): ServiceObject {
|
async function extant(sources: SourceSelector) {
|
||||||
return new ServiceObject();
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const response = await serviceService.extant({ sources })
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'services')
|
||||||
|
return response
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to check services:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
/**
|
||||||
// State
|
* Create a new service with given provider and data
|
||||||
services,
|
*
|
||||||
|
* @param provider - provider identifier for the new service
|
||||||
|
* @param data - partial service data for creation
|
||||||
|
*
|
||||||
|
* @returns Promise with created service object
|
||||||
|
*/
|
||||||
|
async function create(provider: string, data: Partial<ServiceInterface>): Promise<ServiceObject> {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const service = await serviceService.create({ provider, data })
|
||||||
|
|
||||||
|
// Merge created service into state
|
||||||
|
const key = identifierKey(service.provider, service.identifier)
|
||||||
|
_services.value[key] = service
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully created service:', key)
|
||||||
|
return service
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to create service:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update an existing service with given provider, identifier, and data
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier for the service to update
|
||||||
|
* @param identifier - service identifier for the service to update
|
||||||
|
* @param data - partial service data for update
|
||||||
|
*
|
||||||
|
* @returns Promise with updated service object
|
||||||
|
*/
|
||||||
|
async function update(provider: string, identifier: string | number, data: Partial<ServiceInterface>): Promise<ServiceObject> {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
const service = await serviceService.update({ provider, identifier, data })
|
||||||
|
|
||||||
|
// Merge updated service into state
|
||||||
|
const key = identifierKey(service.provider, service.identifier)
|
||||||
|
_services.value[key] = service
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully updated service:', key)
|
||||||
|
return service
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to update service:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a service by provider and identifier
|
||||||
|
*
|
||||||
|
* @param provider - provider identifier for the service to delete
|
||||||
|
* @param identifier - service identifier for the service to delete
|
||||||
|
*
|
||||||
|
* @returns Promise with deletion result
|
||||||
|
*/
|
||||||
|
async function remove(provider: string, identifier: string | number): Promise<any> {
|
||||||
|
transceiving.value = true
|
||||||
|
try {
|
||||||
|
await serviceService.delete({ provider, identifier })
|
||||||
|
|
||||||
|
// Remove deleted service from state
|
||||||
|
const key = identifierKey(provider, identifier)
|
||||||
|
delete _services.value[key]
|
||||||
|
|
||||||
|
console.debug('[Chrono Manager][Store] - Successfully deleted service:', key)
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('[Chrono Manager][Store] - Failed to delete service:', error)
|
||||||
|
throw error
|
||||||
|
} finally {
|
||||||
|
transceiving.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return public API
|
||||||
|
return {
|
||||||
|
// State (readonly)
|
||||||
|
transceiving: readonly(transceiving),
|
||||||
|
// Getters
|
||||||
|
count,
|
||||||
|
has,
|
||||||
|
services,
|
||||||
|
servicesByProvider,
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
service,
|
||||||
list,
|
list,
|
||||||
fetch,
|
fetch,
|
||||||
fresh,
|
extant,
|
||||||
};
|
create,
|
||||||
});
|
update,
|
||||||
|
delete: remove,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|||||||
@@ -1,70 +1,57 @@
|
|||||||
/**
|
/**
|
||||||
* Collection-related type definitions for Chrono Manager
|
* Collection type definitions
|
||||||
*/
|
*/
|
||||||
|
import type { ListFilter, ListSort, SourceSelector } from './common';
|
||||||
import type { ListFilter, ListSort, SourceSelector } from "./common";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Permission settings for a collection
|
* Collection information
|
||||||
*/
|
|
||||||
export interface CollectionPermissionInterface {
|
|
||||||
view: boolean;
|
|
||||||
create: boolean;
|
|
||||||
modify: boolean;
|
|
||||||
destroy: boolean;
|
|
||||||
share: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Permissions settings for multiple users in a collection
|
|
||||||
*/
|
|
||||||
export interface CollectionPermissionsInterface {
|
|
||||||
[userId: string]: CollectionPermissionInterface;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Content type settings for a collection
|
|
||||||
*/
|
*/
|
||||||
export interface CollectionContentsInterface {
|
export interface CollectionContentsInterface {
|
||||||
event?: boolean;
|
event?: boolean;
|
||||||
task?: boolean;
|
task?: boolean;
|
||||||
journal?: boolean;
|
journal?: boolean;
|
||||||
[contentType: string]: boolean | undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a collection (calendar) within a service
|
|
||||||
*/
|
|
||||||
export interface CollectionInterface {
|
export interface CollectionInterface {
|
||||||
|
provider: string;
|
||||||
|
service: string | number;
|
||||||
|
collection: string | number | null;
|
||||||
|
identifier: string | number;
|
||||||
|
signature?: string | null;
|
||||||
|
created?: string | null;
|
||||||
|
modified?: string | null;
|
||||||
|
properties: CollectionPropertiesInterface;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CollectionBaseProperties {
|
||||||
'@type': string;
|
'@type': string;
|
||||||
provider: string | null;
|
version: number;
|
||||||
service: string | null;
|
}
|
||||||
in: number | string | null;
|
|
||||||
id: number | string | null;
|
export interface CollectionImmutableProperties extends CollectionBaseProperties {
|
||||||
label: string | null;
|
total?: number;
|
||||||
description: string | null;
|
|
||||||
priority: number | null;
|
|
||||||
visibility: string | null;
|
|
||||||
color: string | null;
|
|
||||||
enabled: boolean;
|
|
||||||
signature: string | null;
|
|
||||||
permissions: CollectionPermissionsInterface;
|
|
||||||
contents: CollectionContentsInterface;
|
contents: CollectionContentsInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CollectionMutableProperties extends CollectionBaseProperties {
|
||||||
|
label: string;
|
||||||
|
description: string | null;
|
||||||
|
rank: number | null;
|
||||||
|
visibility: boolean | null;
|
||||||
|
color: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CollectionPropertiesInterface extends CollectionMutableProperties, CollectionImmutableProperties {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to collection list endpoint
|
* Collection list
|
||||||
*/
|
*/
|
||||||
export interface CollectionListRequest {
|
export interface CollectionListRequest {
|
||||||
sources?: SourceSelector;
|
sources?: SourceSelector;
|
||||||
filter?: ListFilter;
|
filter?: ListFilter;
|
||||||
sort?: ListSort;
|
sort?: ListSort;
|
||||||
uid?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from collection list endpoint
|
|
||||||
*/
|
|
||||||
export interface CollectionListResponse {
|
export interface CollectionListResponse {
|
||||||
[providerId: string]: {
|
[providerId: string]: {
|
||||||
[serviceId: string]: {
|
[serviceId: string]: {
|
||||||
@@ -74,16 +61,23 @@ export interface CollectionListResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to collection extant endpoint
|
* Collection fetch
|
||||||
|
*/
|
||||||
|
export interface CollectionFetchRequest {
|
||||||
|
provider: string;
|
||||||
|
service: string | number;
|
||||||
|
collection: string | number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CollectionFetchResponse extends CollectionInterface {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Collection extant
|
||||||
*/
|
*/
|
||||||
export interface CollectionExtantRequest {
|
export interface CollectionExtantRequest {
|
||||||
sources: SourceSelector;
|
sources: SourceSelector;
|
||||||
uid?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from collection extant endpoint
|
|
||||||
*/
|
|
||||||
export interface CollectionExtantResponse {
|
export interface CollectionExtantResponse {
|
||||||
[providerId: string]: {
|
[providerId: string]: {
|
||||||
[serviceId: string]: {
|
[serviceId: string]: {
|
||||||
@@ -93,66 +87,42 @@ export interface CollectionExtantResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to collection fetch endpoint
|
* Collection create
|
||||||
*/
|
|
||||||
export interface CollectionFetchRequest {
|
|
||||||
provider: string;
|
|
||||||
service: string;
|
|
||||||
identifier: string | number;
|
|
||||||
uid?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from collection fetch endpoint
|
|
||||||
*/
|
|
||||||
export interface CollectionFetchResponse extends CollectionInterface {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request to collection create endpoint
|
|
||||||
*/
|
*/
|
||||||
export interface CollectionCreateRequest {
|
export interface CollectionCreateRequest {
|
||||||
provider: string;
|
provider: string;
|
||||||
service: string;
|
service: string | number;
|
||||||
data: CollectionInterface;
|
collection?: string | number | null; // Parent Collection Identifier
|
||||||
options?: (string)[]
|
properties: CollectionMutableProperties;
|
||||||
uid?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export interface CollectionCreateResponse extends CollectionInterface {}
|
||||||
* Response from collection create endpoint
|
|
||||||
*/
|
|
||||||
export interface CollectionCreateResponse extends CollectionInterface {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to collection modify endpoint
|
* Collection modify
|
||||||
*/
|
*/
|
||||||
export interface CollectionModifyRequest {
|
export interface CollectionUpdateRequest {
|
||||||
provider: string;
|
provider: string;
|
||||||
service: string;
|
service: string | number;
|
||||||
identifier: string | number;
|
identifier: string | number;
|
||||||
data: CollectionInterface;
|
properties: CollectionMutableProperties;
|
||||||
uid?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export interface CollectionUpdateResponse extends CollectionInterface {}
|
||||||
* Response from collection modify endpoint
|
|
||||||
*/
|
|
||||||
export interface CollectionModifyResponse extends CollectionInterface {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to collection destroy endpoint
|
* Collection delete
|
||||||
*/
|
*/
|
||||||
export interface CollectionDestroyRequest {
|
export interface CollectionDeleteRequest {
|
||||||
provider: string;
|
provider: string;
|
||||||
service: string;
|
service: string | number;
|
||||||
identifier: string | number;
|
identifier: string | number;
|
||||||
uid?: string;
|
options?: {
|
||||||
|
force?: boolean; // Whether to force delete even if collection is not empty
|
||||||
|
recursive?: boolean; // Whether to delete child collections/items as well
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CollectionDeleteResponse {
|
||||||
/**
|
|
||||||
* Response from collection destroy endpoint
|
|
||||||
*/
|
|
||||||
export interface CollectionDestroyResponse {
|
|
||||||
success: boolean;
|
success: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,62 @@
|
|||||||
/**
|
/**
|
||||||
* Common types shared across Chrono Manager services
|
* Common types shared across provider, service, collection, and entity request and responses.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import type { FilterComparisonOperator, FilterConjunctionOperator } from './service';
|
/**
|
||||||
|
* Base API request envelope
|
||||||
|
*/
|
||||||
|
export interface ApiRequest<T = any> {
|
||||||
|
version: number;
|
||||||
|
transaction: string;
|
||||||
|
operation: string;
|
||||||
|
data: T;
|
||||||
|
user?: string;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Source selector structure for hierarchical resource selection
|
* Success response envelope
|
||||||
* Structure: Provider -> Service -> Collection -> Entity
|
*/
|
||||||
|
export interface ApiSuccessResponse<T = any> {
|
||||||
|
version: number;
|
||||||
|
transaction: string;
|
||||||
|
operation: string;
|
||||||
|
status: 'success';
|
||||||
|
data: T;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Error response envelope
|
||||||
|
*/
|
||||||
|
export interface ApiErrorResponse {
|
||||||
|
version: number;
|
||||||
|
transaction: string;
|
||||||
|
operation: string;
|
||||||
|
status: 'error';
|
||||||
|
data: {
|
||||||
|
code: number;
|
||||||
|
message: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Combined response type
|
||||||
|
*/
|
||||||
|
export type ApiResponse<T = any> = ApiSuccessResponse<T> | ApiErrorResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Selector for targeting specific providers, services, collections, or entities in list or extant operations.
|
||||||
*
|
*
|
||||||
* Examples:
|
* Example usage:
|
||||||
* - Simple boolean: { "local": true }
|
* {
|
||||||
* - Nested services: { "system": { "personal": true, "recents": true } }
|
* "provider1": true, // Select all services/collections/entities under provider1
|
||||||
* - Collection IDs: { "system": { "personal": { "299": true, "176": true } } }
|
* "provider2": {
|
||||||
* - Entity IDs: { "system": { "personal": { "299": [1350, 1353, 5000] } } }
|
* "serviceA": true, // Select all collections/entities under serviceA of provider2
|
||||||
|
* "serviceB": {
|
||||||
|
* "collectionX": true, // Select all entities under collectionX of serviceB of provider2
|
||||||
|
* "collectionY": [1, 2, 3] // Select entities with identifiers 1, 2, and 3 under collectionY of serviceB of provider2
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* }
|
||||||
*/
|
*/
|
||||||
export type SourceSelector = {
|
export type SourceSelector = {
|
||||||
[provider: string]: boolean | ServiceSelector;
|
[provider: string]: boolean | ServiceSelector;
|
||||||
@@ -28,38 +72,85 @@ export type CollectionSelector = {
|
|||||||
|
|
||||||
export type EntitySelector = (string | number)[];
|
export type EntitySelector = (string | number)[];
|
||||||
|
|
||||||
/**
|
|
||||||
* Filter condition for building complex queries
|
|
||||||
*/
|
|
||||||
export interface FilterCondition {
|
|
||||||
attribute: string;
|
|
||||||
value: string | number | boolean | any[];
|
|
||||||
comparator?: FilterComparisonOperator;
|
|
||||||
conjunction?: FilterConjunctionOperator;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter criteria for list operations
|
* Filter comparison for list operations
|
||||||
* Can be simple key-value pairs or complex filter conditions
|
*/
|
||||||
|
export const ListFilterComparisonOperator = {
|
||||||
|
EQ: 1, // Equal
|
||||||
|
NEQ: 2, // Not Equal
|
||||||
|
GT: 4, // Greater Than
|
||||||
|
LT: 8, // Less Than
|
||||||
|
GTE: 16, // Greater Than or Equal
|
||||||
|
LTE: 32, // Less Than or Equal
|
||||||
|
IN: 64, // In Array
|
||||||
|
NIN: 128, // Not In Array
|
||||||
|
LIKE: 256, // Like
|
||||||
|
NLIKE: 512, // Not Like
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export type ListFilterComparisonOperator = typeof ListFilterComparisonOperator[keyof typeof ListFilterComparisonOperator];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter conjunction for list operations
|
||||||
|
*/
|
||||||
|
export const ListFilterConjunctionOperator = {
|
||||||
|
NONE: '',
|
||||||
|
AND: 'AND',
|
||||||
|
OR: 'OR',
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export type ListFilterConjunctionOperator = typeof ListFilterConjunctionOperator[keyof typeof ListFilterConjunctionOperator];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter condition for list operations
|
||||||
|
*
|
||||||
|
* Tuple format: [value, comparator?, conjunction?]
|
||||||
|
*/
|
||||||
|
export type ListFilterCondition = [
|
||||||
|
string | number | boolean | string[] | number[],
|
||||||
|
ListFilterComparisonOperator?,
|
||||||
|
ListFilterConjunctionOperator?
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter for list operations
|
||||||
|
*
|
||||||
|
* Values can be:
|
||||||
|
* - Simple primitives (string | number | boolean) for default equality comparison
|
||||||
|
* - ListFilterCondition tuple for explicit comparator/conjunction
|
||||||
|
*
|
||||||
|
* Examples:
|
||||||
|
* - Simple usage: { name: "John" }
|
||||||
|
* - With comparator: { age: [25, ListFilterComparisonOperator.GT] }
|
||||||
|
* - With conjunction: { age: [25, ListFilterComparisonOperator.GT, ListFilterConjunctionOperator.AND] }
|
||||||
|
* - With array value for IN operator: { status: [["active", "pending"], ListFilterComparisonOperator.IN] }
|
||||||
*/
|
*/
|
||||||
export interface ListFilter {
|
export interface ListFilter {
|
||||||
label?: string;
|
[attribute: string]: string | number | boolean | ListFilterCondition;
|
||||||
[key: string]: any;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sort options for list operations
|
* Sort for list operations
|
||||||
|
*
|
||||||
|
* Values can be:
|
||||||
|
* - true for ascending
|
||||||
|
* - false for descending
|
||||||
*/
|
*/
|
||||||
export interface ListSort {
|
export interface ListSort {
|
||||||
[key: string]: boolean;
|
[attribute: string]: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Range specification for pagination/limiting results
|
* Range for list operations
|
||||||
|
*
|
||||||
|
* Values can be:
|
||||||
|
* - relative based on item identifier
|
||||||
|
* - absolute based on item count
|
||||||
*/
|
*/
|
||||||
export interface ListRange {
|
export interface ListRange {
|
||||||
type: 'tally';
|
type: 'tally';
|
||||||
anchor: 'absolute' | 'relative';
|
anchor: 'relative' | 'absolute';
|
||||||
position: number;
|
position: string | number;
|
||||||
tally: number;
|
tally: number;
|
||||||
}
|
}
|
||||||
@@ -1,166 +1,131 @@
|
|||||||
|
/**
|
||||||
|
* Entity type definitions
|
||||||
|
*/
|
||||||
import type { ListFilter, ListRange, ListSort, SourceSelector } from './common';
|
import type { ListFilter, ListRange, ListSort, SourceSelector } from './common';
|
||||||
import type { EventInterface } from './event';
|
import type { EventInterface } from './event';
|
||||||
import type { TaskInterface } from './task';
|
import type { TaskInterface } from './task';
|
||||||
import type { JournalInterface } from './journal';
|
import type { JournalInterface } from './journal';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Entity-related type definitions for Chrono Manager
|
* Entity definition
|
||||||
*/
|
*/
|
||||||
|
export interface EntityInterface<T = EventInterface | TaskInterface | JournalInterface> {
|
||||||
/**
|
provider: string;
|
||||||
* Represents a chrono entity (event, task, or journal)
|
service: string;
|
||||||
*/
|
collection: string | number;
|
||||||
export interface EntityInterface {
|
identifier: string | number;
|
||||||
'@type': string;
|
|
||||||
version: number;
|
|
||||||
in: string | number | null;
|
|
||||||
id: string | number | null;
|
|
||||||
createdOn: Date | null;
|
|
||||||
createdBy: string | null;
|
|
||||||
modifiedOn: Date | null;
|
|
||||||
modifiedBy: string | null;
|
|
||||||
signature: string | null;
|
signature: string | null;
|
||||||
data: EventInterface | TaskInterface | JournalInterface | null;
|
created: string | null;
|
||||||
|
modified: string | null;
|
||||||
|
properties: T;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to entity list endpoint
|
* Entity list
|
||||||
*/
|
*/
|
||||||
export interface EntityListRequest {
|
export interface EntityListRequest {
|
||||||
sources?: SourceSelector;
|
sources?: SourceSelector;
|
||||||
filter?: ListFilter;
|
filter?: ListFilter;
|
||||||
sort?: ListSort;
|
sort?: ListSort;
|
||||||
range?: ListRange;
|
range?: ListRange;
|
||||||
uid?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from entity list endpoint
|
|
||||||
*/
|
|
||||||
export interface EntityListResponse {
|
export interface EntityListResponse {
|
||||||
[providerId: string]: {
|
[providerId: string]: {
|
||||||
[serviceId: string]: {
|
[serviceId: string]: {
|
||||||
[collectionId: string]: {
|
[collectionId: string]: {
|
||||||
[entityId: string]: EntityInterface;
|
[identifier: string]: EntityInterface<EventInterface | TaskInterface | JournalInterface>;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to entity delta endpoint
|
* Entity fetch
|
||||||
*/
|
*/
|
||||||
export interface EntityDeltaRequest {
|
export interface EntityFetchRequest {
|
||||||
sources: SourceSelector;
|
provider: string;
|
||||||
uid?: string;
|
service: string | number;
|
||||||
|
collection: string | number;
|
||||||
|
identifiers: (string | number)[];
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface EntityFetchResponse {
|
||||||
|
[identifier: string]: EntityInterface<EventInterface | TaskInterface | JournalInterface>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Response from entity delta endpoint
|
* Entity extant
|
||||||
*/
|
|
||||||
export interface EntityDeltaResponse {
|
|
||||||
[providerId: string]: {
|
|
||||||
[serviceId: string]: {
|
|
||||||
[collectionId: string]: {
|
|
||||||
signature: string;
|
|
||||||
created?: {
|
|
||||||
[entityId: string]: EntityInterface;
|
|
||||||
};
|
|
||||||
modified?: {
|
|
||||||
[entityId: string]: EntityInterface;
|
|
||||||
};
|
|
||||||
deleted?: string[]; // Array of deleted entity IDs
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request to entity extant endpoint
|
|
||||||
*/
|
*/
|
||||||
export interface EntityExtantRequest {
|
export interface EntityExtantRequest {
|
||||||
sources: SourceSelector;
|
sources: SourceSelector;
|
||||||
uid?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from entity extant endpoint
|
|
||||||
*/
|
|
||||||
export interface EntityExtantResponse {
|
export interface EntityExtantResponse {
|
||||||
[providerId: string]: {
|
[providerId: string]: {
|
||||||
[serviceId: string]: {
|
[serviceId: string]: {
|
||||||
[collectionId: string]: {
|
[collectionId: string]: {
|
||||||
[entityId: string]: boolean;
|
[identifier: string]: boolean;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to entity fetch endpoint
|
* Entity create
|
||||||
*/
|
*/
|
||||||
export interface EntityFetchRequest {
|
export interface EntityCreateRequest<T = EventInterface | TaskInterface | JournalInterface> {
|
||||||
provider: string;
|
provider: string;
|
||||||
service: string;
|
service: string | number;
|
||||||
collection: string | number;
|
collection: string | number;
|
||||||
identifiers: (string | number)[];
|
properties: T;
|
||||||
uid?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export interface EntityCreateResponse<T = EventInterface | TaskInterface | JournalInterface> extends EntityInterface<T> {}
|
||||||
* Response from entity fetch endpoint
|
|
||||||
*/
|
|
||||||
export interface EntityFetchResponse extends Record<string, EntityInterface> {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to entity create endpoint
|
* Entity update
|
||||||
*/
|
*/
|
||||||
export interface EntityCreateRequest {
|
export interface EntityUpdateRequest<T = EventInterface | TaskInterface | JournalInterface> {
|
||||||
provider: string;
|
provider: string;
|
||||||
service: string;
|
service: string | number;
|
||||||
collection: string | number;
|
|
||||||
data: EntityInterface;
|
|
||||||
options?: (string)[]
|
|
||||||
uid?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from entity create endpoint
|
|
||||||
*/
|
|
||||||
export interface EntityCreateResponse extends EntityInterface {}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request to entity modify endpoint
|
|
||||||
*/
|
|
||||||
export interface EntityModifyRequest {
|
|
||||||
provider: string;
|
|
||||||
service: string;
|
|
||||||
collection: string | number;
|
collection: string | number;
|
||||||
identifier: string | number;
|
identifier: string | number;
|
||||||
data: EntityInterface;
|
properties: T;
|
||||||
options?: (string)[]
|
|
||||||
uid?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export interface EntityUpdateResponse<T = EventInterface | TaskInterface | JournalInterface> extends EntityInterface<T> {}
|
||||||
* Response from entity modify endpoint
|
|
||||||
*/
|
|
||||||
export interface EntityModifyResponse extends EntityInterface {}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to entity destroy endpoint
|
* Entity delete
|
||||||
*/
|
*/
|
||||||
export interface EntityDestroyRequest {
|
export interface EntityDeleteRequest {
|
||||||
provider: string;
|
provider: string;
|
||||||
service: string;
|
service: string | number;
|
||||||
collection: string | number;
|
collection: string | number;
|
||||||
identifier: string | number;
|
identifier: string | number;
|
||||||
uid?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
export interface EntityDeleteResponse {
|
||||||
* Response from entity destroy endpoint
|
|
||||||
*/
|
|
||||||
export interface EntityDestroyResponse {
|
|
||||||
success: boolean;
|
success: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entity delta
|
||||||
|
*/
|
||||||
|
export interface EntityDeltaRequest {
|
||||||
|
sources: SourceSelector;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface EntityDeltaResponse {
|
||||||
|
[providerId: string]: false | {
|
||||||
|
[serviceId: string]: false | {
|
||||||
|
[collectionId: string]: false | {
|
||||||
|
signature: string;
|
||||||
|
additions: (string | number)[];
|
||||||
|
modifications: (string | number)[];
|
||||||
|
deletions: (string | number)[];
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -1,12 +1,8 @@
|
|||||||
/**
|
|
||||||
* Central export point for all Chrono Manager types
|
|
||||||
*/
|
|
||||||
|
|
||||||
export type * from './collection';
|
export type * from './collection';
|
||||||
export type * from './common';
|
export type * from './common';
|
||||||
export type * from './entity';
|
export type * from './entity';
|
||||||
export type * from './event';
|
export type * from './event';
|
||||||
export type * from './task';
|
|
||||||
export type * from './journal';
|
export type * from './journal';
|
||||||
export type * from './provider';
|
export type * from './provider';
|
||||||
export type * from './service';
|
export type * from './service';
|
||||||
|
export type * from './task';
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
/**
|
/**
|
||||||
* Provider-specific types
|
* Provider type definitions
|
||||||
*/
|
*/
|
||||||
import type { SourceSelector } from "./common";
|
import type { SourceSelector } from "./common";
|
||||||
|
|
||||||
@@ -11,9 +11,11 @@ export interface ProviderCapabilitiesInterface {
|
|||||||
ServiceFetch?: boolean;
|
ServiceFetch?: boolean;
|
||||||
ServiceExtant?: boolean;
|
ServiceExtant?: boolean;
|
||||||
ServiceCreate?: boolean;
|
ServiceCreate?: boolean;
|
||||||
ServiceModify?: boolean;
|
ServiceUpdate?: boolean;
|
||||||
ServiceDelete?: boolean;
|
ServiceDelete?: boolean;
|
||||||
[key: string]: boolean | undefined;
|
ServiceDiscover?: boolean;
|
||||||
|
ServiceTest?: boolean;
|
||||||
|
[key: string]: boolean | object | string[] | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,33 +23,38 @@ export interface ProviderCapabilitiesInterface {
|
|||||||
*/
|
*/
|
||||||
export interface ProviderInterface {
|
export interface ProviderInterface {
|
||||||
'@type': string;
|
'@type': string;
|
||||||
id: string;
|
identifier: string;
|
||||||
label: string;
|
label: string;
|
||||||
capabilities: ProviderCapabilitiesInterface;
|
capabilities: ProviderCapabilitiesInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to provider list endpoint
|
* Provider list
|
||||||
*/
|
*/
|
||||||
export interface ProviderListRequest {}
|
export interface ProviderListRequest {
|
||||||
|
sources?: SourceSelector;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from provider list endpoint
|
|
||||||
*/
|
|
||||||
export interface ProviderListResponse {
|
export interface ProviderListResponse {
|
||||||
[providerId: string]: ProviderInterface;
|
[identifier: string]: ProviderInterface;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request to provider extant endpoint
|
* Provider fetch
|
||||||
|
*/
|
||||||
|
export interface ProviderFetchRequest {
|
||||||
|
identifier: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ProviderFetchResponse extends ProviderInterface {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provider extant
|
||||||
*/
|
*/
|
||||||
export interface ProviderExtantRequest {
|
export interface ProviderExtantRequest {
|
||||||
sources: SourceSelector;
|
sources: SourceSelector;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from provider extant endpoint
|
|
||||||
*/
|
|
||||||
export interface ProviderExtantResponse {
|
export interface ProviderExtantResponse {
|
||||||
[providerId: string]: boolean;
|
[identifier: string]: boolean;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,40 +1,254 @@
|
|||||||
/**
|
/**
|
||||||
* Service-related type definitions for Chrono Manager
|
* Service type definitions
|
||||||
*/
|
*/
|
||||||
|
import type { SourceSelector, ListFilterComparisonOperator } from './common';
|
||||||
import type { ListFilter, ListSort, SourceSelector } from "./common";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter comparison operators (bitmask values)
|
* Service capabilities
|
||||||
*/
|
*/
|
||||||
export const FilterComparisonOperator = {
|
export interface ServiceCapabilitiesInterface {
|
||||||
EQ: 1, // Equal
|
// Collection capabilities
|
||||||
NEQ: 2, // Not Equal
|
CollectionList?: boolean;
|
||||||
GT: 4, // Greater Than
|
CollectionListFilter?: ServiceListFilterCollection;
|
||||||
LT: 8, // Less Than
|
CollectionListSort?: ServiceListSortCollection;
|
||||||
GTE: 16, // Greater Than or Equal
|
CollectionExtant?: boolean;
|
||||||
LTE: 32, // Less Than or Equal
|
CollectionFetch?: boolean;
|
||||||
IN: 64, // In Array
|
CollectionCreate?: boolean;
|
||||||
NIN: 128, // Not In Array
|
CollectionUpdate?: boolean;
|
||||||
LIKE: 256, // Like (pattern matching)
|
CollectionDelete?: boolean;
|
||||||
NLIKE: 512, // Not Like
|
// Message capabilities
|
||||||
} as const;
|
EntityList?: boolean;
|
||||||
|
EntityListFilter?: ServiceListFilterEntity;
|
||||||
export type FilterComparisonOperator = typeof FilterComparisonOperator[keyof typeof FilterComparisonOperator];
|
EntityListSort?: ServiceListSortEntity;
|
||||||
|
EntityListRange?: ServiceListRange;
|
||||||
|
EntityDelta?: boolean;
|
||||||
|
EntityExtant?: boolean;
|
||||||
|
EntityFetch?: boolean;
|
||||||
|
EntityCreate?: boolean;
|
||||||
|
EntityUpdate?: boolean;
|
||||||
|
EntityDelete?: boolean;
|
||||||
|
EntityMove?: boolean;
|
||||||
|
EntityCopy?: boolean;
|
||||||
|
// Send capability
|
||||||
|
EntityTransmit?: boolean;
|
||||||
|
[key: string]: boolean | object | string | string[] | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter conjunction operators
|
* Service information
|
||||||
*/
|
*/
|
||||||
export const FilterConjunctionOperator = {
|
export interface ServiceInterface {
|
||||||
NONE: '',
|
'@type': string;
|
||||||
AND: 'AND',
|
provider: string;
|
||||||
OR: 'OR',
|
identifier: string | number | null;
|
||||||
} as const;
|
label: string | null;
|
||||||
|
enabled: boolean;
|
||||||
export type FilterConjunctionOperator = typeof FilterConjunctionOperator[keyof typeof FilterConjunctionOperator];
|
capabilities?: ServiceCapabilitiesInterface;
|
||||||
|
location?: ServiceLocation | null;
|
||||||
|
identity?: ServiceIdentity | null;
|
||||||
|
auxiliary?: Record<string, any>; // Provider-specific extension data
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filter specification format
|
* Service list
|
||||||
|
*/
|
||||||
|
export interface ServiceListRequest {
|
||||||
|
sources?: SourceSelector;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServiceListResponse {
|
||||||
|
[provider: string]: {
|
||||||
|
[identifier: string]: ServiceInterface;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service fetch
|
||||||
|
*/
|
||||||
|
export interface ServiceFetchRequest {
|
||||||
|
provider: string;
|
||||||
|
identifier: string | number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServiceFetchResponse extends ServiceInterface {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service extant
|
||||||
|
*/
|
||||||
|
export interface ServiceExtantRequest {
|
||||||
|
sources: SourceSelector;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServiceExtantResponse {
|
||||||
|
[provider: string]: {
|
||||||
|
[identifier: string]: boolean;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service create
|
||||||
|
*/
|
||||||
|
export interface ServiceCreateRequest {
|
||||||
|
provider: string;
|
||||||
|
data: Partial<ServiceInterface>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServiceCreateResponse extends ServiceInterface {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service update
|
||||||
|
*/
|
||||||
|
export interface ServiceUpdateRequest {
|
||||||
|
provider: string;
|
||||||
|
identifier: string | number;
|
||||||
|
data: Partial<ServiceInterface>;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServiceUpdateResponse extends ServiceInterface {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service delete
|
||||||
|
*/
|
||||||
|
export interface ServiceDeleteRequest {
|
||||||
|
provider: string;
|
||||||
|
identifier: string | number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServiceDeleteResponse {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service discovery
|
||||||
|
*/
|
||||||
|
export interface ServiceDiscoverRequest {
|
||||||
|
identity: string; // Email address or domain
|
||||||
|
provider?: string; // Optional: specific provider ('jmap', 'smtp', etc.) or null for all
|
||||||
|
location?: string; // Optional: known hostname (bypasses DNS lookup)
|
||||||
|
secret?: string; // Optional: password/token for credential validation
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServiceDiscoverResponse {
|
||||||
|
[provider: string]: ServiceLocation; // Uses existing ServiceLocation discriminated union
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service connection test
|
||||||
|
*/
|
||||||
|
export interface ServiceTestRequest {
|
||||||
|
provider: string;
|
||||||
|
// For existing service
|
||||||
|
identifier?: string | number | null;
|
||||||
|
// For fresh configuration
|
||||||
|
location?: ServiceLocation | null;
|
||||||
|
identity?: ServiceIdentity | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServiceTestResponse {
|
||||||
|
success: boolean;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service location - Base
|
||||||
|
*/
|
||||||
|
export interface ServiceLocationBase {
|
||||||
|
type: 'URI' | 'FILE';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service location - URI-based type
|
||||||
|
*/
|
||||||
|
export interface ServiceLocationUri extends ServiceLocationBase {
|
||||||
|
type: 'URI';
|
||||||
|
scheme: string; // e.g., 'https', 'http'
|
||||||
|
host: string; // e.g., 'api.example.com'
|
||||||
|
port: number; // e.g., 443
|
||||||
|
path?: string; // e.g., '/v1/api'
|
||||||
|
verifyPeer?: boolean; // Verify SSL/TLS peer certificate
|
||||||
|
verifyHost?: boolean; // Verify SSL/TLS certificate host
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service location - File-based type (e.g., for local mail delivery or Unix socket)
|
||||||
|
*/
|
||||||
|
export interface ServiceLocationFile extends ServiceLocationBase {
|
||||||
|
type: 'FILE';
|
||||||
|
path: string; // File system path
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service location types
|
||||||
|
*/
|
||||||
|
export type ServiceLocation =
|
||||||
|
| ServiceLocationUri
|
||||||
|
| ServiceLocationFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service identity - base
|
||||||
|
*/
|
||||||
|
export interface ServiceIdentityBase {
|
||||||
|
type: 'NA' | 'BA' | 'TA' | 'OA' | 'CC';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service identity - No authentication
|
||||||
|
*/
|
||||||
|
export interface ServiceIdentityNone extends ServiceIdentityBase {
|
||||||
|
type: 'NA';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service identity - Basic authentication type
|
||||||
|
*/
|
||||||
|
export interface ServiceIdentityBasic extends ServiceIdentityBase {
|
||||||
|
type: 'BA';
|
||||||
|
identity: string; // Username/email
|
||||||
|
secret: string; // Password
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Token authentication (API key, static token)
|
||||||
|
*/
|
||||||
|
export interface ServiceIdentityToken extends ServiceIdentityBase {
|
||||||
|
type: 'TA';
|
||||||
|
token: string; // Authentication token/API key
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* OAuth authentication
|
||||||
|
*/
|
||||||
|
export interface ServiceIdentityOAuth extends ServiceIdentityBase {
|
||||||
|
type: 'OA';
|
||||||
|
accessToken: string; // Current access token
|
||||||
|
accessScope?: string[]; // Token scopes
|
||||||
|
accessExpiry?: number; // Unix timestamp when token expires
|
||||||
|
refreshToken?: string; // Refresh token for getting new access tokens
|
||||||
|
refreshLocation?: string; // Token refresh endpoint URL
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Client certificate authentication (mTLS)
|
||||||
|
*/
|
||||||
|
export interface ServiceIdentityCertificate extends ServiceIdentityBase {
|
||||||
|
type: 'CC';
|
||||||
|
certificate: string; // X.509 certificate (PEM format or file path)
|
||||||
|
privateKey: string; // Private key (PEM format or file path)
|
||||||
|
passphrase?: string; // Optional passphrase for encrypted private key
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Service identity configuration
|
||||||
|
* Discriminated union of all identity types
|
||||||
|
*/
|
||||||
|
export type ServiceIdentity =
|
||||||
|
| ServiceIdentityNone
|
||||||
|
| ServiceIdentityBasic
|
||||||
|
| ServiceIdentityToken
|
||||||
|
| ServiceIdentityOAuth
|
||||||
|
| ServiceIdentityCertificate;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List filter specification format
|
||||||
|
*
|
||||||
* Format: "type:length:defaultComparator:supportedComparators"
|
* Format: "type:length:defaultComparator:supportedComparators"
|
||||||
*
|
*
|
||||||
* Examples:
|
* Examples:
|
||||||
@@ -50,165 +264,33 @@ export type FilterConjunctionOperator = typeof FilterConjunctionOperator[keyof t
|
|||||||
*
|
*
|
||||||
* Comparator values are bitmasks that can be combined
|
* Comparator values are bitmasks that can be combined
|
||||||
*/
|
*/
|
||||||
export type FilterSpec = string;
|
export type ServiceListFilterCollection = {
|
||||||
|
'label'?: string;
|
||||||
|
'rank'?: string;
|
||||||
|
[attribute: string]: string | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ServiceListFilterEntity = {
|
||||||
|
'*'?: string;
|
||||||
|
'before'?: string;
|
||||||
|
'after'?: string;
|
||||||
|
[attribute: string]: string | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parsed filter specification
|
* Service list sort specification
|
||||||
*/
|
*/
|
||||||
export interface ParsedFilterSpec {
|
export type ServiceListSortCollection = ("label" | "rank" | string)[];
|
||||||
type: 'string' | 'integer' | 'boolean' | 'array';
|
export type ServiceListSortEntity = ( "sent" | "size" | string)[];
|
||||||
|
|
||||||
|
export type ServiceListRange = {
|
||||||
|
'tally'?: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
export interface ServiceListFilterDefinition {
|
||||||
|
type: 'string' | 'integer' | 'date' | 'boolean' | 'array';
|
||||||
length: number;
|
length: number;
|
||||||
defaultComparator: FilterComparisonOperator;
|
defaultComparator: ListFilterComparisonOperator;
|
||||||
supportedComparators: FilterComparisonOperator[];
|
supportedComparators: ListFilterComparisonOperator[];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Parse a filter specification string into its components
|
|
||||||
*
|
|
||||||
* @param spec - Filter specification string (e.g., "s:200:256:771")
|
|
||||||
* @returns Parsed filter specification object
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* parseFilterSpec("s:200:256:771")
|
|
||||||
* // Returns: {
|
|
||||||
* // type: 'string',
|
|
||||||
* // length: 200,
|
|
||||||
* // defaultComparator: 256 (LIKE),
|
|
||||||
* // supportedComparators: [1, 2, 256, 512] (EQ, NEQ, LIKE, NLIKE)
|
|
||||||
* // }
|
|
||||||
*/
|
|
||||||
export function parseFilterSpec(spec: FilterSpec): ParsedFilterSpec {
|
|
||||||
const [typeCode, lengthStr, defaultComparatorStr, supportedComparatorsStr] = spec.split(':');
|
|
||||||
|
|
||||||
const typeMap: Record<string, ParsedFilterSpec['type']> = {
|
|
||||||
's': 'string',
|
|
||||||
'i': 'integer',
|
|
||||||
'b': 'boolean',
|
|
||||||
'a': 'array',
|
|
||||||
};
|
|
||||||
|
|
||||||
const type = typeMap[typeCode];
|
|
||||||
if (!type) {
|
|
||||||
throw new Error(`Invalid filter type code: ${typeCode}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const length = parseInt(lengthStr, 10);
|
|
||||||
const defaultComparator = parseInt(defaultComparatorStr, 10) as FilterComparisonOperator;
|
|
||||||
|
|
||||||
// Parse supported comparators from bitmask
|
|
||||||
const supportedComparators: FilterComparisonOperator[] = [];
|
|
||||||
const supportedBitmask = parseInt(supportedComparatorsStr, 10);
|
|
||||||
|
|
||||||
if (supportedBitmask !== 0) {
|
|
||||||
const allComparators = Object.values(FilterComparisonOperator).filter(v => typeof v === 'number') as number[];
|
|
||||||
for (const comparator of allComparators) {
|
|
||||||
if ((supportedBitmask & comparator) === comparator) {
|
|
||||||
supportedComparators.push(comparator as FilterComparisonOperator);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
type,
|
|
||||||
length,
|
|
||||||
defaultComparator,
|
|
||||||
supportedComparators,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Capabilities available for a service
|
|
||||||
*/
|
|
||||||
export interface ServiceCapabilitiesInterface {
|
|
||||||
// Collection capabilities
|
|
||||||
CollectionList?: boolean;
|
|
||||||
CollectionListFilter?: {
|
|
||||||
[key: string]: FilterSpec;
|
|
||||||
};
|
|
||||||
CollectionListSort?: string[];
|
|
||||||
CollectionExtant?: boolean;
|
|
||||||
CollectionFetch?: boolean;
|
|
||||||
CollectionCreate?: boolean;
|
|
||||||
CollectionModify?: boolean;
|
|
||||||
CollectionDestroy?: boolean;
|
|
||||||
|
|
||||||
// Entity capabilities
|
|
||||||
EntityList?: boolean;
|
|
||||||
EntityListFilter?: {
|
|
||||||
[key: string]: FilterSpec;
|
|
||||||
};
|
|
||||||
EntityListSort?: string[];
|
|
||||||
EntityListRange?: {
|
|
||||||
[rangeType: string]: string[]; // e.g., { "tally": ["absolute", "relative"] }
|
|
||||||
};
|
|
||||||
EntityDelta?: boolean;
|
|
||||||
EntityExtant?: boolean;
|
|
||||||
EntityFetch?: boolean;
|
|
||||||
EntityCreate?: boolean;
|
|
||||||
EntityModify?: boolean;
|
|
||||||
EntityDestroy?: boolean;
|
|
||||||
EntityCopy?: boolean;
|
|
||||||
EntityMove?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a service within a provider
|
|
||||||
*/
|
|
||||||
export interface ServiceInterface {
|
|
||||||
'@type': string;
|
|
||||||
provider: string;
|
|
||||||
id: string;
|
|
||||||
label: string;
|
|
||||||
capabilities?: ServiceCapabilitiesInterface;
|
|
||||||
enabled: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request to service list endpoint
|
|
||||||
*/
|
|
||||||
export interface ServiceListRequest {
|
|
||||||
sources?: SourceSelector;
|
|
||||||
filter?: ListFilter;
|
|
||||||
sort?: ListSort;
|
|
||||||
uid?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from service list endpoint
|
|
||||||
*/
|
|
||||||
export interface ServiceListResponse {
|
|
||||||
[providerId: string]: {
|
|
||||||
[serviceId: string]: ServiceInterface;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request to service extant endpoint
|
|
||||||
*/
|
|
||||||
export interface ServiceExtantRequest {
|
|
||||||
sources: SourceSelector;
|
|
||||||
uid?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from service extant endpoint
|
|
||||||
*/
|
|
||||||
export interface ServiceExtantResponse {
|
|
||||||
[providerId: string]: {
|
|
||||||
[serviceId: string]: boolean;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request to service fetch endpoint
|
|
||||||
*/
|
|
||||||
export interface ServiceFetchRequest {
|
|
||||||
provider: string;
|
|
||||||
service: string;
|
|
||||||
uid?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from service fetch endpoint
|
|
||||||
*/
|
|
||||||
export interface ServiceFetchResponse extends ServiceInterface {}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user