generated from gilles/template-webapp
81 lines
2.1 KiB
TypeScript
81 lines
2.1 KiB
TypeScript
/**
|
|
* API pour les documents (photos, notices, factures, etc.)
|
|
*/
|
|
|
|
import { apiClient, getApiBaseUrl, SuccessResponse } from './client'
|
|
import { Document, DocumentType, DocumentUpdate, DocumentUploadResponse } from './types'
|
|
|
|
export const documentsApi = {
|
|
/**
|
|
* Upload un document
|
|
*/
|
|
async upload(
|
|
file: File,
|
|
itemId: number,
|
|
docType: DocumentType,
|
|
description?: string
|
|
): Promise<DocumentUploadResponse> {
|
|
const formData = new FormData()
|
|
formData.append('file', file)
|
|
formData.append('item_id', itemId.toString())
|
|
formData.append('doc_type', docType)
|
|
if (description) {
|
|
formData.append('description', description)
|
|
}
|
|
|
|
const response = await apiClient.post<DocumentUploadResponse>('/documents/upload', formData, {
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data',
|
|
},
|
|
})
|
|
return response.data
|
|
},
|
|
|
|
/**
|
|
* Récupère tous les documents d'un item
|
|
*/
|
|
async getByItem(itemId: number, docType?: DocumentType): Promise<Document[]> {
|
|
const response = await apiClient.get<Document[]>(`/documents/item/${itemId}`, {
|
|
params: docType ? { doc_type: docType } : undefined,
|
|
})
|
|
return response.data
|
|
},
|
|
|
|
/**
|
|
* Récupère un document par son ID
|
|
*/
|
|
async getById(id: number): Promise<Document> {
|
|
const response = await apiClient.get<Document>(`/documents/${id}`)
|
|
return response.data
|
|
},
|
|
|
|
/**
|
|
* Met à jour les métadonnées d'un document
|
|
*/
|
|
async update(id: number, data: DocumentUpdate): Promise<Document> {
|
|
const response = await apiClient.patch<Document>(`/documents/${id}`, data)
|
|
return response.data
|
|
},
|
|
|
|
/**
|
|
* Supprime un document
|
|
*/
|
|
async delete(id: number): Promise<void> {
|
|
await apiClient.delete(`/documents/${id}`)
|
|
},
|
|
|
|
/**
|
|
* Retourne l'URL de téléchargement d'un document
|
|
*/
|
|
getDownloadUrl(id: number): string {
|
|
return `${getApiBaseUrl()}/documents/${id}/download`
|
|
},
|
|
|
|
/**
|
|
* Retourne l'URL d'affichage d'une image
|
|
*/
|
|
getImageUrl(id: number): string {
|
|
return `${getApiBaseUrl()}/documents/${id}/download`
|
|
},
|
|
}
|