fix(modeles): normaliser structure et champs perso
This commit is contained in:
@@ -403,6 +403,7 @@ import { useComposants } from '~/composables/useComposants'
|
||||
import { useCustomFields } from '~/composables/useCustomFields'
|
||||
import { useApi } from '~/composables/useApi'
|
||||
import { useToast } from '~/composables/useToast'
|
||||
import { extractRelationId } from '~/shared/apiRelations'
|
||||
import { useDocuments } from '~/composables/useDocuments'
|
||||
import { useConstructeurs } from '~/composables/useConstructeurs'
|
||||
import { formatStructurePreview, normalizeStructureForEditor } from '~/shared/modelUtils'
|
||||
@@ -435,7 +436,7 @@ const { get } = useApi()
|
||||
const { componentTypes, loadComponentTypes } = useComponentTypes()
|
||||
const { updateComposant } = useComposants()
|
||||
const { ensureConstructeurs } = useConstructeurs()
|
||||
const { upsertCustomFieldValue, updateCustomFieldValue } = useCustomFields()
|
||||
const { upsertCustomFieldValue, updateCustomFieldValue, getCustomFieldValuesByEntity } = useCustomFields()
|
||||
const toast = useToast()
|
||||
const { loadDocumentsByComponent, uploadDocuments, deleteDocument } = useDocuments()
|
||||
|
||||
@@ -636,6 +637,11 @@ const fetchComponent = async () => {
|
||||
if (result.success) {
|
||||
component.value = result.data
|
||||
componentDocuments.value = Array.isArray(result.data?.documents) ? result.data.documents : []
|
||||
|
||||
const customValues = await getCustomFieldValuesByEntity('composant', result.data.id)
|
||||
if (customValues.success && Array.isArray(customValues.data)) {
|
||||
component.value.customFieldValues = customValues.data
|
||||
}
|
||||
} else {
|
||||
component.value = null
|
||||
componentDocuments.value = []
|
||||
@@ -651,7 +657,13 @@ watch(
|
||||
return
|
||||
}
|
||||
|
||||
selectedTypeId.value = currentComponent.typeComposantId || ''
|
||||
const resolvedTypeId = currentComponent.typeComposantId
|
||||
|| extractRelationId(currentComponent.typeComposant)
|
||||
|| ''
|
||||
if (resolvedTypeId && !currentComponent.typeComposantId) {
|
||||
currentComponent.typeComposantId = resolvedTypeId
|
||||
}
|
||||
selectedTypeId.value = resolvedTypeId
|
||||
|
||||
editionForm.name = currentComponent.name || ''
|
||||
editionForm.reference = currentComponent.reference || ''
|
||||
|
||||
@@ -47,6 +47,9 @@ export interface ModelType extends BaseModelTypePayload {
|
||||
updatedAt: string;
|
||||
category: ModelCategory;
|
||||
structure: ModelTypeStructure;
|
||||
componentSkeleton?: ComponentModelStructure | null;
|
||||
pieceSkeleton?: PieceModelStructure | null;
|
||||
productSkeleton?: ProductModelStructure | null;
|
||||
}
|
||||
|
||||
export interface ModelTypeListParams {
|
||||
@@ -80,6 +83,46 @@ function createOptions<T>(options: FetchOptions<T> = {}) {
|
||||
};
|
||||
}
|
||||
|
||||
const normalizeModelType = (item: any): ModelType => {
|
||||
if (!item || typeof item !== 'object') {
|
||||
return item as ModelType;
|
||||
}
|
||||
if (!item.structure) {
|
||||
if (item.category === 'COMPONENT' && item.componentSkeleton) {
|
||||
item.structure = item.componentSkeleton;
|
||||
} else if (item.category === 'PIECE' && item.pieceSkeleton) {
|
||||
item.structure = item.pieceSkeleton;
|
||||
} else if (item.category === 'PRODUCT' && item.productSkeleton) {
|
||||
item.structure = item.productSkeleton;
|
||||
}
|
||||
}
|
||||
return item as ModelType;
|
||||
};
|
||||
|
||||
const mapStructureToSkeleton = <T extends Record<string, any>>(payload: T): T => {
|
||||
if (!payload || typeof payload !== 'object') {
|
||||
return payload;
|
||||
}
|
||||
if (!('structure' in payload)) {
|
||||
return payload;
|
||||
}
|
||||
const structure = (payload as any).structure;
|
||||
if (!structure) {
|
||||
return payload;
|
||||
}
|
||||
const category = (payload as any).category;
|
||||
const next = { ...payload } as Record<string, any>;
|
||||
if (category === 'COMPONENT') {
|
||||
next.componentSkeleton = structure;
|
||||
} else if (category === 'PIECE') {
|
||||
next.pieceSkeleton = structure;
|
||||
} else if (category === 'PRODUCT') {
|
||||
next.productSkeleton = structure;
|
||||
}
|
||||
delete next.structure;
|
||||
return next as T;
|
||||
};
|
||||
|
||||
export async function listModelTypes(params: ModelTypeListParams = {}, opts: { signal?: AbortSignal } = {}) {
|
||||
const requestFetch = useRequestFetch();
|
||||
const query: Record<string, string | number> = {};
|
||||
@@ -136,9 +179,9 @@ export async function listModelTypes(params: ModelTypeListParams = {}, opts: { s
|
||||
: Array.isArray(payload?.items)
|
||||
? payload.items.length
|
||||
: rawItems.length;
|
||||
const items = params.category && typeof effectiveLimit === 'number'
|
||||
const items = (params.category && typeof effectiveLimit === 'number'
|
||||
? filteredItems.slice(effectiveOffset, effectiveOffset + effectiveLimit)
|
||||
: filteredItems;
|
||||
: filteredItems).map(normalizeModelType);
|
||||
|
||||
return {
|
||||
items,
|
||||
@@ -150,20 +193,30 @@ export async function listModelTypes(params: ModelTypeListParams = {}, opts: { s
|
||||
|
||||
export function createModelType(payload: ModelTypePayload, opts: { signal?: AbortSignal } = {}) {
|
||||
const requestFetch = useRequestFetch();
|
||||
const mappedPayload = mapStructureToSkeleton(payload);
|
||||
return requestFetch<ModelType>(ENDPOINT, createOptions({
|
||||
method: 'POST',
|
||||
body: payload,
|
||||
headers: {
|
||||
'Content-Type': 'application/ld+json',
|
||||
Accept: 'application/ld+json',
|
||||
},
|
||||
body: mappedPayload,
|
||||
signal: opts.signal,
|
||||
}));
|
||||
})).then(normalizeModelType);
|
||||
}
|
||||
|
||||
export function updateModelType(id: string, payload: Partial<ModelTypePayload>, opts: { signal?: AbortSignal } = {}) {
|
||||
const requestFetch = useRequestFetch();
|
||||
const mappedPayload = mapStructureToSkeleton(payload);
|
||||
return requestFetch<ModelType>(`${ENDPOINT}/${id}`, createOptions({
|
||||
method: 'PATCH',
|
||||
body: payload,
|
||||
headers: {
|
||||
'Content-Type': 'application/merge-patch+json',
|
||||
Accept: 'application/ld+json',
|
||||
},
|
||||
body: mappedPayload,
|
||||
signal: opts.signal,
|
||||
}));
|
||||
})).then(normalizeModelType);
|
||||
}
|
||||
|
||||
export function deleteModelType(id: string, opts: { signal?: AbortSignal } = {}) {
|
||||
@@ -179,5 +232,5 @@ export function getModelType(id: string, opts: { signal?: AbortSignal } = {}) {
|
||||
return requestFetch<ModelType>(`${ENDPOINT}/${id}`, createOptions({
|
||||
method: 'GET',
|
||||
signal: opts.signal,
|
||||
}));
|
||||
})).then(normalizeModelType);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user