feat(documents) : add document type constants and updateDocument method

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-03-23 15:31:52 +01:00
parent 2e82e854bf
commit 7a4a77e3fc
2 changed files with 47 additions and 1 deletions

View File

@@ -11,6 +11,7 @@ export interface Document {
size: number size: number
fileUrl: string fileUrl: string
downloadUrl: string downloadUrl: string
type?: string
/** @deprecated Legacy Base64 data URI — use fileUrl instead */ /** @deprecated Legacy Base64 data URI — use fileUrl instead */
path?: string path?: string
createdAt?: string createdAt?: string
@@ -32,6 +33,7 @@ export interface UploadContext {
composantId?: string composantId?: string
productId?: string productId?: string
pieceId?: string pieceId?: string
type?: string
} }
export interface DocumentResult { export interface DocumentResult {
@@ -63,7 +65,7 @@ const extractTotal = (payload: unknown, fallbackLength: number): number => {
} }
export function useDocuments() { export function useDocuments() {
const { get, postFormData, delete: del } = useApi() const { get, patch, postFormData, delete: del } = useApi()
const { showError, showSuccess } = useToast() const { showError, showSuccess } = useToast()
const loadFromEndpoint = async ( const loadFromEndpoint = async (
@@ -218,6 +220,7 @@ export function useDocuments() {
const formData = new FormData() const formData = new FormData()
formData.append('file', file) formData.append('file', file)
formData.append('name', file.name) formData.append('name', file.name)
if (context.type) formData.append('type', context.type)
if (context.siteId) formData.append('siteId', context.siteId) if (context.siteId) formData.append('siteId', context.siteId)
if (context.machineId) formData.append('machineId', context.machineId) if (context.machineId) formData.append('machineId', context.machineId)
@@ -280,6 +283,33 @@ export function useDocuments() {
} }
} }
const updateDocument = async (
id: string,
data: { name?: string; type?: string },
): Promise<DocumentResult> => {
loading.value = true
try {
const result = await patch(`/documents/${id}`, data)
if (result.success && result.data) {
const updated = result.data as Document
const index = documents.value.findIndex((doc) => doc.id === id)
if (index !== -1) {
documents.value[index] = { ...documents.value[index], ...updated }
}
showSuccess('Document mis à jour')
return { success: true, data: updated }
}
if (result.error) showError(result.error)
return result as DocumentResult
} catch (error) {
const err = error as Error
showError('Impossible de mettre à jour le document')
return { success: false, error: err.message }
} finally {
loading.value = false
}
}
return { return {
documents, documents,
total, total,
@@ -292,6 +322,7 @@ export function useDocuments() {
loadDocumentsByPiece, loadDocumentsByPiece,
loadDocumentsByProduct, loadDocumentsByProduct,
uploadDocuments, uploadDocuments,
updateDocument,
deleteDocument, deleteDocument,
} }
} }

View File

@@ -0,0 +1,15 @@
export const DOCUMENT_TYPES = [
{ value: 'documentation', label: 'Documentation' },
{ value: 'devis', label: 'Devis' },
{ value: 'facture', label: 'Facture' },
{ value: 'plan', label: 'Plan' },
{ value: 'photo', label: 'Photo' },
{ value: 'autre', label: 'Autre' },
] as const
export type DocumentTypeValue = (typeof DOCUMENT_TYPES)[number]['value']
export const getDocumentTypeLabel = (value: string): string => {
const found = DOCUMENT_TYPES.find((t) => t.value === value)
return found?.label ?? value
}