refactor: front end
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -1,292 +1,175 @@
|
||||
/**
|
||||
* Entity (file) management service
|
||||
* Entity management service
|
||||
*/
|
||||
|
||||
import { fileManagerApi } from './api';
|
||||
import type { FilterCondition, SortCondition, RangeCondition } from '@/types/common';
|
||||
import type { FileEntity } from '@/types/node';
|
||||
import type { EntityDeltaResult } from '@/types/api';
|
||||
import { transceivePost } from './transceive';
|
||||
import type {
|
||||
EntityListRequest,
|
||||
EntityListResponse,
|
||||
EntityFetchRequest,
|
||||
EntityFetchResponse,
|
||||
EntityExtantRequest,
|
||||
EntityExtantResponse,
|
||||
EntityCreateRequest,
|
||||
EntityCreateResponse,
|
||||
EntityUpdateRequest,
|
||||
EntityUpdateResponse,
|
||||
EntityDeleteRequest,
|
||||
EntityDeleteResponse,
|
||||
EntityDeltaRequest,
|
||||
EntityDeltaResponse,
|
||||
EntityReadRequest,
|
||||
EntityReadResponse,
|
||||
EntityWriteRequest,
|
||||
EntityWriteResponse,
|
||||
EntityInterface,
|
||||
} from '../types/entity';
|
||||
import { useIntegrationStore } from '@KTXC/stores/integrationStore';
|
||||
import { EntityObject } from '../models';
|
||||
|
||||
/**
|
||||
* 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('documents_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 = {
|
||||
|
||||
/**
|
||||
* List entities within a collection
|
||||
* Retrieve list of entities, optionally filtered by source selector
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param service - Service identifier
|
||||
* @param collection - Collection identifier
|
||||
* @param filter - Optional filter conditions
|
||||
* @param sort - Optional sort conditions
|
||||
* @param range - Optional range/pagination conditions
|
||||
* @returns Promise with entity list
|
||||
* @param request - list request parameters
|
||||
*
|
||||
* @returns Promise with entity object list grouped by provider, service, collection, and entity identifier
|
||||
*/
|
||||
async list(
|
||||
provider: string,
|
||||
service: string,
|
||||
collection: string,
|
||||
filter?: FilterCondition[] | null,
|
||||
sort?: SortCondition[] | null,
|
||||
range?: RangeCondition | null
|
||||
): Promise<FileEntity[]> {
|
||||
return await fileManagerApi.execute<FileEntity[]>('entity.list', {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
filter: filter ?? null,
|
||||
sort: sort ?? null,
|
||||
range: range ?? null,
|
||||
async list(request: EntityListRequest = {}): Promise<Record<string, Record<string, Record<string, Record<string, EntityObject>>>>> {
|
||||
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 since a signature
|
||||
* Retrieve a specific entity by provider and identifier
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param service - Service identifier
|
||||
* @param collection - Collection identifier
|
||||
* @param signature - Previous sync signature
|
||||
* @param detail - Detail level ('ids' or 'full')
|
||||
* @returns Promise with delta changes
|
||||
* @param request - fetch request parameters
|
||||
*
|
||||
* @returns Promise with entity objects keyed by identifier
|
||||
*/
|
||||
async delta(
|
||||
provider: string,
|
||||
service: string,
|
||||
collection: string,
|
||||
signature: string,
|
||||
detail: 'ids' | 'full' = 'ids'
|
||||
): Promise<EntityDeltaResult> {
|
||||
return await fileManagerApi.execute<EntityDeltaResult>('entity.delta', {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
signature,
|
||||
detail,
|
||||
async fetch(request: EntityFetchRequest): Promise<Record<string, EntityObject>> {
|
||||
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
|
||||
* Retrieve entity availability status for a given source selector
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param service - Service identifier
|
||||
* @param collection - Collection identifier
|
||||
* @param identifiers - Entity identifiers to check
|
||||
* @returns Promise with existence map
|
||||
* @param request - extant request parameters
|
||||
*
|
||||
* @returns Promise with entity availability status
|
||||
*/
|
||||
async extant(
|
||||
provider: string,
|
||||
service: string,
|
||||
collection: string,
|
||||
identifiers: string[]
|
||||
): Promise<Record<string, boolean>> {
|
||||
return await fileManagerApi.execute<Record<string, boolean>>('entity.extant', {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
identifiers,
|
||||
});
|
||||
async extant(request: EntityExtantRequest): Promise<EntityExtantResponse> {
|
||||
return await transceivePost<EntityExtantRequest, EntityExtantResponse>('entity.extant', request);
|
||||
},
|
||||
|
||||
/**
|
||||
* Fetch specific entities
|
||||
* Create a new entity
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param service - Service identifier
|
||||
* @param collection - Collection identifier
|
||||
* @param identifiers - Entity identifiers to fetch
|
||||
* @returns Promise with entity list
|
||||
* @param request - create request parameters
|
||||
*
|
||||
* @returns Promise with created entity object
|
||||
*/
|
||||
async fetch(
|
||||
provider: string,
|
||||
service: string,
|
||||
collection: string,
|
||||
identifiers: string[]
|
||||
): Promise<FileEntity[]> {
|
||||
return await fileManagerApi.execute<FileEntity[]>('entity.fetch', {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
identifiers,
|
||||
});
|
||||
async create(request: EntityCreateRequest): Promise<EntityObject> {
|
||||
const response = await transceivePost<EntityCreateRequest, EntityCreateResponse>('entity.create', request);
|
||||
return createEntityObject(response);
|
||||
},
|
||||
|
||||
/**
|
||||
* Read entity content
|
||||
* Update an existing entity
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param service - Service identifier
|
||||
* @param collection - Collection identifier
|
||||
* @param identifier - Entity identifier
|
||||
* @returns Promise with base64 encoded content
|
||||
*/
|
||||
async read(
|
||||
provider: string,
|
||||
service: string,
|
||||
collection: string,
|
||||
identifier: string
|
||||
): Promise<{ content: string | null; encoding: 'base64' }> {
|
||||
return await fileManagerApi.execute<{ content: string | null; encoding: 'base64' }>('entity.read', {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
identifier,
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a new entity (file)
|
||||
* @param request - update request parameters
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param service - Service identifier
|
||||
* @param collection - Collection identifier (null for root)
|
||||
* @param data - Entity data (label, mime, etc.)
|
||||
* @param options - Additional options
|
||||
* @returns Promise with created entity
|
||||
* @returns Promise with updated entity object
|
||||
*/
|
||||
async create(
|
||||
provider: string,
|
||||
service: string,
|
||||
collection: string | null,
|
||||
data: Partial<FileEntity>,
|
||||
options?: Record<string, unknown>
|
||||
): Promise<FileEntity> {
|
||||
return await fileManagerApi.execute<FileEntity>('entity.create', {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
data,
|
||||
options: options ?? {},
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Modify an existing entity
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param service - Service identifier
|
||||
* @param collection - Collection identifier (can be null)
|
||||
* @param identifier - Entity identifier
|
||||
* @param data - Data to modify
|
||||
* @returns Promise with modified entity
|
||||
*/
|
||||
async modify(
|
||||
provider: string,
|
||||
service: string,
|
||||
collection: string | null,
|
||||
identifier: string,
|
||||
data: Partial<FileEntity>
|
||||
): Promise<FileEntity> {
|
||||
return await fileManagerApi.execute<FileEntity>('entity.modify', {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
identifier,
|
||||
data,
|
||||
});
|
||||
async update(request: EntityUpdateRequest): Promise<EntityObject> {
|
||||
const response = await transceivePost<EntityUpdateRequest, EntityUpdateResponse>('entity.update', request);
|
||||
return createEntityObject(response);
|
||||
},
|
||||
|
||||
/**
|
||||
* Delete an entity
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param service - Service identifier
|
||||
* @param collection - Collection identifier (can be null)
|
||||
* @param identifier - Entity identifier
|
||||
* @returns Promise with success status
|
||||
* @param request - delete request parameters
|
||||
*
|
||||
* @returns Promise with deletion result
|
||||
*/
|
||||
async destroy(
|
||||
provider: string,
|
||||
service: string,
|
||||
collection: string | null,
|
||||
identifier: string
|
||||
): Promise<boolean> {
|
||||
const result = await fileManagerApi.execute<{ success: boolean }>('entity.destroy', {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
identifier,
|
||||
});
|
||||
return result.success;
|
||||
async delete(request: EntityDeleteRequest): Promise<EntityDeleteResponse> {
|
||||
return await transceivePost<EntityDeleteRequest, EntityDeleteResponse>('entity.delete', request);
|
||||
},
|
||||
|
||||
/**
|
||||
* Copy an entity to a new location
|
||||
* Retrieve delta changes for entities
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param service - Service identifier
|
||||
* @param collection - Source collection identifier (can be null)
|
||||
* @param identifier - Entity identifier to copy
|
||||
* @param destination - Destination collection ID (null for root)
|
||||
* @returns Promise with copied entity
|
||||
* @param request - delta request parameters
|
||||
*
|
||||
* @returns Promise with delta changes (created, modified, deleted)
|
||||
*/
|
||||
async copy(
|
||||
provider: string,
|
||||
service: string,
|
||||
collection: string | null,
|
||||
identifier: string,
|
||||
destination?: string | null
|
||||
): Promise<FileEntity> {
|
||||
return await fileManagerApi.execute<FileEntity>('entity.copy', {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
identifier,
|
||||
destination: destination ?? null,
|
||||
});
|
||||
async delta(request: EntityDeltaRequest): Promise<EntityDeltaResponse> {
|
||||
return await transceivePost<EntityDeltaRequest, EntityDeltaResponse>('entity.delta', request);
|
||||
},
|
||||
|
||||
/**
|
||||
* Move an entity to a new location
|
||||
* Read entity content
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param service - Service identifier
|
||||
* @param collection - Source collection identifier (can be null)
|
||||
* @param identifier - Entity identifier to move
|
||||
* @param destination - Destination collection ID (null for root)
|
||||
* @returns Promise with moved entity
|
||||
* @param request - read request parameters
|
||||
* @returns Promise with base64 encoded content
|
||||
*/
|
||||
async move(
|
||||
provider: string,
|
||||
service: string,
|
||||
collection: string | null,
|
||||
identifier: string,
|
||||
destination?: string | null
|
||||
): Promise<FileEntity> {
|
||||
return await fileManagerApi.execute<FileEntity>('entity.move', {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
identifier,
|
||||
destination: destination ?? null,
|
||||
});
|
||||
async read(request: EntityReadRequest): Promise<EntityReadResponse> {
|
||||
return await transceivePost<EntityReadRequest, EntityReadResponse>('entity.read', request);
|
||||
},
|
||||
|
||||
/**
|
||||
* Write content to an entity
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param service - Service identifier
|
||||
* @param collection - Collection identifier (can be null)
|
||||
* @param identifier - Entity identifier
|
||||
* @param content - Content to write (base64 encoded)
|
||||
* @returns Promise with bytes written
|
||||
* @param request - write request parameters
|
||||
* @returns Promise with write result
|
||||
*/
|
||||
async write(
|
||||
provider: string,
|
||||
service: string,
|
||||
collection: string | null,
|
||||
identifier: string,
|
||||
content: string
|
||||
): Promise<number> {
|
||||
const result = await fileManagerApi.execute<{ bytesWritten: number }>('entity.write', {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
identifier,
|
||||
content,
|
||||
encoding: 'base64',
|
||||
async write(request: EntityWriteRequest): Promise<EntityWriteResponse> {
|
||||
return await transceivePost<EntityWriteRequest, EntityWriteResponse>('entity.write', {
|
||||
...request,
|
||||
encoding: request.encoding ?? 'base64',
|
||||
});
|
||||
return result.bytesWritten;
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user