import type { TaskDocument } from './dto/task-document' import type { HydraCollection } from '~/utils/api' import { extractHydraMembers } from '~/utils/api' import { $fetch } from 'ofetch' export function useTaskDocumentService() { const api = useApi() const config = useRuntimeConfig() const baseURL = config.public.apiBase || '/api' async function getByTask(taskId: number): Promise { const data = await api.get>('/task_documents', { task: `/api/tasks/${taskId}`, }) return extractHydraMembers(data) } async function uploadWithRelation(relationField: string, relationIri: string, file: File): Promise { const formData = new FormData() formData.append('file', file) formData.append(relationField, relationIri) return $fetch(`${baseURL}/task_documents`, { method: 'POST', body: formData, credentials: 'include', }) } async function upload(taskId: number, file: File): Promise { return uploadWithRelation('task', `/api/tasks/${taskId}`, file) } async function uploadForTicket(clientTicketId: number, file: File): Promise { return uploadWithRelation('clientTicket', `/api/client_tickets/${clientTicketId}`, file) } async function getByTicket(clientTicketId: number): Promise { const data = await api.get>('/task_documents', { clientTicket: `/api/client_tickets/${clientTicketId}`, }) return extractHydraMembers(data) } async function remove(id: number): Promise { await api.delete(`/task_documents/${id}`, {}, { toastSuccessKey: 'taskDocuments.deleted', }) } function getDownloadUrl(id: number): string { return `${baseURL}/task_documents/${id}/download` } return { getByTask, upload, uploadForTicket, getByTicket, remove, getDownloadUrl } }