chore: retire legacy model catalogs
This commit is contained in:
@@ -4,6 +4,11 @@ import {
|
||||
type ComponentModelPiece,
|
||||
type ComponentModelStructure,
|
||||
type ComponentModelStructureNode,
|
||||
type PieceModelCustomField,
|
||||
type PieceModelStructure,
|
||||
type PieceModelStructureEditorField,
|
||||
type PieceModelStructureForEditor,
|
||||
createEmptyPieceModelStructure,
|
||||
} from './types/inventory'
|
||||
|
||||
export const isPlainObject = (value: unknown): value is Record<string, unknown> => {
|
||||
@@ -381,3 +386,126 @@ export const formatStructurePreview = (structure: any) => {
|
||||
if (stats.subcomponents) segments.push(`${stats.subcomponents} sous-composant(s)`)
|
||||
return segments.join(' • ')
|
||||
}
|
||||
|
||||
export const defaultPieceStructure = (): PieceModelStructure => ({
|
||||
...createEmptyPieceModelStructure(),
|
||||
})
|
||||
|
||||
const ensurePieceStructureShape = (input: any): PieceModelStructure => {
|
||||
const base = createEmptyPieceModelStructure()
|
||||
if (!isPlainObject(input)) {
|
||||
return base
|
||||
}
|
||||
|
||||
const clone: PieceModelStructure = {
|
||||
...base,
|
||||
customFields: Array.isArray((input as any).customFields) ? (input as any).customFields : [],
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(input as Record<string, unknown>)) {
|
||||
if (key === 'customFields') {
|
||||
continue
|
||||
}
|
||||
clone[key] = value
|
||||
}
|
||||
|
||||
return clone
|
||||
}
|
||||
|
||||
export const clonePieceStructure = (input: any): PieceModelStructure => {
|
||||
try {
|
||||
const cloned = JSON.parse(JSON.stringify(input ?? defaultPieceStructure()))
|
||||
return ensurePieceStructureShape(cloned)
|
||||
} catch (error) {
|
||||
return defaultPieceStructure()
|
||||
}
|
||||
}
|
||||
|
||||
const sanitizePieceCustomFields = (fields: any[]): PieceModelCustomField[] => {
|
||||
if (!Array.isArray(fields)) {
|
||||
return []
|
||||
}
|
||||
|
||||
return fields
|
||||
.map((field) => {
|
||||
const name = typeof field?.name === 'string' ? field.name.trim() : ''
|
||||
if (!name) {
|
||||
return null
|
||||
}
|
||||
|
||||
const type = typeof field?.type === 'string' && field.type ? field.type : 'text'
|
||||
const required = !!field?.required
|
||||
|
||||
let options: string[] | undefined
|
||||
if (type === 'select') {
|
||||
const rawOptions = typeof field?.optionsText === 'string'
|
||||
? field.optionsText
|
||||
: Array.isArray(field?.options)
|
||||
? field.options.join('\n')
|
||||
: ''
|
||||
const parsed = rawOptions
|
||||
.split(/\r?\n/)
|
||||
.map((option) => option.trim())
|
||||
.filter((option) => option.length > 0)
|
||||
options = parsed.length > 0 ? parsed : undefined
|
||||
}
|
||||
|
||||
const result: PieceModelCustomField = { name, type, required }
|
||||
if (options) {
|
||||
result.options = options
|
||||
}
|
||||
return result
|
||||
})
|
||||
.filter((field): field is PieceModelCustomField => !!field)
|
||||
}
|
||||
|
||||
export const normalizePieceStructureForSave = (input: any): PieceModelStructure => {
|
||||
const source = clonePieceStructure(input)
|
||||
return {
|
||||
...Object.fromEntries(
|
||||
Object.entries(source).filter(([key]) => key !== 'customFields'),
|
||||
),
|
||||
customFields: sanitizePieceCustomFields(source.customFields),
|
||||
}
|
||||
}
|
||||
|
||||
const hydratePieceCustomFields = (fields: any[]): PieceModelStructureEditorField[] => {
|
||||
if (!Array.isArray(fields)) {
|
||||
return []
|
||||
}
|
||||
|
||||
return fields.map((field) => ({
|
||||
name: field?.name ?? '',
|
||||
type: field?.type ?? 'text',
|
||||
required: !!field?.required,
|
||||
options: Array.isArray(field?.options) ? field.options : undefined,
|
||||
optionsText: Array.isArray(field?.options) ? field.options.join('\n') : (field?.optionsText ?? ''),
|
||||
}))
|
||||
}
|
||||
|
||||
export const hydratePieceStructureForEditor = (input: any): PieceModelStructureForEditor => {
|
||||
const source = clonePieceStructure(input)
|
||||
const payload: PieceModelStructureForEditor = {
|
||||
...Object.fromEntries(
|
||||
Object.entries(source).filter(([key]) => key !== 'customFields'),
|
||||
),
|
||||
customFields: hydratePieceCustomFields(source.customFields),
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
export const formatPieceStructurePreview = (structure: any) => {
|
||||
if (!structure || typeof structure !== 'object') {
|
||||
return 'Aucun champ personnalisé'
|
||||
}
|
||||
|
||||
const customFields = Array.isArray((structure as any).customFields)
|
||||
? (structure as any).customFields.length
|
||||
: 0
|
||||
|
||||
if (!customFields) {
|
||||
return 'Aucun champ personnalisé'
|
||||
}
|
||||
|
||||
return `${customFields} champ(s) personnalisé(s)`
|
||||
}
|
||||
|
||||
@@ -27,6 +27,29 @@ export interface ComponentModelStructure extends ComponentModelStructureNode {
|
||||
pieces: ComponentModelPiece[]
|
||||
}
|
||||
|
||||
export type PieceModelCustomFieldType = ComponentModelCustomFieldType
|
||||
|
||||
export interface PieceModelCustomField {
|
||||
name: string
|
||||
type: PieceModelCustomFieldType
|
||||
required: boolean
|
||||
options?: string[]
|
||||
}
|
||||
|
||||
export interface PieceModelStructure {
|
||||
customFields: PieceModelCustomField[]
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export interface PieceModelStructureEditorField extends PieceModelCustomField {
|
||||
optionsText: string
|
||||
}
|
||||
|
||||
export interface PieceModelStructureForEditor {
|
||||
customFields: PieceModelStructureEditorField[]
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
const FIELD_TYPES: ComponentModelCustomFieldType[] = ['text', 'number', 'select', 'boolean', 'date']
|
||||
|
||||
const isPlainObject = (value: unknown): value is Record<string, unknown> => {
|
||||
@@ -219,3 +242,7 @@ export const createEmptyComponentModelStructure = (): ComponentModelStructure =>
|
||||
pieces: [],
|
||||
subcomponents: [],
|
||||
})
|
||||
|
||||
export const createEmptyPieceModelStructure = (): PieceModelStructure => ({
|
||||
customFields: [],
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user