Normalize machine link responses
This commit is contained in:
@@ -5,6 +5,45 @@ import { useApi } from './useApi'
|
||||
const machines = ref([])
|
||||
const loading = ref(false)
|
||||
|
||||
const resolveLinkCollection = (source, keys) => {
|
||||
if (!source || typeof source !== 'object') {
|
||||
return undefined
|
||||
}
|
||||
|
||||
for (const key of keys) {
|
||||
const value = source[key]
|
||||
if (Array.isArray(value)) {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
const normalizeMachineResponse = (payload) => {
|
||||
if (!payload || typeof payload !== 'object') {
|
||||
return null
|
||||
}
|
||||
|
||||
const container = payload.machine && typeof payload.machine === 'object'
|
||||
? payload.machine
|
||||
: payload
|
||||
|
||||
const normalized = { ...container }
|
||||
|
||||
const componentLinks = resolveLinkCollection(payload, ['componentLinks', 'machineComponentLinks']) ??
|
||||
resolveLinkCollection(container, ['componentLinks', 'machineComponentLinks']) ??
|
||||
[]
|
||||
const pieceLinks = resolveLinkCollection(payload, ['pieceLinks', 'machinePieceLinks']) ??
|
||||
resolveLinkCollection(container, ['pieceLinks', 'machinePieceLinks']) ??
|
||||
[]
|
||||
|
||||
normalized.componentLinks = componentLinks
|
||||
normalized.pieceLinks = pieceLinks
|
||||
|
||||
return normalized
|
||||
}
|
||||
|
||||
export function useMachines () {
|
||||
const { showSuccess, showError, showInfo } = useToast()
|
||||
const { get, post, patch, delete: del } = useApi()
|
||||
@@ -14,8 +53,18 @@ export function useMachines () {
|
||||
try {
|
||||
const result = await get('/machines')
|
||||
if (result.success) {
|
||||
machines.value = result.data
|
||||
showInfo(`Chargement de ${machines.value.length} machine(s) réussi`)
|
||||
const machineList = Array.isArray(result.data)
|
||||
? result.data
|
||||
: Array.isArray(result.data?.machines)
|
||||
? result.data.machines
|
||||
: Array.isArray(result.data?.data)
|
||||
? result.data.data
|
||||
: []
|
||||
const normalized = machineList
|
||||
.map((item) => normalizeMachineResponse(item))
|
||||
.filter(Boolean)
|
||||
machines.value = normalized
|
||||
showInfo(`Chargement de ${normalized.length} machine(s) réussi`)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Erreur lors du chargement des machines:', error)
|
||||
@@ -29,8 +78,14 @@ export function useMachines () {
|
||||
try {
|
||||
const result = await post('/machines', machineData)
|
||||
if (result.success) {
|
||||
machines.value.push(result.data)
|
||||
showSuccess(`Machine "${machineData.name}" créée avec succès`)
|
||||
const createdMachine = normalizeMachineResponse(result.data) ||
|
||||
normalizeMachineResponse(result.data?.machine) ||
|
||||
null
|
||||
if (createdMachine) {
|
||||
machines.value.push(createdMachine)
|
||||
}
|
||||
const displayName = createdMachine?.name || machineData?.name || ''
|
||||
showSuccess(`Machine "${displayName}" créée avec succès`)
|
||||
}
|
||||
return result
|
||||
} catch (error) {
|
||||
@@ -58,11 +113,17 @@ export function useMachines () {
|
||||
try {
|
||||
const result = await patch(`/machines/${id}`, machineData)
|
||||
if (result.success) {
|
||||
const updatedMachine = normalizeMachineResponse(result.data) ||
|
||||
normalizeMachineResponse(result.data?.machine) ||
|
||||
null
|
||||
const index = machines.value.findIndex(machine => machine.id === id)
|
||||
if (index !== -1) {
|
||||
machines.value[index] = result.data
|
||||
if (index !== -1 && updatedMachine) {
|
||||
machines.value[index] = {
|
||||
...machines.value[index],
|
||||
...updatedMachine,
|
||||
}
|
||||
}
|
||||
showSuccess(`Machine "${result.data?.name || machineData.name || ''}" mise à jour avec succès`)
|
||||
showSuccess(`Machine "${updatedMachine?.name || machineData.name || ''}" mise à jour avec succès`)
|
||||
}
|
||||
return result
|
||||
} catch (error) {
|
||||
@@ -84,7 +145,13 @@ export function useMachines () {
|
||||
if (result.success) {
|
||||
const index = machines.value.findIndex(machine => machine.id === machineId)
|
||||
if (index !== -1) {
|
||||
machines.value[index] = result.data?.machine || result.data
|
||||
const updatedMachine = normalizeMachineResponse(result.data) ||
|
||||
normalizeMachineResponse(result.data?.machine) ||
|
||||
machines.value[index]
|
||||
machines.value[index] = {
|
||||
...machines.value[index],
|
||||
...(updatedMachine || {}),
|
||||
}
|
||||
}
|
||||
showSuccess('Structure de la machine mise à jour avec succès')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user