Files
PecHub/frontend/src/api/messages.api.ts
T
2026-03-25 17:49:13 +01:00

108 lines
3.2 KiB
TypeScript

import apiClient from './client'
import type {
AttachmentResponse,
MessageListResponse,
MessageResponse,
} from '@/types/api.types'
export interface MessageFilters {
page?: number
page_size?: number
/** Filtra per Virtual Box assegnata all'utente corrente */
vbox_id?: string
mailbox_id?: string
direction?: 'inbound' | 'outbound'
state?: string
is_read?: boolean
is_starred?: boolean
is_archived?: boolean
is_trashed?: boolean
search?: string
}
export interface MessageBulkUpdatePayload {
ids: string[]
is_read?: boolean
is_starred?: boolean
is_archived?: boolean
is_trashed?: boolean
}
export interface MessageBulkUpdateResponse {
updated: number
items: MessageResponse[]
}
export const messagesApi = {
list: (filters: MessageFilters = {}) =>
apiClient
.get<MessageListResponse>('/messages', { params: filters })
.then((r) => r.data),
get: (id: string) =>
apiClient.get<MessageResponse>(`/messages/${id}`).then((r) => r.data),
markRead: (id: string) =>
apiClient.patch<MessageResponse>(`/messages/${id}`, { is_read: true }).then((r) => r.data),
markUnread: (id: string) =>
apiClient.patch<MessageResponse>(`/messages/${id}`, { is_read: false }).then((r) => r.data),
toggleStar: (id: string, starred: boolean) =>
apiClient
.patch<MessageResponse>(`/messages/${id}`, { is_starred: starred })
.then((r) => r.data),
archive: (id: string) =>
apiClient
.patch<MessageResponse>(`/messages/${id}`, { is_archived: true })
.then((r) => r.data),
unarchive: (id: string) =>
apiClient
.patch<MessageResponse>(`/messages/${id}`, { is_archived: false })
.then((r) => r.data),
trash: (id: string) =>
apiClient
.patch<MessageResponse>(`/messages/${id}`, { is_trashed: true })
.then((r) => r.data),
untrash: (id: string) =>
apiClient
.patch<MessageResponse>(`/messages/${id}`, { is_trashed: false })
.then((r) => r.data),
/** Aggiorna in blocco is_starred e/o is_archived e/o is_trashed su più messaggi */
bulkUpdate: (payload: MessageBulkUpdatePayload) =>
apiClient
.patch<MessageBulkUpdateResponse>('/messages/bulk', payload)
.then((r) => r.data),
getAttachments: (id: string) =>
apiClient.get<AttachmentResponse[]>(`/messages/${id}/attachments`).then((r) => r.data),
/**
* Scarica un allegato autenticato e lo salva localmente.
* Utilizza apiClient (con Bearer token) per evitare il 401 che si ottiene
* navigando direttamente verso l'URL con un <a href>.
*/
downloadAttachment: async (messageId: string, attachmentId: string, filename: string): Promise<void> => {
const response = await apiClient.get(
`/messages/${messageId}/attachments/${attachmentId}/download`,
{ responseType: 'blob' },
)
const blobUrl = window.URL.createObjectURL(new Blob([response.data]))
const anchor = document.createElement('a')
anchor.href = blobUrl
anchor.setAttribute('download', filename)
document.body.appendChild(anchor)
anchor.click()
anchor.remove()
window.URL.revokeObjectURL(blobUrl)
},
getReceipts: (id: string) =>
apiClient.get<MessageResponse[]>(`/messages/${id}/receipts`).then((r) => r.data),
}