chore: standardize protocol
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -1,169 +1,329 @@
|
||||
/**
|
||||
* Collections Store
|
||||
*/
|
||||
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { collectionService } from '../services'
|
||||
import type { CollectionInterface, CollectionCreateRequest } from '../types'
|
||||
import { CollectionObject, CollectionPropertiesObject } from '../models/collection'
|
||||
import { CollectionObject } from '../models/collection'
|
||||
import type { SourceSelector, ListFilter, ListSort, CollectionMutableProperties } from '../types'
|
||||
|
||||
export const useCollectionsStore = defineStore('mail-collections', {
|
||||
state: () => ({
|
||||
collections: {} as Record<string, Record<string, Record<string, CollectionObject>>>,
|
||||
loading: false,
|
||||
error: null as string | null,
|
||||
}),
|
||||
export const useCollectionsStore = defineStore('mailCollectionsStore', () => {
|
||||
// State
|
||||
const _collections = ref<Record<string, CollectionObject>>({})
|
||||
const transceiving = ref(false)
|
||||
|
||||
actions: {
|
||||
async loadCollections(sources?: any) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
try {
|
||||
const response = await collectionService.list({ sources })
|
||||
|
||||
// Response is already in nested object format: provider -> service -> collection
|
||||
// Transform to CollectionObject instances
|
||||
const transformed: Record<string, Record<string, Record<string, CollectionObject>>> = {}
|
||||
|
||||
for (const [providerId, providerData] of Object.entries(response)) {
|
||||
transformed[providerId] = {}
|
||||
|
||||
for (const [serviceId, collections] of Object.entries(providerData as any)) {
|
||||
transformed[providerId][serviceId] = {}
|
||||
|
||||
// Collections come as an object keyed by identifier
|
||||
for (const [collectionId, collection] of Object.entries(collections as any)) {
|
||||
// Create CollectionObject instance with provider and service set
|
||||
const collectionData = {
|
||||
...collection,
|
||||
provider: providerId,
|
||||
service: serviceId,
|
||||
} as CollectionInterface
|
||||
|
||||
transformed[providerId][serviceId][collectionId] = new CollectionObject().fromJson(collectionData)
|
||||
}
|
||||
/**
|
||||
* Get count of collections in store
|
||||
*/
|
||||
const count = computed(() => Object.keys(_collections.value).length)
|
||||
|
||||
/**
|
||||
* Check if any collections are present in store
|
||||
*/
|
||||
const has = computed(() => count.value > 0)
|
||||
|
||||
/**
|
||||
* Get all collections present in store
|
||||
*/
|
||||
const collections = computed(() => Object.values(_collections.value))
|
||||
|
||||
/**
|
||||
* Get all collections present in store grouped by service
|
||||
*/
|
||||
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
|
||||
})
|
||||
|
||||
/**
|
||||
* 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(`[Mail Manager][Store] - Force fetching collection "${key}"`)
|
||||
fetch(provider, service, identifier)
|
||||
}
|
||||
|
||||
return _collections.value[key] || null
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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(`[Mail Manager][Store] - Force fetching collections for service "${provider}:${service}"`)
|
||||
const sources: SourceSelector = {
|
||||
[provider]: {
|
||||
[String(service)]: true
|
||||
}
|
||||
}
|
||||
list(sources)
|
||||
}
|
||||
|
||||
return serviceCollections
|
||||
}
|
||||
|
||||
function collectionsInCollection(provider: string, service: string | number, collectionId: string | number, retrieve: boolean = false): CollectionObject[] {
|
||||
const collectionKeyPrefix = `${provider}:${service}:${collectionId}:`
|
||||
const nestedCollections = Object.entries(_collections.value)
|
||||
.filter(([key]) => key.startsWith(collectionKeyPrefix))
|
||||
.map(([_, collection]) => collection)
|
||||
|
||||
if (retrieve === true && nestedCollections.length === 0) {
|
||||
console.debug(`[Mail Manager][Store] - Force fetching collections in collection "${provider}:${service}:${collectionId}"`)
|
||||
const sources: SourceSelector = {
|
||||
[provider]: {
|
||||
[String(service)]: {
|
||||
[String(collectionId)]: true
|
||||
}
|
||||
}
|
||||
|
||||
this.collections = transformed
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
list(sources)
|
||||
}
|
||||
|
||||
return nestedCollections
|
||||
}
|
||||
|
||||
async getCollection(provider: string, service: string | number, collectionId: string | number) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
try {
|
||||
const response = await collectionService.fetch({
|
||||
provider,
|
||||
service,
|
||||
collection: collectionId
|
||||
})
|
||||
|
||||
// Create CollectionObject instance
|
||||
const collectionObject = new CollectionObject().fromJson(response)
|
||||
|
||||
// Update in store
|
||||
if (!this.collections[provider]) {
|
||||
this.collections[provider] = {}
|
||||
}
|
||||
if (!this.collections[provider][String(service)]) {
|
||||
this.collections[provider][String(service)] = {}
|
||||
}
|
||||
this.collections[provider][String(service)][String(collectionId)] = collectionObject
|
||||
|
||||
return collectionObject
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Create unique key for a collection
|
||||
*/
|
||||
function identifierKey(provider: string, service: string | number | null, identifier: string | number | null): string {
|
||||
return `${provider}:${service ?? ''}:${identifier ?? ''}`
|
||||
}
|
||||
|
||||
async createCollection(params: {
|
||||
provider: string
|
||||
service: string | number
|
||||
collection?: string | number | null
|
||||
properties: CollectionPropertiesObject
|
||||
}): Promise<CollectionObject> {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
|
||||
try {
|
||||
// Prepare request data from CollectionPropertiesObject
|
||||
const requestData: CollectionCreateRequest = {
|
||||
provider: params.provider,
|
||||
service: params.service,
|
||||
collection: params.collection ?? null,
|
||||
properties: {
|
||||
'@type': 'mail.collection',
|
||||
label: params.properties.label,
|
||||
role: params.properties.role ?? null,
|
||||
rank: params.properties.rank ?? 0,
|
||||
subscribed: params.properties.subscribed ?? true,
|
||||
},
|
||||
}
|
||||
|
||||
// Call service to create collection
|
||||
const response = await collectionService.create(requestData)
|
||||
|
||||
// Create CollectionObject instance
|
||||
const collectionObject = new CollectionObject().fromJson(response)
|
||||
|
||||
// Update store with new collection
|
||||
const provider = response.provider
|
||||
const service = String(response.service)
|
||||
const identifier = String(response.identifier)
|
||||
|
||||
if (!this.collections[provider]) {
|
||||
this.collections[provider] = {}
|
||||
}
|
||||
if (!this.collections[provider][service]) {
|
||||
this.collections[provider][service] = {}
|
||||
}
|
||||
|
||||
this.collections[provider][service][identifier] = collectionObject
|
||||
|
||||
return collectionObject
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
},
|
||||
// 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 })
|
||||
|
||||
getters: {
|
||||
collectionList: (state) => {
|
||||
const list: CollectionObject[] = []
|
||||
Object.values(state.collections).forEach(providerCollections => {
|
||||
Object.values(providerCollections).forEach(serviceCollections => {
|
||||
Object.values(serviceCollections).forEach(collection => {
|
||||
list.push(collection)
|
||||
// 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
|
||||
})
|
||||
})
|
||||
})
|
||||
return list
|
||||
},
|
||||
|
||||
collectionCount: (state) => {
|
||||
let count = 0
|
||||
Object.values(state.collections).forEach(providerCollections => {
|
||||
Object.values(providerCollections).forEach(serviceCollections => {
|
||||
count += Object.keys(serviceCollections).length
|
||||
})
|
||||
// Merge retrieved collections into state
|
||||
_collections.value = { ..._collections.value, ...collections }
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully retrieved', Object.keys(collections).length, 'collections')
|
||||
return collections
|
||||
} catch (error: any) {
|
||||
console.error('[Mail 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('[Mail Manager][Store] - Successfully fetched collection:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to fetch collection:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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('[Mail Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'collections')
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail 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: CollectionMutableProperties): Promise<CollectionObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.create({
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
properties: data
|
||||
})
|
||||
return count
|
||||
},
|
||||
|
||||
// Merge created collection into state
|
||||
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||
_collections.value[key] = response
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully created collection:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to create collection:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
hasCollections: (state) => {
|
||||
return Object.values(state.collections).some(providerCollections =>
|
||||
Object.values(providerCollections).some(serviceCollections =>
|
||||
Object.keys(serviceCollections).length > 0
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
/**
|
||||
* 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: CollectionMutableProperties): 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('[Mail Manager][Store] - Successfully updated collection:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail 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('[Mail Manager][Store] - Successfully deleted collection:', key)
|
||||
} catch (error: any) {
|
||||
console.error('[Mail 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,
|
||||
collectionsInCollection,
|
||||
// Actions
|
||||
collection,
|
||||
list,
|
||||
fetch,
|
||||
extant,
|
||||
create,
|
||||
update,
|
||||
delete: remove,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user