Initial commit
This commit is contained in:
261
src/stores/entitiesStore.ts
Normal file
261
src/stores/entitiesStore.ts
Normal file
@@ -0,0 +1,261 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { entityService } from '../services'
|
||||
import type { MessageObject, EntityWrapper, MessageSendRequest } from '../types'
|
||||
|
||||
export const useEntitiesStore = defineStore('mail-entities', {
|
||||
state: () => ({
|
||||
messages: {} as Record<string, Record<string, Record<string, Record<string, EntityWrapper<MessageObject>>>>>,
|
||||
signatures: {} as Record<string, Record<string, Record<string, string>>>, // Track delta signatures
|
||||
loading: false,
|
||||
error: null as string | null,
|
||||
}),
|
||||
|
||||
actions: {
|
||||
async loadMessages(sources?: any, filter?: any, sort?: any, range?: any) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
try {
|
||||
const response = await entityService.list({ sources, filter, sort, range })
|
||||
|
||||
// Entities come as objects keyed by identifier
|
||||
Object.entries(response).forEach(([provider, providerData]) => {
|
||||
Object.entries(providerData).forEach(([service, serviceData]) => {
|
||||
Object.entries(serviceData).forEach(([collection, entities]) => {
|
||||
if (!this.messages[provider]) {
|
||||
this.messages[provider] = {}
|
||||
}
|
||||
if (!this.messages[provider][service]) {
|
||||
this.messages[provider][service] = {}
|
||||
}
|
||||
if (!this.messages[provider][service][collection]) {
|
||||
this.messages[provider][service][collection] = {}
|
||||
}
|
||||
|
||||
// Entities are already keyed by identifier
|
||||
this.messages[provider][service][collection] = entities as Record<string, EntityWrapper<MessageObject>>
|
||||
})
|
||||
})
|
||||
})
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
async getMessages(
|
||||
provider: string,
|
||||
service: string | number,
|
||||
collection: string | number,
|
||||
identifiers: (string | number)[],
|
||||
properties?: string[]
|
||||
) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
try {
|
||||
const response = await entityService.fetch({
|
||||
provider,
|
||||
service,
|
||||
collection,
|
||||
identifiers,
|
||||
properties
|
||||
})
|
||||
|
||||
// Update in store
|
||||
if (!this.messages[provider]) {
|
||||
this.messages[provider] = {}
|
||||
}
|
||||
if (!this.messages[provider][String(service)]) {
|
||||
this.messages[provider][String(service)] = {}
|
||||
}
|
||||
if (!this.messages[provider][String(service)][String(collection)]) {
|
||||
this.messages[provider][String(service)][String(collection)] = {}
|
||||
}
|
||||
|
||||
// Index fetched entities by identifier
|
||||
response.entities.forEach((entity: EntityWrapper<MessageObject>) => {
|
||||
this.messages[provider][String(service)][String(collection)][entity.identifier] = entity
|
||||
})
|
||||
|
||||
return response
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
async searchMessages(
|
||||
provider: string,
|
||||
service: string | number,
|
||||
query: string,
|
||||
collections?: (string | number)[],
|
||||
filter?: any,
|
||||
sort?: any,
|
||||
range?: any
|
||||
) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
try {
|
||||
const response = await entityService.search({
|
||||
provider,
|
||||
service,
|
||||
query,
|
||||
collections,
|
||||
filter,
|
||||
sort,
|
||||
range
|
||||
})
|
||||
return response
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
async sendMessage(request: MessageSendRequest) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
try {
|
||||
const response = await entityService.send(request)
|
||||
return response
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
|
||||
async getDelta(sources: any) {
|
||||
this.loading = true
|
||||
this.error = null
|
||||
try {
|
||||
// Sources are already in correct format: { provider: { service: { collection: signature } } }
|
||||
const response = await entityService.delta({ sources })
|
||||
|
||||
// Process delta and update store
|
||||
Object.entries(response).forEach(([provider, providerData]) => {
|
||||
Object.entries(providerData).forEach(([service, serviceData]) => {
|
||||
Object.entries(serviceData).forEach(([collection, collectionData]) => {
|
||||
// Skip if no changes (server returns false or string signature)
|
||||
if (collectionData === false || typeof collectionData === 'string') {
|
||||
return
|
||||
}
|
||||
|
||||
if (!this.messages[provider]) {
|
||||
this.messages[provider] = {}
|
||||
}
|
||||
if (!this.messages[provider][service]) {
|
||||
this.messages[provider][service] = {}
|
||||
}
|
||||
if (!this.messages[provider][service][collection]) {
|
||||
this.messages[provider][service][collection] = {}
|
||||
}
|
||||
|
||||
const collectionMessages = this.messages[provider][service][collection]
|
||||
|
||||
// Update signature if provided
|
||||
if (typeof collectionData === 'object' && collectionData.signature) {
|
||||
if (!this.signatures[provider]) {
|
||||
this.signatures[provider] = {}
|
||||
}
|
||||
if (!this.signatures[provider][service]) {
|
||||
this.signatures[provider][service] = {}
|
||||
}
|
||||
this.signatures[provider][service][collection] = collectionData.signature
|
||||
console.log(`[Store] Updated signature for ${provider}/${service}/${collection}: "${collectionData.signature}"`)
|
||||
}
|
||||
|
||||
// Process additions (from delta response format)
|
||||
if (collectionData.additions) {
|
||||
// Note: additions are just identifiers, need to fetch full entities separately
|
||||
// This is handled by the sync composable
|
||||
}
|
||||
|
||||
// Process modifications
|
||||
if (collectionData.modifications) {
|
||||
// Note: modifications are just identifiers, need to fetch full entities separately
|
||||
}
|
||||
|
||||
// Remove deleted messages
|
||||
if (collectionData.deletions) {
|
||||
collectionData.deletions.forEach((id: string | number) => {
|
||||
delete collectionMessages[String(id)]
|
||||
})
|
||||
}
|
||||
|
||||
// Legacy support: Also handle created/modified/deleted format
|
||||
if (collectionData.created) {
|
||||
collectionData.created.forEach((entity: EntityWrapper<MessageObject>) => {
|
||||
collectionMessages[entity.identifier] = entity
|
||||
})
|
||||
}
|
||||
|
||||
if (collectionData.modified) {
|
||||
collectionData.modified.forEach((entity: EntityWrapper<MessageObject>) => {
|
||||
collectionMessages[entity.identifier] = entity
|
||||
})
|
||||
}
|
||||
|
||||
if (collectionData.deleted) {
|
||||
collectionData.deleted.forEach((id: string | number) => {
|
||||
delete collectionMessages[String(id)]
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
return response
|
||||
} catch (error: any) {
|
||||
this.error = error.message
|
||||
throw error
|
||||
} finally {
|
||||
this.loading = false
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
getters: {
|
||||
messageList: (state) => {
|
||||
const list: EntityWrapper<MessageObject>[] = []
|
||||
Object.values(state.messages).forEach(providerMessages => {
|
||||
Object.values(providerMessages).forEach(serviceMessages => {
|
||||
Object.values(serviceMessages).forEach(collectionMessages => {
|
||||
Object.values(collectionMessages).forEach(message => {
|
||||
list.push(message)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
return list
|
||||
},
|
||||
|
||||
messageCount: (state) => {
|
||||
let count = 0
|
||||
Object.values(state.messages).forEach(providerMessages => {
|
||||
Object.values(providerMessages).forEach(serviceMessages => {
|
||||
Object.values(serviceMessages).forEach(collectionMessages => {
|
||||
count += Object.keys(collectionMessages).length
|
||||
})
|
||||
})
|
||||
})
|
||||
return count
|
||||
},
|
||||
|
||||
hasMessages: (state) => {
|
||||
return Object.values(state.messages).some(providerMessages =>
|
||||
Object.values(providerMessages).some(serviceMessages =>
|
||||
Object.values(serviceMessages).some(collectionMessages =>
|
||||
Object.keys(collectionMessages).length > 0
|
||||
)
|
||||
)
|
||||
)
|
||||
},
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user