feat(front): aligner api platform et sessions [INV-20260111-02]
This commit is contained in:
@@ -2,6 +2,7 @@ import { ref } from 'vue'
|
||||
import { useToast } from './useToast'
|
||||
import { useApi } from './useApi'
|
||||
import { buildConstructeurRequestPayload } from '~/shared/constructeurUtils'
|
||||
import { extractRelationId, normalizeRelationIds } from '~/shared/apiRelations'
|
||||
|
||||
const machines = ref([])
|
||||
const loading = ref(false)
|
||||
@@ -32,6 +33,20 @@ const normalizeMachineResponse = (payload) => {
|
||||
|
||||
const normalized = { ...container }
|
||||
|
||||
if (!normalized.siteId) {
|
||||
const siteId = extractRelationId(container.site)
|
||||
if (siteId) {
|
||||
normalized.siteId = siteId
|
||||
}
|
||||
}
|
||||
|
||||
if (!normalized.typeMachineId) {
|
||||
const typeMachineId = extractRelationId(container.typeMachine)
|
||||
if (typeMachineId) {
|
||||
normalized.typeMachineId = typeMachineId
|
||||
}
|
||||
}
|
||||
|
||||
const componentLinks = resolveLinkCollection(payload, ['componentLinks', 'machineComponentLinks']) ??
|
||||
resolveLinkCollection(container, ['componentLinks', 'machineComponentLinks']) ??
|
||||
[]
|
||||
@@ -56,11 +71,15 @@ export function useMachines () {
|
||||
if (result.success) {
|
||||
const machineList = Array.isArray(result.data)
|
||||
? result.data
|
||||
: Array.isArray(result.data?.machines)
|
||||
? result.data.machines
|
||||
: Array.isArray(result.data?.data)
|
||||
? result.data.data
|
||||
: []
|
||||
: Array.isArray(result.data?.member)
|
||||
? result.data.member
|
||||
: Array.isArray(result.data?.['hydra:member'])
|
||||
? result.data['hydra:member']
|
||||
: 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)
|
||||
@@ -77,7 +96,8 @@ export function useMachines () {
|
||||
const createMachine = async (machineData) => {
|
||||
loading.value = true
|
||||
try {
|
||||
const result = await post('/machines', buildConstructeurRequestPayload(machineData))
|
||||
const normalizedPayload = normalizeRelationIds(buildConstructeurRequestPayload(machineData))
|
||||
const result = await post('/machines', normalizedPayload)
|
||||
if (result.success) {
|
||||
const createdMachine = normalizeMachineResponse(result.data) ||
|
||||
normalizeMachineResponse(result.data?.machine) ||
|
||||
@@ -106,13 +126,14 @@ export function useMachines () {
|
||||
// Les composants et pièces seront créés automatiquement
|
||||
}
|
||||
|
||||
return await createMachine(buildConstructeurRequestPayload(machineWithStructure))
|
||||
return await createMachine(machineWithStructure)
|
||||
}
|
||||
|
||||
const updateMachineData = async (id, machineData) => {
|
||||
loading.value = true
|
||||
try {
|
||||
const result = await patch(`/machines/${id}`, buildConstructeurRequestPayload(machineData))
|
||||
const normalizedPayload = normalizeRelationIds(buildConstructeurRequestPayload(machineData))
|
||||
const result = await patch(`/machines/${id}`, normalizedPayload)
|
||||
if (result.success) {
|
||||
const updatedMachine = normalizeMachineResponse(result.data) ||
|
||||
normalizeMachineResponse(result.data?.machine) ||
|
||||
|
||||
Reference in New Issue
Block a user