refactor(custom-fields) : unify 3 parallel implementations into 1 module

Replace ~2900 lines across 9 files with ~400 lines in 2 files:
- shared/utils/customFields.ts (types + pure helpers)
- composables/useCustomFieldInputs.ts (reactive composable)

Migrated all consumers:
- Backend: add defaultValue to API Platform serialization groups
- Standalone pages: component edit/create, piece edit/create, product edit/create/detail
- Machine page: MachineCustomFieldsCard, MachineInfoCard, useMachineDetailCustomFields
- Hierarchy: ComponentItem, PieceItem
- Shared: CustomFieldDisplay, CustomFieldInputGrid
- Category editor: componentStructure.ts

Deleted:
- entityCustomFieldLogic.ts (335 lines)
- customFieldUtils.ts (440 lines)
- customFieldFormUtils.ts (404 lines)
- useEntityCustomFields.ts (181 lines)
- customFieldFormUtils.test.ts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-04 13:09:27 +02:00
parent 6d3cbf9157
commit c4ed8c8edc
25 changed files with 861 additions and 2279 deletions

View File

@@ -335,13 +335,8 @@ import {
} from '~/shared/utils/documentDisplayUtils'
import { useEntityDocuments } from '~/composables/useEntityDocuments'
import { useEntityProductDisplay } from '~/composables/useEntityProductDisplay'
import { useEntityCustomFields } from '~/composables/useEntityCustomFields'
import {
mergeFieldDefinitionsWithValues,
dedupeMergedFields,
resolveCustomFieldId,
resolveFieldId,
} from '~/shared/utils/entityCustomFieldLogic'
import { useCustomFields } from '~/composables/useCustomFields'
import { mergeDefinitionsWithValues } from '~/shared/utils/customFields'
const props = defineProps({
component: { type: Object, required: true },
@@ -377,25 +372,81 @@ const {
} = useEntityProductDisplay({ entity: () => props.component })
const {
displayedCustomFields,
updateCustomField: updateComponentCustomField,
} = useEntityCustomFields({ entity: () => props.component, entityType: 'composant' })
updateCustomFieldValue: updateCustomFieldValueApi,
upsertCustomFieldValue,
} = useCustomFields()
const { showSuccess, showError } = useToast()
// Parent already pre-merges standalone custom fields into props.component.customFields
const displayedCustomFields = computed(() => {
const fields = props.component?.customFields
return Array.isArray(fields) ? fields.filter((f) => !f.machineContextOnly) : []
})
const updateComponentCustomField = async (field) => {
if (!field || field.readOnly) return
const e = props.component
const fieldValueId = field.customFieldValueId
if (fieldValueId) {
const result = await updateCustomFieldValueApi(fieldValueId, { value: field.value ?? '' })
if (result.success) {
showSuccess(`Champ "${field.name}" mis à jour avec succès`)
} else {
showError(`Erreur lors de la mise à jour du champ "${field.name}"`)
}
return
}
if (!e?.id) {
showError('Impossible de créer la valeur pour ce champ')
return
}
const metadata = field.customFieldId ? undefined : {
customFieldName: field.name,
customFieldType: field.type,
customFieldRequired: field.required,
customFieldOptions: field.options,
}
const result = await upsertCustomFieldValue(
field.customFieldId,
'composant',
e.id,
field.value ?? '',
metadata,
)
if (result.success) {
const newValue = result.data
if (newValue?.id) {
field.customFieldValueId = newValue.id
field.value = newValue.value ?? field.value ?? ''
if (newValue.customField?.id) {
field.customFieldId = newValue.customField.id
}
}
showSuccess(`Champ "${field.name}" créé avec succès`)
} else {
showError(`Erreur lors de la sauvegarde du champ "${field.name}"`)
}
}
// Context fields are NOT pre-merged — merge locally
const mergedContextFields = computed(() => {
const definitions = props.component?.contextCustomFields ?? []
const values = props.component?.contextCustomFieldValues ?? []
if (!definitions.length && !values.length) return []
return dedupeMergedFields(
mergeFieldDefinitionsWithValues(definitions, values),
)
return mergeDefinitionsWithValues(definitions, values)
})
const queueContextCustomFieldUpdate = (field, value) => {
const linkId = props.component?.linkId
if (!linkId || !field) return
const customFieldId = resolveCustomFieldId(field)
const customFieldValueId = resolveFieldId(field)
const customFieldId = field.customFieldId
const customFieldValueId = field.customFieldValueId
if (!customFieldId && !customFieldValueId) return
field.value = value
@@ -405,7 +456,7 @@ const queueContextCustomFieldUpdate = (field, value) => {
fieldId: customFieldId,
customFieldValueId,
value: value ?? '',
fieldName: field.name || field.customField?.name || 'Champ contextuel',
fieldName: field.name || 'Champ contextuel',
})
}

View File

