fix(modeles): normaliser structure et champs perso
This commit is contained in:
@@ -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