Fix fournisseur handling across catalog flows

This commit is contained in:
Matthieu
2025-12-03 11:29:11 +01:00
parent 34af59d054
commit 936a73fde3
16 changed files with 519 additions and 65 deletions

View File

@@ -93,25 +93,48 @@
<th class="w-24">Aperçu</th>
<th>Nom</th>
<th>Référence</th>
<th>Fournisseurs</th>
<th>Type de pièce</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr v-for="piece in visiblePieces" :key="piece.id">
<tr v-for="row in pieceRows" :key="row.piece.id">
<td class="align-middle">
<DocumentThumbnail
:document="resolvePrimaryDocument(piece)"
:alt="resolvePreviewAlt(piece)"
:document="resolvePrimaryDocument(row.piece)"
:alt="resolvePreviewAlt(row.piece)"
/>
</td>
<td>{{ piece.name || 'Pièce sans nom' }}</td>
<td>{{ piece.reference || '—' }}</td>
<td>{{ resolvePieceType(piece) }}</td>
<td>{{ row.piece.name || 'Pièce sans nom' }}</td>
<td>{{ row.piece.reference || '—' }}</td>
<td>
<div
v-if="row.suppliers.visible.length"
class="flex max-w-[14rem] flex-wrap items-center gap-1"
:title="row.suppliers.tooltip"
>
<span
v-for="supplier in row.suppliers.visible"
:key="supplier"
class="badge badge-ghost badge-sm whitespace-nowrap"
>
{{ supplier }}
</span>
<span
v-if="row.suppliers.overflow"
class="badge badge-outline badge-sm"
>
+{{ row.suppliers.overflow }}
</span>
</div>
<span v-else></span>
</td>
<td>{{ resolvePieceType(row.piece) }}</td>
<td>
<div class="flex items-center gap-2">
<NuxtLink
:to="`/pieces/${piece.id}/edit`"
:to="`/pieces/${row.piece.id}/edit`"
class="btn btn-ghost btn-xs"
>
Modifier
@@ -120,7 +143,7 @@
type="button"
class="btn btn-error btn-xs"
:disabled="loadingPieces"
@click="handleDeletePiece(piece)"
@click="handleDeletePiece(row.piece)"
>
Supprimer
</button>
@@ -193,6 +216,88 @@ const resolvePieceType = (piece: Record<string, any>) => {
return '—'
}
const MAX_VISIBLE_SUPPLIERS = 3
const resolvePieceSuppliers = (piece: Record<string, any>) => {
const names: string[] = []
const seen = new Set<string>()
const pushName = (maybeName: unknown) => {
if (typeof maybeName !== 'string') {
return
}
const normalized = maybeName.trim().replace(/\s+/g, ' ')
if (!normalized.length) {
return
}
const key = normalized.toLowerCase()
if (seen.has(key)) {
return
}
seen.add(key)
names.push(normalized)
}
const collectConstructeurs = (value: unknown): void => {
if (!value) {
return
}
if (Array.isArray(value)) {
value.forEach(collectConstructeurs)
return
}
if (typeof value === 'string') {
pushName(value)
return
}
if (typeof value === 'object') {
const record = value as Record<string, any>
pushName(record?.name ?? record?.label ?? record?.companyName ?? record?.company ?? null)
if (record?.constructeur) {
collectConstructeurs(record.constructeur)
}
if (Array.isArray(record?.constructeurs)) {
collectConstructeurs(record.constructeurs)
}
}
}
const collectFromLabel = (value: unknown): void => {
if (typeof value !== 'string') {
return
}
value
.split(/[,;\\/•·|]+/)
.map((part) => part.trim())
.filter(Boolean)
.forEach(pushName)
}
collectConstructeurs(piece?.constructeurs)
collectConstructeurs(piece?.constructeur)
collectConstructeurs(piece?.product?.constructeurs)
collectConstructeurs(piece?.product?.constructeur)
collectFromLabel(piece?.constructeursLabel)
collectFromLabel(piece?.supplierLabel)
collectFromLabel(piece?.product?.constructeursLabel)
collectFromLabel(piece?.product?.supplierLabel)
return names
}
const buildPieceSuppliersDisplay = (piece: Record<string, any>) => {
const suppliers = resolvePieceSuppliers(piece)
const visible = suppliers.slice(0, MAX_VISIBLE_SUPPLIERS)
const overflow = Math.max(suppliers.length - visible.length, 0)
return {
suppliers,
visible,
overflow,
tooltip: suppliers.length ? suppliers.join(', ') : '',
}
}
const resolveDeleteGuard = (piece: Record<string, any>) => {
const blockingReasons: string[] = []
const machineLinks = Array.isArray(piece?.machineLinks)
@@ -269,6 +374,13 @@ const visiblePieces = computed(() => {
})
})
const pieceRows = computed(() =>
visiblePieces.value.map((piece) => ({
piece,
suppliers: buildPieceSuppliersDisplay(piece),
})),
)
const handleDeletePiece = async (piece: Record<string, any>) => {
const { blockingReasons, hasCustomFields } = resolveDeleteGuard(piece)