@@ -319,16 +319,10 @@ import {
uniqueConstructeurIds,
parseConstructeurLinksFromApi,
} from '~/shared/constructeurUtils'
import {
resolveFieldId,
resolveFieldReadOnly,
resolveCustomFieldId,
mergeFieldDefinitionsWithValues,
dedupeMergedFields,
} from '~/shared/utils/entityCustomFieldLogic'
import { mergeDefinitionsWithValues } from '~/shared/utils/customFields'
import { useCustomFields } from '~/composables/useCustomFields'
import { useEntityDocuments } from '~/composables/useEntityDocuments'
import { useEntityProductDisplay } from '~/composables/useEntityProductDisplay'
import { useEntityCustomFields } from '~/composables/useEntityCustomFields'
const props = defineProps({
piece: { type: Object, required: true },
@@ -392,25 +386,81 @@ const {
} = useEntityProductDisplay({ entity: () => props.piece, selectedProduct })
const {
displayedCustomFields,
updateCustomField,
} = useEntityCustomFields({ entity: () => props.piece, entityType: 'piece' })
updateCustomFieldValue: updateCustomFieldValueApi,
upsertCustomFieldValue,
} = useCustomFields()
const { showSuccess, showError } = useToast()
// Parent already pre-merges standalone custom fields into props.piece.customFields
const displayedCustomFields = computed(() => {
const fields = props.piece?.customFields
return Array.isArray(fields) ? fields.filter((f) => !f.machineContextOnly) : []
})
const updateCustomField = async (field) => {
if (!field || field.readOnly) return
const e = props.piece
const fieldValueId = field.customFieldValueId
if (fieldValueId) {
const result = await updateCustomFieldValueApi(fieldValueId, { value: field.value ?? '' })
if (result.success) {
showSuccess(`Champ "${field.name}" mis à jour avec succès`)
} else {
showError(`Erreur lors de la mise à jour du champ "${field.name}"`)
}
return
}
if (!e?.id) {
showError('Impossible de créer la valeur pour ce champ')
return
}
const metadata = field.customFieldId ? undefined : {
customFieldName: field.name,
customFieldType: field.type,
customFieldRequired: field.required,
customFieldOptions: field.options,
}
const result = await upsertCustomFieldValue(
field.customFieldId,
'piece',
e.id,
field.value ?? '',
metadata,
)
if (result.success) {
const newValue = result.data
if (newValue?.id) {
field.customFieldValueId = newValue.id
field.value = newValue.value ?? field.value ?? ''
if (newValue.customField?.id) {
field.customFieldId = newValue.customField.id
}
}
showSuccess(`Champ "${field.name}" créé avec succès`)
} else {
showError(`Erreur lors de la sauvegarde du champ "${field.name}"`)
}
}
// Context fields are NOT pre-merged — merge locally
const mergedContextFields = computed(() => {
const definitions = props.piece?.contextCustomFields ?? []
const values = props.piece?.contextCustomFieldValues ?? []
if (!definitions.length && !values.length) return []
return dedupeMergedFields(
mergeFieldDefinitionsWithValues(definitions, values),
)
return mergeDefinitionsWithValues(definitions, values)
})
const queueContextCustomFieldUpdate = (field, value) => {
const linkId = props.piece?.linkId
if (!linkId || !field) return
const customFieldId = resolveCustomFieldId(field)
const customFieldValueId = resolveFieldId(field)
const customFieldId = field.customFieldId
const customFieldValueId = field.customFieldValueId
if (!customFieldId && !customFieldValueId) return
field.value = value
@@ -420,7 +470,7 @@ const queueContextCustomFieldUpdate = (field, value) => {
fieldId: customFieldId,
customFieldValueId,
value: value ?? '',
fieldName: field.name || field.customField?.name || 'Champ contextuel',
fieldName: field.name || 'Champ contextuel',
})
}
@@ -544,8 +594,8 @@ const handleProductChange = async (value) => {
// --- Custom field event handlers ---
const handleCustomFieldInput = (field, value) => {
if (resolveFieldReadOnly(field)) return
const fieldValueId = resolveFieldId(field)
if (field.readOnly) return
const fieldValueId = field.customFieldValueId
if (!fieldValueId) return
const fieldValue = props.piece.customFieldValues?.find((fv) => fv.id === fieldValueId)
if (fieldValue) fieldValue.value = value
@@ -553,7 +603,7 @@ const handleCustomFieldInput = (field, value) => {
const handleCustomFieldBlur = async (field) => {
await updateCustomField(field)
const cfId = field?.customFieldId || field?.customField?.id || null
const cfId = field?.customFieldId || null
if (cfId || field?.customFieldValueId) {
emit('custom-field-update', {
fieldId: cfId,

View File

@@ -9,15 +9,15 @@
<div :class="layoutClass">
<div
v-for="(field, index) in fields"
:key="resolveFieldKey(field, index)"
:key="fieldKey(field, index)"
class="form-control"
>
<label class="label">
<span class="label-text text-sm">{{
resolveFieldName(field)
field.name
}}</span>
<span
v-if="resolveFieldRequired(field)"
v-if="field.required"
class="label-text-alt text-error"
>*</span>
</label>
@@ -26,32 +26,32 @@
<template v-if="isFieldEditable(field)">
<!-- Champ de type TEXT -->
<input
v-if="resolveFieldType(field) === 'text'"
v-if="field.type === 'text'"
:value="field.value ?? ''"
type="text"
class="input input-bordered input-sm"
:required="resolveFieldRequired(field)"
:required="field.required"
@input="onInput(field, ($event.target as HTMLInputElement).value)"
@blur="onBlur(field)"
>
<!-- Champ de type NUMBER -->
<input
v-else-if="resolveFieldType(field) === 'number'"
v-else-if="field.type === 'number'"
:value="field.value ?? ''"
type="number"
class="input input-bordered input-sm"
:required="resolveFieldRequired(field)"
:required="field.required"
@input="onInput(field, ($event.target as HTMLInputElement).value)"
@blur="onBlur(field)"
>
<!-- Champ de type SELECT -->
<select
v-else-if="resolveFieldType(field) === 'select'"
v-else-if="field.type === 'select'"
:value="field.value ?? ''"
class="select select-bordered select-sm"
:required="resolveFieldRequired(field)"
:required="field.required"
@change="onInput(field, ($event.target as HTMLSelectElement).value)"
@blur="onBlur(field)"
>
@@ -59,7 +59,7 @@
Sélectionner...
</option>
<option
v-for="option in resolveFieldOptions(field)"
v-for="option in field.options"
:key="option"
:value="option"
>
@@ -69,7 +69,7 @@
<!-- Champ de type BOOLEAN -->
<div
v-else-if="resolveFieldType(field) === 'boolean'"
v-else-if="field.type === 'boolean'"
class="flex items-center gap-2"
>
<input
@@ -85,21 +85,21 @@
<!-- Champ de type DATE -->
<input
v-else-if="resolveFieldType(field) === 'date'"
v-else-if="field.type === 'date'"
:value="field.value ?? ''"
type="date"
class="input input-bordered input-sm"
:required="resolveFieldRequired(field)"
:required="field.required"
@input="onInput(field, ($event.target as HTMLInputElement).value)"
@blur="onBlur(field)"
>
<!-- Champ de type TEXTAREA -->
<textarea
v-else-if="resolveFieldType(field) === 'textarea'"
v-else-if="field.type === 'textarea'"
:value="field.value ?? ''"
class="textarea textarea-bordered textarea-sm"
:required="resolveFieldRequired(field)"
:required="field.required"
@input="onInput(field, ($event.target as HTMLTextAreaElement).value)"
@blur="onBlur(field)"
/>
@@ -110,7 +110,7 @@
:value="field.value ?? ''"
type="text"
class="input input-bordered input-sm"
:required="resolveFieldRequired(field)"
:required="field.required"
@input="onInput(field, ($event.target as HTMLInputElement).value)"
@blur="onBlur(field)"
>
@@ -119,7 +119,7 @@
<!-- Mode lecture seule -->
<template v-else>
<div class="input input-bordered input-sm bg-base-200">
{{ formatFieldDisplayValue(field) }}
{{ formatValueForDisplay(field) }}
</div>
</template>
</div>
@@ -128,18 +128,10 @@
</template>
<script setup lang="ts">
import {
resolveFieldKey,
resolveFieldName,
resolveFieldType,
resolveFieldOptions,
resolveFieldRequired,
resolveFieldReadOnly,
formatFieldDisplayValue,
} from '~/shared/utils/entityCustomFieldLogic'
import { fieldKey, formatValueForDisplay, type CustomFieldInput } from '~/shared/utils/customFields'
const props = defineProps<{
fields: any[]
fields: CustomFieldInput[]
isEditMode: boolean
columns?: 1 | 2
title?: string
@@ -150,8 +142,8 @@ const props = defineProps<{
}>()
const emit = defineEmits<{
'field-input': [field: any, value: string]
'field-blur': [field: any]
'field-input': [field: CustomFieldInput, value: string]
'field-blur': [field: CustomFieldInput]
}>()
const layoutClass = computed(() =>
@@ -170,16 +162,16 @@ const containerClass = computed(() =>
const editable = computed(() => props.editable ?? true)
const emitBlur = computed(() => props.emitBlur ?? true)
function isFieldEditable(field: any) {
return props.isEditMode && editable.value && !resolveFieldReadOnly(field)
function isFieldEditable(field: CustomFieldInput) {
return props.isEditMode && editable.value && !field.readOnly
}
function onInput(field: any, value: string) {
function onInput(field: CustomFieldInput, value: string) {
field.value = value
emit('field-input', field, value)
}
function onBooleanChange(field: any, checked: boolean) {
function onBooleanChange(field: CustomFieldInput, checked: boolean) {
const value = checked ? 'true' : 'false'
field.value = value
emit('field-input', field, value)
@@ -188,7 +180,7 @@ function onBooleanChange(field: any, checked: boolean) {
}
}
function onBlur(field: any) {
function onBlur(field: CustomFieldInput) {
if (emitBlur.value) {
emit('field-blur', field)
}

View File

@@ -74,7 +74,7 @@
</template>
<script setup lang="ts">
import { fieldKey, type CustomFieldInput } from '~/shared/utils/customFieldFormUtils'
import { fieldKey, type CustomFieldInput } from '~/shared/utils/customFields'
defineProps<{
fields: CustomFieldInput[]

View File

@@ -25,7 +25,7 @@
<span class="label-text text-sm">{{ field.name }}</span>
</label>
<div class="input input-bordered input-sm bg-base-200">
{{ formatCustomFieldValue(field) }}
{{ formatValueForDisplay(field) }}
</div>
</div>
</div>
@@ -180,7 +180,7 @@
<script setup lang="ts">
import IconLucideTrash from '~icons/lucide/trash'
import { formatCustomFieldValue } from '~/shared/utils/customFieldUtils'
import { formatValueForDisplay } from '~/shared/utils/customFields'
defineProps<{
customFields: any[]

View File

@@ -153,7 +153,7 @@
</template>
<template v-else>
<div class="input input-bordered input-sm bg-base-200">
{{ formatCustomFieldValue(field) }}
{{ formatValueForDisplay(field) }}
</div>
</template>
</div>
@@ -182,7 +182,7 @@ import { watch } from 'vue'
import ConstructeurSelect from '~/components/ConstructeurSelect.vue'
import ConstructeurLinksTable from '~/components/ConstructeurLinksTable.vue'
import MachineCustomFieldDefEditor from '~/components/machine/MachineCustomFieldDefEditor.vue'
import { formatCustomFieldValue } from '~/shared/utils/customFieldUtils'
import { formatValueForDisplay } from '~/shared/utils/customFields'
import { useMachineCustomFieldDefs } from '~/composables/useMachineCustomFieldDefs'
import type { ConstructeurLinkEntry } from '~/shared/constructeurUtils'

View File

@@ -17,15 +17,9 @@ import { useProductTypes } from '~/composables/useProductTypes'
import { useApi } from '~/composables/useApi'
import { useToast } from '~/composables/useToast'
import { humanizeError } from '~/shared/utils/errorMessages'
import { useCustomFields } from '~/composables/useCustomFields'
import { useDocuments } from '~/composables/useDocuments'
import { formatStructurePreview, normalizeStructureForEditor } from '~/shared/modelUtils'
import {
type CustomFieldInput,
normalizeCustomFieldInputs,
requiredCustomFieldsFilled as _requiredCustomFieldsFilled,
saveCustomFieldValues as _saveCustomFieldValues,
} from '~/shared/utils/customFieldFormUtils'
import { useCustomFieldInputs, type CustomFieldInput } from '~/composables/useCustomFieldInputs'
import { useConstructeurLinks } from '~/composables/useConstructeurLinks'
import { uniqueConstructeurIds, constructeurIdsFromLinks } from '~/shared/constructeurUtils'
import type { ConstructeurLinkEntry } from '~/shared/constructeurUtils'
@@ -77,7 +71,6 @@ export function useComponentCreate() {
loading: productsLoading,
} = useProducts()
const toast = useToast()
const { upsertCustomFieldValue, updateCustomFieldValue } = useCustomFields()
const { uploadDocuments } = useDocuments()
const { syncLinks } = useConstructeurLinks()
const { canEdit } = usePermissions()
@@ -98,7 +91,20 @@ export function useComponentCreate() {
const constructeurLinks = ref<ConstructeurLinkEntry[]>([])
const constructeurIdsFromForm = computed(() => constructeurIdsFromLinks(constructeurLinks.value))
const lastSuggestedName = ref('')
const customFieldInputs = ref<CustomFieldInput[]>([])
const createdComponentId = ref<string | null>(null)
const {
fields: customFieldInputs,
requiredFilled: requiredCustomFieldsFilled,
saveAll: saveAllCustomFields,
refresh: refreshCustomFieldInputs,
} = useCustomFieldInputs({
definitions: computed(() => selectedTypeStructure.value?.customFields ?? []),
values: computed(() => []),
entityType: 'composant',
entityId: createdComponentId,
})
const structureAssignments = ref<StructureAssignmentNode | null>(null)
const selectedDocuments = ref<File[]>([])
const uploadingDocuments = ref(false)
@@ -165,10 +171,6 @@ export function useComponentCreate() {
return isAssignmentNodeComplete(structureAssignments.value, true)
})
const requiredCustomFieldsFilled = computed(() =>
_requiredCustomFieldsFilled(customFieldInputs.value),
)
const canSubmit = computed(() => Boolean(
canEdit.value
&& selectedType.value
@@ -225,7 +227,6 @@ export function useComponentCreate() {
watch(selectedType, (type) => {
if (!type) {
clearCreationForm()
customFieldInputs.value = []
structureAssignments.value = null
return
}
@@ -233,7 +234,8 @@ export function useComponentCreate() {
creationForm.name = type.name
}
lastSuggestedName.value = creationForm.name
customFieldInputs.value = normalizeCustomFieldInputs(selectedTypeStructure.value)
// useCustomFieldInputs auto-refreshes via its watcher on definitions
refreshCustomFieldInputs()
structureAssignments.value = initializeStructureAssignments(selectedTypeStructure.value)
})
@@ -323,12 +325,11 @@ export function useComponentCreate() {
const result = await createComposant(payload)
if (result.success) {
const createdComponent = result.data as Record<string, any>
await _saveCustomFieldValues(
'composant',
createdComponent.id,
[createdComponent?.typeComposant?.structure?.customFields],
{ customFieldInputs, upsertCustomFieldValue, updateCustomFieldValue, toast },
)
createdComponentId.value = createdComponent.id
const failedFields = await saveAllCustomFields()
if (failedFields.length) {
toast.showError(`Erreur sur les champs : ${failedFields.join(', ')}`)
}
if (selectedDocuments.value.length && result.data?.id) {
uploadingDocuments.value = true
const uploadResult = await uploadDocuments(

View File

@@ -6,7 +6,6 @@ import { usePieceTypes } from '~/composables/usePieceTypes'
import { useProductTypes } from '~/composables/useProductTypes'
import { usePieces } from '~/composables/usePieces'
import { useProducts } from '~/composables/useProducts'
import { useCustomFields } from '~/composables/useCustomFields'
import { useApi } from '~/composables/useApi'
import { useToast } from '~/composables/useToast'
import { extractRelationId } from '~/shared/apiRelations'
@@ -29,12 +28,7 @@ import {
import type { ComponentModelStructure } from '~/shared/types/inventory'
import type { ModelType } from '~/services/modelTypes'
import { canPreviewDocument } from '~/utils/documentPreview'
import {
type CustomFieldInput,
buildCustomFieldInputs,
requiredCustomFieldsFilled as _requiredCustomFieldsFilled,
saveCustomFieldValues as _saveCustomFieldValues,
} from '~/shared/utils/customFieldFormUtils'
import { useCustomFieldInputs, type CustomFieldInput } from '~/composables/useCustomFieldInputs'
import { collectStructureSelections } from '~/shared/utils/structureSelectionUtils'
interface ComponentCatalogType extends ModelType {
@@ -64,7 +58,6 @@ export function useComponentEdit(componentId: string) {
const { products } = useProducts()
const { ensureConstructeurs } = useConstructeurs()
const { fetchLinks, syncLinks } = useConstructeurLinks()
const { upsertCustomFieldValue, updateCustomFieldValue } = useCustomFields()
const toast = useToast()
const { loadDocumentsByComponent, uploadDocuments, deleteDocument } = useDocuments()
const {
@@ -96,7 +89,23 @@ export function useComponentEdit(componentId: string) {
const originalConstructeurLinks = ref<ConstructeurLinkEntry[]>([])
const constructeurIdsFromForm = computed(() => constructeurIdsFromLinks(constructeurLinks.value))
const customFieldInputs = ref<CustomFieldInput[]>([])
const {
fields: customFieldInputs,
requiredFilled: requiredCustomFieldsFilled,
saveAll: saveAllCustomFields,
refresh: refreshCustomFieldInputs,
} = useCustomFieldInputs({
definitions: computed(() => selectedTypeStructure.value?.customFields ?? []),
values: computed(() => component.value?.customFieldValues ?? []),
entityType: 'composant',
entityId: computed(() => component.value?.id ?? null),
onValueCreated: (newValue) => {
if (component.value && Array.isArray(component.value.customFieldValues)) {
component.value.customFieldValues.push(newValue)
}
},
})
const fetchedPieceTypeMap = ref<Record<string, string>>({})
const pieceTypeLabelMap = computed(() =>
buildTypeLabelMap(pieceTypes.value, fetchedPieceTypeMap.value),
@@ -207,19 +216,6 @@ export function useComponentEdit(componentId: string) {
return structure ? normalizeStructureForEditor(structure) : null
})
const refreshCustomFieldInputs = (
structureOverride?: ComponentModelStructure | null,
valuesOverride?: any[] | null,
) => {
const structure = structureOverride ?? selectedTypeStructure.value ?? null
const values = valuesOverride ?? component.value?.customFieldValues ?? null
customFieldInputs.value = buildCustomFieldInputs(structure, values)
}
const requiredCustomFieldsFilled = computed(() =>
_requiredCustomFieldsFilled(customFieldInputs.value),
)
const canSubmit = computed(() => Boolean(
canEdit.value
&& component.value
@@ -239,8 +235,7 @@ export function useComponentEdit(componentId: string) {
component.value = result.data
componentDocuments.value = Array.isArray(result.data?.documents) ? result.data.documents : []
const customValues = Array.isArray(result.data?.customFieldValues) ? result.data.customFieldValues : []
refreshCustomFieldInputs(undefined, customValues)
// The watcher on useCustomFieldInputs will auto-refresh when component.value changes
loadHistory(result.data.id).catch(() => {})
}
@@ -392,14 +387,10 @@ export function useComponentEdit(componentId: string) {
const result = await updateComposant(component.value.id, payload)
if (result.success && result.data) {
const updatedComponent = result.data as Record<string, any>
await _saveCustomFieldValues(
'composant',
updatedComponent.id,
[
updatedComponent?.typeComposant?.structure?.customFields,
],
{ customFieldInputs, upsertCustomFieldValue, updateCustomFieldValue, toast },
)
const failedFields = await saveAllCustomFields()
if (failedFields.length) {
toast.showError(`Erreur sur les champs : ${failedFields.join(', ')}`)
}
// Save slot edits
const slotPromises: Promise<any>[] = []
@@ -499,7 +490,7 @@ export function useComponentEdit(componentId: string) {
initialized.value = true
}
refreshCustomFieldInputs(selectedTypeStructure.value ?? currentStructure, currentComponent.customFieldValues)
// useCustomFieldInputs auto-refreshes via its watcher on definitions + values
},
{ immediate: true },
)

View File

@@ -0,0 +1,205 @@
/**
* Unified reactive custom field management composable.
*
* Replaces: useEntityCustomFields.ts, custom field parts of useMachineDetailCustomFields.ts,
* and inline custom field logic in useComponentEdit/useComponentCreate/usePieceEdit.
*
* DESIGN NOTE: Uses an internal mutable `ref` (not a `computed`) so that
* save operations can update `customFieldValueId` in place without being
* overwritten on the next reactivity cycle. Call `refresh()` to re-merge
* from the source definitions + values (e.g. after fetching fresh data).
*/
import { ref, watch, computed, type MaybeRef, toValue } from 'vue'
import { useCustomFields } from '~/composables/useCustomFields'
import { useToast } from '~/composables/useToast'
import {
mergeDefinitionsWithValues,
filterByContext,
formatValueForSave,
shouldPersist,
requiredFieldsFilled,
type CustomFieldDefinition,
type CustomFieldValue,
type CustomFieldInput,
} from '~/shared/utils/customFields'
export type { CustomFieldDefinition, CustomFieldValue, CustomFieldInput }
export type CustomFieldEntityType =
| 'machine'
| 'composant'
| 'piece'
| 'product'
| 'machineComponentLink'
| 'machinePieceLink'
export interface UseCustomFieldInputsOptions {
/** Custom field definitions (from ModelType structure or machine.customFields) */
definitions: MaybeRef<any[]>
/** Persisted custom field values (from entity.customFieldValues or link.contextCustomFieldValues) */
values: MaybeRef<any[]>
/** Entity type for API upsert calls */
entityType: CustomFieldEntityType
/** Entity ID for API upsert calls */
entityId: MaybeRef<string | null>
/** Filter context: 'standalone' hides machineContextOnly, 'machine' shows only machineContextOnly */
context?: 'standalone' | 'machine'
/** Optional callback to update the source values array after a save (keeps parent reactive state in sync) */
onValueCreated?: (newValue: { id: string; value: string; customField: any }) => void
}
export function useCustomFieldInputs(options: UseCustomFieldInputsOptions) {
const { entityType, context } = options
const {
updateCustomFieldValue: updateApi,
upsertCustomFieldValue,
} = useCustomFields()
const { showSuccess, showError } = useToast()
// Internal mutable state — NOT a computed, so save can mutate in place
const _allFields = ref<CustomFieldInput[]>([])
// Re-merge from source definitions + values
const refresh = () => {
const defs = toValue(options.definitions)
const vals = toValue(options.values)
_allFields.value = mergeDefinitionsWithValues(defs, vals)
}
// Auto-refresh when reactive sources change
watch(
() => [toValue(options.definitions), toValue(options.values)],
() => refresh(),
{ immediate: true, deep: true },
)
// Filtered by context (standalone vs machine)
const fields = computed<CustomFieldInput[]>(() => {
if (!context) return _allFields.value
return filterByContext(_allFields.value, context)
})
// Validation
const requiredFilled = computed(() => requiredFieldsFilled(fields.value))
// Build metadata for upsert when no customFieldId is available (legacy fallback)
const _buildMetadata = (field: CustomFieldInput) => ({
customFieldName: field.name,
customFieldType: field.type,
customFieldRequired: field.required,
customFieldOptions: field.options,
})
// Update a single field value
const update = async (field: CustomFieldInput): Promise<boolean> => {
const id = toValue(options.entityId)
if (!id) {
showError(`Impossible de sauvegarder le champ "${field.name}"`)
return false
}
const value = formatValueForSave(field)
// Update existing value
if (field.customFieldValueId) {
const result: any = await updateApi(field.customFieldValueId, { value })
if (result.success) {
showSuccess(`Champ "${field.name}" mis à jour`)
return true
}
showError(`Erreur lors de la mise à jour du champ "${field.name}"`)
return false
}
// Create new value via upsert — with metadata fallback when no ID
const metadata = field.customFieldId ? undefined : _buildMetadata(field)
const result: any = await upsertCustomFieldValue(
field.customFieldId,
entityType,
id,
value,
metadata,
)
if (result.success) {
// Mutate in place (safe — _allFields is a ref, not computed)
if (result.data?.id) {
field.customFieldValueId = result.data.id
}
if (result.data?.customField?.id) {
field.customFieldId = result.data.customField.id
}
// Notify parent to update its reactive source
if (options.onValueCreated && result.data) {
options.onValueCreated(result.data)
}
showSuccess(`Champ "${field.name}" enregistré`)
return true
}
showError(`Erreur lors de l'enregistrement du champ "${field.name}"`)
return false
}
// Save all fields that have values
const saveAll = async (): Promise<string[]> => {
const id = toValue(options.entityId)
if (!id) return ['(entity ID missing)']
const failed: string[] = []
for (const field of fields.value) {
if (!shouldPersist(field)) continue
const value = formatValueForSave(field)
if (field.customFieldValueId) {
const result: any = await updateApi(field.customFieldValueId, { value })
if (!result.success) failed.push(field.name)
continue
}
// Upsert with metadata fallback when no customFieldId
const metadata = field.customFieldId ? undefined : _buildMetadata(field)
const result: any = await upsertCustomFieldValue(
field.customFieldId,
entityType,
id,
value,
metadata,
)
if (result.success) {
if (result.data?.id) {
field.customFieldValueId = result.data.id
}
if (result.data?.customField?.id) {
field.customFieldId = result.data.customField.id
}
if (options.onValueCreated && result.data) {
options.onValueCreated(result.data)
}
} else {
failed.push(field.name)
}
}
return failed
}
return {
/** All merged fields filtered by context */
fields,
/** All merged fields (unfiltered) */
allFields: _allFields,
/** Whether all required fields have values */
requiredFilled,
/** Update a single field value via API */
update,
/** Save all fields with values, returns list of failed field names */
saveAll,
/** Re-merge from source definitions + values (call after fetching fresh data) */
refresh,
}
}

View File

@@ -1,181 +0,0 @@
/**
* Reactive custom field management for entity items (ComponentItem, PieceItem).
*
* Wraps the pure logic from entityCustomFieldLogic.ts with Vue reactivity,
* watchers, and API calls for updating/upserting custom field values.
*/
import { computed, watch } from 'vue'
import { useCustomFields } from '~/composables/useCustomFields'
import { useToast } from '~/composables/useToast'
import {
buildDefinitionSources,
buildCandidateCustomFields,
mergeFieldDefinitionsWithValues,
dedupeMergedFields,
ensureCustomFieldId,
resolveFieldId,
resolveFieldName,
resolveFieldType,
resolveFieldReadOnly,
resolveCustomFieldId,
buildCustomFieldMetadata,
} from '~/shared/utils/entityCustomFieldLogic'
export interface EntityCustomFieldsDeps {
entity: () => any
entityType: 'composant' | 'piece'
}
export function useEntityCustomFields(deps: EntityCustomFieldsDeps) {
const { entity, entityType } = deps
const {
updateCustomFieldValue: updateCustomFieldValueApi,
upsertCustomFieldValue,
} = useCustomFields()
const { showSuccess, showError } = useToast()
const definitionSources = computed(() =>
buildDefinitionSources(entity(), entityType),
)
const displayedCustomFields = computed(() =>
dedupeMergedFields(
mergeFieldDefinitionsWithValues(
definitionSources.value,
entity().customFieldValues,
),
).filter((field: any) => !field.machineContextOnly && !field.customField?.machineContextOnly),
)
const candidateCustomFields = computed(() =>
buildCandidateCustomFields(entity(), definitionSources.value),
)
// Watchers to ensure field IDs are resolved
watch(
candidateCustomFields,
() => {
const candidates = candidateCustomFields.value
;(displayedCustomFields.value || []).forEach((field: any) => {
if (field) ensureCustomFieldId(field, candidates)
})
},
{ immediate: true, deep: true },
)
watch(
displayedCustomFields,
(fields) => {
const candidates = candidateCustomFields.value
;(fields || []).forEach((field: any) => {
if (field) ensureCustomFieldId(field, candidates)
})
},
{ immediate: true, deep: true },
)
const updateCustomField = async (field: any) => {
if (!field || resolveFieldReadOnly(field)) return
const e = entity()
const fieldValueId = resolveFieldId(field)
// Update existing field value
if (fieldValueId) {
const result: any = await updateCustomFieldValueApi(fieldValueId, { value: field.value ?? '' })
if (result.success) {
const existingValue = e.customFieldValues?.find((v: any) => v.id === fieldValueId)
if (existingValue?.customField?.id) {
field.customFieldId = existingValue.customField.id
field.customField = existingValue.customField
}
showSuccess(`Champ "${resolveFieldName(field)}" mis à jour avec succès`)
} else {
showError(`Erreur lors de la mise à jour du champ "${resolveFieldName(field)}"`)
}
return
}
// Create new field value
const customFieldId = ensureCustomFieldId(field, candidateCustomFields.value)
const fieldName = resolveFieldName(field)
if (!e?.id) {
showError(`Impossible de créer la valeur pour ce champ`)
return
}
if (!customFieldId && (!fieldName || fieldName === 'Champ')) {
showError(`Impossible de créer la valeur pour ce champ`)
return
}
const metadata = customFieldId ? undefined : buildCustomFieldMetadata(field)
const result: any = await upsertCustomFieldValue(
customFieldId,
entityType,
e.id,
field.value ?? '',
metadata,
)
if (result.success) {
const newValue = result.data
if (newValue?.id) {
field.customFieldValueId = newValue.id
field.value = newValue.value ?? field.value ?? ''
if (newValue.customField?.id) {
field.customFieldId = newValue.customField.id
field.customField = newValue.customField
}
if (Array.isArray(e.customFieldValues)) {
const index = e.customFieldValues.findIndex((v: any) => v.id === newValue.id)
if (index !== -1) {
e.customFieldValues.splice(index, 1, newValue)
} else {
e.customFieldValues.push(newValue)
}
} else {
e.customFieldValues = [newValue]
}
}
showSuccess(`Champ "${resolveFieldName(field)}" créé avec succès`)
// Update definitions list
const definitions = Array.isArray(e.customFields) ? [...e.customFields] : []
const fieldIdentifier = ensureCustomFieldId(field, candidateCustomFields.value)
const existingIndex = definitions.findIndex((definition: any) => {
const definitionId = resolveCustomFieldId(definition)
if (fieldIdentifier && definitionId) return definitionId === fieldIdentifier
return definition?.name === resolveFieldName(field)
})
const updatedDefinition = {
...(existingIndex !== -1 ? definitions[existingIndex] : {}),
customFieldValueId: field.customFieldValueId,
customFieldId: fieldIdentifier,
name: resolveFieldName(field),
type: resolveFieldType(field),
required: field.required ?? false,
options: field.options ?? [],
value: field.value ?? '',
customField: field.customField ?? null,
}
if (existingIndex !== -1) {
definitions.splice(existingIndex, 1, updatedDefinition)
} else {
definitions.push(updatedDefinition)
}
e.customFields = definitions
} else {
showError(`Erreur lors de la sauvegarde du champ "${resolveFieldName(field)}"`)
}
}
return {
displayedCustomFields,
candidateCustomFields,
updateCustomField,
}
}

View File

@@ -8,14 +8,12 @@
import { ref, computed } from 'vue'
import { useCustomFields } from '~/composables/useCustomFields'
import { useToast } from '~/composables/useToast'
import { normalizeStructureForEditor } from '~/shared/modelUtils'
import {
shouldDisplayCustomField,
normalizeExistingCustomFieldDefinitions,
normalizeCustomFieldValueEntry,
mergeCustomFieldValuesWithDefinitions,
dedupeCustomFieldEntries,
} from '~/shared/utils/customFieldUtils'
mergeDefinitionsWithValues,
filterByContext,
hasDisplayableValue,
type CustomFieldInput,
} from '~/shared/utils/customFields'
import {
resolveConstructeurs,
uniqueConstructeurIds,
@@ -53,56 +51,23 @@ export function useMachineDetailCustomFields(deps: MachineDetailCustomFieldsDeps
const visibleMachineCustomFields = computed(() => {
const fields = Array.isArray(machineCustomFields.value) ? machineCustomFields.value : []
if (isEditMode.value) return fields
return fields.filter((field) => shouldDisplayCustomField(field))
return fields.filter((field) => hasDisplayableValue(field as unknown as CustomFieldInput))
})
// ---------------------------------------------------------------------------
// Transform helpers
// ---------------------------------------------------------------------------
const getStructureCustomFields = (structure: unknown): AnyRecord[] => {
if (!structure || typeof structure !== 'object') return []
const normalized = normalizeStructureForEditor(structure as any) as any
return Array.isArray(normalized?.customFields)
? (normalized.customFields as AnyRecord[])
: []
}
const transformCustomFields = (piecesData: AnyRecord[]): AnyRecord[] => {
return (piecesData || []).map((piece) => {
const typePiece = (piece.typePiece as AnyRecord) || {}
const normalizeStructureDefs = (structure: unknown) =>
structure ? normalizeStructureForEditor(structure as AnyRecord) : null
const normalizedStructureDefs = [
normalizeStructureDefs((piece.definition as AnyRecord)?.structure),
normalizeStructureDefs(typePiece.structure),
]
const valueEntries = [
...(Array.isArray(piece.customFieldValues) ? piece.customFieldValues : []),
...(Array.isArray(piece.customFields)
? (piece.customFields as AnyRecord[])
.map(normalizeCustomFieldValueEntry)
.filter((e) => e !== null)
: []),
...(Array.isArray(typePiece.customFieldValues)
? (typePiece.customFieldValues as AnyRecord[])
.map(normalizeCustomFieldValueEntry)
.filter((e) => e !== null)
: []),
]
const customFields = dedupeCustomFieldEntries(
mergeCustomFieldValuesWithDefinitions(
valueEntries,
normalizeExistingCustomFieldDefinitions(piece.customFields),
normalizeExistingCustomFieldDefinitions((piece.definition as AnyRecord)?.customFields),
normalizeExistingCustomFieldDefinitions((piece.typePiece as AnyRecord)?.customFields),
normalizeExistingCustomFieldDefinitions(typePiece.customFields),
...normalizedStructureDefs.map((def) => getStructureCustomFields(def)),
const customFields = filterByContext(
mergeDefinitionsWithValues(
typePiece.customFields ?? (piece.typePiece as AnyRecord)?.customFields ?? [],
piece.customFieldValues ?? [],
),
'standalone',
)
const constructeurIds = uniqueConstructeurIds(
@@ -159,43 +124,16 @@ export function useMachineDetailCustomFields(deps: MachineDetailCustomFieldsDeps
}
const transformComponentCustomFields = (componentsData: AnyRecord[]): AnyRecord[] => {
const normalizeStructureDefs = (structure: unknown) =>
structure ? normalizeStructureForEditor(structure as AnyRecord) : null
return (componentsData || []).map((component) => {
const type = (component.typeComposant as AnyRecord) || {}
const normalizedStructureDefs = [
normalizeStructureDefs((component.definition as AnyRecord)?.structure),
normalizeStructureDefs(type.structure),
]
const actualComponent = (component.originalComposant as AnyRecord) || component
const valueEntries = [
...(Array.isArray(component.customFieldValues) ? component.customFieldValues : []),
...(Array.isArray(component.customFields)
? (component.customFields as AnyRecord[])
.map(normalizeCustomFieldValueEntry)
.filter((e) => e !== null)
: []),
...(Array.isArray(actualComponent?.customFields)
? (actualComponent.customFields as AnyRecord[])
.map(normalizeCustomFieldValueEntry)
.filter((e) => e !== null)
: []),
]
const customFields = dedupeCustomFieldEntries(
mergeCustomFieldValuesWithDefinitions(
valueEntries,
normalizeExistingCustomFieldDefinitions(component.customFields),
normalizeExistingCustomFieldDefinitions((component.definition as AnyRecord)?.customFields),
normalizeExistingCustomFieldDefinitions((component.typeComposant as AnyRecord)?.customFields),
normalizeExistingCustomFieldDefinitions(type.customFields),
normalizeExistingCustomFieldDefinitions(actualComponent?.customFields),
...normalizedStructureDefs.map((def) => getStructureCustomFields(def)),
const customFields = filterByContext(
mergeDefinitionsWithValues(
type.customFields ?? [],
component.customFieldValues ?? actualComponent?.customFieldValues ?? [],
),
'standalone',
)
const piecesTransformed = component.pieces
@@ -271,21 +209,11 @@ export function useMachineDetailCustomFields(deps: MachineDetailCustomFieldsDeps
machineCustomFields.value = []
return
}
const valueEntries = [
...(Array.isArray(machine.value.customFieldValues) ? machine.value.customFieldValues : []),
...(Array.isArray(machine.value.customFields)
? (machine.value.customFields as AnyRecord[])
.map(normalizeCustomFieldValueEntry)
.filter((e) => e !== null)
: []),
]
const merged = dedupeCustomFieldEntries(
mergeCustomFieldValuesWithDefinitions(
valueEntries,
normalizeExistingCustomFieldDefinitions(machine.value.customFields),
),
).map((field: AnyRecord) => ({ ...field, readOnly: false }))
machineCustomFields.value = merged
const merged = mergeDefinitionsWithValues(
machine.value?.customFields ?? [],
machine.value?.customFieldValues ?? [],
)
machineCustomFields.value = merged.map(f => ({ ...f, readOnly: false }))
}
const setMachineCustomFieldValue = (field: AnyRecord, value: unknown) => {
@@ -302,7 +230,8 @@ export function useMachineDetailCustomFields(deps: MachineDetailCustomFieldsDeps
const updateMachineCustomField = async (field: AnyRecord) => {
if (!machine.value || !field) return
const { id: customFieldId, customFieldValueId } = field
const customFieldId = (field.customFieldId ?? field.id) as string | undefined
const customFieldValueId = field.customFieldValueId as string | undefined
const fieldLabel = (field.name as string) || 'Champ personnalisé'
try {
@@ -467,7 +396,8 @@ export function useMachineDetailCustomFields(deps: MachineDetailCustomFieldsDeps
)
for (const field of fieldsToSave) {
const { id: customFieldId, customFieldValueId } = field
const customFieldId = (field.customFieldId ?? field.id) as string | undefined
const customFieldValueId = field.customFieldValueId as string | undefined
try {
if (customFieldValueId) {

View File

@@ -2,7 +2,6 @@ import { computed, onMounted, reactive, ref, watch } from 'vue'
import { useRouter } from '#imports'
import { usePieceTypes } from '~/composables/usePieceTypes'
import { usePieces } from '~/composables/usePieces'
import { useCustomFields } from '~/composables/useCustomFields'
import { useApi } from '~/composables/useApi'
import { useToast } from '~/composables/useToast'
import { useDocuments } from '~/composables/useDocuments'
@@ -26,12 +25,7 @@ import {
collectNormalizedProductIds,
} from '~/shared/utils/pieceProductSelectionUtils'
import { getModelType } from '~/services/modelTypes'
import {
type CustomFieldInput,
buildCustomFieldInputs,
requiredCustomFieldsFilled as _requiredCustomFieldsFilled,
saveCustomFieldValues as _saveCustomFieldValues,
} from '~/shared/utils/customFieldFormUtils'
import { useCustomFieldInputs, type CustomFieldInput } from '~/composables/useCustomFieldInputs'
interface PieceCatalogType extends ModelType {
structure: PieceModelStructure | null
@@ -44,7 +38,6 @@ export function usePieceEdit(pieceId: string) {
const { get } = useApi()
const { pieceTypes, loadPieceTypes } = usePieceTypes()
const { updatePiece } = usePieces()
const { upsertCustomFieldValue, updateCustomFieldValue } = useCustomFields()
const toast = useToast()
const { loadDocumentsByPiece, uploadDocuments, deleteDocument } = useDocuments()
const { ensureConstructeurs } = useConstructeurs()
@@ -90,19 +83,26 @@ export function usePieceEdit(pieceId: string) {
const constructeurIdsFromForm = computed(() => constructeurIdsFromLinks(constructeurLinks.value))
const productSelections = ref<(string | null)[]>([])
const customFieldInputs = ref<CustomFieldInput[]>([])
const resolvedStructure = computed<PieceModelStructure | null>(() =>
pieceTypeDetails.value?.structure ?? selectedType.value?.structure ?? null,
)
const refreshCustomFieldInputs = (
structureOverride?: PieceModelStructure | null,
valuesOverride?: any[] | null,
) => {
const structure = structureOverride ?? resolvedStructure.value ?? null
const values = valuesOverride ?? piece.value?.customFieldValues ?? null
customFieldInputs.value = buildCustomFieldInputs(structure, values)
}
const {
fields: customFieldInputs,
requiredFilled: requiredCustomFieldsFilled,
saveAll: saveAllCustomFields,
refresh: refreshCustomFieldInputs,
} = useCustomFieldInputs({
definitions: computed(() => resolvedStructure.value?.customFields ?? []),
values: computed(() => piece.value?.customFieldValues ?? []),
entityType: 'piece',
entityId: computed(() => piece.value?.id ?? null),
onValueCreated: (newValue) => {
if (piece.value && Array.isArray(piece.value.customFieldValues)) {
piece.value.customFieldValues.push(newValue)
}
},
})
const openPreview = (doc: any) => {
if (!doc || !canPreviewDocument(doc)) {
@@ -221,10 +221,6 @@ export function usePieceEdit(pieceId: string) {
pendingProductIds = []
})
const requiredCustomFieldsFilled = computed(() =>
_requiredCustomFieldsFilled(customFieldInputs.value),
)
const canSubmit = computed(() =>
Boolean(
canEdit.value
@@ -247,9 +243,7 @@ export function usePieceEdit(pieceId: string) {
piece.value = result.data
pieceDocuments.value = Array.isArray(result.data?.documents) ? result.data.documents : []
// Use customFieldValues from entity response (enriched with customField definitions via serialization groups)
const customValues = Array.isArray(result.data?.customFieldValues) ? result.data.customFieldValues : []
refreshCustomFieldInputs(undefined, customValues)
// The watcher on useCustomFieldInputs will auto-refresh when piece.value changes
// Use cached type from loadPieceTypes() instead of separate getModelType() call
loadPieceTypeDetailsFromCache(result.data)
@@ -275,14 +269,14 @@ export function usePieceEdit(pieceId: string) {
const cachedType = (pieceTypes.value || []).find((t: any) => t.id === typeId) ?? null
if (cachedType) {
pieceTypeDetails.value = cachedType
refreshCustomFieldInputs((cachedType.structure as PieceModelStructure | null) ?? null, currentPiece?.customFieldValues ?? null)
// useCustomFieldInputs auto-refreshes via its watcher on resolvedStructure
return
}
// Fallback: fetch if not in cache (edge case)
getModelType(typeId).then((type) => {
if (type && typeof type === 'object') {
pieceTypeDetails.value = type
refreshCustomFieldInputs((type.structure as PieceModelStructure | null) ?? null, currentPiece?.customFieldValues ?? null)
// useCustomFieldInputs auto-refreshes via its watcher on resolvedStructure
}
}).catch(() => {
pieceTypeDetails.value = null
@@ -336,29 +330,21 @@ export function usePieceEdit(pieceId: string) {
pendingProductIds = []
}
// After setting selectedTypeId, read selectedType.value (now updated) instead of
// the stale destructured currentType which was captured before the ID change.
const resolvedType = selectedType.value ?? pieceTypeDetails.value ?? null
refreshCustomFieldInputs(resolvedType?.structure ?? null, currentPiece.customFieldValues)
// useCustomFieldInputs auto-refreshes via its watcher on definitions + values
initialized = true
},
{ immediate: true },
)
watch(selectedType, (currentType) => {
if (!piece.value || !currentType) {
return
}
refreshCustomFieldInputs(currentType.structure, piece.value.customFieldValues)
})
// useCustomFieldInputs auto-refreshes when selectedType changes (via resolvedStructure)
watch(resolvedStructure, (currentStructure) => {
watch(resolvedStructure, () => {
if (!piece.value) {
return
}
ensureProductSelections(structureProducts.value.length)
refreshCustomFieldInputs(currentStructure, piece.value.customFieldValues)
// useCustomFieldInputs auto-refreshes via its watcher on definitions
})
const submitEdition = async () => {
@@ -407,15 +393,10 @@ export function usePieceEdit(pieceId: string) {
try {
const result = await updatePiece(piece.value.id, payload)
if (result.success && result.data) {
const updatedPiece = result.data as Record<string, any>
await _saveCustomFieldValues(
'piece',
updatedPiece.id,
[
updatedPiece?.typePiece?.structure?.customFields,
],
{ customFieldInputs, upsertCustomFieldValue, updateCustomFieldValue, toast },
)
const failedFields = await saveAllCustomFields()
if (failedFields.length) {
toast.showError(`Erreur sur les champs : ${failedFields.join(', ')}`)
}
await syncLinks('piece', piece.value.id, originalConstructeurLinks.value, constructeurLinks.value)
originalConstructeurLinks.value = constructeurLinks.value.map(l => ({ ...l }))
toast.showSuccess('Pièce mise à jour avec succès.')

View File

@@ -336,7 +336,7 @@
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
<div
v-for="field in visibleCustomFields"
:key="field.customFieldValueId || field.id || field.name"
:key="field.customFieldValueId || field.customFieldId || field.name"
class="form-control"
>
<label class="label">

View File

@@ -278,7 +278,7 @@
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
<div
v-for="field in visibleCustomFields"
:key="field.customFieldValueId || field.id || field.name"
:key="field.customFieldValueId || field.customFieldId || field.name"
class="form-control"
>
<label class="label">

View File

@@ -225,7 +225,6 @@ import { usePieceTypes } from '~/composables/usePieceTypes'
import { usePieces } from '~/composables/usePieces'
import { useToast } from '~/composables/useToast'
import { humanizeError } from '~/shared/utils/errorMessages'
import { useCustomFields } from '~/composables/useCustomFields'
import { useDocuments } from '~/composables/useDocuments'
import { useConstructeurLinks } from '~/composables/useConstructeurLinks'
import { useConstructeurs } from '~/composables/useConstructeurs'
@@ -243,12 +242,7 @@ import {
applyProductSelection,
collectNormalizedProductIds,
} from '~/shared/utils/pieceProductSelectionUtils'
import {
type CustomFieldInput,
normalizeCustomFieldInputs,
requiredCustomFieldsFilled as _requiredCustomFieldsFilled,
saveCustomFieldValues as _saveCustomFieldValues,
} from '~/shared/utils/customFieldFormUtils'
import { useCustomFieldInputs, type CustomFieldEntityType } from '~/composables/useCustomFieldInputs'
interface PieceCatalogType extends ModelType {
structure: PieceModelStructure | null
@@ -261,7 +255,6 @@ const router = useRouter()
const { pieceTypes, loadPieceTypes, loadingPieceTypes: loadingTypes } = usePieceTypes()
const { createPiece } = usePieces()
const toast = useToast()
const { upsertCustomFieldValue, updateCustomFieldValue } = useCustomFields()
const { uploadDocuments } = useDocuments()
const { syncLinks } = useConstructeurLinks()
const { getConstructeurById } = useConstructeurs()
@@ -281,7 +274,14 @@ const constructeurLinks = ref<ConstructeurLinkEntry[]>([])
const productSelections = ref<(string | null)[]>([])
const lastSuggestedName = ref('')
const customFieldInputs = ref<CustomFieldInput[]>([])
const cfDefinitions = ref<any[]>([])
const createdEntityId = ref<string | null>(null)
const { fields: customFieldInputs, requiredFilled: requiredCustomFieldsFilled, saveAll: saveAllCustomFields } = useCustomFieldInputs({
definitions: cfDefinitions,
values: [] as any[],
entityType: 'piece' as CustomFieldEntityType,
entityId: createdEntityId,
})
const selectedDocuments = ref<File[]>([])
const uploadingDocuments = ref(false)
@@ -360,21 +360,17 @@ watch(structureProducts, (products) => {
watch(selectedType, (type) => {
if (!type) {
clearCreationForm()
customFieldInputs.value = []
cfDefinitions.value = []
return
}
if (!creationForm.name || creationForm.name === lastSuggestedName.value) {
creationForm.name = type.name
}
lastSuggestedName.value = creationForm.name
customFieldInputs.value = normalizeCustomFieldInputs(type.structure)
cfDefinitions.value = type.structure?.customFields ?? []
productSelections.value = Array.from({ length: structureProducts.value.length }, () => null)
})
const requiredCustomFieldsFilled = computed(() =>
_requiredCustomFieldsFilled(customFieldInputs.value),
)
const canSubmit = computed(() =>
Boolean(
canEdit.value &&
@@ -450,14 +446,11 @@ const submitCreation = async () => {
const result = await createPiece(payload)
if (result.success && result.data) {
const createdPiece = result.data as Record<string, any>
await _saveCustomFieldValues(
'piece',
createdPiece.id,
[
createdPiece?.typePiece?.structure?.customFields,
],
{ customFieldInputs, upsertCustomFieldValue, updateCustomFieldValue, toast },
)
createdEntityId.value = createdPiece.id
const failedFields = await saveAllCustomFields()
if (failedFields.length) {
toast.showError(`Pièce créée, mais impossible d'enregistrer ${failedFields.length} champ(s): ${failedFields.join(', ')}`)
}
// Sync constructeur links after creation
if (constructeurLinks.value.length) {
await syncLinks('piece', createdPiece.id, [], constructeurLinks.value)

View File

@@ -237,7 +237,6 @@ import ConstructeurSelect from '~/components/ConstructeurSelect.vue'
import DocumentUpload from '~/components/DocumentUpload.vue'
import DocumentPreviewModal from '~/components/DocumentPreviewModal.vue'
import { useProducts } from '~/composables/useProducts'
import { useCustomFields } from '~/composables/useCustomFields'
import { useToast } from '~/composables/useToast'
import { humanizeError } from '~/shared/utils/errorMessages'
import { useDocuments } from '~/composables/useDocuments'
@@ -250,12 +249,7 @@ import type { ConstructeurLinkEntry } from '~/shared/constructeurUtils'
import { getModelType } from '~/services/modelTypes'
import type { ProductModelStructure } from '~/shared/types/inventory'
import { canPreviewDocument } from '~/utils/documentPreview'
import {
type CustomFieldInput,
buildCustomFieldInputs,
requiredCustomFieldsFilled as _requiredCustomFieldsFilled,
saveCustomFieldValues as _saveCustomFieldValues,
} from '~/shared/utils/customFieldFormUtils'
import { useCustomFieldInputs, type CustomFieldEntityType } from '~/composables/useCustomFieldInputs'
const { canEdit } = usePermissions()
const versionRefreshKey = ref(0)
@@ -263,7 +257,6 @@ const route = useRoute()
const router = useRouter()
const toast = useToast()
const { getProduct, updateProduct } = useProducts()
const { upsertCustomFieldValue, updateCustomFieldValue } = useCustomFields()
const {
loadDocumentsByProduct,
uploadDocuments: uploadProductDocuments,
@@ -282,7 +275,19 @@ const {
const product = ref<any | null>(null)
const productType = ref<any | null>(null)
const structure = ref<ProductModelStructure | null>(null)
const customFieldInputs = ref<CustomFieldInput[]>([])
const cfDefinitions = ref<any[]>([])
const cfValues = ref<any[]>([])
const entityId = computed(() => product.value?.id ?? null)
const {
fields: customFieldInputs,
requiredFilled: requiredCustomFieldsFilled,
saveAll: saveAllCustomFields,
} = useCustomFieldInputs({
definitions: cfDefinitions,
values: cfValues,
entityType: 'product' as CustomFieldEntityType,
entityId,
})
const loading = ref(true)
const saving = ref(false)
const selectedFiles = ref<File[]>([])
@@ -311,7 +316,8 @@ const refreshCustomFieldInputs = (
) => {
const nextStructure = structureOverride ?? structure.value ?? null
const nextValues = valuesOverride ?? product.value?.customFieldValues ?? null
customFieldInputs.value = buildCustomFieldInputs(nextStructure, nextValues)
cfDefinitions.value = nextStructure?.customFields ?? []
cfValues.value = Array.isArray(nextValues) ? nextValues : []
}
const editionForm = reactive({
@@ -321,9 +327,7 @@ const editionForm = reactive({
supplierPrice: '' as string,
})
const requiredCustomFieldsFilled = computed(() =>
_requiredCustomFieldsFilled(customFieldInputs.value),
)
// requiredCustomFieldsFilled comes from useCustomFieldInputs composable
const canSubmit = computed(() =>
Boolean(canEdit.value && product.value && editionForm.name.trim().length >= 2 && requiredCustomFieldsFilled.value && !saving.value),
@@ -517,12 +521,7 @@ const submitEdition = async () => {
const result = await updateProduct(product.value.id, payload)
if (result.success && result.data?.id) {
product.value = result.data
const failedFields = await _saveCustomFieldValues(
'product',
result.data.id,
[result.data?.typeProduct?.structure?.customFields],
{ customFieldInputs, upsertCustomFieldValue, updateCustomFieldValue, toast },
)
const failedFields = await saveAllCustomFields()
if (failedFields.length) {
toast.showError(`Impossible d'enregistrer ${failedFields.length} champ(s): ${failedFields.join(', ')}`)
return

View File

@@ -194,7 +194,7 @@
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
<div
v-for="field in visibleCustomFields"
:key="field.customFieldValueId || field.id || field.name"
:key="field.customFieldValueId || field.customFieldId || field.name"
class="form-control"
>
<label class="label">
@@ -309,7 +309,6 @@ import ConstructeurSelect from '~/components/ConstructeurSelect.vue'
import DocumentUpload from '~/components/DocumentUpload.vue'
import DocumentPreviewModal from '~/components/DocumentPreviewModal.vue'
import { useProducts } from '~/composables/useProducts'
import { useCustomFields } from '~/composables/useCustomFields'
import { useToast } from '~/composables/useToast'
import { humanizeError } from '~/shared/utils/errorMessages'
import { useDocuments } from '~/composables/useDocuments'
@@ -323,19 +322,13 @@ import type { ConstructeurLinkEntry } from '~/shared/constructeurUtils'
import { getModelType } from '~/services/modelTypes'
import type { ProductModelStructure } from '~/shared/types/inventory'
import { canPreviewDocument } from '~/utils/documentPreview'
import {
type CustomFieldInput,
buildCustomFieldInputs,
requiredCustomFieldsFilled as _requiredCustomFieldsFilled,
saveCustomFieldValues as _saveCustomFieldValues,
} from '~/shared/utils/customFieldFormUtils'
import { useCustomFieldInputs, type CustomFieldEntityType } from '~/composables/useCustomFieldInputs'
const { canEdit } = usePermissions()
const route = useRoute()
const router = useRouter()
const toast = useToast()
const { getProduct, updateProduct } = useProducts()
const { upsertCustomFieldValue, updateCustomFieldValue } = useCustomFields()
const {
loadDocumentsByProduct,
uploadDocuments: uploadProductDocuments,
@@ -359,7 +352,19 @@ const originalConstructeurLinks = ref<ConstructeurLinkEntry[]>([])
const product = ref<any | null>(null)
const productType = ref<any | null>(null)
const structure = ref<ProductModelStructure | null>(null)
const customFieldInputs = ref<CustomFieldInput[]>([])
const cfDefinitions = ref<any[]>([])
const cfValues = ref<any[]>([])
const entityId = computed(() => product.value?.id ?? null)
const {
fields: customFieldInputs,
requiredFilled: requiredCustomFieldsFilled,
saveAll: saveAllCustomFields,
} = useCustomFieldInputs({
definitions: cfDefinitions,
values: cfValues,
entityType: 'product' as CustomFieldEntityType,
entityId,
})
const loading = ref(true)
const saving = ref(false)
const selectedFiles = ref<File[]>([])
@@ -385,7 +390,8 @@ const refreshCustomFieldInputs = (
) => {
const nextStructure = structureOverride ?? structure.value ?? null
const nextValues = valuesOverride ?? product.value?.customFieldValues ?? null
customFieldInputs.value = buildCustomFieldInputs(nextStructure, nextValues)
cfDefinitions.value = nextStructure?.customFields ?? []
cfValues.value = Array.isArray(nextValues) ? nextValues : []
}
const editionForm = reactive({
@@ -395,9 +401,7 @@ const editionForm = reactive({
supplierPrice: '' as string,
})
const requiredCustomFieldsFilled = computed(() =>
_requiredCustomFieldsFilled(customFieldInputs.value),
)
// requiredCustomFieldsFilled comes from useCustomFieldInputs composable
const canSubmit = computed(() =>
Boolean(canEdit.value && product.value && editionForm.name.trim().length >= 2 && requiredCustomFieldsFilled.value && !saving.value),
@@ -595,12 +599,7 @@ const submitEdition = async () => {
const result = await updateProduct(product.value.id, payload)
if (result.success && result.data?.id) {
product.value = result.data
const failedFields = await _saveCustomFieldValues(
'product',
result.data.id,
[result.data?.typeProduct?.structure?.customFields],
{ customFieldInputs, upsertCustomFieldValue, updateCustomFieldValue, toast },
)
const failedFields = await saveAllCustomFields()
if (failedFields.length) {
toast.showError(`Impossible d'enregistrer ${failedFields.length} champ(s): ${failedFields.join(', ')}`)
return

View File

@@ -177,7 +177,6 @@ import DocumentUpload from '~/components/DocumentUpload.vue'
import { useProductTypes } from '~/composables/useProductTypes'
import { useProducts } from '~/composables/useProducts'
import { useToast } from '~/composables/useToast'
import { useCustomFields } from '~/composables/useCustomFields'
import { useDocuments } from '~/composables/useDocuments'
import { formatProductStructurePreview, normalizeProductStructureForSave } from '~/shared/modelUtils'
import { constructeurIdsFromLinks } from '~/shared/constructeurUtils'
@@ -186,10 +185,7 @@ import { useConstructeurLinks } from '~/composables/useConstructeurLinks'
import { useConstructeurs } from '~/composables/useConstructeurs'
import type { ProductModelStructure } from '~/shared/types/inventory'
import type { ModelType } from '~/services/modelTypes'
import {
type CustomFieldInput,
normalizeCustomFieldInputs as normalizeCustomFieldInputsFromUtils,
} from '~/shared/utils/customFieldFormUtils'
import { useCustomFieldInputs, type CustomFieldEntityType } from '~/composables/useCustomFieldInputs'
interface ProductCatalogType extends ModelType {
structure: ProductModelStructure | null
@@ -202,7 +198,6 @@ const router = useRouter()
const { productTypes, loadProductTypes, loadingProductTypes: loadingTypes } = useProductTypes()
const { createProduct } = useProducts()
const toast = useToast()
const { upsertCustomFieldValue } = useCustomFields()
const { uploadDocuments } = useDocuments()
const { canEdit } = usePermissions()
const { syncLinks } = useConstructeurLinks()
@@ -221,7 +216,14 @@ const constructeurLinks = ref<ConstructeurLinkEntry[]>([])
const selectedDocuments = ref<File[]>([])
const uploadingDocuments = ref(false)
const customFieldInputs = ref<CustomFieldInput[]>([])
const cfDefinitions = ref<any[]>([])
const createdEntityId = ref<string | null>(null)
const { fields: customFieldInputs, requiredFilled: requiredCustomFieldsFilled, saveAll: saveAllCustomFields } = useCustomFieldInputs({
definitions: cfDefinitions,
values: [] as any[],
entityType: 'product' as CustomFieldEntityType,
entityId: createdEntityId,
})
const productTypeList = computed<ProductCatalogType[]>(() =>
(productTypes.value || []) as ProductCatalogType[],
@@ -264,26 +266,17 @@ watch(selectedTypeId, (id) => {
watch(selectedType, (type) => {
if (!type) {
clearForm()
customFieldInputs.value = []
cfDefinitions.value = []
return
}
if (!creationForm.name) {
creationForm.name = type.name
}
customFieldInputs.value = normalizeCustomFieldInputsFromUtils(normalizeProductStructureForSave(type.structure))
const normalized = normalizeProductStructureForSave(type.structure)
cfDefinitions.value = normalized?.customFields ?? []
})
const requiredCustomFieldsFilled = computed(() =>
customFieldInputs.value.every((field) => {
if (!field.required) {
return true
}
if (field.type === 'boolean') {
return field.value === 'true' || field.value === 'false'
}
return field.value.trim().length > 0
}),
)
// requiredCustomFieldsFilled comes from useCustomFieldInputs composable
const canSubmit = computed(() => Boolean(
canEdit.value &&
@@ -336,7 +329,8 @@ const submitCreation = async () => {
const result = await createProduct(payload)
if (result.success && result.data?.id) {
const productId = result.data.id
const failedFields = await saveCustomFieldValues(result.data.id)
createdEntityId.value = productId
const failedFields = await saveAllCustomFields()
if (failedFields.length) {
toast.showError(`Produit créé, mais impossible d'enregistrer ${failedFields.length} champ(s): ${failedFields.join(', ')}`)
await router.replace(`/product/${result.data.id}?edit=true`)
@@ -375,39 +369,6 @@ const submitCreation = async () => {
}
}
const saveCustomFieldValues = async (productId: string) => {
const failed: string[] = []
for (const field of customFieldInputs.value) {
if (!field.name) {
continue
}
const value = field.value ?? ''
const metadata = field.customFieldId
? undefined
: { customFieldName: field.name, customFieldType: field.type, customFieldRequired: field.required }
const result = await upsertCustomFieldValue(
field.customFieldId,
'product',
productId,
String(value ?? ''),
metadata,
)
if (!result.success) {
failed.push(field.name)
} else {
const createdValue = result.data
if (createdValue?.id) {
field.customFieldValueId = createdValue.id
}
const resolvedId = createdValue?.customField?.id || field.customFieldId
if (resolvedId) {
field.customFieldId = resolvedId
}
}
}
return failed
}
// Sync constructeurIds → constructeurLinks when IDs are added via ConstructeurSelect
watch(
() => creationForm.constructeurIds,

View File

@@ -4,6 +4,7 @@ import {
type ComponentModelStructure,
type ComponentModelStructureNode,
} from '../types/inventory'
import { mergeDefinitionsWithValues } from '../utils/customFields'
// Import for internal use in this file
import { sanitizeCustomFields, sanitizePieces, sanitizeProducts, sanitizeSubcomponents } from './componentStructureSanitize'
@@ -86,30 +87,22 @@ export const cloneStructure = (input: any): ComponentModelStructure => {
export const normalizeStructureForEditor = (input: any): ComponentModelStructure => {
const source = cloneStructure(input)
const sanitizedCustomFields = sanitizeCustomFields(source.customFields)
const customFields = sanitizedCustomFields.map((field) => {
const options = Array.isArray(field.options) ? [...field.options] : []
const optionsText = options.length ? options.join('\n') : ''
const defaultValue =
field.defaultValue !== undefined && field.defaultValue !== null && field.defaultValue !== ''
? String(field.defaultValue)
: null
const copy: ComponentModelCustomField = {
name: field.name,
type: field.type,
required: field.required,
machineContextOnly: !!field.machineContextOnly,
options,
defaultValue,
optionsText,
id: field.id,
customFieldId: field.customFieldId,
}
return copy
})
const merged = mergeDefinitionsWithValues(source.customFields, [])
const customFields: ComponentModelCustomField[] = merged.map((field) => ({
name: field.name,
type: field.type as ComponentModelCustomField['type'],
required: field.required,
machineContextOnly: field.machineContextOnly,
options: field.options,
defaultValue: field.defaultValue,
optionsText: field.optionsText,
id: field.customFieldId ?? undefined,
customFieldId: field.customFieldId ?? undefined,
orderIndex: field.orderIndex,
}))
const result: ComponentModelStructure = {
customFields: customFields as ComponentModelCustomField[],
customFields,
pieces: sanitizePieces(source.pieces),
products: sanitizeProducts(source.products),
subcomponents: hydrateSubcomponents(source.subcomponents),

View File

@@ -1,404 +0,0 @@
/**
* Custom field form normalization, merge, and persistence utilities.
*
* Extracted from pages/component/create.vue, component/[id]/edit.vue,
* pieces/create.vue, pieces/[id]/edit.vue, product/[id]/edit.vue.
*
* Every create/edit page was shipping its own copy of these helpers
* this module unifies them behind a single, entity-agnostic API.
*/
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
export interface CustomFieldInput {
id: string | null
name: string
type: string
required: boolean
options: string[]
value: string
customFieldId: string | null
customFieldValueId: string | null
orderIndex: number
}
export interface SaveCustomFieldDeps {
customFieldInputs: { value: CustomFieldInput[] }
upsertCustomFieldValue: (
definitionId: string | null,
entityType: string,
entityId: string,
value: string,
metadata?: Record<string, unknown>,
) => Promise<{ success: boolean; data?: any }>
updateCustomFieldValue: (
id: string,
payload: { value: string },
) => Promise<{ success: boolean }>
toast: { showError: (msg: string) => void }
}
// ---------------------------------------------------------------------------
// Primitive helpers
// ---------------------------------------------------------------------------
export const toFieldString = (value: unknown): string => {
if (value === null || value === undefined) return ''
if (typeof value === 'string') return value
if (typeof value === 'number' || typeof value === 'boolean') return String(value)
return ''
}
export const fieldKey = (field: CustomFieldInput, index: number): string =>
field.customFieldValueId || field.id || `${field.name}-${index}`
// ---------------------------------------------------------------------------
// Field resolution helpers
// ---------------------------------------------------------------------------
export const resolveFieldName = (field: any): string => {
if (typeof field?.name === 'string' && field.name.trim()) return field.name.trim()
if (typeof field?.key === 'string' && field.key.trim()) return field.key.trim()
if (typeof field?.label === 'string' && field.label.trim()) return field.label.trim()
return ''
}
export const resolveFieldType = (field: any): string => {
const allowed = ['text', 'number', 'select', 'boolean', 'date']
const rawType =
typeof field?.type === 'string'
? field.type
: typeof field?.value?.type === 'string'
? field.value.type
: ''
const value = rawType.toLowerCase()
return allowed.includes(value) ? value : 'text'
}
export const resolveRequiredFlag = (field: any): boolean => {
if (typeof field?.required === 'boolean') return field.required
const nested = field?.value?.required
if (typeof nested === 'boolean') return nested
if (typeof nested === 'string') {
const normalized = nested.toLowerCase()
return normalized === 'true' || normalized === '1'
}
return false
}
export const resolveOptions = (field: any): string[] => {
const sources = [field?.options, field?.value?.options, field?.value?.choices]
for (const source of sources) {
if (Array.isArray(source)) {
const mapped = source
.map((option: unknown) => {
if (option === null || option === undefined) return ''
if (typeof option === 'string') return option.trim()
if (typeof option === 'object') {
const record = (option || {}) as Record<string, unknown>
for (const key of ['value', 'label', 'name']) {
const candidate = record[key]
if (typeof candidate === 'string' && candidate.trim().length > 0) return candidate.trim()
}
}
const fallback = String(option).trim()
return fallback === '[object Object]' ? '' : fallback
})
.filter((o) => o.length > 0)
if (mapped.length) return mapped
}
}
return []
}
export const resolveDefaultValue = (field: any): any => {
if (!field || typeof field !== 'object') return null
if (field.defaultValue !== undefined && field.defaultValue !== null) return field.defaultValue
if (field.value !== undefined && field.value !== null && typeof field.value !== 'object') return field.value
if (field.default !== undefined && field.default !== null) return field.default
if (field.value && typeof field.value === 'object') {
if (field.value.defaultValue !== undefined && field.value.defaultValue !== null) return field.value.defaultValue
if (field.value.value !== undefined && field.value.value !== null && typeof field.value.value !== 'object') return field.value.value
}
return null
}
export const formatDefaultValue = (type: string, defaultValue: any): string => {
if (defaultValue === null || defaultValue === undefined) return ''
if (typeof defaultValue === 'object') {
if (defaultValue === null) return ''
if ('defaultValue' in (defaultValue as Record<string, any>)) {
return formatDefaultValue(type, (defaultValue as Record<string, any>).defaultValue)
}
if ('value' in (defaultValue as Record<string, any>)) {
return formatDefaultValue(type, (defaultValue as Record<string, any>).value)
}
return ''
}
if (type === 'boolean') {
const normalized = String(defaultValue).toLowerCase()
if (normalized === 'true' || normalized === '1') return 'true'
if (normalized === 'false' || normalized === '0') return 'false'
return ''
}
return String(defaultValue)
}
// ---------------------------------------------------------------------------
// Normalize a single raw custom-field definition into CustomFieldInput
// ---------------------------------------------------------------------------
export const normalizeCustomField = (rawField: any, fallbackIndex = 0): CustomFieldInput | null => {
if (!rawField || typeof rawField !== 'object') return null
const name = resolveFieldName(rawField)
if (!name) return null
const type = resolveFieldType(rawField)
const required = resolveRequiredFlag(rawField)
const options = resolveOptions(rawField)
const defaultSource = resolveDefaultValue(rawField)
const value = formatDefaultValue(type, defaultSource)
const id = typeof rawField.id === 'string' ? rawField.id : null
const customFieldId = typeof rawField.customFieldId === 'string' ? rawField.customFieldId : id
const customFieldValueId = typeof rawField.customFieldValueId === 'string' ? rawField.customFieldValueId : null
const orderIndex = typeof rawField.orderIndex === 'number' ? rawField.orderIndex : fallbackIndex
return { id, name, type, required, options, value, customFieldId, customFieldValueId, orderIndex }
}
// ---------------------------------------------------------------------------
// Normalize ALL custom-field definitions from a structure
// ---------------------------------------------------------------------------
export const normalizeCustomFieldInputs = (structure: any): CustomFieldInput[] => {
if (!structure || typeof structure !== 'object') return []
const fields = Array.isArray(structure.customFields) ? structure.customFields : []
return fields
.map((field: any, index: number) => normalizeCustomField(field, index))
.filter((field: CustomFieldInput | null): field is CustomFieldInput => field !== null)
.sort((a: CustomFieldInput, b: CustomFieldInput) => a.orderIndex - b.orderIndex)
}
// ---------------------------------------------------------------------------
// Extract stored value from a persisted custom-field entry
// ---------------------------------------------------------------------------
export const extractStoredCustomFieldValue = (entry: any): any => {
if (entry === null || entry === undefined) return ''
if (typeof entry === 'string' || typeof entry === 'number' || typeof entry === 'boolean') return entry
if (typeof entry !== 'object') return String(entry)
const direct = entry.value
if (direct !== undefined && direct !== null) {
if (typeof direct === 'object') {
if (direct === null) return ''
if ('value' in direct && direct.value !== undefined && direct.value !== null) return direct.value
if ('defaultValue' in direct && direct.defaultValue !== undefined && direct.defaultValue !== null) return direct.defaultValue
return ''
}
return direct
}
if (entry.defaultValue !== undefined && entry.defaultValue !== null) return entry.defaultValue
if (entry.customFieldValue?.value !== undefined && entry.customFieldValue?.value !== null) return entry.customFieldValue.value
return ''
}
// ---------------------------------------------------------------------------
// Build inputs for edit pages (merge definitions + stored values)
// ---------------------------------------------------------------------------
export const buildCustomFieldInputs = (
structure: any,
values: any[] | null | undefined,
): CustomFieldInput[] => {
const definitions = normalizeCustomFieldInputs(structure)
const valueList = Array.isArray(values) ? values : []
const mapById = new Map<string, any>()
const mapByName = new Map<string, any>()
valueList.forEach((entry) => {
if (!entry || typeof entry !== 'object') return
const fieldId = entry.customField?.id || entry.customFieldId || null
if (fieldId) mapById.set(fieldId, entry)
const fieldName = entry.customField?.name || entry.name || entry.key || null
if (fieldName) mapByName.set(fieldName, entry)
})
const matchedIds = new Set<string>()
const matchedNames = new Set<string>()
const result = definitions
.map((definition) => {
const definitionId = definition.customFieldId || definition.id || null
const matched = (definitionId ? mapById.get(definitionId) : null) || mapByName.get(definition.name)
if (!matched) {
return {
...definition,
customFieldId: definition.customFieldId || definition.id,
customFieldValueId: null,
orderIndex: definition.orderIndex,
}
}
const matchedFieldId = matched.customField?.id || matched.customFieldId || null
if (matchedFieldId) matchedIds.add(matchedFieldId)
const matchedFieldName = matched.customField?.name || matched.name || null
if (matchedFieldName) matchedNames.add(matchedFieldName)
const resolvedValue = extractStoredCustomFieldValue(matched)
return {
...definition,
customFieldId: matched.customField?.id || definition.customFieldId || definition.id,
customFieldValueId: matched.id ?? null,
value: formatDefaultValue(definition.type, resolvedValue),
orderIndex: Math.min(
definition.orderIndex ?? 0,
typeof matched.customField?.orderIndex === 'number'
? matched.customField.orderIndex
: definition.orderIndex ?? 0,
),
}
})
// Include values with embedded definitions that didn't match any structure definition
valueList.forEach((entry, index) => {
if (!entry || typeof entry !== 'object') return
const cf = entry.customField
if (!cf || typeof cf !== 'object') return
const fieldId = cf.id || entry.customFieldId || null
const fieldName = cf.name || entry.name || null
if (fieldId && matchedIds.has(fieldId)) return
if (fieldName && matchedNames.has(fieldName)) return
const name = resolveFieldName(cf)
if (!name) return
const type = resolveFieldType(cf)
const resolvedValue = extractStoredCustomFieldValue(entry)
result.push({
id: fieldId,
name,
type,
required: resolveRequiredFlag(cf),
options: resolveOptions(cf),
value: formatDefaultValue(type, resolvedValue),
customFieldId: fieldId,
customFieldValueId: entry.id ?? null,
orderIndex: typeof cf.orderIndex === 'number' ? cf.orderIndex : definitions.length + index,
})
})
return result.sort((a, b) => (a.orderIndex ?? 0) - (b.orderIndex ?? 0))
}
// ---------------------------------------------------------------------------
// Validation helpers
// ---------------------------------------------------------------------------
export const buildCustomFieldMetadata = (field: CustomFieldInput): Record<string, unknown> => ({
customFieldName: field.name,
customFieldType: field.type,
customFieldRequired: field.required,
customFieldOptions: field.options,
})
export const shouldPersistField = (field: CustomFieldInput): boolean => {
if (field.type === 'boolean') return field.value === 'true' || field.value === 'false'
return toFieldString(field.value).trim() !== ''
}
export const formatValueForPersistence = (field: CustomFieldInput): string => {
if (field.type === 'boolean') return field.value === 'true' ? 'true' : 'false'
return toFieldString(field.value).trim()
}
export const requiredCustomFieldsFilled = (inputs: CustomFieldInput[]): boolean =>
inputs.every((field) => {
if (!field.required) return true
if (field.type === 'boolean') return field.value === 'true' || field.value === 'false'
return toFieldString(field.value).trim() !== ''
})
// ---------------------------------------------------------------------------
// Persistence
// ---------------------------------------------------------------------------
/**
* Save custom-field values for an entity.
*
* @param entityType - API entity slug ('composant' | 'piece' | 'product')
* @param entityId - ID of the created/updated entity
* @param definitionSources - arrays of raw definition objects to build a name→id map
* @param deps - injected composable references
* @returns list of field names that failed to save (empty = all OK)
*/
export const saveCustomFieldValues = async (
entityType: string,
entityId: string,
definitionSources: any[][],
deps: SaveCustomFieldDeps,
): Promise<string[]> => {
if (!entityId) return []
const definitionMap = new Map<string, string>()
const registerDefinitions = (fields: any[]) => {
if (!Array.isArray(fields)) return
fields.forEach((field) => {
if (!field || typeof field !== 'object') return
const name = typeof field.name === 'string' ? field.name : null
const id = typeof field.id === 'string' ? field.id : null
if (name && id && !definitionMap.has(name)) definitionMap.set(name, id)
})
}
definitionSources.forEach(registerDefinitions)
const resolveDefinitionId = (field: CustomFieldInput) => {
if (field.customFieldId) return field.customFieldId
if (field.id) return field.id
return definitionMap.get(field.name) ?? null
}
const failed: string[] = []
for (const field of deps.customFieldInputs.value) {
if (!shouldPersistField(field)) continue
const definitionId = resolveDefinitionId(field)
const metadata = definitionId ? undefined : buildCustomFieldMetadata(field)
const value = formatValueForPersistence(field)
if (field.customFieldValueId) {
const result = await deps.updateCustomFieldValue(field.customFieldValueId, { value })
if (!result.success) {
deps.toast.showError(`Impossible de mettre à jour le champ personnalisé "${field.name}"`)
failed.push(field.name)
} else if (definitionId && !field.customFieldId) {
field.customFieldId = definitionId
}
continue
}
const result = await deps.upsertCustomFieldValue(
definitionId,
entityType,
entityId,
value,
metadata,
)
if (!result.success) {
deps.toast.showError(`Impossible d'enregistrer le champ personnalisé "${field.name}"`)
failed.push(field.name)
} else {
const createdValue = result.data
if (createdValue?.id) field.customFieldValueId = createdValue.id
const resolvedId = createdValue?.customField?.id || definitionId
if (resolvedId) field.customFieldId = resolvedId
}
}
return failed
}

View File

@@ -1,440 +0,0 @@
/**
* Custom field normalization, merging and display utilities.
*
* Extracted from pages/machine/[id].vue to be reusable across
* machine detail, component, piece and product views.
*/
// ---------------------------------------------------------------------------
// Primitive helpers
// ---------------------------------------------------------------------------
export const coerceValueForType = (type: string, rawValue: unknown): string => {
if (rawValue === undefined || rawValue === null || rawValue === '') {
return ''
}
if (type === 'boolean') {
const normalized = String(rawValue).toLowerCase()
if (normalized === 'true' || normalized === '1') return 'true'
if (normalized === 'false' || normalized === '0') return 'false'
return ''
}
return String(rawValue)
}
export const formatCustomFieldValue = (field: Record<string, unknown> | null | undefined): string => {
if (!field) return 'Non défini'
const value = (field.value ?? field.defaultValue ?? '') as string
if (value === '' || value === null || value === undefined) return 'Non défini'
if (field.type === 'boolean') {
const normalized = String(value).toLowerCase()
if (normalized === 'true' || normalized === '1') return 'Oui'
if (normalized === 'false' || normalized === '0') return 'Non'
}
return String(value)
}
export const shouldDisplayCustomField = (field: Record<string, unknown> | null | undefined): boolean => {
if (!field) return false
if (field.readOnly) return true
if (field.type === 'boolean') return field.value !== undefined && field.value !== null
const value = field.value
if (value === null || value === undefined) return false
if (typeof value === 'string') return value.trim().length > 0
return true
}
// ---------------------------------------------------------------------------
// Definition extraction helpers
// ---------------------------------------------------------------------------
export const extractDefinitionName = (definition: Record<string, unknown> = {}): string => {
if (typeof definition?.name === 'string' && (definition.name as string).trim()) {
return (definition.name as string).trim()
}
if (typeof definition?.key === 'string' && (definition.key as string).trim()) {
return (definition.key as string).trim()
}
if (typeof definition?.label === 'string' && (definition.label as string).trim()) {
return (definition.label as string).trim()
}
return ''
}
export const extractDefinitionType = (
definition: Record<string, unknown> = {},
fallback = 'text',
): string => {
const allowed = ['text', 'number', 'select', 'boolean', 'date']
const rawType =
typeof definition?.type === 'string'
? definition.type
: typeof (definition?.value as Record<string, unknown>)?.type === 'string'
? (definition.value as Record<string, unknown>).type as string
: typeof fallback === 'string'
? fallback
: 'text'
const normalized = (rawType as string).toLowerCase()
return allowed.includes(normalized) ? normalized : 'text'
}
export const extractDefinitionRequired = (
definition: Record<string, unknown> = {},
fallback = false,
): boolean => {
if (typeof definition?.required === 'boolean') return definition.required
const nested = (definition?.value as Record<string, unknown>)?.required
if (typeof nested === 'boolean') return nested
if (typeof nested === 'string') {
const normalized = nested.toLowerCase()
if (normalized === 'true' || normalized === '1') return true
if (normalized === 'false' || normalized === '0') return false
}
return !!fallback
}
const extractOptionList = (input: unknown): string[] | undefined => {
if (!Array.isArray(input)) return undefined
const mapped = input
.map((option) => {
if (option === null || option === undefined) return ''
if (typeof option === 'string') return option.trim()
if (typeof option === 'object') {
const record = (option || {}) as Record<string, unknown>
for (const key of ['value', 'label', 'name']) {
const candidate = record[key]
if (typeof candidate === 'string' && candidate.trim().length > 0) return candidate.trim()
}
}
const fallback = String(option).trim()
return fallback === '[object Object]' ? '' : fallback
})
.filter((option) => option.length > 0)
return mapped.length ? mapped : undefined
}
export const extractDefinitionOptions = (definition: Record<string, unknown> = {}): string[] => {
const sources = [
definition?.options,
(definition?.value as Record<string, unknown>)?.options,
(definition?.value as Record<string, unknown>)?.choices,
]
for (const source of sources) {
const list = extractOptionList(source)
if (list) return list
}
return []
}
export const extractDefinitionDefaultValue = (definition: Record<string, unknown> = {}): unknown => {
const candidates = [
definition?.defaultValue,
(definition?.value as Record<string, unknown>)?.defaultValue,
(definition?.value as Record<string, unknown>)?.value,
definition?.value,
definition?.default,
]
for (const candidate of candidates) {
if (candidate === undefined || candidate === null || candidate === '') continue
if (typeof candidate === 'object') {
if (candidate === null) continue
const nestedDefault =
(candidate as Record<string, unknown>).defaultValue !== undefined &&
(candidate as Record<string, unknown>).defaultValue !== null
? (candidate as Record<string, unknown>).defaultValue
: (candidate as Record<string, unknown>).value
if (nestedDefault !== undefined && nestedDefault !== null && nestedDefault !== '') {
return nestedDefault
}
continue
}
return candidate
}
return undefined
}
// ---------------------------------------------------------------------------
// Normalization
// ---------------------------------------------------------------------------
export interface NormalizedCustomFieldDefinition {
id?: string
customFieldId?: string
name: string
type: string
required: boolean
options: string[]
defaultValue?: unknown
readOnly: boolean
orderIndex: number
}
export interface NormalizedCustomFieldEntry {
customFieldValueId: unknown
id: string | undefined
customFieldId: string | null
name: string
type: string
required: boolean
options: string[]
optionsText: string
defaultValue: unknown
value: string
readOnly: boolean
}
export const normalizeCustomFieldDefinitionEntry = (
definition: Record<string, unknown> = {},
fallbackIndex = 0,
): NormalizedCustomFieldDefinition | null => {
const name = extractDefinitionName(definition)
if (!name) return null
const type = extractDefinitionType(definition)
const required = extractDefinitionRequired(definition)
const options = extractDefinitionOptions(definition)
const defaultValue = extractDefinitionDefaultValue(definition)
const id = typeof definition?.id === 'string' ? definition.id : undefined
const customFieldId = typeof definition?.customFieldId === 'string' ? definition.customFieldId : id
const orderIndex = typeof definition?.orderIndex === 'number' ? definition.orderIndex : fallbackIndex
return { id, customFieldId, name, type, required, options, defaultValue, readOnly: !!definition?.readOnly, orderIndex }
}
export const normalizeExistingCustomFieldDefinitions = (
fields: unknown,
): NormalizedCustomFieldDefinition[] => {
if (!Array.isArray(fields)) return []
return fields
.map((field, index) => normalizeCustomFieldDefinitionEntry(field as Record<string, unknown>, index))
.filter((d): d is NormalizedCustomFieldDefinition => d !== null)
.sort((a, b) => (a.orderIndex ?? 0) - (b.orderIndex ?? 0))
}
// ---------------------------------------------------------------------------
// Custom field value normalization
// ---------------------------------------------------------------------------
export const normalizeCustomFieldValueEntry = (entry: Record<string, unknown> = {}): Record<string, unknown> | null => {
if (!entry || typeof entry !== 'object') return null
const normalizedDefinition = normalizeCustomFieldDefinitionEntry(entry)
if (!normalizedDefinition) return null
const value = coerceValueForType(
normalizedDefinition.type,
(entry?.value ?? entry?.defaultValue ?? normalizedDefinition.defaultValue ?? '') as string,
)
return {
id: (entry?.customFieldValueId ?? entry?.id ?? null) as string | null,
customFieldId:
(entry?.customFieldId ?? normalizedDefinition.customFieldId ?? normalizedDefinition.id ?? null) as string | null,
customField: {
id: normalizedDefinition.id ?? normalizedDefinition.customFieldId ?? null,
name: normalizedDefinition.name,
type: normalizedDefinition.type,
required: normalizedDefinition.required,
options: normalizedDefinition.options,
defaultValue: normalizedDefinition.defaultValue ?? '',
},
value,
}
}
// ---------------------------------------------------------------------------
// Merge & dedup
// ---------------------------------------------------------------------------
export const mergeCustomFieldValuesWithDefinitions = (
valueEntries: Record<string, unknown>[] = [],
...definitionSources: unknown[][]
): Record<string, unknown>[] => {
const normalizedValues: Record<string, unknown>[] = (Array.isArray(valueEntries) ? valueEntries : [])
.map((entry): Record<string, unknown> | null => {
if (!entry || typeof entry !== 'object') return null
const normalizedDefinition = normalizeCustomFieldDefinitionEntry(
((entry as Record<string, unknown>).customField || entry) as Record<string, unknown>,
)
if (!normalizedDefinition) return null
const value = coerceValueForType(
normalizedDefinition.type,
((entry as Record<string, unknown>)?.value ??
(entry as Record<string, unknown>)?.defaultValue ??
normalizedDefinition.defaultValue ??
'') as string,
)
return {
customFieldValueId: (entry as Record<string, unknown>)?.id ?? (entry as Record<string, unknown>)?.customFieldValueId ?? null,
id: normalizedDefinition.id,
customFieldId: normalizedDefinition.customFieldId ?? normalizedDefinition.id ?? null,
name: normalizedDefinition.name,
type: normalizedDefinition.type,
required: normalizedDefinition.required,
options: normalizedDefinition.options,
optionsText: normalizedDefinition.options?.length ? normalizedDefinition.options.join('\n') : '',
defaultValue: normalizedDefinition.defaultValue ?? '',
value,
readOnly: !!(entry as Record<string, unknown>)?.readOnly,
} as Record<string, unknown>
})
.filter((entry): entry is Record<string, unknown> => entry !== null)
const result = [...normalizedValues]
const keyFor = (item: Record<string, unknown>) => (item?.id as string) ?? `${item?.name ?? ''}::${item?.type ?? ''}`
const existingMap = new Map<string, Record<string, unknown>>()
result.forEach((item) => {
const key = keyFor(item)
if (key) existingMap.set(key, item)
const fallbackKey = item?.name ? `${item.name}::${item.type ?? ''}` : null
if (fallbackKey) existingMap.set(fallbackKey, item)
})
const definitions = definitionSources
.flatMap((source) => (Array.isArray(source) ? source : []))
.map((definition) => normalizeCustomFieldDefinitionEntry(definition as Record<string, unknown>))
.filter((d): d is NormalizedCustomFieldDefinition => d !== null)
definitions.forEach((normalizedDefinition) => {
const key = normalizedDefinition.id ?? `${normalizedDefinition.name}::${normalizedDefinition.type}`
if (!key) return
if (normalizedDefinition.id) {
const fallbackKey = `${normalizedDefinition.name}::${normalizedDefinition.type}`
if (existingMap.has(fallbackKey)) {
const existingFallback = existingMap.get(fallbackKey)
if (existingFallback) {
existingFallback.id = existingFallback.id || normalizedDefinition.id
existingFallback.customFieldId = normalizedDefinition.id
existingFallback.readOnly = (existingFallback.readOnly as boolean) && normalizedDefinition.readOnly
existingMap.delete(fallbackKey)
existingMap.set(normalizedDefinition.id, existingFallback)
existingMap.set(fallbackKey, existingFallback)
return
}
}
}
const existing =
existingMap.get(key) ||
(normalizedDefinition.name ? existingMap.get(`${normalizedDefinition.name}::${normalizedDefinition.type}`) : null)
if (existing) {
existing.name = existing.name || normalizedDefinition.name
existing.type = existing.type || normalizedDefinition.type
existing.required = (existing.required as boolean) || normalizedDefinition.required
if (!(existing.options as string[])?.length && normalizedDefinition.options?.length) {
existing.options = normalizedDefinition.options
}
if (!existing.defaultValue && normalizedDefinition.defaultValue) {
existing.defaultValue = String(normalizedDefinition.defaultValue)
if (!existing.value) {
existing.value = coerceValueForType(existing.type as string, normalizedDefinition.defaultValue)
}
}
existing.customFieldId = existing.customFieldId || normalizedDefinition.id
existing.readOnly = (existing.readOnly as boolean) && normalizedDefinition.readOnly
if (!existing.optionsText && normalizedDefinition.options?.length) {
existing.optionsText = normalizedDefinition.options.join('\n')
}
if (normalizedDefinition.id) existingMap.set(normalizedDefinition.id, existing)
if (normalizedDefinition.name) {
existingMap.set(`${normalizedDefinition.name}::${normalizedDefinition.type}`, existing)
}
return
}
const entry: Record<string, unknown> = {
customFieldValueId: null,
id: normalizedDefinition.id,
customFieldId: normalizedDefinition.id,
name: normalizedDefinition.name,
type: normalizedDefinition.type,
required: normalizedDefinition.required,
options: normalizedDefinition.options,
optionsText: normalizedDefinition.options?.length ? normalizedDefinition.options.join('\n') : '',
defaultValue: normalizedDefinition.defaultValue ?? '',
value: coerceValueForType(normalizedDefinition.type, (normalizedDefinition.defaultValue ?? '') as string),
readOnly: false,
}
result.push(entry)
existingMap.set(key, entry)
const fallbackKey = entry.name ? `${entry.name}::${entry.type}` : null
if (fallbackKey) existingMap.set(fallbackKey, entry)
})
return result
}
export const dedupeCustomFieldEntries = (fields: Record<string, unknown>[]): Record<string, unknown>[] => {
if (!Array.isArray(fields) || fields.length <= 1) {
return Array.isArray(fields) ? fields : []
}
const seen = new Set<string>()
const result: Record<string, unknown>[] = []
for (const field of fields) {
if (!field) continue
field.type = field.type || 'text'
let normalizedName = typeof field.name === 'string' ? (field.name as string).trim() : ''
if (!normalizedName && (field.customField as Record<string, unknown>)?.name) {
normalizedName = String((field.customField as Record<string, unknown>).name).trim()
field.name = normalizedName
} else if (typeof field.name === 'string') {
field.name = normalizedName
}
const key =
(field.customFieldId as string) ||
(field.id as string) ||
(normalizedName ? `${normalizedName}::${field.type || 'text'}` : null)
if (!key && !normalizedName) continue
if (key && seen.has(key)) continue
if (!normalizedName) continue
if (key) seen.add(key)
if (normalizedName) seen.add(`${normalizedName}::${field.type || 'text'}`)
result.push(field)
}
return result
}
// ---------------------------------------------------------------------------
// Summarize for display
// ---------------------------------------------------------------------------
export const summarizeCustomFields = (
fields: Record<string, unknown>[] = [],
): { key: string; label: string; value: string }[] => {
const seen = new Set<string>()
return fields
.slice()
.sort((a, b) => {
const left = typeof a?.orderIndex === 'number' ? a.orderIndex : 0
const right = typeof b?.orderIndex === 'number' ? b.orderIndex : 0
return (left as number) - (right as number)
})
.filter(shouldDisplayCustomField)
.filter((field) => {
const key = (field.customFieldId || field.id || field.name) as string
if (!key) return true
if (seen.has(key)) return false
seen.add(key)
return true
})
.map((field, index) => ({
key: ((field.customFieldId || field.id || field.name) as string) || `custom-field-${index}`,
label: (field.name as string) || 'Champ',
value: formatCustomFieldValue(field),
}))
}

View File

@@ -0,0 +1,303 @@
/**
* Unified custom field types and pure helpers.
*
* Replaces: entityCustomFieldLogic.ts, customFieldUtils.ts, customFieldFormUtils.ts
*/
// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------
/** A custom field definition (from ModelType structure or CustomField entity) */
export interface CustomFieldDefinition {
id: string | null
name: string
type: string
required: boolean
options: string[]
defaultValue: string | null
orderIndex: number
machineContextOnly: boolean
}
/** A persisted custom field value (from CustomFieldValue entity via API) */
export interface CustomFieldValue {
id: string
value: string
customField: CustomFieldDefinition
}
/** Merged definition + value for form display and editing */
export interface CustomFieldInput {
customFieldId: string | null
customFieldValueId: string | null
name: string
type: string
required: boolean
options: string[]
defaultValue: string | null
orderIndex: number
machineContextOnly: boolean
value: string
readOnly?: boolean
/** options joined by newline — used by category editor textareas (v-model) */
optionsText?: string
}
// ---------------------------------------------------------------------------
// Normalization — accept any shape, return canonical types
// ---------------------------------------------------------------------------
const ALLOWED_TYPES = ['text', 'number', 'select', 'boolean', 'date'] as const
/**
* Normalize any raw field definition object into a CustomFieldDefinition.
* Handles both standard `{name, type}` and legacy `{key, value: {type}}` formats.
*/
export function normalizeDefinition(raw: any, fallbackIndex = 0): CustomFieldDefinition | null {
if (!raw || typeof raw !== 'object') return null
// Resolve name: standard → legacy key → label
const name = (
typeof raw.name === 'string' ? raw.name.trim()
: typeof raw.key === 'string' ? raw.key.trim()
: typeof raw.label === 'string' ? raw.label.trim()
: ''
)
if (!name) return null
// Resolve type: standard → nested in value → fallback
const rawType = (
typeof raw.type === 'string' ? raw.type
: typeof raw.value?.type === 'string' ? raw.value.type
: 'text'
).toLowerCase()
const type = ALLOWED_TYPES.includes(rawType as any) ? rawType : 'text'
// Resolve required
const required = typeof raw.required === 'boolean' ? raw.required
: typeof raw.value?.required === 'boolean' ? raw.value.required
: false
// Resolve options
const optionSource = Array.isArray(raw.options) ? raw.options
: Array.isArray(raw.value?.options) ? raw.value.options
: []
const options = optionSource
.map((o: any) => typeof o === 'string' ? o.trim() : typeof o?.value === 'string' ? o.value.trim() : String(o ?? '').trim())
.filter((o: string) => o.length > 0 && o !== '[object Object]')
// Resolve defaultValue
const dv = raw.defaultValue ?? raw.value?.defaultValue ?? null
const defaultValue = dv !== null && dv !== undefined && dv !== '' ? String(dv) : null
// Resolve orderIndex
const orderIndex = typeof raw.orderIndex === 'number' ? raw.orderIndex : fallbackIndex
// Resolve machineContextOnly
const machineContextOnly = !!raw.machineContextOnly
// Resolve id
const id = typeof raw.id === 'string' ? raw.id
: typeof raw.customFieldId === 'string' ? raw.customFieldId
: null
return { id, name, type, required, options, defaultValue, orderIndex, machineContextOnly }
}
/**
* Normalize a raw value entry into a CustomFieldValue.
* Accepts the API format: `{ id, value, customField: {...} }`
*/
export function normalizeValue(raw: any): CustomFieldValue | null {
if (!raw || typeof raw !== 'object') return null
const cf = raw.customField
const definition = normalizeDefinition(cf)
if (!definition) return null
const id = typeof raw.id === 'string' ? raw.id : ''
const value = raw.value !== null && raw.value !== undefined ? String(raw.value) : ''
return { id, value, customField: definition }
}
/**
* Normalize an array of raw definitions into CustomFieldDefinition[].
*/
export function normalizeDefinitions(raw: any): CustomFieldDefinition[] {
if (!Array.isArray(raw)) return []
return raw
.map((item: any, i: number) => normalizeDefinition(item, i))
.filter((d: any): d is CustomFieldDefinition => d !== null)
.sort((a, b) => a.orderIndex - b.orderIndex)
}
/**
* Normalize an array of raw values into CustomFieldValue[].
*/
export function normalizeValues(raw: any): CustomFieldValue[] {
if (!Array.isArray(raw)) return []
return raw
.map((item: any) => normalizeValue(item))
.filter((v: any): v is CustomFieldValue => v !== null)
}
// ---------------------------------------------------------------------------
// Merge — THE one merge function
// ---------------------------------------------------------------------------
/**
* Merge definitions from a ModelType with persisted values from an entity.
* Returns a CustomFieldInput[] ready for form display.
*
* Match strategy: by customField.id first, then by name (case-sensitive).
* When no value exists for a definition, uses defaultValue as initial value.
*/
export function mergeDefinitionsWithValues(
rawDefinitions: any,
rawValues: any,
): CustomFieldInput[] {
const definitions = normalizeDefinitions(rawDefinitions)
const values = normalizeValues(rawValues)
// Build lookup maps for values
const valueById = new Map<string, CustomFieldValue>()
const valueByName = new Map<string, CustomFieldValue>()
for (const v of values) {
if (v.customField.id) valueById.set(v.customField.id, v)
valueByName.set(v.customField.name, v)
}
const matchedValueIds = new Set<string>()
const matchedNames = new Set<string>()
// 1. Map definitions to inputs, matching values
const result: CustomFieldInput[] = definitions.map((def) => {
const matched = (def.id ? valueById.get(def.id) : undefined) ?? valueByName.get(def.name)
const optionsText = def.options.length ? def.options.join('\n') : undefined
if (matched) {
if (matched.id) matchedValueIds.add(matched.id)
matchedNames.add(def.name)
return {
customFieldId: def.id,
customFieldValueId: matched.id || null,
name: def.name,
type: def.type,
required: def.required,
options: def.options,
defaultValue: def.defaultValue,
orderIndex: def.orderIndex,
machineContextOnly: def.machineContextOnly,
value: matched.value,
optionsText,
}
}
// No value found — use defaultValue
return {
customFieldId: def.id,
customFieldValueId: null,
name: def.name,
type: def.type,
required: def.required,
options: def.options,
defaultValue: def.defaultValue,
orderIndex: def.orderIndex,
machineContextOnly: def.machineContextOnly,
value: def.defaultValue ?? '',
optionsText,
}
})
// 2. Add orphan values (have a value but no matching definition)
for (const v of values) {
if (matchedValueIds.has(v.id)) continue
if (matchedNames.has(v.customField.name)) continue
const orphanOptionsText = v.customField.options.length ? v.customField.options.join('\n') : undefined
result.push({
customFieldId: v.customField.id,
customFieldValueId: v.id || null,
name: v.customField.name,
type: v.customField.type,
required: v.customField.required,
options: v.customField.options,
defaultValue: v.customField.defaultValue,
orderIndex: v.customField.orderIndex,
machineContextOnly: v.customField.machineContextOnly,
value: v.value,
optionsText: orphanOptionsText,
})
}
return result.sort((a, b) => a.orderIndex - b.orderIndex)
}
// ---------------------------------------------------------------------------
// Filter & sort
// ---------------------------------------------------------------------------
/** Filter fields by context: standalone hides machineContextOnly, machine shows only machineContextOnly */
export function filterByContext(
fields: CustomFieldInput[],
context: 'standalone' | 'machine',
): CustomFieldInput[] {
if (context === 'machine') return fields.filter((f) => f.machineContextOnly)
return fields.filter((f) => !f.machineContextOnly)
}
/** Sort fields by orderIndex */
export function sortByOrder(fields: CustomFieldInput[]): CustomFieldInput[] {
return [...fields].sort((a, b) => a.orderIndex - b.orderIndex)
}
// ---------------------------------------------------------------------------
// Display helpers
// ---------------------------------------------------------------------------
/** Format a field value for display (e.g. boolean → Oui/Non) */
export function formatValueForDisplay(field: CustomFieldInput): string {
const raw = field.value ?? ''
if (field.type === 'boolean') {
const normalized = String(raw).toLowerCase()
if (normalized === 'true' || normalized === '1') return 'Oui'
if (normalized === 'false' || normalized === '0') return 'Non'
}
return raw || 'Non défini'
}
/** Whether a field has a displayable value (readOnly fields always display) */
export function hasDisplayableValue(field: CustomFieldInput): boolean {
if (field.readOnly) return true
if (field.type === 'boolean') return field.value !== undefined && field.value !== null && field.value !== ''
return typeof field.value === 'string' && field.value.trim().length > 0
}
/** Stable key for v-for rendering */
export function fieldKey(field: CustomFieldInput, index: number): string {
return field.customFieldValueId || field.customFieldId || `${field.name}-${index}`
}
// ---------------------------------------------------------------------------
// Persistence helpers
// ---------------------------------------------------------------------------
/** Whether a field should be persisted (non-empty value) */
export function shouldPersist(field: CustomFieldInput): boolean {
if (field.type === 'boolean') return field.value === 'true' || field.value === 'false'
return typeof field.value === 'string' && field.value.trim() !== ''
}
/** Format value for save (trim, boolean coercion) */
export function formatValueForSave(field: CustomFieldInput): string {
if (field.type === 'boolean') return field.value === 'true' ? 'true' : 'false'
return typeof field.value === 'string' ? field.value.trim() : ''
}
/** Check if all required fields are filled */
export function requiredFieldsFilled(fields: CustomFieldInput[]): boolean {
return fields.every((field) => {
if (!field.required) return true
return shouldPersist(field)
})
}

View File

@@ -1,335 +0,0 @@
/**
* Pure functions for custom field resolution, merging, and deduplication.
*
* Extracted from ComponentItem.vue and PieceItem.vue which had ~350 LOC
* of identical custom field logic duplicated between them.
*/
// ---------------------------------------------------------------------------
// Field key / identity helpers
// ---------------------------------------------------------------------------
export function fieldKeyFromNameAndType(name: unknown, type: unknown): string | null {
const normalizedName = typeof name === 'string' ? name.trim().toLowerCase() : ''
const normalizedType = typeof type === 'string' ? type.trim().toLowerCase() : ''
return normalizedName ? `${normalizedName}::${normalizedType}` : null
}
export function resolveOrderIndex(field: any): number {
if (!field || typeof field !== 'object') return 0
if (typeof field.orderIndex === 'number') return field.orderIndex
if (field.customField && typeof field.customField.orderIndex === 'number') return field.customField.orderIndex
return 0
}
// ---------------------------------------------------------------------------
// Field accessors
// ---------------------------------------------------------------------------
export function resolveFieldKey(field: any, index: number): string {
return field?.id ?? field?.customFieldValueId ?? field?.customFieldId ?? field?.name ?? `field-${index}`
}
export function resolveFieldId(field: any): string | null {
return field?.customFieldValueId ?? null
}
export function resolveFieldName(field: any): string {
return field?.name ?? 'Champ'
}
export function resolveFieldType(field: any): string {
return field?.type ?? 'text'
}
export function resolveFieldOptions(field: any): string[] {
return field?.options ?? []
}
export function resolveFieldRequired(field: any): boolean {
return !!field?.required
}
export function resolveFieldReadOnly(field: any): boolean {
return !!field?.readOnly
}
export function resolveCustomFieldId(field: any): string | null {
return field?.customFieldId ?? field?.id ?? field?.customField?.id ?? null
}
export function buildCustomFieldMetadata(field: any) {
return {
customFieldName: resolveFieldName(field),
customFieldType: resolveFieldType(field),
customFieldRequired: resolveFieldRequired(field),
customFieldOptions: resolveFieldOptions(field),
}
}
export function formatFieldDisplayValue(field: any): string {
const type = resolveFieldType(field)
const rawValue = field?.value ?? ''
if (type === 'boolean') {
const normalized = String(rawValue).toLowerCase()
if (normalized === 'true') return 'Oui'
if (normalized === 'false') return 'Non'
}
return rawValue || 'Non défini'
}
// ---------------------------------------------------------------------------
// Custom field ID resolution against candidate pool
// ---------------------------------------------------------------------------
export function ensureCustomFieldId(field: any, candidateFields: any[]): string | null {
const existingId = resolveCustomFieldId(field)
if (existingId) return existingId
const name = resolveFieldName(field)
if (!name || name === 'Champ') return null
const matches = candidateFields.filter((candidate) => {
if (!candidate || typeof candidate !== 'object') return false
const candidateId = candidate.id || candidate.customFieldId
if (candidateId && (candidateId === field?.id || candidateId === field?.customFieldId)) return true
return typeof candidate.name === 'string' && candidate.name === name
})
if (matches.length) {
const withId = matches.find((c) => c?.id || c?.customFieldId) || matches[0]
const id = withId?.id || withId?.customFieldId || null
if (id) field.customFieldId = id
if (!field.customField && typeof withId === 'object') field.customField = withId
return id
}
return null
}
// ---------------------------------------------------------------------------
// Structure extraction
// ---------------------------------------------------------------------------
export function extractStructureCustomFields(structure: any): any[] {
if (!structure || typeof structure !== 'object') return []
const customFields = structure.customFields
return Array.isArray(customFields) ? customFields : []
}
// ---------------------------------------------------------------------------
// Deduplication & merge
// ---------------------------------------------------------------------------
export function deduplicateFieldDefinitions(definitions: any[]): any[] {
const result: any[] = []
const seenIds = new Set<string>()
const seenNames = new Set<string>()
const orderedDefinitions = (Array.isArray(definitions) ? definitions.slice() : [])
.sort((a, b) => resolveOrderIndex(a) - resolveOrderIndex(b))
orderedDefinitions.forEach((field) => {
if (!field || typeof field !== 'object') return
const id = field.id ?? field.customFieldId ?? field.customField?.id ?? null
const nameKey = fieldKeyFromNameAndType(field.name, field.type)
// Deduplicate by name+type (primary) AND by id — a field with the same
// name+type is the same field even when stored with different IDs.
if (nameKey && seenNames.has(nameKey)) return
if (id && seenIds.has(id)) return
if (id) seenIds.add(id)
if (nameKey) seenNames.add(nameKey)
field.orderIndex = resolveOrderIndex(field)
result.push(field)
})
return result
}
export function mergeFieldDefinitionsWithValues(definitions: any[], values: any[]): any[] {
const definitionList = Array.isArray(definitions) ? definitions : []
const valueList = Array.isArray(values) ? values : []
const valueMap = new Map<string, any>()
valueList.forEach((entry) => {
if (!entry || typeof entry !== 'object') return
const fieldId = entry.customField?.id ?? entry.customFieldId ?? null
if (fieldId) valueMap.set(fieldId, entry)
const nameKey = fieldKeyFromNameAndType(entry.customField?.name, entry.customField?.type)
if (nameKey) valueMap.set(nameKey, entry)
})
const merged = definitionList.map((field) => {
if (!field || typeof field !== 'object') return field
const fieldId = resolveCustomFieldId(field)
const nameKey = fieldKeyFromNameAndType(resolveFieldName(field), resolveFieldType(field))
const matchedValue = (fieldId ? valueMap.get(fieldId) : undefined) ?? (nameKey ? valueMap.get(nameKey) : undefined)
if (!matchedValue) {
return { ...field, value: field?.value ?? '', orderIndex: resolveOrderIndex(field) }
}
const resolvedOrder = Math.min(resolveOrderIndex(field), resolveOrderIndex(matchedValue.customField))
return {
...field,
customFieldValueId: matchedValue.id ?? field.customFieldValueId ?? null,
customFieldId: matchedValue.customField?.id ?? matchedValue.customFieldId ?? fieldId ?? null,
customField: matchedValue.customField ?? field.customField ?? null,
value: matchedValue.value ?? field.value ?? '',
orderIndex: resolvedOrder,
}
})
valueList.forEach((entry) => {
if (!entry || typeof entry !== 'object') return
const fieldId = entry.customField?.id ?? entry.customFieldId ?? null
const nameKey = fieldKeyFromNameAndType(entry.customField?.name, entry.customField?.type)
const exists = merged.some((field) => {
if (!field || typeof field !== 'object') return false
if (field.customFieldValueId && field.customFieldValueId === entry.id) return true
const existingId = resolveCustomFieldId(field)
if (fieldId && existingId && existingId === fieldId) return true
if (!fieldId && nameKey) {
return fieldKeyFromNameAndType(resolveFieldName(field), resolveFieldType(field)) === nameKey
}
return false
})
if (!exists) {
merged.push({
customFieldValueId: entry.id ?? null,
customFieldId: fieldId,
name: entry.customField?.name ?? '',
type: entry.customField?.type ?? 'text',
required: entry.customField?.required ?? false,
options: entry.customField?.options ?? [],
value: entry.value ?? '',
customField: entry.customField ?? null,
orderIndex: resolveOrderIndex(entry.customField),
})
}
})
return merged.sort((a, b) => resolveOrderIndex(a) - resolveOrderIndex(b))
}
export function dedupeMergedFields(fields: any[]): any[] {
if (!Array.isArray(fields) || fields.length <= 1) {
return Array.isArray(fields)
? fields.slice().sort((a, b) => resolveOrderIndex(a) - resolveOrderIndex(b))
: []
}
const seenById = new Map<string, any>()
const seenByName = new Map<string, any>()
const result: any[] = []
const orderedFields = fields.slice().sort((a, b) => resolveOrderIndex(a) - resolveOrderIndex(b))
orderedFields.forEach((field) => {
if (!field || typeof field !== 'object') return
const rawName = resolveFieldName(field)
const normalizedName = typeof rawName === 'string' ? rawName.trim() : ''
if (!normalizedName) return
field.name = normalizedName
field.type = field.type || resolveFieldType(field)
const fieldId = resolveCustomFieldId(field)
const nameKey = fieldKeyFromNameAndType(normalizedName, field.type)
// Check duplicates by name+type first (same field can have different IDs)
const existing = (nameKey ? seenByName.get(nameKey) : undefined) ?? (fieldId ? seenById.get(fieldId) : undefined)
if (!existing) {
field.orderIndex = resolveOrderIndex(field)
if (fieldId) seenById.set(fieldId, field)
if (nameKey) seenByName.set(nameKey, field)
result.push(field)
return
}
const existingHasValue = existing.value !== undefined && existing.value !== null && String(existing.value).trim().length > 0
const incomingHasValue = field.value !== undefined && field.value !== null && String(field.value).trim().length > 0
if (!existingHasValue && incomingHasValue) {
Object.assign(existing, field)
existing.orderIndex = Math.min(resolveOrderIndex(existing), resolveOrderIndex(field))
if (fieldId) seenById.set(fieldId, existing)
if (nameKey) seenByName.set(nameKey, existing)
}
})
return result.sort((a, b) => resolveOrderIndex(a) - resolveOrderIndex(b))
}
// ---------------------------------------------------------------------------
// Definition sources builder
// ---------------------------------------------------------------------------
export function buildDefinitionSources(entity: any, entityType: 'composant' | 'piece'): any[] {
const definitions: any[] = []
const pushFields = (collection: any) => {
if (Array.isArray(collection)) definitions.push(...collection)
}
if (entityType === 'composant') {
const type = entity.typeComposant || {}
pushFields(entity.customFields)
pushFields(entity.definition?.customFields)
pushFields(type.customFields)
;[
entity.definition?.structure,
type.structure,
].forEach((structure) => {
const fields = extractStructureCustomFields(structure)
if (fields.length) definitions.push(...fields)
})
} else {
const type = entity.typePiece || {}
pushFields(entity.customFields)
pushFields(entity.definition?.customFields)
pushFields(type.customFields)
;[
entity.definition?.structure,
type.structure,
].forEach((structure) => {
const fields = extractStructureCustomFields(structure)
if (fields.length) definitions.push(...fields)
})
}
return deduplicateFieldDefinitions(definitions)
}
// ---------------------------------------------------------------------------
// Candidate fields builder
// ---------------------------------------------------------------------------
export function buildCandidateCustomFields(entity: any, definitionSources: any[]): any[] {
const map = new Map<string, any>()
const register = (collection: any[]) => {
if (!Array.isArray(collection)) return
collection.forEach((item) => {
if (!item || typeof item !== 'object') return
const id = item.id || item.customFieldId
const name = typeof item.name === 'string' ? item.name : null
const key = id || (name ? `${name}::${item.type ?? ''}` : null)
if (!key || map.has(key)) return
map.set(key, item)
})
}
register((entity.customFieldValues || []).map((value: any) => value?.customField))
register(definitionSources)
return Array.from(map.values())
}

View File

@@ -1,508 +0,0 @@
import { describe, it, expect } from 'vitest'
import {
toFieldString,
fieldKey,
resolveFieldName,
resolveFieldType,
resolveRequiredFlag,
resolveOptions,
resolveDefaultValue,
formatDefaultValue,
normalizeCustomField,
normalizeCustomFieldInputs,
extractStoredCustomFieldValue,
buildCustomFieldInputs,
requiredCustomFieldsFilled,
shouldPersistField,
formatValueForPersistence,
buildCustomFieldMetadata,
type CustomFieldInput,
} from '~/shared/utils/customFieldFormUtils'
// ---------------------------------------------------------------------------
// toFieldString
// ---------------------------------------------------------------------------
describe('toFieldString', () => {
it('returns empty string for null', () => {
expect(toFieldString(null)).toBe('')
})
it('returns empty string for undefined', () => {
expect(toFieldString(undefined)).toBe('')
})
it('returns string as-is', () => {
expect(toFieldString('hello')).toBe('hello')
})
it('converts number to string', () => {
expect(toFieldString(42)).toBe('42')
})
it('converts boolean to string', () => {
expect(toFieldString(true)).toBe('true')
expect(toFieldString(false)).toBe('false')
})
it('returns empty string for objects', () => {
expect(toFieldString({ foo: 'bar' })).toBe('')
})
})
// ---------------------------------------------------------------------------
// fieldKey
// ---------------------------------------------------------------------------
describe('fieldKey', () => {
it('prefers customFieldValueId', () => {
const field = { customFieldValueId: 'cfv-1', id: 'id-1', name: 'field' } as CustomFieldInput
expect(fieldKey(field, 0)).toBe('cfv-1')
})
it('falls back to id', () => {
const field = { customFieldValueId: null, id: 'id-1', name: 'field' } as CustomFieldInput
expect(fieldKey(field, 0)).toBe('id-1')
})
it('falls back to name-index', () => {
const field = { customFieldValueId: null, id: null, name: 'weight' } as CustomFieldInput
expect(fieldKey(field, 3)).toBe('weight-3')
})
})
// ---------------------------------------------------------------------------
// resolveFieldName
// ---------------------------------------------------------------------------
describe('resolveFieldName', () => {
it('resolves from name property', () => {
expect(resolveFieldName({ name: 'Poids' })).toBe('Poids')
})
it('falls back to key', () => {
expect(resolveFieldName({ key: 'poids' })).toBe('poids')
})
it('falls back to label', () => {
expect(resolveFieldName({ label: 'Poids' })).toBe('Poids')
})
it('returns empty string for empty object', () => {
expect(resolveFieldName({})).toBe('')
})
it('returns empty string for null', () => {
expect(resolveFieldName(null)).toBe('')
})
it('trims whitespace', () => {
expect(resolveFieldName({ name: ' Poids ' })).toBe('Poids')
})
})
// ---------------------------------------------------------------------------
// resolveFieldType
// ---------------------------------------------------------------------------
describe('resolveFieldType', () => {
it('resolves valid type', () => {
expect(resolveFieldType({ type: 'number' })).toBe('number')
})
it('resolves case-insensitive', () => {
expect(resolveFieldType({ type: 'SELECT' })).toBe('select')
})
it('falls back to text for unknown type', () => {
expect(resolveFieldType({ type: 'blob' })).toBe('text')
})
it('resolves nested value.type', () => {
expect(resolveFieldType({ value: { type: 'date' } })).toBe('date')
})
it('returns text for missing type', () => {
expect(resolveFieldType({})).toBe('text')
})
it('handles all allowed types', () => {
for (const type of ['text', 'number', 'select', 'boolean', 'date']) {
expect(resolveFieldType({ type })).toBe(type)
}
})
})
// ---------------------------------------------------------------------------
// resolveRequiredFlag
// ---------------------------------------------------------------------------
describe('resolveRequiredFlag', () => {
it('resolves boolean true', () => {
expect(resolveRequiredFlag({ required: true })).toBe(true)
})
it('resolves boolean false', () => {
expect(resolveRequiredFlag({ required: false })).toBe(false)
})
it('resolves nested value.required', () => {
expect(resolveRequiredFlag({ value: { required: true } })).toBe(true)
})
it('resolves string "true"', () => {
expect(resolveRequiredFlag({ value: { required: 'true' } })).toBe(true)
})
it('resolves string "1"', () => {
expect(resolveRequiredFlag({ value: { required: '1' } })).toBe(true)
})
it('defaults to false', () => {
expect(resolveRequiredFlag({})).toBe(false)
})
})
// ---------------------------------------------------------------------------
// resolveOptions
// ---------------------------------------------------------------------------
describe('resolveOptions', () => {
it('resolves array of strings', () => {
expect(resolveOptions({ options: ['A', 'B'] })).toEqual(['A', 'B'])
})
it('resolves array of objects with value key', () => {
expect(resolveOptions({ options: [{ value: 'A' }, { value: 'B' }] })).toEqual(['A', 'B'])
})
it('resolves array of objects with label key', () => {
expect(resolveOptions({ options: [{ label: 'Foo' }] })).toEqual(['Foo'])
})
it('falls back to value.options', () => {
expect(resolveOptions({ value: { options: ['X'] } })).toEqual(['X'])
})
it('falls back to value.choices', () => {
expect(resolveOptions({ value: { choices: ['Y'] } })).toEqual(['Y'])
})
it('returns empty array for no options', () => {
expect(resolveOptions({})).toEqual([])
})
it('filters out empty strings', () => {
expect(resolveOptions({ options: ['A', '', 'B'] })).toEqual(['A', 'B'])
})
it('filters out null values', () => {
expect(resolveOptions({ options: [null, 'A'] })).toEqual(['A'])
})
})
// ---------------------------------------------------------------------------
// resolveDefaultValue
// ---------------------------------------------------------------------------
describe('resolveDefaultValue', () => {
it('returns null for null input', () => {
expect(resolveDefaultValue(null)).toBeNull()
})
it('resolves defaultValue', () => {
expect(resolveDefaultValue({ defaultValue: 'hello' })).toBe('hello')
})
it('resolves value (non-object)', () => {
expect(resolveDefaultValue({ value: 42 })).toBe(42)
})
it('resolves nested value.defaultValue', () => {
expect(resolveDefaultValue({ value: { defaultValue: 'nested' } })).toBe('nested')
})
it('returns null when nothing found', () => {
expect(resolveDefaultValue({})).toBeNull()
})
})
// ---------------------------------------------------------------------------
// formatDefaultValue
// ---------------------------------------------------------------------------
describe('formatDefaultValue', () => {
it('returns empty string for null', () => {
expect(formatDefaultValue('text', null)).toBe('')
})
it('converts number to string', () => {
expect(formatDefaultValue('number', 42)).toBe('42')
})
it('handles boolean type with true', () => {
expect(formatDefaultValue('boolean', 'true')).toBe('true')
expect(formatDefaultValue('boolean', true)).toBe('true')
expect(formatDefaultValue('boolean', '1')).toBe('true')
})
it('handles boolean type with false', () => {
expect(formatDefaultValue('boolean', 'false')).toBe('false')
expect(formatDefaultValue('boolean', false)).toBe('false')
expect(formatDefaultValue('boolean', '0')).toBe('false')
})
it('unwraps nested defaultValue object', () => {
expect(formatDefaultValue('text', { defaultValue: 'inner' })).toBe('inner')
})
})
// ---------------------------------------------------------------------------
// normalizeCustomField
// ---------------------------------------------------------------------------
describe('normalizeCustomField', () => {
it('normalizes a complete field', () => {
const result = normalizeCustomField({
id: 'cf-1',
name: 'Weight',
type: 'number',
required: true,
options: [],
orderIndex: 2,
})
expect(result).toEqual({
id: 'cf-1',
name: 'Weight',
type: 'number',
required: true,
options: [],
value: '',
customFieldId: 'cf-1',
customFieldValueId: null,
orderIndex: 2,
})
})
it('returns null for null input', () => {
expect(normalizeCustomField(null)).toBeNull()
})
it('returns null for field without name', () => {
expect(normalizeCustomField({ type: 'text' })).toBeNull()
})
it('uses fallback index', () => {
const result = normalizeCustomField({ name: 'Test' }, 5)
expect(result?.orderIndex).toBe(5)
})
it('defaults type to text', () => {
const result = normalizeCustomField({ name: 'Field' })
expect(result?.type).toBe('text')
})
})
// ---------------------------------------------------------------------------
// normalizeCustomFieldInputs
// ---------------------------------------------------------------------------
describe('normalizeCustomFieldInputs', () => {
it('returns empty array for null structure', () => {
expect(normalizeCustomFieldInputs(null)).toEqual([])
})
it('returns empty array for structure without customFields', () => {
expect(normalizeCustomFieldInputs({})).toEqual([])
})
it('normalizes and sorts fields by orderIndex', () => {
const result = normalizeCustomFieldInputs({
customFields: [
{ name: 'B', orderIndex: 2 },
{ name: 'A', orderIndex: 1 },
],
})
expect(result).toHaveLength(2)
expect(result[0].name).toBe('A')
expect(result[1].name).toBe('B')
})
it('filters out invalid fields', () => {
const result = normalizeCustomFieldInputs({
customFields: [{ name: 'Valid' }, null, { type: 'text' }],
})
expect(result).toHaveLength(1)
expect(result[0].name).toBe('Valid')
})
})
// ---------------------------------------------------------------------------
// extractStoredCustomFieldValue
// ---------------------------------------------------------------------------
describe('extractStoredCustomFieldValue', () => {
it('returns string directly', () => {
expect(extractStoredCustomFieldValue('hello')).toBe('hello')
})
it('returns number directly', () => {
expect(extractStoredCustomFieldValue(42)).toBe(42)
})
it('returns empty string for null', () => {
expect(extractStoredCustomFieldValue(null)).toBe('')
})
it('extracts .value from object', () => {
expect(extractStoredCustomFieldValue({ value: 'test' })).toBe('test')
})
it('extracts nested .value.value', () => {
expect(extractStoredCustomFieldValue({ value: { value: 'deep' } })).toBe('deep')
})
it('extracts customFieldValue.value', () => {
expect(extractStoredCustomFieldValue({ customFieldValue: { value: 'cfv' } })).toBe('cfv')
})
})
// ---------------------------------------------------------------------------
// buildCustomFieldInputs
// ---------------------------------------------------------------------------
describe('buildCustomFieldInputs', () => {
const definitions = {
customFields: [
{ name: 'Weight', type: 'number', required: true, id: 'cf-1', orderIndex: 0 },
{ name: 'Color', type: 'select', options: ['Red', 'Blue'], id: 'cf-2', orderIndex: 1 },
],
}
it('builds inputs from definitions without values', () => {
const result = buildCustomFieldInputs(definitions, null)
expect(result).toHaveLength(2)
expect(result[0].name).toBe('Weight')
expect(result[0].value).toBe('')
expect(result[1].name).toBe('Color')
})
it('merges stored values by id', () => {
const values = [
{ customField: { id: 'cf-1', name: 'Weight' }, id: 'cfv-1', value: '42' },
]
const result = buildCustomFieldInputs(definitions, values)
expect(result[0].value).toBe('42')
expect(result[0].customFieldValueId).toBe('cfv-1')
})
it('merges stored values by name fallback', () => {
const values = [
{ name: 'Color', id: 'cfv-2', value: 'Blue' },
]
const result = buildCustomFieldInputs(definitions, values)
expect(result[1].value).toBe('Blue')
})
it('returns empty array for null structure', () => {
expect(buildCustomFieldInputs(null, [])).toEqual([])
})
})
// ---------------------------------------------------------------------------
// requiredCustomFieldsFilled
// ---------------------------------------------------------------------------
describe('requiredCustomFieldsFilled', () => {
it('returns true when all required fields are filled', () => {
const inputs: CustomFieldInput[] = [
{ id: null, name: 'A', type: 'text', required: true, options: [], value: 'hello', customFieldId: null, customFieldValueId: null, orderIndex: 0 },
]
expect(requiredCustomFieldsFilled(inputs)).toBe(true)
})
it('returns false when required field is empty', () => {
const inputs: CustomFieldInput[] = [
{ id: null, name: 'A', type: 'text', required: true, options: [], value: '', customFieldId: null, customFieldValueId: null, orderIndex: 0 },
]
expect(requiredCustomFieldsFilled(inputs)).toBe(false)
})
it('returns true for non-required empty field', () => {
const inputs: CustomFieldInput[] = [
{ id: null, name: 'A', type: 'text', required: false, options: [], value: '', customFieldId: null, customFieldValueId: null, orderIndex: 0 },
]
expect(requiredCustomFieldsFilled(inputs)).toBe(true)
})
it('treats boolean "false" as filled', () => {
const inputs: CustomFieldInput[] = [
{ id: null, name: 'Active', type: 'boolean', required: true, options: [], value: 'false', customFieldId: null, customFieldValueId: null, orderIndex: 0 },
]
expect(requiredCustomFieldsFilled(inputs)).toBe(true)
})
it('treats boolean "true" as filled', () => {
const inputs: CustomFieldInput[] = [
{ id: null, name: 'Active', type: 'boolean', required: true, options: [], value: 'true', customFieldId: null, customFieldValueId: null, orderIndex: 0 },
]
expect(requiredCustomFieldsFilled(inputs)).toBe(true)
})
it('treats boolean empty as not filled', () => {
const inputs: CustomFieldInput[] = [
{ id: null, name: 'Active', type: 'boolean', required: true, options: [], value: '', customFieldId: null, customFieldValueId: null, orderIndex: 0 },
]
expect(requiredCustomFieldsFilled(inputs)).toBe(false)
})
it('returns true for empty array', () => {
expect(requiredCustomFieldsFilled([])).toBe(true)
})
})
// ---------------------------------------------------------------------------
// shouldPersistField & formatValueForPersistence
// ---------------------------------------------------------------------------
describe('shouldPersistField', () => {
it('returns true for non-empty text field', () => {
expect(shouldPersistField({ value: 'hello' } as CustomFieldInput)).toBe(true)
})
it('returns false for empty text field', () => {
expect(shouldPersistField({ value: '', type: 'text' } as CustomFieldInput)).toBe(false)
})
it('returns true for boolean "true"', () => {
expect(shouldPersistField({ value: 'true', type: 'boolean' } as CustomFieldInput)).toBe(true)
})
it('returns true for boolean "false"', () => {
expect(shouldPersistField({ value: 'false', type: 'boolean' } as CustomFieldInput)).toBe(true)
})
it('returns false for boolean empty', () => {
expect(shouldPersistField({ value: '', type: 'boolean' } as CustomFieldInput)).toBe(false)
})
})
describe('formatValueForPersistence', () => {
it('trims text value', () => {
expect(formatValueForPersistence({ value: ' hello ', type: 'text' } as CustomFieldInput)).toBe('hello')
})
it('returns "true" for boolean true', () => {
expect(formatValueForPersistence({ value: 'true', type: 'boolean' } as CustomFieldInput)).toBe('true')
})
it('returns "false" for boolean non-true', () => {
expect(formatValueForPersistence({ value: 'false', type: 'boolean' } as CustomFieldInput)).toBe('false')
expect(formatValueForPersistence({ value: '', type: 'boolean' } as CustomFieldInput)).toBe('false')
})
})
// ---------------------------------------------------------------------------
// buildCustomFieldMetadata
// ---------------------------------------------------------------------------
describe('buildCustomFieldMetadata', () => {
it('builds metadata from field', () => {
const field: CustomFieldInput = {
id: null, name: 'Color', type: 'select', required: true,
options: ['Red', 'Blue'], value: 'Red',
customFieldId: null, customFieldValueId: null, orderIndex: 0,
}
expect(buildCustomFieldMetadata(field)).toEqual({
customFieldName: 'Color',
customFieldType: 'select',
customFieldRequired: true,
customFieldOptions: ['Red', 'Blue'],
})
})
})

View File

@@ -60,6 +60,7 @@ class CustomField
private bool $machineContextOnly = false;
#[ORM\Column(type: Types::STRING, length: 255, nullable: true, name: 'defaultValue')]
#[Groups(['composant:read', 'piece:read', 'product:read', 'machine:read'])]
private ?string $defaultValue = null;
#[ORM\Column(type: Types::JSON, nullable: true)]