refactor: front end code to unified manager api
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
+113
-125
@@ -4,11 +4,17 @@
|
||||
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import {
|
||||
type ServiceIdentifier,
|
||||
type CollectionIdentifier,
|
||||
type ListFilter,
|
||||
type ListSort,
|
||||
} from '../types'
|
||||
import { collectionService } from '../services'
|
||||
import { CollectionObject, CollectionPropertiesObject } from '../models/collection'
|
||||
import type { SourceSelector, ListFilter, ListSort } from '../types'
|
||||
|
||||
export const useCollectionsStore = defineStore('peopleCollectionsStore', () => {
|
||||
|
||||
// State
|
||||
const _collections = ref<Record<string, CollectionObject>>({})
|
||||
const transceiving = ref(false)
|
||||
@@ -33,85 +39,65 @@ export const useCollectionsStore = defineStore('peopleCollectionsStore', () => {
|
||||
*/
|
||||
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)
|
||||
const serviceKey = String(collection.service)
|
||||
const serviceCollections = (groups[serviceKey] ??= [])
|
||||
serviceCollections.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 target - 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(`[People Manager][Store] - Force fetching collection "${key}"`)
|
||||
fetch(provider, service, identifier)
|
||||
function collection(target: CollectionIdentifier, retrieve: boolean = false): CollectionObject | null {
|
||||
if (retrieve === true && !_collections.value[target]) {
|
||||
console.debug(`[People Manager][Store] - Force fetching collection "${target}"`)
|
||||
fetch([target])
|
||||
}
|
||||
|
||||
return _collections.value[key] || null
|
||||
|
||||
return _collections.value[target] || 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)
|
||||
|
||||
const serviceIdentifier = `${provider}:${service}` as ServiceIdentifier
|
||||
const serviceCollections = Object.values(_collections.value)
|
||||
.filter(collection => String(collection.service) === serviceIdentifier)
|
||||
|
||||
if (retrieve === true && serviceCollections.length === 0) {
|
||||
console.debug(`[People Manager][Store] - Force fetching collections for service "${provider}:${service}"`)
|
||||
const sources: SourceSelector = {
|
||||
[provider]: {
|
||||
[String(service)]: true
|
||||
}
|
||||
}
|
||||
list(sources)
|
||||
console.debug(`[People Manager][Store] - Force fetching collections for service "${serviceIdentifier}"`)
|
||||
list([serviceIdentifier])
|
||||
}
|
||||
|
||||
|
||||
return serviceCollections
|
||||
}
|
||||
|
||||
/**
|
||||
* Create unique key for a collection
|
||||
*/
|
||||
function identifierKey(provider: string, service: string | number | null, identifier: string | number | null): string {
|
||||
return `${provider}:${service ?? ''}:${identifier ?? ''}`
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/**
|
||||
* Retrieve all or specific collections, optionally filtered by source selector
|
||||
*
|
||||
* @param sources - optional source selector
|
||||
* Retrieve all or specific collections, optionally filtered by service/collection identifiers
|
||||
*
|
||||
* @param sources - optional service/collection identifiers
|
||||
* @param filter - optional list filter
|
||||
* @param sort - optional list sort
|
||||
*
|
||||
* @returns Promise with collection object list keyed by provider, service, and collection identifier
|
||||
*
|
||||
* @returns Promise with collection object list keyed by collection identifier
|
||||
*/
|
||||
async function list(sources?: SourceSelector, filter?: ListFilter, sort?: ListSort): Promise<Record<string, CollectionObject>> {
|
||||
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 })
|
||||
@@ -121,8 +107,7 @@ export const useCollectionsStore = defineStore('peopleCollectionsStore', () => {
|
||||
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
|
||||
collections[collectionObj.identifier] = collectionObj
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -139,29 +124,28 @@ export const useCollectionsStore = defineStore('peopleCollectionsStore', () => {
|
||||
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
|
||||
* Retrieve specific collections by their identifiers
|
||||
*
|
||||
* @param targets - collection identifiers to fetch
|
||||
*
|
||||
* @returns Promise with collection objects keyed by identifier
|
||||
*/
|
||||
async function fetch(provider: string, service: string | number, identifier: string | number): Promise<CollectionObject> {
|
||||
async function fetch(targets: CollectionIdentifier[]): Promise<Record<string, 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
|
||||
const response = await collectionService.fetch({ targets })
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully fetched collection:', key)
|
||||
// Merge fetched collections into state
|
||||
Object.values(response).forEach(collectionObj => {
|
||||
_collections.value[collectionObj.identifier] = collectionObj
|
||||
})
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully fetched collections:', Object.keys(response).join(', '))
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to fetch collection:', error)
|
||||
console.error('[People Manager][Store] - Failed to fetch collections:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
@@ -169,18 +153,18 @@ export const useCollectionsStore = defineStore('peopleCollectionsStore', () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve collection availability status for a given source selector
|
||||
*
|
||||
* @param sources - source selector to check availability for
|
||||
*
|
||||
* Retrieve collection availability status for the given collection identifiers
|
||||
*
|
||||
* @param targets - collection identifiers to check availability for
|
||||
*
|
||||
* @returns Promise with collection availability status
|
||||
*/
|
||||
async function extant(sources: SourceSelector) {
|
||||
async function extant(targets: CollectionIdentifier[]): Promise<Record<string, Record<string, Record<string, boolean>>>> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.extant({ sources })
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'collections')
|
||||
const response = await collectionService.extant({ targets })
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully checked', targets ? targets.length : 0, 'collections')
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to check collections:', error)
|
||||
@@ -191,30 +175,28 @@ export const useCollectionsStore = defineStore('peopleCollectionsStore', () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new collection with given provider, service, and data
|
||||
*
|
||||
* Create a new collection with given provider, service, and properties
|
||||
*
|
||||
* @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
|
||||
*
|
||||
* @param properties - collection properties for creation
|
||||
*
|
||||
* @returns Promise with created collection object
|
||||
*/
|
||||
async function create(provider: string, service: string | number, collection: string | number | null, data: CollectionPropertiesObject): Promise<CollectionObject> {
|
||||
async function create(provider: string, service: string | number, properties: CollectionPropertiesObject): Promise<CollectionObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.create({
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
properties: data
|
||||
const response = await collectionService.create({
|
||||
provider,
|
||||
service,
|
||||
properties: properties.toJson(),
|
||||
})
|
||||
|
||||
// Merge created collection into state
|
||||
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||
_collections.value[key] = response
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully created collection:', key)
|
||||
|
||||
if (response instanceof CollectionObject) {
|
||||
_collections.value[response.identifier] = response
|
||||
}
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully created collection:', response.identifier)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to create collection:', error)
|
||||
@@ -225,30 +207,26 @@ export const useCollectionsStore = defineStore('peopleCollectionsStore', () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* Update an existing collection with given target and properties
|
||||
*
|
||||
* @param target - collection identifier for the collection to update
|
||||
* @param properties - collection properties for update
|
||||
*
|
||||
* @returns Promise with updated collection object
|
||||
*/
|
||||
async function update(provider: string, service: string | number, identifier: string | number, data: CollectionPropertiesObject): Promise<CollectionObject> {
|
||||
async function update(target: CollectionIdentifier, properties: CollectionPropertiesObject): Promise<CollectionObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await collectionService.update({
|
||||
provider,
|
||||
service,
|
||||
identifier,
|
||||
properties: data
|
||||
const response = await collectionService.update({
|
||||
target,
|
||||
properties: properties.toJson(),
|
||||
})
|
||||
|
||||
// Merge updated collection into state
|
||||
const key = identifierKey(response.provider, response.service, response.identifier)
|
||||
_collections.value[key] = response
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully updated collection:', key)
|
||||
|
||||
if (response instanceof CollectionObject) {
|
||||
_collections.value[response.identifier] = response
|
||||
}
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully updated collection:', response.identifier)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to update collection:', error)
|
||||
@@ -259,24 +237,34 @@ export const useCollectionsStore = defineStore('peopleCollectionsStore', () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* Delete a collection by identifier, with optional force delete if collection is not empty.
|
||||
*
|
||||
* @param target - collection identifier for the collection to delete
|
||||
* @param force - optional flag to force delete if collection is not empty
|
||||
*
|
||||
* @returns Promise with deletion result
|
||||
*/
|
||||
async function remove(provider: string, service: string | number, identifier: string | number): Promise<any> {
|
||||
async function remove(target: CollectionIdentifier, force?: boolean): Promise<CollectionObject | boolean> {
|
||||
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]
|
||||
const response = await collectionService.delete({ target, options: { force } })
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully deleted collection:', key)
|
||||
if (response !== true && !(response instanceof CollectionObject)) {
|
||||
console.warn('[People Manager][Store] - Delete failed. Received unexpected response from delete operation:', response)
|
||||
return false
|
||||
}
|
||||
|
||||
delete _collections.value[target]
|
||||
|
||||
if (response instanceof CollectionObject) {
|
||||
_collections.value[response.identifier] = response
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully moved collection to trash', target, '->', response.identifier)
|
||||
return response
|
||||
}
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully deleted collection:', target)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to delete collection:', error)
|
||||
throw error
|
||||
|
||||
+251
-187
@@ -5,8 +5,15 @@
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { entityService } from '../services'
|
||||
import { EntityObject } from '../models'
|
||||
import type { SourceSelector, ListFilter, ListSort, ListRange } from '../types/common'
|
||||
import { EntityObject, GroupObject, IndividualObject, OrganizationObject } from '../models'
|
||||
import type {
|
||||
CollectionIdentifier,
|
||||
EntityIdentifier,
|
||||
ListFilter,
|
||||
ListRange,
|
||||
ListSort,
|
||||
} from '../types/common'
|
||||
import type { EntityPropertiesInterface } from '@/types/entity'
|
||||
|
||||
export const useEntitiesStore = defineStore('peopleEntitiesStore', () => {
|
||||
// State
|
||||
@@ -30,95 +37,63 @@ export const useEntitiesStore = defineStore('peopleEntitiesStore', () => {
|
||||
|
||||
/**
|
||||
* Get a specific entity from store, with optional retrieval
|
||||
*
|
||||
* @param provider - provider identifier
|
||||
* @param service - service identifier
|
||||
* @param collection - collection identifier
|
||||
* @param identifier - entity identifier
|
||||
*
|
||||
* @param target - entity identifier
|
||||
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||
*
|
||||
*
|
||||
* @returns Entity object or null
|
||||
*/
|
||||
function entity(provider: string, service: string | number, collection: string | number, identifier: string | number, retrieve: boolean = false): EntityObject | null {
|
||||
const key = identifierKey(provider, service, collection, identifier)
|
||||
if (retrieve === true && !_entities.value[key]) {
|
||||
console.debug(`[People Manager][Store] - Force fetching entity "${key}"`)
|
||||
fetch(provider, service, collection, [identifier])
|
||||
function entity(target: EntityIdentifier, retrieve: boolean = false): EntityObject | null {
|
||||
if (retrieve === true && !_entities.value[target]) {
|
||||
console.debug(`[People Manager][Store] - Force fetching entity "${target}"`)
|
||||
fetch([target])
|
||||
}
|
||||
|
||||
return _entities.value[key] || null
|
||||
|
||||
return _entities.value[target] || null
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all entities for a specific collection
|
||||
*
|
||||
* @param provider - provider identifier
|
||||
* @param service - service identifier
|
||||
* @param collection - collection identifier
|
||||
*
|
||||
* @param target - collection identifier
|
||||
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||
*
|
||||
*
|
||||
* @returns Array of entity objects
|
||||
*/
|
||||
function entitiesForCollection(provider: string, service: string | number, collection: string | number, retrieve: boolean = false): EntityObject[] {
|
||||
const collectionKeyPrefix = `${provider}:${service}:${collection}:`
|
||||
function entitiesForCollection(target: CollectionIdentifier, retrieve: boolean = false): EntityObject[] {
|
||||
const collectionEntities = Object.entries(_entities.value)
|
||||
.filter(([key]) => key.startsWith(collectionKeyPrefix))
|
||||
.filter(([key]) => key.startsWith(`${target}:`))
|
||||
.map(([_, entity]) => entity)
|
||||
|
||||
|
||||
if (retrieve === true && collectionEntities.length === 0) {
|
||||
console.debug(`[People Manager][Store] - Force fetching entities for collection "${provider}:${service}:${collection}"`)
|
||||
const sources: SourceSelector = {
|
||||
[provider]: {
|
||||
[String(service)]: {
|
||||
[String(collection)]: true
|
||||
}
|
||||
}
|
||||
}
|
||||
list(sources)
|
||||
console.debug(`[People Manager][Store] - Force fetching entities for collection "${target}"`)
|
||||
list([target])
|
||||
}
|
||||
|
||||
|
||||
return collectionEntities
|
||||
}
|
||||
|
||||
/**
|
||||
* Create unique key for an entity
|
||||
*/
|
||||
function identifierKey(provider: string, service: string | number, collection: string | number, identifier: string | number): string {
|
||||
return `${provider}:${service}:${collection}:${identifier}`
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve all or specific entities, optionally filtered by source selector
|
||||
*
|
||||
* @param sources - optional source selector
|
||||
* Retrieve all or specific entities, optionally filtered by source collection identifiers
|
||||
*
|
||||
* @param sources - collection identifiers to stream entities from
|
||||
* @param filter - optional list filter
|
||||
* @param sort - optional list sort
|
||||
* @param range - optional list range
|
||||
*
|
||||
*
|
||||
* @returns Promise with entity object list keyed by identifier
|
||||
*/
|
||||
async function list(sources?: SourceSelector, filter?: ListFilter, sort?: ListSort, range?: ListRange): Promise<Record<string, EntityObject>> {
|
||||
async function list(sources: CollectionIdentifier[], filter?: ListFilter, sort?: ListSort, range?: ListRange): Promise<Record<string, EntityObject>> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.list({ sources, filter, sort, range })
|
||||
|
||||
// Flatten nested structure: provider:service:collection:entity -> "provider:service:collection:entity": object
|
||||
const entities: Record<string, EntityObject> = {}
|
||||
Object.entries(response).forEach(([providerId, providerServices]) => {
|
||||
Object.entries(providerServices).forEach(([serviceId, serviceCollections]) => {
|
||||
Object.entries(serviceCollections).forEach(([collectionId, collectionEntities]) => {
|
||||
Object.entries(collectionEntities).forEach(([entityId, entityData]) => {
|
||||
const key = identifierKey(providerId, serviceId, collectionId, entityId)
|
||||
entities[key] = entityData
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
// Merge retrieved entities into state
|
||||
_entities.value = { ..._entities.value, ...entities }
|
||||
await entityService.listStream({ sources, filter, sort, range }, (entity: EntityObject) => {
|
||||
_entities.value[entity.identifier] = entity
|
||||
entities[entity.identifier] = entity
|
||||
})
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully retrieved', Object.keys(entities).length, 'entities')
|
||||
return entities
|
||||
@@ -129,28 +104,24 @@ export const useEntitiesStore = defineStore('peopleEntitiesStore', () => {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve specific entities by provider, service, collection, and identifiers
|
||||
*
|
||||
* @param provider - provider identifier
|
||||
* @param service - service identifier
|
||||
* @param collection - collection identifier
|
||||
* @param identifiers - array of entity identifiers to fetch
|
||||
*
|
||||
* Retrieve specific entities by their identifiers
|
||||
*
|
||||
* @param targets - array of entity identifiers to fetch
|
||||
*
|
||||
* @returns Promise with entity objects keyed by identifier
|
||||
*/
|
||||
async function fetch(provider: string, service: string | number, collection: string | number, identifiers: (string | number)[]): Promise<Record<string, EntityObject>> {
|
||||
async function fetch(targets: EntityIdentifier[]): Promise<Record<string, EntityObject>> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.fetch({ provider, service, collection, identifiers })
|
||||
|
||||
const response = await entityService.fetch({ targets })
|
||||
|
||||
// Merge fetched entities into state
|
||||
const entities: Record<string, EntityObject> = {}
|
||||
Object.entries(response).forEach(([identifier, entityData]) => {
|
||||
const key = identifierKey(provider, service, collection, identifier)
|
||||
entities[key] = entityData
|
||||
_entities.value[key] = entityData
|
||||
Object.entries(response).forEach(([identifier, entity]) => {
|
||||
entities[identifier] = entity
|
||||
_entities.value[identifier] = entity
|
||||
})
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully fetched', Object.keys(entities).length, 'entities')
|
||||
@@ -164,16 +135,16 @@ export const useEntitiesStore = defineStore('peopleEntitiesStore', () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve entity availability status for a given source selector
|
||||
*
|
||||
* @param sources - source selector to check availability for
|
||||
*
|
||||
* Retrieve entity availability status for a given set of entity identifiers
|
||||
*
|
||||
* @param targets - array of entity identifiers to check availability for
|
||||
*
|
||||
* @returns Promise with entity availability status
|
||||
*/
|
||||
async function extant(sources: SourceSelector) {
|
||||
async function extant(targets: EntityIdentifier[]) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.extant({ sources })
|
||||
const response = await entityService.extant({ targets })
|
||||
console.debug('[People Manager][Store] - Successfully checked entity availability')
|
||||
return response
|
||||
} catch (error: any) {
|
||||
@@ -184,130 +155,43 @@ export const useEntitiesStore = defineStore('peopleEntitiesStore', () => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new entity with given provider, service, collection, and data
|
||||
*
|
||||
* @param provider - provider identifier for the new entity
|
||||
* @param service - service identifier for the new entity
|
||||
* @param collection - collection identifier for the new entity
|
||||
* @param data - entity properties for creation
|
||||
*
|
||||
* @returns Promise with created entity object
|
||||
*/
|
||||
async function create(provider: string, service: string | number, collection: string | number, data: any): Promise<EntityObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.create({ provider, service, collection, properties: data })
|
||||
|
||||
// Add created entity to state
|
||||
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
|
||||
_entities.value[key] = response
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully created entity:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to create entity:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing entity with given provider, service, collection, identifier, and data
|
||||
*
|
||||
* @param provider - provider identifier for the entity to update
|
||||
* @param service - service identifier for the entity to update
|
||||
* @param collection - collection identifier for the entity to update
|
||||
* @param identifier - entity identifier for the entity to update
|
||||
* @param data - entity properties for update
|
||||
*
|
||||
* @returns Promise with updated entity object
|
||||
*/
|
||||
async function update(provider: string, service: string | number, collection: string | number, identifier: string | number, data: any): Promise<EntityObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.update({ provider, service, collection, identifier, properties: data })
|
||||
|
||||
// Update entity in state
|
||||
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
|
||||
_entities.value[key] = response
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully updated entity:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to update entity:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an entity by provider, service, collection, and identifier
|
||||
*
|
||||
* @param provider - provider identifier for the entity to delete
|
||||
* @param service - service identifier for the entity to delete
|
||||
* @param collection - collection identifier for the entity to delete
|
||||
* @param identifier - entity identifier for the entity to delete
|
||||
*
|
||||
* @returns Promise with deletion result
|
||||
*/
|
||||
async function remove(provider: string, service: string | number, collection: string | number, identifier: string | number): Promise<any> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.delete({ provider, service, collection, identifier })
|
||||
|
||||
// Remove entity from state
|
||||
const key = identifierKey(provider, service, collection, identifier)
|
||||
delete _entities.value[key]
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully deleted entity:', key)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to delete entity:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve delta changes for entities
|
||||
*
|
||||
* @param sources - source selector for delta check
|
||||
*
|
||||
*
|
||||
* @param targets - collection identifiers (provider:service:collection), optionally
|
||||
* suffixed with a known signature (provider:service:collection:signature)
|
||||
* to request a delta relative to that signature
|
||||
*
|
||||
* @returns Promise with delta changes (additions, modifications, deletions)
|
||||
*
|
||||
*
|
||||
* Note: Delta returns only identifiers, not full entities.
|
||||
* Caller should fetch full entities for additions/modifications separately.
|
||||
*/
|
||||
async function delta(sources: SourceSelector) {
|
||||
async function delta(targets: (CollectionIdentifier | EntityIdentifier)[]) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.delta({ sources })
|
||||
|
||||
const response = await entityService.delta({ targets })
|
||||
|
||||
// Process delta and update store
|
||||
Object.entries(response).forEach(([provider, providerData]) => {
|
||||
Object.entries(response).forEach(([, providerData]) => {
|
||||
// Skip if no changes for provider
|
||||
if (providerData === false) return
|
||||
|
||||
Object.entries(providerData).forEach(([service, serviceData]) => {
|
||||
|
||||
Object.entries(providerData).forEach(([, serviceData]) => {
|
||||
// Skip if no changes for service
|
||||
if (serviceData === false) return
|
||||
|
||||
Object.entries(serviceData).forEach(([collection, collectionData]) => {
|
||||
|
||||
Object.entries(serviceData).forEach(([, collectionData]) => {
|
||||
// Skip if no changes for collection
|
||||
if (collectionData === false) return
|
||||
|
||||
|
||||
// Process deletions (remove from store)
|
||||
if (collectionData.deletions && collectionData.deletions.length > 0) {
|
||||
collectionData.deletions.forEach((identifier) => {
|
||||
const key = identifierKey(provider, service, collection, identifier)
|
||||
delete _entities.value[key]
|
||||
delete _entities.value[identifier]
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
// Note: additions and modifications contain only identifiers
|
||||
// The caller should fetch full entities using the fetch() method
|
||||
})
|
||||
@@ -324,6 +208,184 @@ export const useEntitiesStore = defineStore('peopleEntitiesStore', () => {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new empty entity object
|
||||
*
|
||||
* @returns New entity object instance
|
||||
*/
|
||||
function fresh(): EntityObject {
|
||||
return new EntityObject()
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new entity with given collection identifier and properties
|
||||
*
|
||||
* @param target - collection identifier for the new entity
|
||||
* @param properties - entity properties for creation
|
||||
*
|
||||
* @returns Promise with created entity object
|
||||
*/
|
||||
async function create(target: CollectionIdentifier, properties: EntityPropertiesInterface | IndividualObject | OrganizationObject | GroupObject): Promise<EntityObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
if (properties instanceof IndividualObject || properties instanceof OrganizationObject || properties instanceof GroupObject) {
|
||||
properties = properties.toJson()
|
||||
}
|
||||
const response = await entityService.create({ target, properties })
|
||||
|
||||
// Add created entity to state
|
||||
_entities.value[response.identifier] = response
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully created entity:', response.identifier)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to create entity:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing entity with given entity identifier and properties
|
||||
*
|
||||
* @param target - entity identifier for the entity to update
|
||||
* @param properties - entity properties for update
|
||||
*
|
||||
* @returns Promise with updated entity object
|
||||
*/
|
||||
async function update(target: EntityIdentifier, properties: EntityPropertiesInterface | IndividualObject | OrganizationObject | GroupObject): Promise<EntityObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
if (properties instanceof IndividualObject || properties instanceof OrganizationObject || properties instanceof GroupObject) {
|
||||
properties = properties.toJson()
|
||||
}
|
||||
const response = await entityService.update({ target, properties })
|
||||
|
||||
// Update entity in state
|
||||
_entities.value[response.identifier] = response
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully updated entity:', response.identifier)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to update entity:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete entities by their identifiers.
|
||||
*
|
||||
* Removes successfully deleted entities from the local store.
|
||||
*
|
||||
* @param targets - entity identifiers to delete
|
||||
*
|
||||
* @returns Promise with successes/failures keyed by target identifier
|
||||
*/
|
||||
async function remove(targets: EntityIdentifier[]): Promise<{ successes: EntityIdentifier[], failures: EntityIdentifier[] }> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.delete({ targets })
|
||||
const successes: EntityIdentifier[] = []
|
||||
const failures: EntityIdentifier[] = []
|
||||
|
||||
Object.entries(response).forEach(([targetIdentifier, result]) => {
|
||||
const originalIdentifier = targetIdentifier as EntityIdentifier
|
||||
if (!result.disposition || result.disposition === 'error') {
|
||||
console.warn(`[People Manager][Store] - Entity delete on "${originalIdentifier}" returned an error: ${result.error})`)
|
||||
failures.push(originalIdentifier)
|
||||
return
|
||||
}
|
||||
|
||||
if (result.disposition !== 'moved' && result.disposition !== 'deleted') {
|
||||
console.warn(`[People Manager][Store] - Entity delete on "${originalIdentifier}" returned invalid disposition: ${result.disposition})`)
|
||||
failures.push(originalIdentifier)
|
||||
return
|
||||
}
|
||||
|
||||
const cachedEntity = _entities.value[originalIdentifier]
|
||||
|
||||
if (result.disposition === 'moved' && cachedEntity && result.mutation) {
|
||||
const movedEntity = cachedEntity.clone().fromJson({
|
||||
...cachedEntity.toJson(),
|
||||
collection: result.destination,
|
||||
identifier: result.mutation,
|
||||
})
|
||||
_entities.value[result.mutation] = movedEntity
|
||||
}
|
||||
|
||||
delete _entities.value[originalIdentifier]
|
||||
successes.push(originalIdentifier)
|
||||
})
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully deleted', successes.length, 'entities')
|
||||
return { successes, failures }
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to delete entities:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move entities to another collection.
|
||||
*
|
||||
* Updates local store keys for successfully moved entities when they are
|
||||
* already present in cache.
|
||||
*
|
||||
* @param target - target collection identifier
|
||||
* @param sources - source entity identifiers
|
||||
*
|
||||
* @returns Promise with successes/failures keyed by source identifier
|
||||
*/
|
||||
async function move(target: CollectionIdentifier, sources: EntityIdentifier[]): Promise<{ successes: EntityIdentifier[], failures: EntityIdentifier[] }> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.move({ target, sources })
|
||||
const successes: EntityIdentifier[] = []
|
||||
const failures: EntityIdentifier[] = []
|
||||
|
||||
Object.entries(response).forEach(([sourceIdentifier, result]) => {
|
||||
const originalIdentifier = sourceIdentifier as EntityIdentifier
|
||||
if (!result.disposition || result.disposition === 'error') {
|
||||
console.warn(`[People Manager][Store] - Entity move on "${originalIdentifier}" returned an error: ${result.error})`)
|
||||
failures.push(originalIdentifier)
|
||||
return
|
||||
}
|
||||
|
||||
if (result.disposition !== 'moved') {
|
||||
console.warn(`[People Manager][Store] - Entity move on "${originalIdentifier}" returned invalid disposition: ${result.disposition})`)
|
||||
failures.push(originalIdentifier)
|
||||
return
|
||||
}
|
||||
|
||||
const cachedEntity = _entities.value[originalIdentifier]
|
||||
if (cachedEntity && result.mutation) {
|
||||
const movedEntity = cachedEntity.clone().fromJson({
|
||||
...cachedEntity.toJson(),
|
||||
collection: result.destination,
|
||||
identifier: result.mutation,
|
||||
})
|
||||
_entities.value[result.mutation] = movedEntity
|
||||
delete _entities.value[originalIdentifier]
|
||||
}
|
||||
|
||||
successes.push(originalIdentifier)
|
||||
})
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully moved', successes.length, 'entities')
|
||||
return { successes, failures }
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to move entities:', error)
|
||||
throw error
|
||||
} finally {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
// Return public API
|
||||
return {
|
||||
// State (readonly)
|
||||
@@ -338,9 +400,11 @@ export const useEntitiesStore = defineStore('peopleEntitiesStore', () => {
|
||||
list,
|
||||
fetch,
|
||||
extant,
|
||||
fresh,
|
||||
create,
|
||||
update,
|
||||
delete: remove,
|
||||
delta,
|
||||
move,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { providerService } from '../services'
|
||||
import { ProviderObject } from '../models/provider'
|
||||
import type { SourceSelector } from '../types'
|
||||
import type { ProviderIdentifier } from '../types'
|
||||
|
||||
export const useProvidersStore = defineStore('peopleProvidersStore', () => {
|
||||
// State
|
||||
@@ -54,10 +54,10 @@ export const useProvidersStore = defineStore('peopleProvidersStore', () => {
|
||||
*
|
||||
* @returns Promise with provider object list keyed by provider identifier
|
||||
*/
|
||||
async function list(sources?: SourceSelector): Promise<Record<string, ProviderObject>> {
|
||||
async function list(targets?: ProviderIdentifier[]): Promise<Record<string, ProviderObject>> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const providers = await providerService.list({ sources })
|
||||
const providers = await providerService.list({ targets })
|
||||
|
||||
// Merge retrieved providers into state
|
||||
_providers.value = { ..._providers.value, ...providers }
|
||||
@@ -82,7 +82,7 @@ export const useProvidersStore = defineStore('peopleProvidersStore', () => {
|
||||
async function fetch(identifier: string): Promise<ProviderObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const provider = await providerService.fetch({ identifier })
|
||||
const provider = await providerService.fetch({ target: identifier })
|
||||
|
||||
// Merge fetched provider into state
|
||||
_providers.value[provider.identifier] = provider
|
||||
@@ -104,10 +104,10 @@ export const useProvidersStore = defineStore('peopleProvidersStore', () => {
|
||||
*
|
||||
* @returns Promise with provider availability status
|
||||
*/
|
||||
async function extant(sources: SourceSelector) {
|
||||
async function extant(targets: ProviderIdentifier[]) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await providerService.extant({ sources })
|
||||
const response = await providerService.extant({ targets })
|
||||
|
||||
Object.entries(response).forEach(([providerId, providerStatus]) => {
|
||||
if (providerStatus === false) {
|
||||
@@ -115,7 +115,7 @@ export const useProvidersStore = defineStore('peopleProvidersStore', () => {
|
||||
}
|
||||
})
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'providers')
|
||||
console.debug('[People Manager][Store] - Successfully checked', targets ? targets.length : 0, 'providers')
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to check providers:', error)
|
||||
|
||||
+74
-47
@@ -7,7 +7,8 @@ import { defineStore } from 'pinia'
|
||||
import { serviceService } from '../services'
|
||||
import { ServiceObject } from '../models/service'
|
||||
import type {
|
||||
SourceSelector,
|
||||
CollectionIdentifier,
|
||||
ServiceIdentifier,
|
||||
ServiceInterface,
|
||||
} from '../types'
|
||||
|
||||
@@ -31,59 +32,78 @@ export const useServicesStore = defineStore('peopleServicesStore', () => {
|
||||
*/
|
||||
const services = computed(() => Object.values(_services.value))
|
||||
|
||||
/**
|
||||
* Get all enabled services present in store
|
||||
*/
|
||||
const servicesEnabled = computed(() => services.value.filter(service => service.enabled))
|
||||
|
||||
/**
|
||||
* Get all services present in store grouped by provider
|
||||
*/
|
||||
const servicesByProvider = computed(() => {
|
||||
const groups: Record<string, ServiceObject[]> = {}
|
||||
|
||||
|
||||
Object.values(_services.value).forEach((service) => {
|
||||
const providerServices = (groups[service.provider] ??= [])
|
||||
providerServices.push(service)
|
||||
})
|
||||
|
||||
|
||||
return groups
|
||||
})
|
||||
|
||||
/**
|
||||
* Get a specific service from store, with optional retrieval
|
||||
*
|
||||
*
|
||||
* @param provider - provider identifier
|
||||
* @param identifier - service identifier
|
||||
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||
*
|
||||
*
|
||||
* @returns Service object or null
|
||||
*/
|
||||
function service(provider: string, identifier: string | number, retrieve: boolean = false): ServiceObject | null {
|
||||
const key = identifierKey(provider, identifier)
|
||||
if (retrieve === true && !_services.value[key]) {
|
||||
console.debug(`[People Manager][Store] - Force fetching service "${key}"`)
|
||||
fetch(provider, identifier)
|
||||
return serviceByIdentifier(identifierKey(provider, identifier), retrieve)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a service from store by its unique identifier, with optional retrieval
|
||||
*
|
||||
* @param identifier - unique service identifier
|
||||
* @param retrieve - Retrieve behavior: true = fetch if missing or refresh, false = cache only
|
||||
* @returns Service object or null
|
||||
*/
|
||||
function serviceByIdentifier(identifier: ServiceIdentifier, retrieve: boolean = false): ServiceObject | null {
|
||||
if (retrieve === true && !_services.value[identifier]) {
|
||||
console.debug(`[People Manager][Store] - Force fetching service "${identifier}"`)
|
||||
const separatorIndex = identifier.indexOf(':')
|
||||
const provider = identifier.slice(0, separatorIndex)
|
||||
const serviceIdentifier = identifier.slice(separatorIndex + 1)
|
||||
|
||||
void fetch(provider, serviceIdentifier)
|
||||
}
|
||||
|
||||
return _services.value[key] || null
|
||||
|
||||
return _services.value[identifier] ?? null
|
||||
}
|
||||
|
||||
/**
|
||||
* Unique key for a service
|
||||
*/
|
||||
function identifierKey(provider: string, identifier: string | number | null): string {
|
||||
return `${provider}:${identifier ?? ''}`
|
||||
function identifierKey(provider: string, identifier: string | number | null): ServiceIdentifier {
|
||||
return `${provider}:${identifier ?? ''}` as ServiceIdentifier
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve all or specific services, optionally filtered by source selector
|
||||
*
|
||||
* @param sources - optional source selector
|
||||
*
|
||||
* Retrieve all or specific services, optionally filtered by provider/service identifiers
|
||||
*
|
||||
* @param targets - optional array of provider:service (or provider:service:collection) identifiers
|
||||
*
|
||||
* @returns Promise with service object list keyed by provider and service identifier
|
||||
*/
|
||||
async function list(sources?: SourceSelector): Promise<Record<string, ServiceObject>> {
|
||||
async function list(targets?: ServiceIdentifier[] | CollectionIdentifier[]): Promise<Record<string, ServiceObject>> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await serviceService.list({ sources })
|
||||
const response = await serviceService.list({ targets })
|
||||
|
||||
// Flatten nested structure: provider-id: { service-id: object } -> "provider-id:service-id": object
|
||||
const services: Record<string, ServiceObject> = {}
|
||||
@@ -106,20 +126,20 @@ export const useServicesStore = defineStore('peopleServicesStore', () => {
|
||||
transceiving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieve a specific service by provider and identifier
|
||||
*
|
||||
*
|
||||
* @param provider - provider identifier
|
||||
* @param identifier - service identifier
|
||||
*
|
||||
*
|
||||
* @returns Promise with service object
|
||||
*/
|
||||
async function fetch(provider: string, identifier: string | number): Promise<ServiceObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const service = await serviceService.fetch({ provider, identifier })
|
||||
|
||||
|
||||
// Merge fetched service into state
|
||||
const key = identifierKey(service.provider, service.identifier)
|
||||
_services.value[key] = service
|
||||
@@ -135,18 +155,18 @@ export const useServicesStore = defineStore('peopleServicesStore', () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve service availability status for a given source selector
|
||||
*
|
||||
* @param sources - source selector to check availability for
|
||||
*
|
||||
* Retrieve service availability status for the given service identifiers
|
||||
*
|
||||
* @param targets - array of provider:service identifiers to check availability for
|
||||
*
|
||||
* @returns Promise with service availability status
|
||||
*/
|
||||
async function extant(sources: SourceSelector) {
|
||||
async function extant(targets: ServiceIdentifier[]) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await serviceService.extant({ sources })
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully checked', sources ? Object.keys(sources).length : 0, 'services')
|
||||
const response = await serviceService.extant({ targets })
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully checked', targets?.length ?? 0, 'services')
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[People Manager][Store] - Failed to check services:', error)
|
||||
@@ -158,21 +178,21 @@ export const useServicesStore = defineStore('peopleServicesStore', () => {
|
||||
|
||||
/**
|
||||
* Create a new service with given provider and data
|
||||
*
|
||||
*
|
||||
* @param provider - provider identifier for the new service
|
||||
* @param data - partial service data for creation
|
||||
*
|
||||
*
|
||||
* @returns Promise with created service object
|
||||
*/
|
||||
async function create(provider: string, data: Partial<ServiceInterface>): Promise<ServiceObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const service = await serviceService.create({ provider, data })
|
||||
|
||||
|
||||
// Merge created service into state
|
||||
const key = identifierKey(service.provider, service.identifier)
|
||||
_services.value[key] = service
|
||||
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully created service:', key)
|
||||
return service
|
||||
} catch (error: any) {
|
||||
@@ -185,22 +205,28 @@ export const useServicesStore = defineStore('peopleServicesStore', () => {
|
||||
|
||||
/**
|
||||
* Update an existing service with given provider, identifier, and data
|
||||
*
|
||||
*
|
||||
* @param provider - provider identifier for the service to update
|
||||
* @param identifier - service identifier for the service to update
|
||||
* @param data - partial service data for update
|
||||
*
|
||||
* @param delta - whether the update is a delta (partial) update or a full replacement
|
||||
* @param data - service data for update
|
||||
*
|
||||
* @returns Promise with updated service object
|
||||
*/
|
||||
async function update(provider: string, identifier: string | number, data: Partial<ServiceInterface>): Promise<ServiceObject> {
|
||||
async function update(provider: string, identifier: string | number, delta: boolean, data: ServiceObject | Partial<ServiceInterface>): Promise<ServiceObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const service = await serviceService.update({ provider, identifier, data })
|
||||
|
||||
// convert ServiceObject to JSON if needed
|
||||
const payload: Partial<ServiceInterface> = data instanceof ServiceObject
|
||||
? (delta ? data.toJson(true) : data.toJson())
|
||||
: data
|
||||
|
||||
const service = await serviceService.update({ provider, identifier, delta, data: payload })
|
||||
|
||||
// Merge updated service into state
|
||||
const key = identifierKey(service.provider, service.identifier)
|
||||
_services.value[key] = service
|
||||
|
||||
|
||||
console.debug('[People Manager][Store] - Successfully updated service:', key)
|
||||
return service
|
||||
} catch (error: any) {
|
||||
@@ -213,17 +239,17 @@ export const useServicesStore = defineStore('peopleServicesStore', () => {
|
||||
|
||||
/**
|
||||
* Delete a service by provider and identifier
|
||||
*
|
||||
*
|
||||
* @param provider - provider identifier for the service to delete
|
||||
* @param identifier - service identifier for the service to delete
|
||||
*
|
||||
*
|
||||
* @returns Promise with deletion result
|
||||
*/
|
||||
async function remove(provider: string, identifier: string | number): Promise<any> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
await serviceService.delete({ provider, identifier })
|
||||
|
||||
|
||||
// Remove deleted service from state
|
||||
const key = identifierKey(provider, identifier)
|
||||
delete _services.value[key]
|
||||
@@ -245,10 +271,11 @@ export const useServicesStore = defineStore('peopleServicesStore', () => {
|
||||
count,
|
||||
has,
|
||||
services,
|
||||
servicesEnabled,
|
||||
servicesByProvider,
|
||||
|
||||
// Actions
|
||||
service,
|
||||
serviceByIdentifier,
|
||||
list,
|
||||
fetch,
|
||||
extant,
|
||||
|
||||
Reference in New Issue
Block a user