refactor(frontend) : split useMachineDetailData into focused composables
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
214
app/composables/useMachineDetailUpdates.ts
Normal file
214
app/composables/useMachineDetailUpdates.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
/**
|
||||
* 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>
|
||||
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>
|
||||
toast: { showInfo: (msg: string) => void }
|
||||
}
|
||||
|
||||
export function useMachineDetailUpdates(deps: UseMachineDetailUpdatesDeps) {
|
||||
const {
|
||||
machine,
|
||||
machineName,
|
||||
machineReference,
|
||||
machineConstructeurIds,
|
||||
machineComponentLinks,
|
||||
machinePieceLinks,
|
||||
applyMachineLinks,
|
||||
loadProductDocuments,
|
||||
transformComponentCustomFields,
|
||||
transformCustomFields,
|
||||
upsertCustomFieldValue,
|
||||
updateMachineApi,
|
||||
updateComposantApi,
|
||||
updatePieceApi,
|
||||
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,
|
||||
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,
|
||||
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)
|
||||
} 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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user