Initial commit
This commit is contained in:
@@ -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,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user