43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
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<TaskDocument[]> {
|
|
const data = await api.get<HydraCollection<TaskDocument>>('/task_documents', {
|
|
task: `/api/tasks/${taskId}`,
|
|
})
|
|
return extractHydraMembers(data)
|
|
}
|
|
|
|
async function upload(taskId: number, file: File): Promise<TaskDocument> {
|
|
const formData = new FormData()
|
|
formData.append('file', file)
|
|
formData.append('task', `/api/tasks/${taskId}`)
|
|
|
|
return await $fetch<TaskDocument>(`${baseURL}/task_documents`, {
|
|
method: 'POST',
|
|
body: formData,
|
|
credentials: 'include',
|
|
// Do NOT set Content-Type — browser sets multipart boundary automatically
|
|
})
|
|
}
|
|
|
|
async function remove(id: number): Promise<void> {
|
|
await api.delete(`/task_documents/${id}`, {}, {
|
|
toastSuccessKey: 'taskDocuments.deleted',
|
|
})
|
|
}
|
|
|
|
function getDownloadUrl(id: number): string {
|
|
return `${baseURL}/task_documents/${id}/download`
|
|
}
|
|
|
|
return { getByTask, upload, remove, getDownloadUrl }
|
|
}
|