- MachinePiecesCard : passer isEditMode au PieceItem + forward event update - useMachineHierarchy : mapper quantity depuis le backend + construire les pièces de structure du composant en lecture seule - useMachineDetailUpdates : PATCH MachinePieceLink.quantity + fix reference null - ComponentItem : séparer pièces liées / pièces incluses par défaut - useEntityDocuments : skip chargement documents pour pièces de structure Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
227 lines
7.3 KiB
TypeScript
227 lines
7.3 KiB
TypeScript
/**
|
|
* Machine detail page — update/mutation methods.
|
|
*
|
|
* Extracted from useMachineDetailData.ts to keep the orchestrator under 500 lines.
|
|
*/
|
|
|
|
import type { Ref } from 'vue'
|
|
import { uniqueConstructeurIds } from '~/shared/constructeurUtils'
|
|
|
|
type AnyRecord = Record<string, unknown>
|
|
|
|
export interface UseMachineDetailUpdatesDeps {
|
|
machine: Ref<AnyRecord | null>
|
|
machineName: Ref<string>
|
|
machineReference: Ref<string>
|
|
machineSiteId: Ref<string>
|
|
machineConstructeurIds: Ref<string[]>
|
|
machineDocumentsLoaded: Ref<boolean>
|
|
machineComponentLinks: Ref<AnyRecord[]>
|
|
machinePieceLinks: Ref<AnyRecord[]>
|
|
machineProductLinks: Ref<AnyRecord[]>
|
|
applyMachineLinks: (data: AnyRecord) => boolean
|
|
refreshMachineDocuments: () => Promise<void>
|
|
transformComponentCustomFields: (items: AnyRecord[]) => AnyRecord[]
|
|
transformCustomFields: (items: AnyRecord[]) => AnyRecord[]
|
|
loadProductDocuments: () => Promise<void>
|
|
upsertCustomFieldValue: (
|
|
fieldId: string,
|
|
entityType: string,
|
|
entityId: string,
|
|
value: unknown,
|
|
) => Promise<unknown>
|
|
updateMachineApi: (id: string, data: any) => Promise<unknown>
|
|
updateComposantApi: (id: string, data: any) => Promise<unknown>
|
|
updatePieceApi: (id: string, data: any) => Promise<unknown>
|
|
apiPatch: (endpoint: string, data?: unknown) => Promise<any>
|
|
toast: { showInfo: (msg: string) => void }
|
|
}
|
|
|
|
export function useMachineDetailUpdates(deps: UseMachineDetailUpdatesDeps) {
|
|
const {
|
|
machine,
|
|
machineName,
|
|
machineReference,
|
|
machineSiteId,
|
|
machineConstructeurIds,
|
|
machineComponentLinks,
|
|
machinePieceLinks,
|
|
applyMachineLinks,
|
|
loadProductDocuments,
|
|
transformComponentCustomFields,
|
|
transformCustomFields,
|
|
upsertCustomFieldValue,
|
|
updateMachineApi,
|
|
updateComposantApi,
|
|
updatePieceApi,
|
|
apiPatch,
|
|
toast,
|
|
} = deps
|
|
|
|
const updateMachineInfo = async () => {
|
|
if (!machine.value) return
|
|
try {
|
|
const cIds = uniqueConstructeurIds(machineConstructeurIds.value)
|
|
machineConstructeurIds.value = cIds
|
|
|
|
const result: any = await updateMachineApi(machine.value.id as string, {
|
|
name: machineName.value,
|
|
reference: machineReference.value,
|
|
siteId: machineSiteId.value || undefined,
|
|
constructeurIds: cIds,
|
|
} as any)
|
|
if (result.success) {
|
|
const machinePayload =
|
|
result.data?.machine && typeof result.data.machine === 'object'
|
|
? result.data.machine
|
|
: result.data
|
|
if (machinePayload && typeof machinePayload === 'object') {
|
|
machine.value = {
|
|
...machine.value,
|
|
...machinePayload,
|
|
documents: machinePayload.documents || machine.value.documents || [],
|
|
customFieldValues: machinePayload.customFieldValues || machine.value.customFieldValues || [],
|
|
}
|
|
machineConstructeurIds.value = uniqueConstructeurIds(
|
|
machine.value!.constructeurIds,
|
|
machine.value!.constructeurs,
|
|
machine.value!.constructeur,
|
|
)
|
|
const linksApplied = applyMachineLinks(result.data)
|
|
if (linksApplied && machine.value) {
|
|
machine.value.componentLinks = machineComponentLinks.value
|
|
machine.value.pieceLinks = machinePieceLinks.value
|
|
}
|
|
loadProductDocuments().catch(() => {})
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Erreur lors de la mise à jour de la machine:', error)
|
|
}
|
|
}
|
|
|
|
const updateComponent = async (updatedComponent: AnyRecord) => {
|
|
try {
|
|
const cIds = uniqueConstructeurIds(
|
|
updatedComponent.constructeurIds,
|
|
updatedComponent.constructeurId,
|
|
updatedComponent.constructeur,
|
|
)
|
|
const productId = updatedComponent.productId
|
|
? String(updatedComponent.productId)
|
|
: null
|
|
const prix =
|
|
updatedComponent.prix !== null &&
|
|
updatedComponent.prix !== undefined &&
|
|
String(updatedComponent.prix).trim() !== ''
|
|
? Number(updatedComponent.prix)
|
|
: null
|
|
|
|
const result: any = await updateComposantApi(updatedComponent.id as string, {
|
|
name: updatedComponent.name,
|
|
reference: updatedComponent.reference,
|
|
constructeurIds: cIds,
|
|
prix: Number.isNaN(prix) ? null : prix,
|
|
productId,
|
|
} as any)
|
|
if (result.success) {
|
|
const transformed = transformComponentCustomFields([result.data])[0]
|
|
Object.assign(updatedComponent, transformed)
|
|
}
|
|
} catch (error) {
|
|
console.error('Erreur lors de la mise à jour du composant:', error)
|
|
}
|
|
}
|
|
|
|
const _buildAndUpdatePiece = async (updatedPiece: AnyRecord) => {
|
|
const cIds = uniqueConstructeurIds(
|
|
updatedPiece.constructeurIds,
|
|
updatedPiece.constructeurId,
|
|
updatedPiece.constructeur,
|
|
)
|
|
const productId = updatedPiece.productId ? String(updatedPiece.productId) : null
|
|
const prix =
|
|
updatedPiece.prix !== null &&
|
|
updatedPiece.prix !== undefined &&
|
|
String(updatedPiece.prix).trim() !== ''
|
|
? Number(updatedPiece.prix)
|
|
: null
|
|
|
|
const result: any = await updatePieceApi(updatedPiece.id as string, {
|
|
name: updatedPiece.name,
|
|
reference: updatedPiece.reference || null,
|
|
constructeurIds: cIds,
|
|
prix: Number.isNaN(prix) ? null : prix,
|
|
productId,
|
|
} as any)
|
|
if (result.success) {
|
|
const transformed = transformCustomFields([result.data])[0]
|
|
Object.assign(updatedPiece, transformed)
|
|
}
|
|
return result
|
|
}
|
|
|
|
const updatePieceFromComponent = async (updatedPiece: AnyRecord) => {
|
|
try {
|
|
const result = await _buildAndUpdatePiece(updatedPiece)
|
|
if (result?.success && updatedPiece.customFields) {
|
|
const fieldsToSave = (updatedPiece.customFields as AnyRecord[]).filter(
|
|
(field) => field.value !== undefined,
|
|
)
|
|
if (fieldsToSave.length) {
|
|
await Promise.allSettled(
|
|
fieldsToSave.map((field) =>
|
|
upsertCustomFieldValue(
|
|
field.id as string,
|
|
'piece',
|
|
updatedPiece.id as string,
|
|
field.value,
|
|
),
|
|
),
|
|
)
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Erreur lors de la mise à jour de la pièce:', error)
|
|
}
|
|
}
|
|
|
|
const updatePieceInfo = async (updatedPiece: AnyRecord) => {
|
|
try {
|
|
await _buildAndUpdatePiece(updatedPiece)
|
|
|
|
// Update link quantity if this is a direct machine piece
|
|
const linkId = updatedPiece.linkId || updatedPiece.machinePieceLinkId
|
|
const quantity = typeof updatedPiece.quantity === 'number' ? Math.max(1, updatedPiece.quantity) : null
|
|
if (linkId && quantity !== null) {
|
|
await apiPatch(`/machine_piece_links/${linkId}`, { quantity })
|
|
}
|
|
} catch (error) {
|
|
console.error('Erreur lors de la mise à jour de la pièce:', error)
|
|
}
|
|
}
|
|
|
|
const handleMachineConstructeurChange = async (value: unknown) => {
|
|
machineConstructeurIds.value = uniqueConstructeurIds(value)
|
|
await updateMachineInfo()
|
|
}
|
|
|
|
const editComponent = () => {
|
|
toast.showInfo('La modification des composants sera bientôt disponible')
|
|
}
|
|
|
|
const editPiece = () => {
|
|
toast.showInfo('La modification des pièces sera bientôt disponible')
|
|
}
|
|
|
|
return {
|
|
updateMachineInfo,
|
|
updateComponent,
|
|
updatePieceFromComponent,
|
|
updatePieceInfo,
|
|
handleMachineConstructeurChange,
|
|
editComponent,
|
|
editPiece,
|
|
}
|
|
}
|