Initial commit
This commit is contained in:
@@ -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