recator: entity move and delete
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -359,17 +359,46 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.delete({ sources })
|
||||
const successes: EntityIdentifier[] = []
|
||||
const failures: EntityIdentifier[] = []
|
||||
|
||||
Object.entries(response).forEach(([sourceIdentifier, result]) => {
|
||||
if (!result.success) {
|
||||
if (!result.disposition || result.disposition === 'error') {
|
||||
console.warn(`[Mail Manager][Store] - Entity move on "${sourceIdentifier}" returned an error: ${result.error})`)
|
||||
failures.push(sourceIdentifier)
|
||||
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)
|
||||
return
|
||||
}
|
||||
|
||||
const cachedEntity = _entities.value[sourceIdentifier]
|
||||
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,
|
||||
})
|
||||
const key = identifierKey(mutation.provider, mutation.service, mutation.collection, mutation.identifier)
|
||||
_entities.value[key] = movedEntity
|
||||
}
|
||||
|
||||
delete _entities.value[sourceIdentifier]
|
||||
successes.push(sourceIdentifier)
|
||||
})
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully deleted', Object.keys(response).length, 'entities')
|
||||
return response
|
||||
return [successes, failures]
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to delete entities:', error)
|
||||
throw error
|
||||
@@ -389,13 +418,23 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
*
|
||||
* @returns Promise with move results keyed by source identifier
|
||||
*/
|
||||
async function move(target: CollectionIdentifier, sources: EntityIdentifier[]): Promise<EntityMoveResponse> {
|
||||
async function move(target: CollectionIdentifier, sources: EntityIdentifier[]): Promise<EntityIdentifier[]> {
|
||||
transceiving.value = true
|
||||
try {
|
||||
const response = await entityService.move({ target, sources })
|
||||
const successes: EntityIdentifier[] = []
|
||||
const failures: EntityIdentifier[] = []
|
||||
|
||||
Object.entries(response).forEach(([sourceIdentifier, result]) => {
|
||||
if (!result.success) {
|
||||
if (!result.disposition || result.disposition === 'error') {
|
||||
console.warn(`[Mail Manager][Store] - Entity move on "${sourceIdentifier}" returned an error: ${result.error})`)
|
||||
failures.push(sourceIdentifier)
|
||||
return
|
||||
}
|
||||
|
||||
if (!result.disposition || result.disposition !== 'moved') {
|
||||
console.warn(`[Mail Manager][Store] - Entity move on "${sourceIdentifier}" returned invalid disposition: ${result.disposition})`)
|
||||
failures.push(sourceIdentifier)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -404,22 +443,23 @@ export const useEntitiesStore = defineStore('mailEntitiesStore', () => {
|
||||
return
|
||||
}
|
||||
|
||||
const destination = parseEntityIdentifier(result.identifier)
|
||||
const mutation = parseEntityIdentifier(result.mutation?.identifier || sourceIdentifier)
|
||||
const movedEntity = cachedEntity.clone().fromJson({
|
||||
...cachedEntity.toJson(),
|
||||
provider: destination.provider,
|
||||
service: destination.service,
|
||||
collection: destination.collection,
|
||||
identifier: destination.identifier,
|
||||
provider: mutation.provider,
|
||||
service: mutation.service,
|
||||
collection: mutation.collection,
|
||||
identifier: mutation.identifier,
|
||||
})
|
||||
const movedKey = identifierKey(mutation.provider, mutation.service, mutation.collection, mutation.identifier)
|
||||
_entities.value[movedKey] = movedEntity
|
||||
|
||||
delete _entities.value[sourceIdentifier]
|
||||
|
||||
_entities.value[result.identifier] = movedEntity
|
||||
successes.push(sourceIdentifier)
|
||||
})
|
||||
|
||||
console.debug('[Mail Manager][Store] - Successfully moved', Object.keys(response).length, 'entities')
|
||||
return response
|
||||
return [successes, failures]
|
||||
} catch (error: any) {
|
||||
console.error('[Mail Manager][Store] - Failed to move entities:', error)
|
||||
throw error
|
||||
|
||||
@@ -132,17 +132,13 @@ export interface EntityDeleteRequest {
|
||||
sources: EntityIdentifier[];
|
||||
}
|
||||
|
||||
export interface EntityDeleteResultSuccess {
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export interface EntityDeleteResultFailure {
|
||||
success: boolean;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export interface EntityDeleteResponse {
|
||||
[sourceIdentifier: EntityIdentifier]: EntityDeleteResultSuccess | EntityDeleteResultFailure;
|
||||
[sourceIdentifier: EntityIdentifier]: {
|
||||
disposition: 'deleted' | 'moved' | 'error';
|
||||
destination: CollectionIdentifier | null;
|
||||
mutation: EntityIdentifier | null;
|
||||
error?: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,18 +149,13 @@ export interface EntityMoveRequest {
|
||||
sources: EntityIdentifier[];
|
||||
}
|
||||
|
||||
export interface EntityMoveResultSuccess {
|
||||
success: boolean;
|
||||
identifier: EntityIdentifier;
|
||||
}
|
||||
|
||||
export interface EntityMoveResultFailure {
|
||||
success: boolean;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export interface EntityMoveResponse {
|
||||
[sourceIdentifier: EntityIdentifier]: EntityMoveResultSuccess | EntityMoveResultFailure;
|
||||
[sourceIdentifier: EntityIdentifier]: {
|
||||
disposition: 'moved' | 'error';
|
||||
destination: CollectionIdentifier| null;
|
||||
mutation: EntityIdentifier | null;
|
||||
error?: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user