chore: standardize chrono provider
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -1,202 +1,307 @@
|
||||
/**
|
||||
* Chrono Manager - Collections Store
|
||||
* Collections Store
|
||||
*/
|
||||
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
import { collectionService } from '../services/collectionService';
|
||||
import type {
|
||||
SourceSelector,
|
||||
ListFilter,
|
||||
ListSort,
|
||||
} from '../types/common';
|
||||
import { CollectionObject } from '../models/collection';
|
||||
import type { ServiceObject } from '../models/service';
|
||||
import type { CollectionInterface } from '../types/collection';
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { collectionService } from '../services'
|
||||
import { CollectionObject, CollectionPropertiesObject } from '../models/collection'
|
||||
import type { SourceSelector, ListFilter, ListSort } from '../types'
|
||||
|
||||
export const useCollectionsStore = defineStore('chronoCollectionsStore', () => {
|
||||
export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
|
||||
// State
|
||||
const collections = ref<CollectionObject[]>([]);
|
||||
|
||||
// Actions
|
||||
const _collections = ref<Record<string, CollectionObject>>({})
|
||||
const transceiving = ref(false)
|
||||
|
||||
/**
|
||||
* Retrieve collections from the server
|
||||
* Get count of collections in store
|
||||
*/
|
||||
async function list(
|
||||
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;
|
||||
}
|
||||
}
|
||||
const count = computed(() => Object.keys(_collections.value).length)
|
||||
|
||||
/**
|
||||
* Fetch a specific collection
|
||||
* Check if any collections are present in store
|
||||
*/
|
||||
async function fetch(
|
||||
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;
|
||||
}
|
||||
}
|
||||
const has = computed(() => count.value > 0)
|
||||
|
||||
/**
|
||||
* Create a fresh collection object with default values
|
||||
* Get all collections present in store
|
||||
*/
|
||||
function fresh(): CollectionObject {
|
||||
return new CollectionObject();
|
||||
}
|
||||
const collections = computed(() => Object.values(_collections.value))
|
||||
|
||||
/**
|
||||
* Create a new collection
|
||||
* Get all collections present in store grouped by service
|
||||
*/
|
||||
async function create(
|
||||
service: ServiceObject,
|
||||
collection: CollectionObject,
|
||||
options?: string[],
|
||||
uid?: string
|
||||
): Promise<CollectionObject | null> {
|
||||
try {
|
||||
if (service.provider === null || service.id === null) {
|
||||
throw new Error('Invalid service object, must have a provider and identifier');
|
||||
const collectionsByService = computed(() => {
|
||||
const groups: Record<string, CollectionObject[]> = {}
|
||||
|
||||
Object.values(_collections.value).forEach((collection) => {
|
||||
const serviceKey = `${collection.provider}:${collection.service}`
|
||||
if (!groups[serviceKey]) {
|
||||
groups[serviceKey] = []
|
||||
}
|
||||
groups[serviceKey].push(collection)
|
||||
})
|
||||
|
||||
return groups
|
||||
})
|
||||
|
||||
const response = await collectionService.create({
|
||||
provider: service.provider,
|
||||
service: service.id,
|
||||
data: collection.toJson(),
|
||||
options,
|
||||
uid
|
||||
});
|
||||
|
||||
const createdCollection = new CollectionObject().fromJson(response);
|
||||
collections.value.push(createdCollection);
|
||||
|
||||
console.debug('[Chrono Manager](Store) - Successfully created collection');
|
||||
|
||||
return createdCollection;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to create collection:', error);
|
||||
throw error;
|
||||
/**
|
||||
* Get a specific collection from store, with optional retrieval
|
||||
*
|
||||
* @param provider - provider identifier
|
||||
* @param service - service identifier
|
||||
* @param identifier - collection identifier
|
||||
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||
*
|
||||
* @returns Collection object or null
|
||||
*/
|
||||
function collection(provider: string, service: string | number, identifier: string | number, retrieve: boolean = false): CollectionObject | null {
|
||||
const key = identifierKey(provider, service, identifier)
|
||||
if (retrieve === true && !_collections.value[key]) {
|
||||
console.debug(`[Chrono Manager][Store] - Force fetching collection "${key}"`)
|
||||
fetch(provider, service, identifier)
|
||||
}
|
||||
|
||||
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(
|
||||
collection: CollectionObject,
|
||||
uid?: string
|
||||
): Promise<CollectionObject | null> {
|
||||
try {
|
||||
if (!collection.provider || !collection.service || !collection.id) {
|
||||
throw new Error('Collection must have provider, service, and id');
|
||||
}
|
||||
|
||||
const response = await collectionService.modify({
|
||||
provider: collection.provider,
|
||||
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);
|
||||
function collectionsForService(provider: string, service: string | number, retrieve: boolean = false): CollectionObject[] {
|
||||
const serviceKeyPrefix = `${provider}:${service}:`
|
||||
const serviceCollections = Object.entries(_collections.value)
|
||||
.filter(([key]) => key.startsWith(serviceKeyPrefix))
|
||||
.map(([_, collection]) => collection)
|
||||
|
||||
if (retrieve === true && serviceCollections.length === 0) {
|
||||
console.debug(`[Chrono Manager][Store] - Force fetching collections for service "${provider}:${service}"`)
|
||||
const sources: SourceSelector = {
|
||||
[provider]: {
|
||||
[String(service)]: true
|
||||
}
|
||||
}
|
||||
|
||||
console.debug('[Chrono Manager](Store) - Successfully destroyed collection');
|
||||
|
||||
return response.success;
|
||||
list(sources)
|
||||
}
|
||||
|
||||
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) {
|
||||
console.error('[Chrono Manager](Store) - Failed to destroy collection:', error);
|
||||
throw error;
|
||||
console.error('[Chrono Manager][Store] - Failed to retrieve collections:', 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
|
||||
collections,
|
||||
/**
|
||||
* Retrieve collection availability status for a given source selector
|
||||
*
|
||||
* @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
|
||||
collection,
|
||||
list,
|
||||
fetch,
|
||||
fresh,
|
||||
extant,
|
||||
create,
|
||||
modify,
|
||||
destroy,
|
||||
};
|
||||
});
|
||||
update,
|
||||
delete: remove,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,281 +1,346 @@
|
||||
/**
|
||||
* Chrono Manager - Entities Store
|
||||
* Entities Store
|
||||
*/
|
||||
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
import { entityService } from '../services/entityService';
|
||||
import { EntityObject } from '../models/entity';
|
||||
import { EventObject } from '../models/event';
|
||||
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';
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { entityService } from '../services'
|
||||
import { EntityObject } from '../models'
|
||||
import type { SourceSelector, ListFilter, ListSort, ListRange } from '../types/common'
|
||||
|
||||
export const useEntitiesStore = defineStore('chronoEntitiesStore', () => {
|
||||
// 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
|
||||
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
entities.value = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 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[]> {
|
||||
async function list(sources?: SourceSelector, filter?: ListFilter, sort?: ListSort, range?: ListRange): Promise<Record<string, EntityObject>> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
// Validate hierarchical requirements
|
||||
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');
|
||||
}
|
||||
const response = await entityService.list({ sources, filter, sort, range })
|
||||
|
||||
// Build sources object level by level
|
||||
const sources: SourceSelector = {};
|
||||
if (provider !== null) {
|
||||
if (service !== null) {
|
||||
if (collection !== null) {
|
||||
sources[provider] = { [service]: { [collection]: true } };
|
||||
} else {
|
||||
sources[provider] = { [service]: true };
|
||||
}
|
||||
} else {
|
||||
sources[provider] = true;
|
||||
}
|
||||
}
|
||||
// Flatten nested structure: provider:service:collection:entity -> "provider:service:collection:entity": object
|
||||
const entities: Record<string, EntityObject> = {}
|
||||
Object.entries(response).forEach(([providerId, providerServices]) => {
|
||||
Object.entries(providerServices).forEach(([serviceId, serviceCollections]) => {
|
||||
Object.entries(serviceCollections).forEach(([collectionId, collectionEntities]) => {
|
||||
Object.entries(collectionEntities).forEach(([entityId, entityData]) => {
|
||||
const key = identifierKey(providerId, serviceId, collectionId, entityId)
|
||||
entities[key] = entityData
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// Transmit
|
||||
const response = await entityService.list({ sources, filter, sort, range, uid });
|
||||
|
||||
// Flatten the nested response into a flat array
|
||||
const flatEntities: EntityObject[] = [];
|
||||
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;
|
||||
// Merge retrieved entities into state
|
||||
_entities.value = { ..._entities.value, ...entities }
|
||||
|
||||
console.debug('[Chrono Manager][Store] - Successfully retrieved', Object.keys(entities).length, 'entities')
|
||||
return entities
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to retrieve entities:', error);
|
||||
throw error;
|
||||
console.error('[Chrono Manager][Store] - Failed to retrieve entities:', 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(
|
||||
collection: CollectionObject,
|
||||
identifiers: (string | number)[],
|
||||
uid?: string
|
||||
): Promise<EntityObject[]> {
|
||||
async function fetch(provider: string, service: string | number, collection: string | number, identifiers: (string | number)[]): Promise<Record<string, EntityObject>> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
if (!collection.provider || !collection.service || !collection.id) {
|
||||
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
|
||||
});
|
||||
const response = await entityService.fetch({ provider, service, collection, identifiers })
|
||||
|
||||
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) {
|
||||
console.error('[Chrono Manager](Store) - Failed to fetch entities:', error);
|
||||
throw error;
|
||||
console.error('[Chrono Manager][Store] - Failed to fetch entities:', 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 {
|
||||
const entity = new EntityObject();
|
||||
|
||||
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> {
|
||||
async function extant(sources: SourceSelector) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
if (!collection.provider || !collection.service || !collection.id) {
|
||||
throw new Error('Collection must have provider, service, and id');
|
||||
}
|
||||
|
||||
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;
|
||||
const response = await entityService.extant({ sources })
|
||||
console.debug('[Chrono Manager][Store] - Successfully checked entity availability')
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to create entity:', error);
|
||||
throw error;
|
||||
console.error('[Chrono Manager][Store] - Failed to check entity availability:', 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(
|
||||
collection: CollectionObject,
|
||||
entity: EntityObject,
|
||||
uid?: string
|
||||
): Promise<EntityObject | null> {
|
||||
async function create(provider: string, service: string | number, collection: string | number, data: any): Promise<EntityObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
if (!collection.provider || !collection.service || !collection.id) {
|
||||
throw new Error('Collection must have provider, service, and id');
|
||||
}
|
||||
if (!entity.in || !entity.id) {
|
||||
throw new Error('Invalid entity object, must have an collection and entity identifier');
|
||||
}
|
||||
if (collection.id !== entity.in) {
|
||||
throw new Error('Invalid entity object, does not belong to the specified collection');
|
||||
}
|
||||
const response = await entityService.create({ provider, service, collection, properties: data })
|
||||
|
||||
// Add created entity to state
|
||||
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
|
||||
_entities.value[key] = response
|
||||
|
||||
const response = await entityService.modify({
|
||||
provider: collection.provider,
|
||||
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;
|
||||
console.debug('[Chrono Manager][Store] - Successfully created entity:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to modify entity:', error);
|
||||
throw error;
|
||||
console.error('[Chrono Manager][Store] - Failed to create entity:', 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(
|
||||
collection: CollectionObject,
|
||||
entity: EntityObject,
|
||||
uid?: string
|
||||
): Promise<boolean> {
|
||||
async function update(provider: string, service: string | number, collection: string | number, identifier: string | number, data: any): Promise<EntityObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
if (!collection.provider || !collection.service || !collection.id) {
|
||||
throw new Error('Collection must have provider, service, and id');
|
||||
}
|
||||
if (!entity.in || !entity.id) {
|
||||
throw new Error('Invalid entity object, must have an collection and entity identifier');
|
||||
}
|
||||
if (collection.id !== entity.in) {
|
||||
throw new Error('Invalid entity object, does not belong to the specified collection');
|
||||
}
|
||||
const response = await entityService.update({ provider, service, collection, identifier, properties: data })
|
||||
|
||||
// Update entity in state
|
||||
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
|
||||
_entities.value[key] = response
|
||||
|
||||
const response = await entityService.destroy({
|
||||
provider: collection.provider,
|
||||
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;
|
||||
console.debug('[Chrono Manager][Store] - Successfully updated entity:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to destroy entity:', error);
|
||||
throw error;
|
||||
console.error('[Chrono Manager][Store] - Failed to update entity:', 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 {
|
||||
// State
|
||||
// State (readonly)
|
||||
transceiving: readonly(transceiving),
|
||||
// Getters
|
||||
count,
|
||||
has,
|
||||
entities,
|
||||
|
||||
entitiesForCollection,
|
||||
// Actions
|
||||
reset,
|
||||
entity,
|
||||
list,
|
||||
fetch,
|
||||
fresh,
|
||||
extant,
|
||||
create,
|
||||
modify,
|
||||
destroy,
|
||||
};
|
||||
});
|
||||
update,
|
||||
delete: remove,
|
||||
delta,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
/**
|
||||
* Central export point for all Chrono Manager stores
|
||||
*/
|
||||
|
||||
export { useCollectionsStore } from './collectionsStore';
|
||||
export { useEntitiesStore } from './entitiesStore';
|
||||
export { useProvidersStore } from './providersStore';
|
||||
|
||||
@@ -1,62 +1,142 @@
|
||||
/**
|
||||
* Chrono Manager - Providers Store
|
||||
* Providers Store
|
||||
*/
|
||||
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
import { providerService } from '../services/providerService';
|
||||
import type {
|
||||
SourceSelector,
|
||||
ProviderInterface,
|
||||
} from '../types';
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { providerService } from '../services'
|
||||
import { ProviderObject } from '../models/provider'
|
||||
import type { SourceSelector } from '../types'
|
||||
|
||||
export const useProvidersStore = defineStore('chronoProvidersStore', () => {
|
||||
// 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
|
||||
|
||||
/**
|
||||
* List all available providers
|
||||
*
|
||||
* @returns Promise with provider list keyed by provider ID
|
||||
* Retrieve all or specific providers, optionally filtered by source selector
|
||||
*
|
||||
* @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 {
|
||||
const response = await providerService.list();
|
||||
|
||||
console.debug('[Chrono Manager](Store) - Successfully retrieved', Object.keys(response).length, 'providers:', Object.keys(response));
|
||||
|
||||
providers.value = response;
|
||||
return response;
|
||||
const providers = await providerService.list({ sources })
|
||||
|
||||
// Merge retrieved providers into state
|
||||
_providers.value = { ..._providers.value, ...providers }
|
||||
|
||||
console.debug('[Chrono Manager][Store] - Successfully retrieved', Object.keys(providers).length, 'providers')
|
||||
return providers
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to retrieve providers:', error);
|
||||
throw error;
|
||||
console.error('[Chrono Manager][Store] - Failed to retrieve providers:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check which providers exist/are available
|
||||
*
|
||||
* @param sources - Source selector with provider IDs to check
|
||||
* @returns Promise with provider availability status
|
||||
* Retrieve a specific provider by identifier
|
||||
*
|
||||
* @param identifier - provider identifier
|
||||
*
|
||||
* @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 {
|
||||
const response = await providerService.extant(sources);
|
||||
return response;
|
||||
const provider = await providerService.fetch({ identifier })
|
||||
|
||||
// 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) {
|
||||
console.error('[Chrono Manager](Store) - Failed to check provider existence:', error);
|
||||
throw error;
|
||||
console.error('[Chrono Manager][Store] - Failed to fetch provider:', 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 {
|
||||
// State
|
||||
transceiving: readonly(transceiving),
|
||||
// computed
|
||||
count,
|
||||
has,
|
||||
providers,
|
||||
|
||||
// Actions
|
||||
provider,
|
||||
// functions
|
||||
list,
|
||||
fetch,
|
||||
extant,
|
||||
};
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,95 +1,259 @@
|
||||
/**
|
||||
* Chrono Manager - Services Store
|
||||
* Services Store
|
||||
*/
|
||||
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
import { serviceService } from '../services/serviceService';
|
||||
import { ServiceObject } from '../models/service';
|
||||
import type { ServiceInterface } from '../types/service';
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { serviceService } from '../services'
|
||||
import { ServiceObject } from '../models/service'
|
||||
import type {
|
||||
SourceSelector,
|
||||
ListFilter,
|
||||
ListSort,
|
||||
} from '../types/common';
|
||||
ServiceInterface,
|
||||
} from '../types'
|
||||
|
||||
export const useServicesStore = defineStore('chronoServicesStore', () => {
|
||||
// State
|
||||
const services = ref<ServiceObject[]>([]);
|
||||
// Actions
|
||||
const _services = ref<Record<string, ServiceObject>>({})
|
||||
const transceiving = ref(false)
|
||||
|
||||
/**
|
||||
* Retrieve services from the server
|
||||
* Get count of services in store
|
||||
*/
|
||||
async function list(
|
||||
sources?: SourceSelector,
|
||||
filter?: ListFilter,
|
||||
sort?: ListSort,
|
||||
uid?: string
|
||||
): Promise<ServiceObject[]> {
|
||||
try {
|
||||
const response = await serviceService.list({ sources, filter, sort, uid });
|
||||
const count = computed(() => Object.keys(_services.value).length)
|
||||
|
||||
// Flatten the nested response into a flat array
|
||||
const flatServices: ServiceObject[] = [];
|
||||
Object.entries(response).forEach(([providerId, providerServices]) => {
|
||||
Object.values(providerServices).forEach((service: ServiceInterface) => {
|
||||
// Ensure provider is set on the service object
|
||||
service.provider = service.provider || providerId;
|
||||
flatServices.push(new ServiceObject().fromJson(service));
|
||||
});
|
||||
});
|
||||
/**
|
||||
* Check if any services are present in store
|
||||
*/
|
||||
const has = computed(() => count.value > 0)
|
||||
|
||||
console.debug('[Chrono Manager](Store) - Successfully retrieved', flatServices.length, 'services:', flatServices.map(s => ({
|
||||
id: s.id,
|
||||
label: s.label,
|
||||
provider: s.provider
|
||||
})));
|
||||
/**
|
||||
* Get all services present in store
|
||||
*/
|
||||
const services = computed(() => Object.values(_services.value))
|
||||
|
||||
services.value = flatServices;
|
||||
return flatServices;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to retrieve services:', error);
|
||||
throw error;
|
||||
/**
|
||||
* Get all services present in store grouped by provider
|
||||
*/
|
||||
const servicesByProvider = computed(() => {
|
||||
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
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param identifier - Service identifier
|
||||
* @param uid - Optional user identifier
|
||||
* Unique key for a service
|
||||
*/
|
||||
function identifierKey(provider: string, identifier: string | number | null): string {
|
||||
return `${provider}:${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
|
||||
*/
|
||||
async function fetch(
|
||||
provider: string,
|
||||
identifier: string,
|
||||
uid?: string
|
||||
): Promise<ServiceObject | null> {
|
||||
async function fetch(provider: string, identifier: string | number): Promise<ServiceObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await serviceService.fetch({ provider, service: identifier, uid });
|
||||
return new ServiceObject().fromJson(response);
|
||||
const service = await serviceService.fetch({ provider, identifier })
|
||||
|
||||
// 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) {
|
||||
console.error('[Chrono Manager](Store) - Failed to fetch service:', error);
|
||||
throw error;
|
||||
console.error('[Chrono Manager][Store] - Failed to fetch service:', 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 {
|
||||
return new ServiceObject();
|
||||
async function extant(sources: SourceSelector) {
|
||||
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
|
||||
services,
|
||||
/**
|
||||
* Create a new service with given provider and data
|
||||
*
|
||||
* @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
|
||||
service,
|
||||
list,
|
||||
fetch,
|
||||
fresh,
|
||||
};
|
||||
});
|
||||
extant,
|
||||
create,
|
||||
update,
|
||||
delete: remove,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user