Initial commit
This commit is contained in:
202
src/stores/collectionsStore.ts
Normal file
202
src/stores/collectionsStore.ts
Normal file
@@ -0,0 +1,202 @@
|
||||
/**
|
||||
* Chrono Manager - 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';
|
||||
|
||||
export const useCollectionsStore = defineStore('chronoCollectionsStore', () => {
|
||||
// State
|
||||
const collections = ref<CollectionObject[]>([]);
|
||||
|
||||
// Actions
|
||||
|
||||
/**
|
||||
* Retrieve collections from the server
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a specific collection
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fresh collection object with default values
|
||||
*/
|
||||
function fresh(): CollectionObject {
|
||||
return new CollectionObject();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new collection
|
||||
*/
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify an existing collection
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
console.debug('[Chrono Manager](Store) - Successfully destroyed collection');
|
||||
|
||||
return response.success;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to destroy collection:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
collections,
|
||||
|
||||
// Actions
|
||||
list,
|
||||
fetch,
|
||||
fresh,
|
||||
create,
|
||||
modify,
|
||||
destroy,
|
||||
};
|
||||
});
|
||||
281
src/stores/entitiesStore.ts
Normal file
281
src/stores/entitiesStore.ts
Normal file
@@ -0,0 +1,281 @@
|
||||
/**
|
||||
* Chrono Manager - 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';
|
||||
|
||||
export const useEntitiesStore = defineStore('chronoEntitiesStore', () => {
|
||||
// State
|
||||
const entities = ref<EntityObject[]>([]);
|
||||
|
||||
// Actions
|
||||
|
||||
/**
|
||||
* Reset the store to initial state
|
||||
*/
|
||||
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[]> {
|
||||
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');
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
// 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;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to retrieve entities:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch entities for a specific collection
|
||||
*/
|
||||
async function fetch(
|
||||
collection: CollectionObject,
|
||||
identifiers: (string | number)[],
|
||||
uid?: string
|
||||
): Promise<EntityObject[]> {
|
||||
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
|
||||
});
|
||||
|
||||
return Object.values(response).map(entity => new EntityObject().fromJson(entity));
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to fetch entities:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fresh entity object
|
||||
*/
|
||||
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> {
|
||||
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;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to create entity:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Modify an existing entity
|
||||
*/
|
||||
async function modify(
|
||||
collection: CollectionObject,
|
||||
entity: EntityObject,
|
||||
uid?: string
|
||||
): Promise<EntityObject | null> {
|
||||
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.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;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to modify entity:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an entity
|
||||
*/
|
||||
async function destroy(
|
||||
collection: CollectionObject,
|
||||
entity: EntityObject,
|
||||
uid?: string
|
||||
): Promise<boolean> {
|
||||
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.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;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to destroy entity:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
entities,
|
||||
|
||||
// Actions
|
||||
reset,
|
||||
list,
|
||||
fetch,
|
||||
fresh,
|
||||
create,
|
||||
modify,
|
||||
destroy,
|
||||
};
|
||||
});
|
||||
8
src/stores/index.ts
Normal file
8
src/stores/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Central export point for all Chrono Manager stores
|
||||
*/
|
||||
|
||||
export { useCollectionsStore } from './collectionsStore';
|
||||
export { useEntitiesStore } from './entitiesStore';
|
||||
export { useProvidersStore } from './providersStore';
|
||||
export { useServicesStore } from './servicesStore';
|
||||
62
src/stores/providersStore.ts
Normal file
62
src/stores/providersStore.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Chrono Manager - Providers Store
|
||||
*/
|
||||
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
import { providerService } from '../services/providerService';
|
||||
import type {
|
||||
SourceSelector,
|
||||
ProviderInterface,
|
||||
} from '../types';
|
||||
|
||||
export const useProvidersStore = defineStore('chronoProvidersStore', () => {
|
||||
// State
|
||||
const providers = ref<Record<string, ProviderInterface>>({});
|
||||
|
||||
// Actions
|
||||
|
||||
/**
|
||||
* List all available providers
|
||||
*
|
||||
* @returns Promise with provider list keyed by provider ID
|
||||
*/
|
||||
async function list(): Promise<Record<string, ProviderInterface>> {
|
||||
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;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to retrieve providers:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check which providers exist/are available
|
||||
*
|
||||
* @param sources - Source selector with provider IDs to check
|
||||
* @returns Promise with provider availability status
|
||||
*/
|
||||
async function extant(sources: SourceSelector): Promise<Record<string, boolean>> {
|
||||
try {
|
||||
const response = await providerService.extant(sources);
|
||||
return response;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to check provider existence:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
providers,
|
||||
|
||||
// Actions
|
||||
list,
|
||||
extant,
|
||||
};
|
||||
});
|
||||
95
src/stores/servicesStore.ts
Normal file
95
src/stores/servicesStore.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Chrono Manager - 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 type {
|
||||
SourceSelector,
|
||||
ListFilter,
|
||||
ListSort,
|
||||
} from '../types/common';
|
||||
|
||||
export const useServicesStore = defineStore('chronoServicesStore', () => {
|
||||
// State
|
||||
const services = ref<ServiceObject[]>([]);
|
||||
// Actions
|
||||
|
||||
/**
|
||||
* Retrieve services from the server
|
||||
*/
|
||||
async function list(
|
||||
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[] = [];
|
||||
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));
|
||||
});
|
||||
});
|
||||
|
||||
console.debug('[Chrono Manager](Store) - Successfully retrieved', flatServices.length, 'services:', flatServices.map(s => ({
|
||||
id: s.id,
|
||||
label: s.label,
|
||||
provider: s.provider
|
||||
})));
|
||||
|
||||
services.value = flatServices;
|
||||
return flatServices;
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to retrieve services:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a specific service
|
||||
*
|
||||
* @param provider - Provider identifier
|
||||
* @param identifier - Service identifier
|
||||
* @param uid - Optional user identifier
|
||||
* @returns Promise with service object
|
||||
*/
|
||||
async function fetch(
|
||||
provider: string,
|
||||
identifier: string,
|
||||
uid?: string
|
||||
): Promise<ServiceObject | null> {
|
||||
try {
|
||||
const response = await serviceService.fetch({ provider, service: identifier, uid });
|
||||
return new ServiceObject().fromJson(response);
|
||||
} catch (error: any) {
|
||||
console.error('[Chrono Manager](Store) - Failed to fetch service:', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a fresh service object with default values
|
||||
*/
|
||||
function fresh(): ServiceObject {
|
||||
return new ServiceObject();
|
||||
}
|
||||
|
||||
return {
|
||||
// State
|
||||
services,
|
||||
|
||||
// Actions
|
||||
list,
|
||||
fetch,
|
||||
fresh,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user