050467919b
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
217 lines
7.5 KiB
TypeScript
217 lines
7.5 KiB
TypeScript
/**
|
|
* Collections Store
|
|
*/
|
|
|
|
import { ref, computed, readonly } from 'vue'
|
|
import { defineStore } from 'pinia'
|
|
import { collectionService } from '../services/collectionService'
|
|
import type {
|
|
CollectionIdentifier,
|
|
ServiceIdentifier,
|
|
ListFilter,
|
|
ListSort,
|
|
CollectionMutableProperties,
|
|
} from '../types'
|
|
import { CollectionObject } from '../models/collection'
|
|
|
|
export const useCollectionsStore = defineStore('documentsCollectionsStore', () => {
|
|
// State
|
|
const _collections = ref<Record<string, CollectionObject>>({})
|
|
const transceiving = ref(false)
|
|
|
|
// Getters
|
|
const count = computed(() => Object.keys(_collections.value).length)
|
|
const has = computed(() => count.value > 0)
|
|
const collections = computed(() => Object.values(_collections.value))
|
|
|
|
const collectionsByService = computed(() => {
|
|
const groups: Record<string, CollectionObject[]> = {}
|
|
|
|
Object.values(_collections.value).forEach((collection) => {
|
|
const serviceKey = `${collection.provider}:${collection.service}`
|
|
const serviceCollections = (groups[serviceKey] ??= [])
|
|
serviceCollections.push(collection)
|
|
})
|
|
|
|
return groups
|
|
})
|
|
|
|
function identifierKey(provider: string, service: string | number | null, identifier: string | number | null): string {
|
|
return `${provider}:${service ?? ''}:${identifier ?? ''}`
|
|
}
|
|
|
|
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(`[Documents Manager][Store] - Force fetching collection "${key}"`)
|
|
fetch(provider, service, identifier)
|
|
}
|
|
|
|
return _collections.value[key] || null
|
|
}
|
|
|
|
function collectionsForService(provider: string, service: string | number): CollectionObject[] {
|
|
const serviceKeyPrefix = `${provider}:${service}:`
|
|
return Object.entries(_collections.value)
|
|
.filter(([key]) => key.startsWith(serviceKeyPrefix))
|
|
.map(([_, collectionObj]) => collectionObj)
|
|
}
|
|
|
|
function clearService(provider: string, service: string | number): void {
|
|
const serviceKeyPrefix = `${provider}:${service}:`
|
|
Object.keys(_collections.value)
|
|
.filter((key) => key.startsWith(serviceKeyPrefix))
|
|
.forEach((key) => {
|
|
delete _collections.value[key]
|
|
})
|
|
}
|
|
|
|
function clearAll(): void {
|
|
_collections.value = {}
|
|
}
|
|
|
|
// Actions
|
|
async function list(sources?: (ServiceIdentifier | CollectionIdentifier)[], filter?: ListFilter, sort?: ListSort): Promise<Record<string, CollectionObject>> {
|
|
transceiving.value = true
|
|
try {
|
|
const response = await collectionService.list({ sources, filter, sort })
|
|
|
|
const hydrated: Record<string, CollectionObject> = {}
|
|
Object.entries(response).forEach(([providerId, providerServices]) => {
|
|
Object.entries(providerServices).forEach(([serviceId, serviceCollections]) => {
|
|
Object.entries(serviceCollections).forEach(([collectionId, collectionObj]) => {
|
|
const key = identifierKey(providerId, serviceId, collectionId)
|
|
hydrated[key] = collectionObj
|
|
})
|
|
})
|
|
})
|
|
|
|
_collections.value = { ..._collections.value, ...hydrated }
|
|
|
|
console.debug('[Documents Manager][Store] - Successfully retrieved', Object.keys(hydrated).length, 'collections')
|
|
return hydrated
|
|
} catch (error: any) {
|
|
console.error('[Documents Manager][Store] - Failed to retrieve collections:', error)
|
|
throw error
|
|
} finally {
|
|
transceiving.value = false
|
|
}
|
|
}
|
|
|
|
async function fetch(provider: string, service: string | number, identifier: string | number): Promise<CollectionObject> {
|
|
transceiving.value = true
|
|
try {
|
|
const response = await collectionService.fetch({ targets: [`${provider}:${service}:${identifier}`] })
|
|
const key = identifierKey(response.provider, response.service, response.identifier)
|
|
_collections.value[key] = response
|
|
|
|
console.debug('[Documents Manager][Store] - Successfully fetched collection:', key)
|
|
return response
|
|
} catch (error: any) {
|
|
console.error('[Documents Manager][Store] - Failed to fetch collection:', error)
|
|
throw error
|
|
} finally {
|
|
transceiving.value = false
|
|
}
|
|
}
|
|
|
|
async function extant(targets: CollectionIdentifier[]) {
|
|
transceiving.value = true
|
|
try {
|
|
const response = await collectionService.extant({ targets })
|
|
console.debug('[Documents Manager][Store] - Successfully checked collection availability')
|
|
return response
|
|
} catch (error: any) {
|
|
console.error('[Documents Manager][Store] - Failed to check collection availability:', error)
|
|
throw error
|
|
} finally {
|
|
transceiving.value = false
|
|
}
|
|
}
|
|
|
|
async function create(
|
|
provider: string,
|
|
service: string | number,
|
|
collection: string | number | null,
|
|
properties: CollectionMutableProperties,
|
|
): Promise<CollectionObject> {
|
|
transceiving.value = true
|
|
try {
|
|
const target: CollectionIdentifier | undefined = collection !== null && collection !== undefined && collection !== ''
|
|
? `${provider}:${service}:${collection}`
|
|
: undefined
|
|
const response = await collectionService.create({ provider, service: String(service), target, properties })
|
|
const key = identifierKey(response.provider, response.service, response.identifier)
|
|
_collections.value[key] = response
|
|
|
|
console.debug('[Documents Manager][Store] - Successfully created collection:', key)
|
|
return response
|
|
} catch (error: any) {
|
|
console.error('[Documents Manager][Store] - Failed to create collection:', error)
|
|
throw error
|
|
} finally {
|
|
transceiving.value = false
|
|
}
|
|
}
|
|
|
|
async function update(
|
|
provider: string,
|
|
service: string | number,
|
|
identifier: string | number,
|
|
properties: CollectionMutableProperties,
|
|
): Promise<CollectionObject> {
|
|
transceiving.value = true
|
|
try {
|
|
const response = await collectionService.update({ target: `${provider}:${service}:${identifier}`, properties })
|
|
const key = identifierKey(response.provider, response.service, response.identifier)
|
|
_collections.value[key] = response
|
|
|
|
console.debug('[Documents Manager][Store] - Successfully updated collection:', key)
|
|
return response
|
|
} catch (error: any) {
|
|
console.error('[Documents Manager][Store] - Failed to update collection:', error)
|
|
throw error
|
|
} finally {
|
|
transceiving.value = false
|
|
}
|
|
}
|
|
|
|
async function remove(provider: string, service: string | number, identifier: string | number): Promise<{ success: boolean }> {
|
|
transceiving.value = true
|
|
try {
|
|
const response = await collectionService.delete({ target: `${provider}:${service}:${identifier}` })
|
|
const success = response.disposition === 'deleted' || response.disposition === 'moved'
|
|
if (success) {
|
|
const key = identifierKey(provider, service, identifier)
|
|
delete _collections.value[key]
|
|
}
|
|
|
|
console.debug('[Documents Manager][Store] - Successfully deleted collection:', `${provider}:${service}:${identifier}`)
|
|
return { success }
|
|
} catch (error: any) {
|
|
console.error('[Documents Manager][Store] - Failed to delete collection:', error)
|
|
throw error
|
|
} finally {
|
|
transceiving.value = false
|
|
}
|
|
}
|
|
|
|
return {
|
|
transceiving: readonly(transceiving),
|
|
count,
|
|
has,
|
|
collections,
|
|
collectionsByService,
|
|
collection,
|
|
collectionsForService,
|
|
clearService,
|
|
clearAll,
|
|
list,
|
|
fetch,
|
|
extant,
|
|
create,
|
|
update,
|
|
delete: remove,
|
|
}
|
|
})
|