refactor: use resource identifiers
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -5,10 +5,8 @@
|
||||
import { ref, computed, readonly } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
import { entityService } from '../services'
|
||||
import { EntityObject } from '../models'
|
||||
import { EntityObject, MessageObject } from '../models'
|
||||
import type {
|
||||
EntityDeleteResponse,
|
||||
EntityMoveResponse,
|
||||
EntityStreamRequest,
|
||||
EntityTransmitRequest,
|
||||
EntityTransmitResponse,
|
||||
@@ -19,8 +17,8 @@ import type {
|
||||
ListFilter,
|
||||
ListRange,
|
||||
ListSort,
|
||||
SourceSelector,
|
||||
} from '../types/common'
|
||||
import type { MessageInterface } from '@/types/message'
|
||||
|
||||
export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
// State
|
||||
@@ -53,43 +51,13 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
*
|
||||
* @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(`[Mail 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(`[Mail Manager][Store] - Force fetching entity "${target}"`)
|
||||
fetch([target])
|
||||
}
|
||||
|
||||
return _entities.value[key] || null
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve an entity from cache by full entity identifier.
|
||||
*/
|
||||
function entityByIdentifier(identifier: EntityIdentifier, retrieve: boolean = false): EntityObject | null {
|
||||
if (retrieve === true && !_entities.value[identifier]) {
|
||||
console.debug(`[Mail Manager][Store] - Force fetching entity "${identifier}"`)
|
||||
const { provider, service, collection, identifier: id } = parseEntityIdentifier(identifier)
|
||||
fetch(provider, service, collection, [id])
|
||||
}
|
||||
|
||||
return _entities.value[identifier] || null
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve multiple entities from cache by full entity identifiers.
|
||||
*/
|
||||
function entitiesByIdentifiers(identifiers: EntityIdentifier[], retrieve: boolean = false): Record<EntityIdentifier, EntityObject> {
|
||||
const resolved: Record<EntityIdentifier, EntityObject> = {} as Record<EntityIdentifier, EntityObject>
|
||||
|
||||
Array.from(new Set(identifiers)).forEach(identifier => {
|
||||
const entity = entityByIdentifier(identifier, retrieve)
|
||||
if (entity) {
|
||||
resolved[identifier] = entity
|
||||
}
|
||||
})
|
||||
|
||||
return resolved
|
||||
return _entities.value[target] || null
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -102,52 +70,19 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
*
|
||||
* @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(`[Mail Manager][Store] - Force fetching entities for collection "${provider}:${service}:${collection}"`)
|
||||
const sources: SourceSelector = {
|
||||
[provider]: {
|
||||
[String(service)]: {
|
||||
[String(collection)]: true
|
||||
}
|
||||
}
|
||||
}
|
||||
list(sources)
|
||||
console.debug(`[Mail 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}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a full entity identifier into its components.
|
||||
*/
|
||||
function parseEntityIdentifier(identifier: EntityIdentifier): {
|
||||
provider: string
|
||||
service: string
|
||||
collection: string
|
||||
identifier: string
|
||||
} {
|
||||
const [provider, service, collection, entity] = identifier.split(':', 4)
|
||||
return {
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
identifier: entity,
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
|
||||
/**
|
||||
@@ -160,19 +95,18 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
*
|
||||
* @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 added: Record<string, EntityObject> = {}
|
||||
const entities: Record<string, EntityObject> = {}
|
||||
|
||||
await entityService.stream({ sources, filter, sort, range }, (entity: EntityObject) => {
|
||||
const key = identifierKey(entity.provider, entity.service, entity.collection, entity.identifier)
|
||||
_entities.value[key] = entity
|
||||
added[key] = entity
|
||||
_entities.value[entity.identifier] = entity
|
||||
entities[entity.identifier] = entity
|
||||
})
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully retrieved', Object.keys(added).length, 'entities')
|
||||
return added
|
||||
console.debug('[Mail Manager][Store] - Successfully retrieved', Object.keys(entities).length, 'entities')
|
||||
return entities
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to retrieve entities:', error)
|
||||
throw error
|
||||
@@ -191,17 +125,16 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
*
|
||||
* @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('[Mail Manager][Store] - Successfully fetched', Object.keys(entities).length, 'entities')
|
||||
@@ -215,16 +148,16 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve entity availability status for a given source selector
|
||||
* Retrieve entity availability status for a given set of entity identifiers
|
||||
*
|
||||
* @param sources - source selector to check availability for
|
||||
* @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('[Mail Manager][Store] - Successfully checked entity availability')
|
||||
return response
|
||||
} catch (error: any) {
|
||||
@@ -245,7 +178,7 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
* 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(sources: CollectionIdentifier[]) {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.delta({ sources })
|
||||
@@ -266,13 +199,9 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
// 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
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -288,25 +217,25 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new entity with given provider, service, collection, and data
|
||||
* Create a new entity with given collection identifier and properties
|
||||
*
|
||||
* @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
|
||||
* @param target - collection identifier for the new entity
|
||||
* @param properties - 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> {
|
||||
async function create(target: CollectionIdentifier, properties: MessageInterface | MessageObject): Promise<EntityObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.create({ provider, service, collection, properties: data })
|
||||
if (properties instanceof MessageObject) {
|
||||
properties = properties.toJson()
|
||||
}
|
||||
const response = await entityService.create({ target, properties })
|
||||
|
||||
// Add created entity to state
|
||||
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
|
||||
_entities.value[key] = response
|
||||
_entities.value[response.identifier] = response
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully created entity:', key)
|
||||
console.debug('[Mail Manager][Store] - Successfully created entity:', response.identifier)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to create entity:', error)
|
||||
@@ -327,16 +256,18 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
*
|
||||
* @returns Promise with updated entity object
|
||||
*/
|
||||
async function update(provider: string, service: string | number, collection: string | number, identifier: string | number, data: any): Promise<EntityObject> {
|
||||
async function update(target: EntityIdentifier, properties: MessageInterface | MessageObject): Promise<EntityObject> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.update({ provider, service, collection, identifier, properties: data })
|
||||
if (properties instanceof MessageObject) {
|
||||
properties = properties.toJson()
|
||||
}
|
||||
const response = await entityService.update({ target, properties })
|
||||
|
||||
// Update entity in state
|
||||
const key = identifierKey(response.provider, response.service, response.collection, response.identifier)
|
||||
_entities.value[key] = response
|
||||
_entities.value[response.identifier] = response
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully updated entity:', key)
|
||||
console.debug('[Mail Manager][Store] - Successfully updated entity:', response.identifier)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to update entity:', error)
|
||||
@@ -355,7 +286,7 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
*
|
||||
* @returns Promise with deletion results keyed by source identifier
|
||||
*/
|
||||
async function remove(sources: EntityIdentifier[]): Promise<EntityDeleteResponse> {
|
||||
async function remove(sources: EntityIdentifier[]): Promise<{successes: EntityIdentifier[], failures: EntityIdentifier[]}> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.delete({ sources })
|
||||
@@ -363,42 +294,39 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
const failures: EntityIdentifier[] = []
|
||||
|
||||
Object.entries(response).forEach(([sourceIdentifier, result]) => {
|
||||
const originalIdentifier = sourceIdentifier as EntityIdentifier
|
||||
if (!result.disposition || result.disposition === 'error') {
|
||||
console.warn(`[Mail Manager][Store] - Entity move on "${sourceIdentifier}" returned an error: ${result.error})`)
|
||||
failures.push(sourceIdentifier)
|
||||
console.warn(`[Mail Manager][Store] - Entity move on "${originalIdentifier}" returned an error: ${result.error})`)
|
||||
failures.push(originalIdentifier)
|
||||
return
|
||||
}
|
||||
|
||||
if (!result.disposition || (result.disposition !== 'moved' && result.disposition !== 'deleted')) {
|
||||
console.warn(`[Mail Manager][Store] - Entity move on "${sourceIdentifier}" returned invalid disposition: ${result.disposition})`)
|
||||
failures.push(sourceIdentifier)
|
||||
console.warn(`[Mail Manager][Store] - Entity move on "${originalIdentifier}" returned invalid disposition: ${result.disposition})`)
|
||||
failures.push(originalIdentifier)
|
||||
return
|
||||
}
|
||||
|
||||
const cachedEntity = _entities.value[sourceIdentifier]
|
||||
const cachedEntity = _entities.value[originalIdentifier]
|
||||
if (!cachedEntity) {
|
||||
return
|
||||
}
|
||||
|
||||
if (result.disposition === 'moved') {
|
||||
const mutation = parseEntityIdentifier(result.mutation?.identifier || sourceIdentifier)
|
||||
const movedEntity = cachedEntity.clone().fromJson({
|
||||
...cachedEntity.toJson(),
|
||||
provider: mutation.provider,
|
||||
service: mutation.service,
|
||||
collection: mutation.collection,
|
||||
identifier: mutation.identifier,
|
||||
collection: result.destination,
|
||||
identifier: result.mutation,
|
||||
})
|
||||
const key = identifierKey(mutation.provider, mutation.service, mutation.collection, mutation.identifier)
|
||||
_entities.value[key] = movedEntity
|
||||
_entities.value[result.mutation] = movedEntity
|
||||
}
|
||||
|
||||
delete _entities.value[sourceIdentifier]
|
||||
successes.push(sourceIdentifier)
|
||||
delete _entities.value[originalIdentifier]
|
||||
successes.push(originalIdentifier)
|
||||
})
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully deleted', Object.keys(response).length, 'entities')
|
||||
return [successes, failures]
|
||||
console.debug('[Mail Manager][Store] - Successfully deleted', successes.length, 'entities')
|
||||
return { successes, failures }
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to delete entities:', error)
|
||||
throw error
|
||||
@@ -418,7 +346,7 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
*
|
||||
* @returns Promise with move results keyed by source identifier
|
||||
*/
|
||||
async function move(target: CollectionIdentifier, sources: EntityIdentifier[]): Promise<EntityIdentifier[]> {
|
||||
async function move(target: CollectionIdentifier, sources: EntityIdentifier[]): Promise<{successes: EntityIdentifier[], failures: EntityIdentifier[]}> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.move({ target, sources })
|
||||
@@ -426,40 +354,37 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
const failures: EntityIdentifier[] = []
|
||||
|
||||
Object.entries(response).forEach(([sourceIdentifier, result]) => {
|
||||
const originalIdentifier = sourceIdentifier as EntityIdentifier
|
||||
if (!result.disposition || result.disposition === 'error') {
|
||||
console.warn(`[Mail Manager][Store] - Entity move on "${sourceIdentifier}" returned an error: ${result.error})`)
|
||||
failures.push(sourceIdentifier)
|
||||
console.warn(`[Mail Manager][Store] - Entity move on "${originalIdentifier}" returned an error: ${result.error})`)
|
||||
failures.push(originalIdentifier)
|
||||
return
|
||||
}
|
||||
|
||||
if (!result.disposition || result.disposition !== 'moved') {
|
||||
console.warn(`[Mail Manager][Store] - Entity move on "${sourceIdentifier}" returned invalid disposition: ${result.disposition})`)
|
||||
failures.push(sourceIdentifier)
|
||||
console.warn(`[Mail Manager][Store] - Entity move on "${originalIdentifier}" returned invalid disposition: ${result.disposition})`)
|
||||
failures.push(originalIdentifier)
|
||||
return
|
||||
}
|
||||
|
||||
const cachedEntity = _entities.value[sourceIdentifier]
|
||||
const cachedEntity = _entities.value[originalIdentifier]
|
||||
if (!cachedEntity) {
|
||||
return
|
||||
}
|
||||
|
||||
const mutation = parseEntityIdentifier(result.mutation?.identifier || sourceIdentifier)
|
||||
const movedEntity = cachedEntity.clone().fromJson({
|
||||
...cachedEntity.toJson(),
|
||||
provider: mutation.provider,
|
||||
service: mutation.service,
|
||||
collection: mutation.collection,
|
||||
identifier: mutation.identifier,
|
||||
collection: result.destination,
|
||||
identifier: result.mutation,
|
||||
})
|
||||
const movedKey = identifierKey(mutation.provider, mutation.service, mutation.collection, mutation.identifier)
|
||||
_entities.value[movedKey] = movedEntity
|
||||
_entities.value[result.mutation] = movedEntity
|
||||
|
||||
delete _entities.value[sourceIdentifier]
|
||||
successes.push(sourceIdentifier)
|
||||
delete _entities.value[originalIdentifier]
|
||||
successes.push(originalIdentifier)
|
||||
})
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully moved', Object.keys(response).length, 'entities')
|
||||
return [successes, failures]
|
||||
console.debug('[Mail Manager][Store] - Successfully moved', successes.length, 'entities')
|
||||
return { successes, failures }
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to move entities:', error)
|
||||
throw error
|
||||
@@ -502,18 +427,12 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
*
|
||||
* @returns Promise resolving to { total } when the stream completes
|
||||
*/
|
||||
async function stream(
|
||||
sources?: SourceSelector,
|
||||
filter?: ListFilter,
|
||||
sort?: ListSort,
|
||||
range?: ListRange
|
||||
): Promise<{ total: number }> {
|
||||
async function stream(sources?: CollectionIdentifier[], filter?: ListFilter, sort?: ListSort, range?: ListRange): Promise<{ total: number }> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const request: EntityStreamRequest = { sources, filter, sort, range }
|
||||
const result = await entityService.stream(request, (entity: EntityObject) => {
|
||||
const key = identifierKey(entity.provider, entity.service, entity.collection, entity.identifier)
|
||||
_entities.value[key] = entity
|
||||
_entities.value[entity.identifier] = entity
|
||||
})
|
||||
console.debug('[Mail Manager][Store] - Successfully streamed', result.total, 'entities')
|
||||
return result
|
||||
@@ -534,9 +453,7 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
has,
|
||||
entities,
|
||||
entitiesForCollection,
|
||||
entitiesByIdentifiers,
|
||||
entity,
|
||||
entityByIdentifier,
|
||||
list,
|
||||
fetch,
|
||||
extant,
|
||||
|
||||
Reference in New Issue
Block a user